Program 26 - CSU1128 - CSE 2026 - Shoolini University

Program to print the average of the numbers stored in an array entered by the user

Explanation

The program calculates and prints the average of numbers stored in an array entered by the user.

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and four variables are declared:

  • int d: Stores the total number of numbers entered by the user.
  • int m: Used as a loop counter.
  • int o: Initialized to 0 and used to store the sum of the numbers.
  • float n: Initialized to 0 and used to store the average of the numbers.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the total number of numbers they want to find the average of. The input is read using scanf("%d", &d);.

Array and Sum Calculation

The program dynamically creates an array to store the numbers entered by the user and calculates the sum:

  • int j[d];: Declares an array j of size d to store the numbers.
  • for (m = 0; m < d; m++): Loops through each position in the array.
  • printf("\tNumber %d: ", m + 1);: Prompts the user to enter a number.
  • scanf("%d", &j[m]);: Reads the entered number and stores it in the array.
  • o += j[m];: Adds the current number to the sum o.

Average Calculation

The program calculates the average of the numbers entered:

  • n = (float)o / d;: Calculates the average by dividing the sum o by the total number of numbers d.

Output Result

The program prints the total sum and the average of the numbers entered, formatted to two decimal places.

Program End

The program ends with printf("\n\n"); to print a new line and return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    int d, m, o = 0;
    float n = 0;
    printf("\n\n Program to print the average of the numbers stored in an array entered by the user. \n\n");

    printf("Enter total number of numbers you want to find its average - (Example: 5) - and press enter: ");
    scanf("%d", &d);

    for (m = 0; m < d; m++)
    {
        int j[d];
        printf("\tNumber %d: ", m + 1);
        scanf("%d", &j[m]);
        o += j[m];
    }
    n = o / m;
    printf("Total sum of the numbers entered is %d and averge is %.2f \n", o, n);
    printf("\n\n");
    return 0;
}                    
                

Output

Program to print the average of the numbers stored in an array entered by the user.

Enter total number of numbers you want to find its average - (Example: 5) - and press enter: 3
 Number 1: 23
 Number 2: 54
 Number 3: 34
  Total sum of the numbers entered is 111 and averge is 37.00