In this tutorial you will learn about the C program to calculate Sum and Average of N Numbers and its application with practical example.
C Program to calculate Sum and Average of N Numbers
In this tutorial, we will learn to create a C program that will calculate Sum and Average of N Numbers using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Arithmetic operations in C Programming.
What is the average of numbers?
The average of numbers in mathematics means that all the numbers are added and then the sum of all the numbers is divided by the total number of objects.
It means if there are 4 number as follows {2,4,6,8} then the average will be:-
average = 2+4+6+8/4
average = 5.
What is a Natural number?
The natural numbers are all those positive numbers from 1 to infinity. The set of natural numbers contains all whole positive numbers. The number with decimal points does not come under the natural numbers.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Passing that number to a for loop. 4. Adding the series to a variable. 5. Finding the average of that series. 5. Printing the result numbers. 7. End Program. |
Program to calculate Sum and Average of N Numbers.
In this program, we will first take the point up to which we want to sum from zero to that point from the user. Then we will generate the series up to that point. At last, we will add those numbers and calculate the average of those numbers & will print the sum of n natural numbers and the average.
With the help of this program, we can Find Sum & Average of n natural numbers.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* C Program to calculate Sum and Average of N Numbers */ #include <stdio.h> int main() { //declaring the required variable for the program int n, i, sum = 0; float avg=0; /* n = it will hold the series last point. i = it will hold the integer value for the for loop. sum = it will hold the sum of all the numbers. avg = it will hold the average of n numbers. */ /* Taking input number from the user for the program. */ printf("Enter a positive integer: "); /* Scanning the value the program. */ scanf("%d", &n); /* Generating the series upto that point. */ for (i = 1; i <= n; ++i) { /* Adding the series up to that number. */ sum += i; } /* Printing the sum of all the n natural numbers up to that point. */ printf("Sum = %d \n", sum); /* Printing the average of all the n natural numbers up to that point. */ avg=sum/n; printf("Average = %f", avg); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- n = it will hold the integer value.
- i = it will hold the integer value.
- sum = it will hold the integer value.
- avg = it will hold the integer value.
Input number from the user.
Program Logic Code.
Printing output.