Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, July 21, 2020

C program to find maximum number among three numbers

C program to find maximum number among three numbers 

C program to Find the Largest Number Among Three Numbers ...

C program to input any three numbers from user and find maximum among three numbrs using if...else and  if...else...if. Let's see the below step by step logic to find the maximum number among three numbers. 

Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.

  1. INPUT

  2. Enter any three numbers: 15, 30, 60

  OUTPUT

60 is maximum.

Required knowledge for this exercise

Logic to find maximum between two numbers:

In C programming, finding maximum or minimum between two or more numbers we need comparision of all numbers unig relational operator either > or < along with if...else to find maximum or minimum number. Relational operator evaluates either 1 (true) or 0 (false) depending on condition.

1) Input three numbers from user and store it in variables num1,num2 and num3 using scanf()
2) Compare num1 and num2i.eif(num1 > num2)If it's true then perform one more comparision between num1andnum3 i.e if(num1 > num3) if it's also true then num1 is greater otherwise num3 
3)if(num1 > num2)If it's flase then it means is less than . Hence, this time compare between num2andnum3i.e if(num2 > num3)If  it's true then num2 is maximum otherwise num3.
4) finally print the value of max. that's it!

Program to find maximum number among three umbers using nested if...else: 

  1. /* C program to find maximum among three numbers */ #include <stdio.h> int main() { int num1, num2, num3, max; /* Input three numbers from user */ printf("Enter three numbers: "); scanf("%d%d%d", &num1, &num2, &num3); if(num1 > num2) { if(num1 > num3) { /* If num1 is greater than the both num2 & num3 then */ max = num1; } else { /* If num1 > num2 but num1 > num3 is not true */ max = num3; } } else { if(num2 > num3) { /* If num1 is not > num2 and num2 > num3 */ max = num2; } else { /* If num1 is not > num2 and num2 > num3 */ max = num3; } } /* Print maximum value */ printf("Maximum among all three numbers = %d", max); return 0; }

The above approach is lengthy. In place of  nested if you can use ladder if...else...if with relational and logical operator to make it more simple way.

Program to find maximum number among three numbers using ladder if...else...if: 

Here we are using logical AND && operator to combine two conditions togather. To find maximum number among three numbers, we need to compare each number with rest of two numbers. Let's see the below logic to check maximum among three numbers.
if((num1 > num2) && (num1 > num3)) is true then num1 is maximum
if((num1 > num2) && (num1 > num3)) is true then num2 is maximum
if((num1 > num2) && (num1 > num3)) is true then num3 is maximum

  1. /* C program to find maximum among three numbers */ #include <stdio.h> int main() { int num1, num2, num3, max; /* Input three numbers from user */ printf("Enter three numbers: "); scanf("%d%d%d", &num1, &num2, &num3); if((num1 > num2) && (num1 > num3)) { /* If num1 is greater than both */ max = num1; } else if((num2 > num1) && (num2 > num3)) { /* If num2 is greater than both */ max = num2; } else if((num3 > num1) && (num3 > num2)) { /* If num3 is greater than both */ max = num3; } /* Print maximum number */ printf("Maximum among all three numbers = %d", max); return 0; }

Above program can also be written as below with a little bit chages: 
Instead of checking six unnecessary conditions you can further short the logic using below approach.

  1. /* C program to find maximum among three numbers */ #include <stdio.h> int main() { int num1, num2, num3, max; /* Input three numbers from user */ printf("Enter three numbers: "); scanf("%d%d%d", &num1, &num2, &num3); if((num1 > num2) && (num1 > num3)) { /* If num1 > num2 and num1 > num3 */ max = num1; } else if(num2 > num3) { /* If num1 is not > num2 and num2 > num3 */ max = num2; } else { /* If num1 is not > num2 and num2 is also not > num3 */ max = num3; } /* Print maximum number */ printf("Maximum among all three numbers = %d", max); return 0; }

Output:
  1. Enter three number: 15, 30, 60
  2. Maximum among all three numbers = 60






Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us