C Program to Find Minimum, Maximum and Average of Three Numbers

Problem:

Write a c program to find minimum, maximum and average of three numbers using conditional statements.

Algorithm to find minimum, maximum and average of 3 numbers

  1. Accept three numbers from user
  2. Find maximum number by using conditional if-else
  3. Find minimum number by using conditional if-else
  4. Print the average of three numbers

C program to find minimum, maximum and average of 3 numbers

/* Aim: To find maximum, minimum and average of three numbers */

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int a,b,c;

	printf("\n Enter 3 numbers:- ");
	scanf(" %d %d %d",&a,&b,&c);

// Code to find maximum number

	if(a>b)
	{
		if(a>c)
			printf("\n %d is maximum \n",a);
	}
	else if(b>c)
			printf("\n %d is maximum \n",b);
	else
			printf("\n %d is maximum \n",c);


// Code to find minimum number

	if(a<b)
	{
		if(a<c)
			printf("\n %d is minimum \n",a);
	}
	else if(b<c)
			printf("\n %d is minimum \n",b);
	else
			printf("\n %d is minimum \n",c);


printf("\n Average of (%d,%d,%d) is %d \n \n",a,b,c,(a+b+c)/3);

	return 0;
}

/* Output of above code:- 

 Enter 3 numbers:- 10 11 12

 12 is maximum

 10 is minimum

 Average of (10,11,12) is 11 
 
*/
We can also find the average of n numbers by using command line arguments.

C program to find average of n numbers

/* Aim: Write a C program to accept n integers as command line arguments and find average of n numbers */

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
	int avg=0,i;

	if(argc<=1)
	{
	printf("\n Enter appropriate number of arguments to calculate average. \n \n");
	exit(0);
	}
	else
	{
		for(i=1;i<argc;i++)
		{
			avg+=atoi(argv[i]);
		}
	}

avg/=argc-1;

	printf("\n Average of all command line arguments is %d \n \n",avg); 
}