Go to Top

Monday 16 May 2011

To print the sum of the first ten numbers in Java

Question 9 : Write a program in Java to print the sum of the first ten numbers using the for loop.


Java Program : 


class sum
{
 public static void main()
 {
     int s=0,i;
     for(i=1;i<=10;i++)
     s=s+i;
     System.out.print("Sum : "+s);
    }
}

Output : 


Sum : 55

3 comments:

  1. Hi, in this code i found some small bug, that is with out using the objects of string ,that means (string object[]),there should an exception while run the code , and your class name is in lower case. hope you are correct it ,
    thank you.

    ReplyDelete
    Replies
    1. how to correct that bug??(string arg[]) one...??

      Delete
  2. qwertyuiopasdfghjllzxvcbmmmmkhgvdtyiojj++15/:))4480.112 6 2 587554*/4%=:,d f g dzrxfz, ex

    ReplyDelete

ShareThis