Write a java program to find the sum of all natural numbers from 1 to n
Answers
Answered by
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
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
Social Sciences,
5 months ago
Physics,
5 months ago
English,
5 months ago
Biology,
11 months ago
English,
11 months ago
Social Sciences,
1 year ago
Social Sciences,
1 year ago