-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex1.c
27 lines (21 loc) · 811 Bytes
/
ex1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Write a program that sums a sequence of integers. Assume that the first integer read with scanf specifies the number of values remaining to be entered. Your program should read only one value each time scanf is executed. A typical input sequence might be
// 5 100 200 300 400 500
// where the 5 indicates that the subsequent five values are to be summed.
#include <stdio.h>
int main()
{
int n, value, sum = 0;
// Read the number of integers to sum
printf("Enter the number of integers to sum: ");
scanf("%d", &n);
// Loop to read and sum the integers
for (int i = 0; i < n; i++)
{
printf("Enter integer #%d: ", i + 1);
scanf("%d", &value);
sum += value;
}
// Print the sum
printf("The sum of the integers is: %d\n", sum);
return 0;
}