What is if statement:
if statement is used to take a decision. if statement is a conditional statement that means in a program when we need to take a decision on the basis any data then we used a statement called "if" statement.
SYNTAX:
if(statement)
{
Statement 1
Statement 2
Statement n
}
else
{
Statement 1
Statement 2
Statement n
}
EXAMPLE OF SOME SAMPLE PROGRAM OF IF ELSE STATEMENT
1. Write a program where show "TRUE" when the user input 1 and if press others then show "FALSE".
CODE:-
OUTPUT:-
2. Write a program to show a given number is ODD or EVEN.
CODE:-
OUTPUT:-
3. Write a program to show a given number is greater than 100 or not.
4. Write a program where the user input a digit and if the digit is grater than 10 then show your name and if not then show your friend's name.
CODE:-
5. Write a program to show a given marks is PASS or FAIL, PASS marks is 50 and greater.
CODE:-
6. Write a program to show "CAPITAL LETTER" when the user input capital letter and show "SMALL LETTER" when the user input small letter.
CODE:-# include <stdio.h>
#include <conio.h>
int main()
{
char Letter;
printf("Enter your letter: ");
scanf("%c",&Letter);
if(Letter>=97)
{
printf("..............%c",Letter-32);
}
else
{
printf("...............%c",Letter+32);
}
return 0;
}
OUTPUT:-
7. Write a program where if the user input small letter then the result will be the capital letter and if the user input capital letter then the result will be small letter.
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
char Letter;
printf("Enter your letter: ");
scanf("%c",&Letter);
if(Letter>=97)
{
printf("..............%c",Letter-32);
}
else
{
printf("...............%c",Letter+32);
}
return 0;
}
OUTPUT:-
8. Write a program to show greater number between two given numbers.
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
int Number1,Number2;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
if(Number1>Number2)
{
printf("%d is greater than %d",Number1,Number2);
}
else
{
printf("%d is greater than %d",Number2,Number1);
}
return 0;
}
OUTPUT:-
9. Write a program to show greater number between 3 given numbers.
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
int Number1,Number2,Number3;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
printf("Enter your Number3: ");
scanf("%d",&Number3);
if((Number1>Number2)&&(Number1>Number3))
printf("%d is greatest Number ",Number1);
else
if((Number2>Number1)&&(Number2>Number3))
printf("%d is greatest number",Number2);
else
if((Number3>Number1)&&(Number3>Number2))
printf("%d is greatest number",Number3);
return 0;
}
OUTPUT:-
Write a program to show smaller number between two given numbers
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
int Number1,Number2;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
if(Number1<Number2)
{
printf("%d is smaller than %d",Number1,Number2);
}
else
{
printf("%d is smaller than %d",Number2,Number1);
}
return 0;
}
OUTPUT:-
10. Write a program to show smaller number between 3 given numbers.
# include <stdio.h>
#include <conio.h>
int main()
{
int Number1,Number2,Number3;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
printf("Enter your Number3: ");
scanf("%d",&Number3);
if((Number1<Number2)&&(Number1<Number3))
printf("%d is smallest Number ",Number1);
else
if((Number2<Number1)&&(Number2<Number3))
printf("%d is smallest number",Number2);
else
if((Number3<Number1)&&(Number3<Number2))
printf("%d is smallest number",Number3);
return 0;
}
OUTPUT:-
1. Write a program where show "TRUE" when the user input 1 and if press others then show "FALSE".
CODE:-
- # include <stdio.h>
- #include <conio.h>
- int main()
- {
- int Num;
- printf("Enter your number: ");
- scanf("%d",&Num);
- if(Num == 1)
- {
- printf("Input number is TRUE ");
- }
- else
- {
- printf("Enter number is FALSE");
- }
- return 0;
- }
OUTPUT:-
2. Write a program to show a given number is ODD or EVEN.
CODE:-
- # include <stdio.h>
- #include <conio.h>
- int main()
- {
- int Num;
- printf("Enter your number: ");
- scanf("%d",&Num);
- if(Num%2==0)
- {
- printf("Input number is EVEN ");
- }
- else
- {
- printf("Enter number is ODD");
- }
- return 0;
- }
OUTPUT:-
3. Write a program to show a given number is greater than 100 or not.
- # include <stdio.h>
- #include <conio.h>
- int main()
- {
- int Num;
- printf("Enter your number: ");
- scanf("%d",&Num);
- if(Num>100){
- printf("Input number is GREATER ");
- }
- else{
- printf("Enter number is NOT GREATER");
- }
- return 0;
- }
OUTPUT:-
4. Write a program where the user input a digit and if the digit is grater than 10 then show your name and if not then show your friend's name.
CODE:-
- # include <stdio.h>
- #include <conio.h>
- int main()
- {
- int Num;
- printf("Enter your number: ");
- scanf("%d",&Num);
- if(Num>10){
- printf("MY name is John ");
- }
- else{
- printf(""My friend's name is Mike");
- }
- return 0;
- }
OUTPUT:-
CODE:-
- # include <stdio.h>
- #include <conio.h>
- int main()
- {
- int Num;
- printf("Enter your number: ");
- scanf("%d",&Num);
- if(Num>10){
- printf("MY name is John ");
- }
- else{
- printf(""My friend's name is Mike");
- }
- return 0;
- }
OUTPUT:-
6. Write a program to show "CAPITAL LETTER" when the user input capital letter and show "SMALL LETTER" when the user input small letter.
CODE:-# include <stdio.h>
#include <conio.h>
int main()
{
char Letter;
printf("Enter your letter: ");
scanf("%c",&Letter);
if(Letter>=97)
{
printf("..............%c",Letter-32);
}
else
{
printf("...............%c",Letter+32);
}
return 0;
}
OUTPUT:-
7. Write a program where if the user input small letter then the result will be the capital letter and if the user input capital letter then the result will be small letter.
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
char Letter;
printf("Enter your letter: ");
scanf("%c",&Letter);
if(Letter>=97)
{
printf("..............%c",Letter-32);
}
else
{
printf("...............%c",Letter+32);
}
return 0;
}
OUTPUT:-
8. Write a program to show greater number between two given numbers.
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
int Number1,Number2;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
if(Number1>Number2)
{
printf("%d is greater than %d",Number1,Number2);
}
else
{
printf("%d is greater than %d",Number2,Number1);
}
return 0;
}
OUTPUT:-
9. Write a program to show greater number between 3 given numbers.
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
int Number1,Number2,Number3;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
printf("Enter your Number3: ");
scanf("%d",&Number3);
if((Number1>Number2)&&(Number1>Number3))
printf("%d is greatest Number ",Number1);
else
if((Number2>Number1)&&(Number2>Number3))
printf("%d is greatest number",Number2);
else
if((Number3>Number1)&&(Number3>Number2))
printf("%d is greatest number",Number3);
return 0;
}
OUTPUT:-
Write a program to show smaller number between two given numbers
CODE:-
# include <stdio.h>
#include <conio.h>
int main()
{
int Number1,Number2;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
if(Number1<Number2)
{
printf("%d is smaller than %d",Number1,Number2);
}
else
{
printf("%d is smaller than %d",Number2,Number1);
}
return 0;
}
10. Write a program to show smaller number between 3 given numbers.
#include <conio.h>
int main()
{
int Number1,Number2,Number3;
printf("Enter your Number1: ");
scanf("%d",&Number1);
printf("Enter your Number2: ");
scanf("%d",&Number2);
printf("Enter your Number3: ");
scanf("%d",&Number3);
if((Number1<Number2)&&(Number1<Number3))
printf("%d is smallest Number ",Number1);
else
if((Number2<Number1)&&(Number2<Number3))
printf("%d is smallest number",Number2);
else
if((Number3<Number1)&&(Number3<Number2))
printf("%d is smallest number",Number3);
return 0;
}
OUTPUT:-