C Program to find Average of N Numbers

You are Here:

Using for loop

In the following example, we will find the average of numbers between 1 and 10 using for loop.

Example

C Compiler
#include <stdio.h> int main() { int start = 1; int end = 10; int total = 0; float count = (end - start) + 1; float average = 0; printf("Average of numbers between %d and %d:\n", start, end); for(start=start; start<=end; start++) total += start; printf("\nTotal = %d", total); printf("\ncount = %.f", count); average = total / count; printf("\nAverage is %.1f", average); return 0; }

Output

Average of numbers between 1 and 10: Total = 55 count = 10 Average is 5.5

Using while loop

In the following example, we will find the average of numbers between 1 and 10 using while loop.

Example

C Compiler
#include <stdio.h> int main() { int start = 1; int end = 10; int total = 0; float count = (end - start) + 1; float average = 0; printf("Average of numbers between %d and %d:\n", start, end); while(start <= end) { total += start; start++; } printf("\nTotal = %d", total); printf("\ncount = %.f", count); average = total / count; printf("\nAverage is %.1f", average); return 0; }

Output

Average of numbers between 1 and 10: Total = 55 count = 10 Average is 5.5

Using do while loop

In the following example, we will find the average of numbers between 1 and 10 using do while loop.

Example

C Compiler
#include <stdio.h> int main() { int start = 1; int end = 10; int total = 0; float count = (end - start) + 1; float average = 0; printf("Average of numbers between %d and %d:\n", start, end); do{ total += start; start++; }while(start <= end); printf("\nTotal = %d", total); printf("\ncount = %.f", count); average = total / count; printf("\nAverage is %.1f", average); return 0; }

Output

Average of numbers between 1 and 10: Total = 55 count = 10 Average is 5.5

Find Average of N Numbers for any Given Range

In the following example, we will find the average of N numbers between the user given range.

Example

C Compiler
#include <stdio.h> int main() { int start, end; int total = 0; float count = 0; float average = 0; printf("Enter a (int) starting number: "); scanf("%d", &start); printf("Enter an (int) ending number: "); scanf("%d", &end); count = (end - start) + 1; printf("Average of numbers between %d and %d:\n", start, end); for(start=start; start<=end; start++) total += start; printf("\nTotal = %d", total); printf("\ncount = %.f", count); average = total / count; printf("\nAverage is %.1f", average); return 0; }

Output

Enter a (int) starting number: 5 Enter an (int) ending number: 14 Average of numbers between 5 and 14: Total = 95 count = 10 Average is 9.5

Reminder

Hi Developers, we almost covered 98% of String functions and Interview Question on C with examples for quick and easy learning.

We are working to cover every Single Concept in C.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author