This statement is also a conditional statement. It starts with keyword ‘if’ followed by an expression followed by “ first statement” and “ second statement” is given after ‘else’ clause.
Format:
if(expression)
statement1;
else
statement2;
If the expression is false then statement2 will be executed.
Similarly, if there are more statement before ‘else’ then a block is to be formed and similarly if there are many statements after the ‘else’ clause then it’s separate block is formed.
Such statement is called “Compound if else” statement.
/*Program to check whether the number is odd or even and data is entered by the user*/
#include<stdio.h>#include<conio.h>
void main()
{
int n1;
printf("\n enter any number");
scanf("%d",&n1);
if(n1%2==0)
printf("number %d is even",n1);
else
printf("number %d is odd",n1);
getch();
}
/*Program to find which number is greater*/
#include<stdio.h>#include<conio.h>
void main()
{
int n1,n2;
printf("\n enter 1st number");
scanf("%d",&n1);
printf("\n enter 2nd number");
scanf("%d",&n2);
if(n1>n2)
printf("\n %d is greater than %d",n1,n2);
else
printf("\n %d is smaller than %d",n1,n2);
getch();
}
/*Program to enter item code, name, price, quantity by the user and calculate total amount that a customer have to pay*/
#include<stdio.h>#include<conio.h>
void main()
{
int itcd;
char nm[40];
float qty,pr,d,tamt,namt;
printf("\n enter item name");
scanf("%s",nm);
printf("\n enter item code");
scanf("%d",&itcd);
printf("\n enter price and quantity(in Kg) of the item");
scanf("%f%f",&pr,&qty);
tamt=pr*qty;
if(tamt>500)
{
d=10;
namt=tamt-(tamt*10.0/100.0);
}
else
{
d=5;
namt=tamt-(tamt*5.0/100.0);
}
printf("\n discount on purchase is %f",d);
printf("\n net amount given by customer is %f",namt);
getch();
}
/*Program to enter any number and check whether number entered is positive or negative*/
#include<stdio.h>#include<conio.h>
void main()
{
int n1;
printf("\n enter any real number");
scanf("%d",&n1);
if(n1>=0)
printf("\n number %d is positive",n1);
else
printf("\n number %d is negative",n1);
getch();
}
PRECAUTIONS
1. Use accurate header files.2. Show proper expression in if statement.
3. End statement with ';'.
4. Declare the statements carefully using proper symbols.
No comments:
Post a Comment