Sum of n natural numbers || for loop || Java programs || Java
In this, we are going to see the program for Sum of 'n' natural Numbers  in Java .



import java.util.Scanner;

class Sum_natural
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int n, sum=0;
        System.out.print("Enter the number to find sum of all natural numbers till there: ");
        n = sc.nextInt();
        
        for(int i=1; i<=n; i++)
        {
            sum = sum +i;
        }
        System.out.println("The sum of all natural numbers till "+ n + " = " + sum);
    }
}
	
#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post