Java Program to find the sum of first 10 natural numbers using class


In this program, You will learn how to find the sum of the first 10 natural numbers using class in java.


Some natural numbers is: 1 2 3 4 5 6 7 8 9 10

Example: How to find the sum of the first 10 natural numbers using class in java.

class Main {

    int i, s = 0;

    void print() {
        for (i = 1; i <= 10; i++) {
            s = s + i;
        }

        System.out.println("Sum of 10 natural numbers:" + s);

    }

    public static void main(String args[]) {

        Main obj = new Main();
        obj.print();
    }
}

Output:

Sum of 10 natural numbers:55