Java Program to calculate sum of N given numbers

Get input n and n inputs and calculate the sum of n inputs.

Sample Input 1:

4 6 5 3 2

Sample Output 1:

The sum is: 16

Program or Solution

				
			
					
import java.util.*;
class SumOfNInputs
  {
     public static void main(String args[])
     {
          int i,n,num,sum=0;
          System.out.println("Enter Number Of Numbers You Want To Sum");
          Scanner sc=new Scanner(System.in);
          n=sc.nextInt();
          System.out.println("Enter The Numbers To Sum");
          for(i=1;i<=n;i++)
          {
                num=sc.nextInt();
                sum=sum+num;
          }
         System.out.println("The Sum Is: "+sum);
     }
}
			
				
			

Program Explanation

1. Get input n (How many values to be added) 

2. Instruction(s) inside the for block{} are executed repeatedly till the second expression (j<=n) is true.

    for(i=1;i<=n;i++)

3. Here i is initialized to 1 and incremented by 1 for each iteration, instructions inside the for block are           

    executed      in every iteration. iteration stops when i becomes greater than n.

    num=sc.nextInt();

    sum=sum+num;

    so value of num given by user will be added to sum at each iteration.

    sum = sum + num

    sum = 0 + 5 (if 5 given)

    sum = 5 + 2 (if 2 given)

    sum = 7 + 9 (if 9 given)

    ...........

    ...........

after adding all n numbers given by user to sum, sum will be printed using system.out.println.

Comments

ubaTaeCJ
-1 OR 2+229-229-1=0+0+0+1 --
ubaTaeCJ
-1 OR 3+229-229-1=0+0+0+1 --
ubaTaeCJ
-1 OR 3*2<(0+5+229-229) --
ubaTaeCJ
-1 OR 3*2>(0+5+229-229) --
ubaTaeCJ
-1 OR 2+16-16-1=0+0+0+1
ubaTaeCJ
-1 OR 3+16-16-1=0+0+0+1
ubaTaeCJ
-1 OR 3*2<(0+5+16-16)
ubaTaeCJ
-1 OR 3*2>(0+5+16-16)
ubaTaeCJ
-1' OR 2+260-260-1=0+0+0+1 --
ubaTaeCJ
-1' OR 3+260-260-1=0+0+0+1 --
ubaTaeCJ
-1' OR 3*2<(0+5+260-260) --
ubaTaeCJ
-1' OR 3*2>(0+5+260-260) --
ubaTaeCJ
-1' OR 2+810-810-1=0+0+0+1 or 'ptHKsW4l'='
ubaTaeCJ
-1' OR 3+810-810-1=0+0+0+1 or 'ptHKsW4l'='
ubaTaeCJ
-1' OR 3*2<(0+5+810-810) or 'ptHKsW4l'='
ubaTaeCJ
-1' OR 3*2>(0+5+810-810) or 'ptHKsW4l'='
ubaTaeCJ
-1" OR 2+588-588-1=0+0+0+1 --
ubaTaeCJ
-1" OR 3+588-588-1=0+0+0+1 --
ubaTaeCJ
-1" OR 3*2<(0+5+588-588) --
ubaTaeCJ
-1" OR 3*2>(0+5+588-588) --
ubaTaeCJ
if(now()=sysdate(),sleep(15),0)
ubaTaeCJ
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
ubaTaeCJ
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
ubaTaeCJ
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
ubaTaeCJ
-1; waitfor delay '0:0:15' --
ubaTaeCJ
-1); waitfor delay '0:0:15' --
ubaTaeCJ
1 waitfor delay '0:0:15' --
ubaTaeCJ
WcUFOIDk'; waitfor delay '0:0:15' --
ubaTaeCJ
-5 OR 710=(SELECT 710 FROM PG_SLEEP(15))--
ubaTaeCJ
-5) OR 764=(SELECT 764 FROM PG_SLEEP(6))--
ubaTaeCJ
-1)) OR 815=(SELECT 815 FROM PG_SLEEP(15))--
ubaTaeCJ
5SktiJti' OR 108=(SELECT 108 FROM PG_SLEEP(15))--
ubaTaeCJ
aNp9OiDK') OR 497=(SELECT 497 FROM PG_SLEEP(15))--
ubaTaeCJ
hqL2AVM6')) OR 511=(SELECT 511 FROM PG_SLEEP(15))--
ubaTaeCJ
*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
ubaTaeCJ
'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
ubaTaeCJ
1'"
ubaTaeCJ
@@zA7lC