Average of N Numbers

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to write a C Program to find the average of n numbers. We will use a loop to input n numbers from the user and then find the average of the entered numbers.

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/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123205{{"`Average of N Numbers`"}} c/comments -.-> lab-123205{{"`Average of N Numbers`"}} c/variables -.-> lab-123205{{"`Average of N Numbers`"}} c/data_types -.-> lab-123205{{"`Average of N Numbers`"}} c/operators -.-> lab-123205{{"`Average of N Numbers`"}} c/for_loop -.-> lab-123205{{"`Average of N Numbers`"}} c/user_input -.-> lab-123205{{"`Average of N Numbers`"}} c/memory_address -.-> lab-123205{{"`Average of N Numbers`"}} c/pointers -.-> lab-123205{{"`Average of N Numbers`"}} c/function_parameters -.-> lab-123205{{"`Average of N Numbers`"}} c/function_declaration -.-> lab-123205{{"`Average of N Numbers`"}} end

Declare variables and initialize sum to 0

First, we will declare and initialize variables that will hold the values of n, counter, sum, and x.

#include<stdio.h>
int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, i;
    float sum = 0, x;

    /* code continues... */
}

Get the input from the user

In this step, we will display the prompt to the user to enter the number of elements, and scan the input from the user.

    printf("Enter number of elements:  ");
    scanf("%d", &n);
    printf("\n\n\nEnter %d elements\n\n", n);

Get the input numbers and calculate the sum

In this step, we will scan the inputs from the user and calculate the sum of all the entered numbers using a for loop.

    for(i = 0; i < n; i++)
    {
        scanf("%f", &x);
        sum += x;
    }

Calculate the average and display the result

In this step, we will calculate the average of the numbers, and display the result to the user.

    printf("\n\n\nAverage of the entered numbers is =  %f", (sum/n));
    printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;
}

Here's the complete code for the program:

#include<stdio.h>
int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, i;
    float sum = 0, x;

    printf("Enter number of elements:  ");
    scanf("%d", &n);
    printf("\n\n\nEnter %d elements\n\n", n);

    for(i = 0; i < n; i++)
    {
        scanf("%f", &x);
        sum += x;
    }

    printf("\n\n\nAverage of the entered numbers is =  %f", (sum/n));
    printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;
}

Summary

In this lab, we learned how to use a for loop to read n numbers from the user, calculate their sum, and find their average. The average of n numbers is an essential concept in programming, and this lab provides an excellent opportunity to practice these concepts.

Other C Tutorials you may like