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

1 comment:

  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

ShareThis