Exercises_1_3_Conditional_Statement_Solution
Exercises_1_3_Conditional_Statement_Solution
1. Write a C program to accept two integers and check whether they are equal or not.
Test Data : 15 15
Expected Output :
#include <stdio.h>
void main()
{
int int1, int2;
Test Data : 15
Expected Output :
15 is an odd integer
#include <stdio.h>
void main()
{
int num1, rem1;
Test Data : 14
Expected Output :
14 is a positive number
#include <stdio.h>
void main()
{
int num;
Expected Output :
#include <stdio.h>
void main()
{
int chk_year;
5. Write a C program to read the age of a candidate and determine whether it is eligible for
casting his/her own vote (his age must be greater than 18 year).
Test Data : 21
Expected Output :
#include <stdio.h>
void main()
{
int vote_age;
6. Write a C program to read the value of an integer m and display the value of n is 1 when
m is larger than 0, 0 when m is 0 and -1 when m is less than 0.
Test Data : -5
Expected Output :
The value of n = -1
#include <stdio.h>
void main()
{
int m,n;
printf("Input the value of m :");
scanf("%d",&m);
if(m!=0)
if(m>0)
n=1;
else
n=-1;
else
n=0;
printf("The value of m = %d \n",m);
printf("The value of n = %d \n",n);
}
7. Write a C program to accept the height of a person in centimeter and categorize the
person according to their height (he is abnormal height if his tall less than 135 ,he is
dwarf if his tall less than 150, he is average heighted if his tall is between 150 and 165, he
is taller if his tall greater than or equal to 165 ).
#include <stdio.h>
void main()
{
float PerHeight;
Test Data : 12 25 52
Expected Output :
#include <stdio.h>
void main()
{
int num1, num2, num3;
Test Data : 7 9
Expected Output :
#include <stdio.h>
void main()
{
int co1,co2;
}
}
10. Write a C program to read temperature in centigrade and display a suitable message
according to temperature state below :
Test Data :
42
Expected Output :
#include <stdio.h>
void main()
{
int tmp;
Test Data :
50 50 60
Expected Output :
#include <stdio.h>
int main()
{
int sidea, sideb, sidec; //are three sides of a triangle
/*
* Reads all sides of a triangle
*/
printf("Input three sides of triangle: ");
scanf("%d %d %d", &sidea, &sideb, &sidec);
return 0;
}
12. Write a C program to check whether a triangle can be formed by the given value for the
angles.
Test Data :
40 55 65
Expected Output :
#include <stdio.h>
void main()
{
int anga, angb, angc, sum; //are three angles of a triangle
13. Write a C program to check whether a character is an alphabet, digit or special character.
Test Data :
Expected Output :
#include <stdio.h>
void main()
{
char sing_ch;
Test Data :
Expected Output :
#include <stdio.h>
void main()
{
char sing_ch;
|| sing_ch=='U')
{
printf("The alphabet is a vowel.\n");
}
else if((sing_ch>='a' && sing_ch<='z') || (sing_ch>='A' && sing_ch<='Z'))
{
printf("The alphabet is a consonant.\n");
}
else
{
printf("The character is not an alphabet.\n");
}
}
Test Data :
500 700
Expected Output :
#include <stdio.h>
void main()
{
int cprice,sprice, plamt; //cprice is Cost Price and sprice is Selling
Price, plamt denotes total profit/loss
16. Write a program in C to accept a grade and declare the equivalent description :
Grade Description
E Excellent
V Very Good
G Good
A Average
F Fail
Test Data :
Expected Output :
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void main()
{
char grd;
switch(grd)
{
case 'E':
strcpy(notes, " Excellent");
break;
case 'V':
strcpy(notes, " Very Good");
break;
case 'G':
strcpy(notes, " Good ");
break;
case 'A':
strcpy(notes, " Average");
break;
case 'F':
strcpy(notes, " Fails");
break;
default :
strcpy(notes, "Invalid Grade Found. \n");
break;
}
printf("You have chosen : %s\n", notes);
}
17. Write a program in C to read any day number in integer and display day name in the
word.
Test Data :
Expected Output :
Thursday
#include <stdio.h>
void main()
{
int dayno;
printf("Input Day No : ");
scanf("%d",&dayno);
switch(dayno)
{
case 1:
printf("Monday \n");
break;
case 2:
printf("Tuesday \n");
break;
case 3:
printf("Wednesday \n");
break;
case 4:
printf("Thursday \n");
break;
case 5:
printf("Friday \n");
break;
case 6:
printf("Saturday \n");
break;
case 7:
printf("Sunday \n");
break;
default:
printf("Invalid day number. \nPlease try again ....\n");
break;
}
}
Test Data :
Expected Output :
Four
#include <stdio.h>
void main()
{
int cdigit;
case 1:
printf("one\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("invalid digit. \nPlease try again ....\n");
break;
}
}
19. Write a program in C to read any Month Number in integer and display Month name in
the word.
Data :
Expected Output :
April
#include <stdio.h>
void main()
{
int monno;
printf("Input Month No : ");
scanf("%d",&monno);
switch(monno)
{
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("MAy\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("invalid Month number. \nPlease try again ....\n");
break;
}
}
20. Write a program in C which is a Menu-Driven Program to compute the area of the
various geometrical shape.
Test Data :
Expected Output :
#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h;
float area;
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radious of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
case 2:
printf("Input length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
printf("Input the base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
}
printf("The area is : %f\n",area);
}
Test Data :
10
Expected Output :
#include <stdio.h>
void main() {
int num1,num2,opt;
printf("Enter the first Integer :");
scanf("%d",&num1);
printf("Enter the second Integer :");
scanf("%d",&num2);
case 2:
printf("The Substraction of %d and %d is: %d\n",num1,num2,num1-num2);
break;
case 3:
printf("The Multiplication of %d and %d is: %d\
n",num1,num2,num1*num2);
break;
case 4:
if(num2==0) {
printf("The second integer is zero. Devide by zero.\n");
} else {
printf("The Division of %d and %d is : %d\n",num1,num2,num1/num2);
}
break;
case 5:
break;
default:
printf("Input correct option\n");
break;
}
}