Write a program that will print sum of N integers entered by the user.
Answers
import java.util.*;
public class Sum
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int s=0, N,a;
System.out.println("Enter the value of N");
N=in.nextInt();
for(a=1;a<=n;a++)
{
s=s+a;
System.out.println("Display the sum"+s);
}
}
}
Explanation:
* We should have to input the value of 'N' so we should use scanner class here.
*we have to run the program for several times so we should use loop here.
*we have to calculate the sum of the integers individually from 1 to N so, we should take a variable, here 'a' and at first we should declare its value as 1 and then we should use 'ternary operator' here such that, a++ or a=a+1, to get each individual integer.
* We should add the the next integer with the sum because, we have to show the sum of all the integers together upto 'N' terms and when the value of 'a' will be equal to 'N' then the loop will execute.
* Instead of 'for' loop, we could use here 'Do-while' loop, then we should declare at first, a=1 and the underlined portion would get changed as like_
do(s=s+a)
{
System.out.println("Display the sum"+s);
while
{
a<=N
a++
}
}
* For more programming follow the given links as I solved earlier_
https://brainly.in/question/14335606
https://brainly.in/question/14323927