C program to find sum and average of three numbers

This C program adds three numbers entered by the user, finds their average up to two decimal places and then prints the results.

Prerequisite knowledge:

  • Basic syntax.
  • Average of 3 numbers = sum of the numbers/3

Program: C program to calculate the sum and average of three numbers

#include<stdio.h>
int main()
{
    int a,b,c,sum;
    float avg;
    printf("Enter three numbers: ");
    scanf("%d %d %d",&a,&b,&c);
    sum=a+b+c;                /*calculating sum*/
    avg=sum/3;                /*calculating average*/
    printf("\nSum: %d \nAverage: %.2f\n",sum,avg);
    return 0;
}

Output:

Enter three numbers: 15 17 13

Sum: 45 
Average: 15.00

The sum and average of three numbers can also be calculated using functions. Below is the code for that. To understand the program, you should know about functions in C.

Program 2: C program to find sum and average of three numbers using function

#include<stdio.h>
int sum(int,int,int);
float avg(int,int,int);
int main()
{
    int a,b,c;
    printf("Enter three numbers: ");
    scanf("%d %d %d",&a,&b,&c); 
    printf("Sum: %d \nAverage: %.2f\n",sum(a,b,c),avg(a,b,c));
    return 0;
}
int sum(int a, int b, int c)
{
    return a+b+c;   //calculate and return the sum
}

float avg(int a, int b, int c)
{
    return (a+b+c)/3;   //calculate and return the average
}

Output:

Enter three numbers: 10 20 60
Sum: 90 
Average: 30.00

That’s it, this is the code.

In this article, we learnt how to find the sum and average of three numbers in C using functions and without using functions.

Do share your code in the comment section to help others. You’re going to make it big one day.

If you didn’t understand the concept fully, you can learn from the YouTube video below:

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →

Leave a Reply

Your email address will not be published. Required fields are marked *