Sum Of Natural Numbers Using WHILE Loop | Java Programs

Output:

*** Sum Of Natural Numbers Using WHILE Loop ***

While lOOp used !!!

Sum of first 10 natural numbers is: 55

Click Here For Java Online Compiler

Solution:

public class SumNaturalWhile {

    public static void main(String[] args) {

        int num = 10; // <------ How many numbers???
        int count = 1;
        int total = 0;

        while (count <= num) {
            total = total + count;
            count++;
        }
        System.out.println("*** Sum Of Natural Numbers Using WHILE Loop ***" + "\n");
        System.out.println("While lOOp used !!!" + "\n");
        System.out.println("Sum of first 10 natural numbers is: " + total);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon