C program to find largest number among three number

Learn how to find the largest number among three number in C language using if statement:

 

 

In this article, you’ll learn to find the largest number among the three number in details with example and explanation in C language without giving input from the keyboard or giving input from the keyboard.

Q: What is the purpose of this program

Ans: We create a program in C language that helps us to find among largest three number without giving input from the keyboard or giving input from the keyboard.

There are many processes to find the largest among three number some are below given:

  • Using if statement
  • Using if…else statement
  • Using Nested if…else statement

Here, we find the largest number among three number by using if statement.

Explanation:

  • First, create three variable here we create num1, num2, num3 for storing three number then
  • Check num1 with num2 and num1 with num3
  • If num1 is largest than num2 and num3, then print on the screen the value of num1 and check the next statement.
  • Check num2 with num3 and num2 with num1
  • If num2 is largest than num3 and num1, then print on the screen the value of num2 and check the next statement.
  • Check num3 with num1 and num3 with num2
  • If num3 is largest than num1 and num2, then print on the screen the value of num3 and stop checking and print on the screen which is largest among three number.

Q: Write a program in C language to find largest among three number

Solution:

#include <stdio.h>

#include <conio.h>

int main()

{

int num1=45,num2=46,num3=48;

if(num1>=num2 && num1>=num3)

{

printf(“The largest number is %d “,num1 );

}

if(num2>=num3 && num2>=num1)

{

printf(“The largest number is %d “,num2 );

}

if(num3>=num1 && num3>=num2)

{

printf(“The largest number is %d “,num3 );

}

getch();

}

 

The output of this program:

The largest number is 48

 

Q: Write a program in C language to find largest among three number, input is given by user

Solution:

#include <stdio.h>

#include <conio.h>

int main()

{

int num1,num2,num3;

printf(“Enter three number: “);

scanf(“%d %d %d”, &num1, &num2, &num3);

if(num1>=num2 && num1>=num3)

{

printf(“The largest number is %d “,num1 );

}

if(num2>=num3 && num2>=num1)

{

printf(“The largest number is %d “,num2 );

}

if(num3>=num1 && num3>=num2)

{

printf(“The largest number is %d “,num3 );

}

getch();

}

 

The output of this program:

Enter three number: 45

96

59

The largest number is 96

Note: In the above two program supports only integer value if enter a real value like 2.2, 4.5, 8.6 then it will not show the result. To show the result we declare the variable with a float in place of int. Examples are given below:

Q: Write a program in C language to find largest among three number

Solution:

#include <stdio.h>

#include <conio.h>

int main()

{

float num1=41.90, num2=41.14, num3=41.56;

if(num1>=num2 && num1>=num3)

{

printf(“The largest number is %.2f “,num1 );

}

if(num2>=num3 && num2>=num1)

{

printf(“The largest number is %.2f “,num2 );

}

if(num3>=num1 && num3>=num2)

{

printf(“The largest number is %.2f “,num3 );

}

getch();

}

 

The Output of this program:

The largest number is 41.90

 

Q: Write a program in C language to find largest among three number, input is given by user

Solution:

#include <stdio.h>

#include <conio.h>

int main()

{

float num1,num2,num3;

printf(“Enter three number: “);

scanf(“%f %f %f”, &num1, &num2, &num3);

if(num1>=num2 && num1>=num3)

{

printf(“The largest number is %.3f “,num1 );

}

if(num2>=num3 && num2>=num1)

{

printf(“The largest number is %.3f “,num2 );

}

if(num3>=num1 && num3>=num2)

{

printf(“The largest number is %.3f “,num3 );

}

getch();

}

The output of this program:

Enter three number: 55.235

55.258

55.894

The largest number is 55.894

Leave a Reply

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

itetrade.com