Anda di halaman 1dari 12

Conditional Statements :

==================

The term "Conditional Statements" refers to


specifying a condition and the condition can hold
true or false.
The general form is ,

if(condition)
Statement1;
else
Statement2;

Statement1 will get executed when the specified


condition is true and Statement2 will get executed
when the specified condition is false.
Q. WAP to read the number and check whether it is
positive or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;

clrscr();

//read the number

printf(“\nEnter the number :”);


scanf(“%d”,&num);

//check whether the number is positive or not

if(num>0)
printf(“\nNumber is positive”);
else
printf(“\nNumber is not positive.”);

getch();
}

Q WAP to read the number and check whether the


number is even or odd .
#include<stdio.h>
#include<conio.h>
void main()
{
int num;

clrscr();

//read the number

printf(“\nEnter the number :”);


scanf(“%d”,&num);

//check the number

if(num%2==0)
printf(“\nNumber is Even”);
else
printf(“\nNumber is Odd.”);

getch();
}

Q WAP to read the points (x1,y1) , (x2,y2) and


(x3,y3) and check whether all the points lies on the
same line

Formula used ,

y2-y1 = y3-y2
---------- ----------
x2-x1 x3-x2

#include<stdio.h>
#include<conio.h>
void main()
{
int x1,x2,y1,y2,x3,y3;

int d1,d2;
clrscr();

//read all the points

printf(“\nEnter the value of x1,y1,x2,y2,x3,and


y3 :”);
scanf(“%d %d %d %d %d
%d”,&x1,&y1,&x2,&y2,&x3,&y3);

//Find the values of d1 and d2

d1=(y2-y1)/(x2-x1);

d2=(y3-y2)/(x3-x2);

if(d1==d2)
printf(“\nLies in the same line”);
else
printf(“\nDoes not lies on the same line”);
getch();
}

Q WAp to read the three digit number and check


whether the number is palindrome or not .

num=121

rev=121

Number is palindrome

Now consider the following statements ,

a=num/100 => 121/100 = 1

b=num/10%10 => 121/10%10 = 2

c= num%10 = > 121%10 =1

Q WAP to read the three digit number and check


whether the number is armstrong or not
num=153

sum = 1^3+5^3+3^3=153

Number is armstrong

Q WAP to read a number and check whether the


number is positive , negative or zero.

#include<stdio.h>
#include<conio.h>
void main()
{
int num;

clrscr();

printf(“\nEnter the number :”);


scanf(“%d”,&num);
if(num>0)
printf(“\nNumber is positive.”);
else
if(num<0)
printf(“\nNumber is negative”);
else
printf(“\nNumber is zero.”);

getch();

Q . WAP to read the year and check whether the year


is leap or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int year;

clrscr();
printf(“\nEnter the year :”);
scanf(“%d”,&year);

if((year%4==0&&year%100!=0)||(year
%400==0))
printf(“\nLeap Year.”);
else
printf(“\nNot a Leap Year.”);

getch();
}

Q .WAp to read the three numbers and find the


second maximum of the three numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max,smax;
clrscr();

printf(“\nEnter the value of a,b and c:”);


scanf(“%d %d %d”,&a,&b,&c);

if(a>b)
{
max=a;

smax=b;

}
else
{
max=b;

smax=a;
}

if(c>max)
{
smax=max;
max=c;

}
elseif(c>smax)
smax=c;

printf(“\nSecond Maximum = %d ", smax);

getch();
}