C Program To Determine Smallest Of Two Numbers Using Logical Operator.
#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("\n Enter any Two numbers\n"); printf("\n Enter First Number : "); scanf("%d",&a); printf("\n Enter Second Number : "); scanf("%d",&b); if(a<b) //logical test { printf("\n %d is Smaller",a); } else { printf("\n %d is Smaller",b); } getch(); }
Output :
C Program To Calculate Smallest Of Two Numbers Using Conditional Operator/ Ternary Operator
#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("\n Enter any Two numbers\n"); printf("\n Enter First Number : "); scanf("%d",&a); printf("\n Enter Second Number : "); scanf("%d",&b); c=(a<b) ? a : b ; // Conditional operator printf("\n %d is Smaller",c); getch(); }
We have also used the conditional operator to find greatest of two numbers in c, here the condition changes from (a>b) to (a<b) give us the smaller number.
C Program To Find Smallest of Two Numbers Using Arithmetical Operator.
#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("\n Enter any Two numbers\n"); printf("\n Enter First Number : "); scanf("%d",&a); printf("\n Enter Second Number : "); scanf("%d",&b); c=a-b; if(c<0) printf("\n %d is Smaller",a); else printf("\n %d is Smaller",b); getch(); }
In the above code,
- First we get two numbers from user and subtract the first one from second.
- If the subtraction is a negative number that means first number (a) is smaller than the second one (b), for example if we take a=4 and b=5 then a-b will be equal to -1 which is a negative number. It means the first number is smaller.
- On the contrary if the subtraction is a positive number the second number (b) is smaller than the first one.
Dev C++ Code for the above program
#include<stdio.h>
int main()
{
int a,b;
printf("\n Enter Any Two numbers\n");
printf("\n Enter First Number : ");
scanf("%d",&a);
printf("\n Enter Second Number : ");
scanf("%d",&b);
if(a<b)//logical test
{
printf("\n %d is Smaller",a);
}
else
{
printf("\n %d is Smaller",b);
}
getch();
return 0;
}