Summation.java

Aim:
To find sum of n numbers using for loop.

Source code:
import java.lang.*;
import java.util.*;
class Summation
 {
   public static void main(String args[])
     {
       int sum=0,n, i;
       Scanner input=new Scanner(System.in);
       System.out.println("Enter value for n");
       n=input.nextInt();
          for(i=1;i<=n;i++)
          {
            sum=sum+i;     
           }
       System.out.println("Summation="+sum);
      }
  } 

Output:
Enter value for n
10
Summation=55






No comments:

Post a Comment