WAP to input two number and Find maximum between two.
Write a C program to find maximum between two numbers using if else. C program to enter two numbers from user and find maximum between two numbers using if else. How to find maximum or minimum between two numbers using if else in C programming.
Example
Input
Input num1: 33
Input num2: 99
Output
Maximum = 99
Logic to find maximum or minimum
Before we get on to this program, basic knowledge of if else and relational operator is needed. What we have learnt till now is if else statements work with true or false values. If the condition is true the if block is executed otherwise else get executed. In the series of learning we also learnt that there is a set of relational operators that evaluates to true or false values. That's all we need for this program.
Now, if I ask question to you. How will you manually check maximum between two numbers? The answer is simple, by looking both numbers and comparing them either by num1 > num2 or num1 < num2. The given two expressions in mathematics means that num1 is greater than num2; and num1 is less than num2 respectively. Now for C programming language, since num1 and num2 both are variables hence values will be determined during the execution of program. So is the truthness will be determined at runtime. Which means num1 > num2will evaluate to true iff num1 is greater than num2 otherwise evaluate to false.
Let us now implement our logic.
Program to find maximum or minimum
Note: If you want to find minimum between two numbers. You only need to reverse the sign of ifstatement i.e. you can write it as if(num1 < num2) for checking minimum.
Before you move on to next program or exercise. Advance your programming skills by learning this program using other approaches
#include<stdio.h>
#include<conio.h>
int largest(int x, int y);
void main()
{
int a, b, largest;
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
largest = larger(a,b);
printf("\n Larger amongst %d and %d is %d\n",a, b, largest);
getch();
}
int largest(int x, int y)
{
if(x>y)
return x;
else
return y;
}
Output:
Enter value a and b:33 and 99
Largest amongst 33 and 99 is 99
#include<stdio.h>
#include<conio.h>
int largest(int x, int y);
void main()
{
int a, b, largest;
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
largest = larger(a,b);
printf("\n Larger amongst %d and %d is %d\n",a, b, largest);
getch();
}
int largest(int x, int y)
{
if(x>y)
return x;
else
return y;
}
Output:
Enter value a and b:33 and 99
Largest amongst 33 and 99 is 99
Comments
Post a Comment