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
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);
}
}
{
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
2
int sum = 0;
for (int count = 1; count < n + 1; count++) { sum += count; }
Similar questions
Computer Science,
7 months ago
English,
7 months ago
English,
7 months ago
Computer Science,
1 year ago
Science,
1 year ago
English,
1 year ago
Math,
1 year ago
Math,
1 year ago