KnowledgeBoat Logo

Computer Applications

Write a program using while loop to generate the first 10 natural numbers and their sum.

Java

Java Iterative Stmts

87 Likes

Answer

public class KboatNaturalNumbers
{
    public static void main(String args[]) {
        int n = 1;
        int sum = 0;
        while (n <= 10) {
            System.out.println(n);
            sum = sum + n;
            n = n + 1;
        }
        System.out.println("Sum = " + sum);
    }
}

Output

BlueJ output of Write a program using while loop to generate the first 10 natural numbers and their sum.

Answered By

37 Likes


Related Questions