If Statement In C Programming Language

⇒ For video tutorial you can see below video:


If Condition
if condition syntax:

If(condition)
{
                Printf(“True”);   if above condition will true then this message will print
}
Else
{
                Printf(“False”); if above condition will false then this message will print
}

Flow chart:

Program for if condition:
✔ C programming language assumes any non-zero and non-null values as true and if it is        either zero or null, then it is assumed as false value.
✔ The if statement evaluates the test expression inside the { }.
 If the test expression is evaluated to true, the first statements inside the body of if are          executed.
 If the test expression is evaluated to false, the second statements inside the body                  of if are not executed.

                                 Programs with output:

1. check the the value is 5 or not.
 #include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the value of a: \n");
scanf("%d",&a);
                if(a==5)
                                {
                                                printf("True");
                                }
                else
                                {
                                                printf("False");
                                }

getch();
}

Output:- 
If user enter 5 Then “True” and if user enter other number than “False”

2. User can give vote or not
#include<stdio.h>
#include<conio.h>
void main()
{

int a;
clrscr();
printf("Enter Age: \n");
scanf("%d",&a);
                if(a>=18)
                                {
                                                printf("Can Give Vote");
                                }
                else
                                {
                                                printf("Cannot Give Vote");
                                }

getch();
}

Output :-
If user enter more than 18 than “Can give vote” Or less than 18 than “Cannot give vote”.

3. Find number is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{

int a;
clrscr();
printf("Enter Number: \n");
scanf("%d",&a);
                if(a>0)
                                {
                                                printf("No is Positive");
                                }
                else
                                {
                                                printf("No is Negitive");
                                }

getch();
}

Output:-
If user enter value more than 0 mean 1, 5, any number than “Positive”
Or user enter less than 0 mean -1 , -2 or any number than “Negitive”.

4. Student is pass or fail.
#include<stdio.h>
#include<conio.h>
void main()
{

int a;
clrscr();
printf("Enter Marks: \n");
scanf("%d",&a);
                if(a>=35)
                                {
                                                printf("Student is Pass");
                                }
                else
                                {
                                                printf("Student is fail");
                                }

getch();
}

Output:-
If user enter more than 35 mean 36, 66, 87 any number than “Student is Pass”
If user enter less than 35 mean 34, 34, 22 any number than “Student is Fail”.

5. Person is young or child.
#include<stdio.h>
#include<conio.h>
void main()
{

int a;
clrscr();
printf("Enter Age: \n");
scanf("%d",&a);
                if(a>18)
                                {
                                                printf("Person is Young");
                                }
                else
                                {
                                                printf("Person is Child");
                                }

getch();
}

Output:-
If user enter age more than 18 than like 19, 22, 34 or any number than “Person is Young”
If user enter age less than 18 than like 17, 16, 10 or any number than “Person is Child”.

6. Find number is odd or even.
#include<stdio.h>
#include<conio.h>
void main()
{

int a;
clrscr();
printf("Enter the value of a: ");
scanf("%d",&a);
                if(a%2==0)
                                {
                                                printf("No is even");
                                }
                else
                                {
                                                printf("No is Odd");
                                }

getch();
}

Output:-
If user enter 2,4,6,8,…any even number than “No is even”
If user enter 1,3,5,7,9… any odd number than “No is odd”.

7. Find year is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{

int a;
clrscr();
printf("Enter Year: ");
scanf("%d",&a);
                if(a%4==0)
                                {
                                                printf("Year is leap year");
                                }
                else
                                {
                                                printf("Year is not leap year");
                                }

getch();
}

Output:-
If user enter 2000,2004,2008,1998,2020 like this year than “Year is leap year”
If user enter 2001, 2002, 2005, 2019 like this year than “Year is not leap year”.

8. Compare 2 number and find which number is greater.
#include<stdio.h>
#include<conio.h>
void main()
{

int a,b;
clrscr();
printf("Enter The Value of A: \n");
scanf("%d",&a);
printf("Enter The Value of B: \n");
scanf("%d",&b);
                if(a>b)
                                {
                                                printf("A is greater than B");
                                }
                else
                                {
                                                printf("B is greater than A");
                                }

getch();
}

Output:-
Enter the value of A : 20
Enter the value of B: 10
A is greater than B


Post a Comment

0 Comments