Sum of N Integers Using Arrays

CCBeginner
Practice Now

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.c yourself 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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/comments -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/variables -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/data_types -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/operators -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/for_loop -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/user_input -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/memory_address -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} c/function_declaration -.-> lab-123344{{"`Sum of N Integers Using Arrays`"}} end

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.

Other C Tutorials you may like