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

No comments:

Post a Comment

ShareThis