write a program sum of natural number series by using looping statement
Answers
Answered by
4
void display(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
{
s=s+i;
}
System.out.println("Sum:"+s);
}
Hope it helps
{
int i,s=0;
for(i=1;i<=n;i++)
{
s=s+i;
}
System.out.println("Sum:"+s);
}
Hope it helps
Answered by
3
// To the sum of all natural numbers 1 to n using scanner class
import java.util.*;
public class Natural
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,n,s=0;
System.out.println("Enter the value of n");
n=in.nextInt();
for(a=1;a<=n;a++)
s=s+a;
System.out.println("The sum of natural numbers up to "+n"="+s);
}
}
*Hope it helps u out!!
Thank you!!
Similar questions