Chapter # 4 (Conditional
Control structure)
LAB ACTIVITIES
1)
Write a program that reads a number and prints its
square if it is greater than 10.
Ans:
Program:
#include<stdio.h>
#include<conio.h>
int main(int)
{
int
n;
printf(“\nEnter
a number:”);
scanf(“%d”,
&n);
if(n>10)
printf(“\nThe
square of %d is %d”, n, n*n);
getch();
}
Output:
Enter a number:12
The square of 12 is 144.
2) Write a program
that reads two numbers and prints the larger.
Program:
#include<stdio.h>
#include<conio.h>
int main(int)
{
int x, y;
printf(“\nEnter two numbers:”);
scanf(“%d, &d”, &x,&y);
if(x>y)
printf(“\nLarger=x”);
else
printf(“\nLarger=y”);
getch();
}
Output:
Enter two numbers:5,
2
Larger=5
4) Write a program that reads a number (n) and prints a message based on its
value as given below.
Value of n Message to Print
N is greater
than zero It
is a positive number
N is less than
zero It
is a negative number
N is equal to
zero It
is equal to zero
Program:
#include<stdio.h>
#include<conio.h>
int main(int)
{
int n;
printf(“\nEnter the value of n:”);
scanf(“%d, &n”);
if(n>0)
printf(“\nIt is a positive number”);
if else(n<0)
printf(“\nIt is a negative number”);
else(n==0)
printf(“\nIt is equal to zero”);
getch();
}
5) Write a program that reads temperature (t) in Celsius and prints a
message as given below.
Temperature Message to Print
t>35 It
is hot!
t>20, t<35 Nice
Day!
t<20 It
is cold!
Program:
#include<stdio.h>
#include<conio.h>
int main(int)
{
int t;
printf(“\nEnter temperature in
Celsius:”);
scanf(“%d, &t”);
if(t>35)
printf(“\nIt is hot!”);
if else((t≤ 20)&&( t≤ 35))
printf(“\nIt Nice Day!”);
else(t<0)
printf(“\nIt is Cold!”);
getch();
}
Comments
Post a Comment