Computer Science, asked by esha1515pal, 11 months ago

Write a java program to find the sum of all natural numbers from 1 to n

Answers

Answered by Ankit02
8

class add

{

public static void main(int n)

{

int sum =0;

for(int m = 1 ; m<=n ; m++)

{

sum+=m;

}

System.out.println(sum);

}

}

Answered by anindyaadhikari13
3

Question:-

Write a java program to find the sum of all the natural numbers from 1 to N.

Program:-

This is the program written in Java.

class Sum

{

static void main(int n)

{

int i=1,s=0;

while(i++<=n)

s+=i;

System.out.println("Sum is: "+s);

}

}

Similar questions