Computer Science, asked by kraghaver, 1 year ago

Which block of code will sum the numbers from 1 to n (including n) and store it in the variable "sum"?

int sum = 0; for (int count = 1; count <= n; count++) { sum = count; }

int sum = 0; for (int count = 1; count < n + 1; count++) { sum += count; }

for (int count = 1; count < n; count++) { int sum = 0; sum += count; }

int sum = 0; for (int count = 1; count < n; count++) { sum += count; }

Answers

Answered by mandaldebankur
3
public class Number
{
public static void main(int n)
{
int a,s=0;
for(a=1;a<=n;a++)
{
s=s+a;
}
System.out.println("The Sum : "+s);
}
}
Answered by amitathawale45
2

int sum = 0;

for (int count = 1; count < n + 1; count++) { sum += count; }

Similar questions