find Sum of Natural Numbers

Java Program to find Sum of Natural Numbers with Example

Natural numbers are the positive integers 1, 2, 3, 4, and so on. We will look at three programs that will calculate and display the sum of natural numbers.

  • The first program uses a while loop to calculate the sum.
  • The second program computes the sum using a for loop.
  • Third, the program takes the value of n (as entered by the user) and computes the sum of the n natural numbers.

Example: Program to find the sum of natural numbers using while loop

 public class Demo {

    public static void main(String[] args) {

       int num = 10, count = 1, total = 0;

       while(count <= num)
       {
           total = total + count;
           count++;
       }

       System.out.println("Sum of first 10 natural numbers is: "+total);
    }
}

Output:

Sum of first 10 natural numbers is: 55

Example 2: Program to calculate the sum of natural numbers using for loop

 public class Demo {

    public static void main(String[] args) {

       int num = 10, count, total = 0;

       for(count = 1; count <= num; count++){
           total = total + count;
       }

       System.out.println("Sum of first 10 natural numbers is: "+total);
    }
}

Output:

Sum of first 10 natural numbers is: 55

Example 3: Program to find sum of first n (entered by user) natural numbers

 public class Demo {

    public static void main(String[] args) {

        int num, count, total = 0;

        
        System.out.println("Enter the value of n:");
        //Scanner is used for reading user input
        Scanner scan = new Scanner(System.in);
        //nextInt() method reads integer entered by user
        num = scan.nextInt();
        //closing scanner after use
        scan.close();
        for(count = 1; count <= num; count++){
            total = total + count;
        }

        System.out.println("Sum of first "+num+" natural numbers is: "+total);
    }
}

Output:

Enter the value of n:
20
Sum of first 20 natural numbers is: 210

theudaipurstore.com

Fibonacci sequence Fibonacci sequence v Fibonacci sequence v Fibonacci sequence v Fibonacci sequence vFibonacci sequence

In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing. sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array sum the elements of an array convert a char array convert a char array convert a char array vv convert a char array vconvert a char array

Leave a Reply