Summation.java
Aim:
To find sum of n numbers using do-while loop.
Source code:
import java.lang.*;
import java.util.*;
class Summation
{
public static void main(String args[])
{
int sum=0,n, i=1;
Scanner input=new Scanner(System.in);
System.out.println("Enter value for n");
n=input.nextInt();
do
{
sum=sum+i;
i++;
}while(i<=n);
System.out.println("Summation="+sum);
}
}
Output:
Enter value for n
10
Summation=55
No comments:
Post a Comment