Sum of N Input Numbers in C

CCBeginner
Practice Now

Introduction

This lab will guide you on how to write a program in C that sums up N input numbers. We will use a loop to get the sum of all input numbers and display the result.


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_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/comments -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/variables -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/data_types -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/operators -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/for_loop -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/user_input -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/memory_address -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/function_parameters -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} c/function_declaration -.-> lab-123346{{"`Sum of N Input Numbers in C`"}} end

Getting Started

Before we begin, make sure you have installed a C Compiler on your machine. We will be using GCC for this lab. If you do not have GCC installed, please refer to the official GCC website for installation instructions.

Creating the main.c File

Open your preferred text editor and create a new file named main.c in the ~/project/ directory.

Initializing Variables

In this step, we shall initialize our variables. We need three variables; n, sum and value.

#include <stdio.h>

int main() {
    int n, sum = 0, value;
    // ...
}

Getting User Input

We shall prompt the user to enter the number of integers they want to add, n. Then, we will ask the user to enter all n integers to add.

#include <stdio.h>

int main() {
    int n, sum = 0, value;

    printf("Enter the number of integers you want to add: ");
    scanf("%d", &n);

    printf("Enter %d integers\n", n);
    for (int i = 0; i < n; i++) {
        printf("Enter the number %d: ", (i + 1));
        scanf("%d", &value);

        sum += value; // Add value to sum
    }
    // ...
}

Displaying the Result

Finally, we shall display the sum of all entered integers using the printf function.

#include <stdio.h>

int main() {
    int n, sum = 0, value;

    printf("Enter the number of integers you want to add: ");
    scanf("%d", &n);

    printf("Enter %d integers\n", n);
    for (int i = 0; i < n; i++) {
        printf("Enter the number %d: ", (i + 1));
        scanf("%d", &value);

        sum += value; // Add value to sum
    }

    printf("Sum of entered numbers = %d\n", sum);

    return 0;
}

Summary

We have successfully written a C program that sums up N input integers and displays the result. In summary, we have covered the following steps:

  1. Initialized variables n, sum and value.
  2. Prompted the user to input the number of integers they want to add, n.
  3. Prompted the user to enter all n integers.
  4. Added all the input integers to the sum variable during each iteration.
  5. Displayed the sum of all entered integers.

Copy the final code below into your main.c file:

#include <stdio.h>

int main() {

    int n, sum = 0, value;

    printf("Enter the number of integers you want to add: ");
    scanf("%d", &n);

    printf("Enter %d integers\n", n);
    for (int i = 0; i < n; i++) {
        printf("Enter the number %d: ", (i + 1));
        scanf("%d", &value);

        sum += value; // Add value to sum
    }

    printf("Sum of entered numbers = %d\n", sum);

    return 0;
}

Other C Tutorials you may like