program to print greater number in C ?
Q.) W.A.P to print greater number in C using single if ?
#include<stdio.h> #include<conio.h> int main() { int a,b,big; printf("enter two numbers :\n"); scanf("%d%d",&a,&b); big=a; if (b>big) { big=b; } printf( "greater number is : %d",big); getch(); }
Output
9 5 // say a = 9 and b = 5
greater number is : 9
Program Explanation
Step 1: Include header files (#include<stdio.h> and #include<conio.h>).
Step 2: Start with main function with return type.
Step 3: parenthesis to start and end the program { }.
Step 4: declare variables with data type i.e, 'a' and 'b' are of integer type so we use "int" data type.
we also declare 'big' with "int" data type because it returns an integer value.
Step 5: Use output function printf() to print the output on the screen.
Step 6: Use input function scanf() to get input from the user.
Step 7: Here, we have to print greater number using only if condition not else,
so we take an extra variable. i.e, 'big' and we assume 'a' is the biggest number and assign 'a' value to 'big'.
Here, we use the conditional operator (i.e > greater than) to check condition.
if condition checks whether 'big' is greater than 'b' or not.
- if this condition is true, then it will execute if block, which will assign 'b' value to 'big' and print the value of 'b' as a greater number.
- if the condition is not true, then it will print the value of 'a' as a greater number.
Q.) W.A.P to print greater number in C using if else ?
#include<stdio.h> #include<conio.h> int main() { int a,b; printf("enter two numbers :\n"); scanf("%d%d",&a,&b); if (a>b) { printf("a is greater than b :%d",a); } else{ printf( "b is greater than a :%d",b); } getch(); }
Output
7 8 /* say a = 7 and b = 8 */.
b is greater than a :8
Program Explanation
Step 1: Include header files (#include<stdio.h> and #include<conio.h>).
Step 2: Start with main function with return type.
Step 3: parenthesis to start and end the program { }.
Step 4: declare variables with data type i.e, 'a' and 'b' are of integer type so we use "int" data type.
Step 5: Use output function printf() to print the output on the screen.
Step 6: Use input function scanf() to get input from the user.
Step 7: Here, we have to print greater number using if-else condition, Here, we use the conditional operator (i.e > greater than) to check the condition. if condition checks whether 'a' is greater than 'b' or not.
- this condition is true, then it will execute if block, which will show the output as 'a' is greater than 'b'.
- if the condition is not true, then it will execute the else block, which will show the output as 'b' is greater than 'a'.
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.