Computer Science, asked by anjuraghtiwar, 3 months ago

write the program to print sum of first 15 natural number in java​

Answers

Answered by Ladylaurel
5

Answer :-

Program ( Source cσde )

public class cls_sum_of_numbers

{

⠀ public static void main (string args [])

⠀ {

⠀ ⠀ int sum = 0;

⠀ ⠀ int I = 1;

⠀ ⠀ while (i<=10)

⠀ ⠀ {

⠀ ⠀ ⠀ sum = sum + i;

⠀ ⠀ ⠀ i+=1;

⠀ ⠀ }

⠀ ⠀ System.σut.println ("The sum of 10 natural numbers is "+sum);

⠀ }

}

_______________________________

Output:

The sum of 10 natural numbers is 55

_____________________________

Program explanation:

The program initializes two variables, sum to 0 and control variable i to 1. The while statement checks a test condition and the loop will continue till i is less than or equal to 10. Inside the loop, variable i is added to the variable sum and the result is stored in the sum. The sum is then printed using ‘System.σut.println’ statement.

Similar questions