Introduction
In this lab, you will learn how to create a program to find the sum of n integers using an array. You will use C programming language to create the program.
Note: You need to create the file
~/project/main.cyourself to practice coding and learn how to compile and run it using gcc.
cd ~/project
## create main.c
touch main.c
## compile main.c
gcc main.c -o main
## run main
./main
Creating an array and accepting user input
#include<stdio.h>
int main()
{
int n, sum = 0, c, array[100]; // Declaring the variables
printf("Enter the number of integers you want to add: ");
scanf("%d", &n); // Accepting the number of integers from user
printf("\n\nEnter %d integers \n\n", n);
for(c = 0; c < n; c++) // Loop to accept the n numbers from user
{
scanf("%d", &array[c]); // Accepts the numbers from user and stores in an array
sum += array[c]; // Sums the numbers and stores in a variable named 'sum'
}
- In the above code block, we have declared the variables 'n', 'sum', 'c', and 'array'.
- Then, we prompt the user to enter the number of integers they want to add, and store the value in 'n'.
- The user is prompted to enter 'n' integers.
- We then accept the 'n' integers and store them in an array named 'array'.
- Using a for loop, we sum the values entered by the user and store the sum in a variable named 'sum'.
Printing the sum of the entered integers
printf("\n\nSum = %d\n\n", sum); // Prints the sum of the entered integers
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
- In the above code block, we print the sum of the n integers that the user entered using the printf function.
Adding appropriate comments to the code to make it readable
#include<stdio.h>
int main()
{
int n, sum = 0, c, array[100]; // Declaring the variables
printf("Enter the number of integers you want to add: ");
scanf("%d", &n); // Accepting the number of integers from user
printf("\n\nEnter %d integers \n\n", n);
for(c = 0; c < n; c++) // Loop to accept the n numbers from user
{
scanf("%d", &array[c]); // Accepts the numbers from user and stores in an array
sum += array[c]; // Sums the numbers and stores in a variable named 'sum'
}
printf("\n\nSum = %d\n\n", sum); // Prints the sum of the entered integers
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
- In the above code block, we have added comments to describe each step of the program to help make it more readable.
Full Code
#include<stdio.h>
int main()
{
int n, sum = 0, c, array[100]; // Declaring the variables
printf("Enter the number of integers you want to add: ");
scanf("%d", &n); // Accepting the number of integers from user
printf("\n\nEnter %d integers \n\n", n);
for(c = 0; c < n; c++) // Loop to accept the n numbers from user
{
scanf("%d", &array[c]); // Accepts the numbers from user and stores in an array
sum += array[c]; // Sums the numbers and stores in a variable named 'sum'
}
printf("\n\nSum = %d\n\n", sum); // Prints the sum of the entered integers
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Summary
In this lab, we have learned how to create a C program to find the sum of n integers using an array. We accomplished this by creating an array, accepting user input, summing the inputted integers, and printing the resulting sum. We added appropriate comments throughout the program to make it more readable.



