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

Saturday, July 18, 2020

C program to find maximum between two numbers

C program to find maximum between two numbers 

C program to input any two numbers from user and find maximum between two both numbrs using if...else and  if...else-if. Let's see the below step by step logic to find the maximum between two 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 two number: 15, 30

  OUTPUT

30 is maximum.

Required knowledge for this exercise

Logic to find maximum between two numbers:

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

1) Input two numbers from user and store it in variables num1 or num2 using scanf()
2) Compare both numbersnum1 and num2using below expression
if(num1 > num2).If it's true then print num1is maximum.
else if(num2 > num1).If it's true then print num2is maximum.
else .If above both conditions are false then print both are equal.
Above expressions will return true(1) if condition is satisfied otherwise return false(0).
3)if(num1 > num2)If it's true then printnum1is maximum else print num2is maximum.

Program to find maximum between two umbers using if...else-if: 

  1. /* C program to find maximum between two numbers */ #include <stdio.h> int main() { int num1, num2; /* Input two numbers from user */ printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); /* Compare num1 with num2 */ if(num1 > num2) { printf("%d is maximum", num1); } else if (num2 > num1) { printf("%d is maximum", num2); }
  2. else { printf("Both are equal"); } return 0; }

Program to find maximum between two umbers using if...else: 

  1. /* C program to find maximum between two numbers */ #include <stdio.h> int main() { int num1, num2; /* Input two numbers from user */ printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); /* Compare num1 with num2 */ if(num1 > num2) { printf("%d is maximum", num1); }
  2. else { printf("%d is maximum", num1); } return 0; }

Above program can also be written as below with a little bit chages: 
By storing maximum number in a max variable and then print then resultant value. 

  1. /* C program to find maximum between two numbers */ #include <stdio.h> int main() { int num1, num2, max; /* Input two numbers from user */ printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); /* Compare num1 with num2 */ if(num1 > num2) max = num1;
  2. else max = num2; printf("%d is maximum", max); return 0; }

As you can see in above programs if or else body contains only single statement. Hence, you can ignore braces { } after if and else statement.

Output:
  1. Enter any two number: 15, 30
  2. 30 is maximum




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