[go: up one dir, main page]

0% found this document useful (0 votes)
14 views16 pages

Exercises_1_3_Conditional_Statement_Solution

Uploaded by

jojojo2197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Exercises_1_3_Conditional_Statement_Solution

Uploaded by

jojojo2197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Exercises_1_3

1. Write a C program to accept two integers and check whether they are equal or not.

Test Data : 15 15

Expected Output :

Number1 and Number2 are equal

#include <stdio.h>

void main()
{
int int1, int2;

printf("Input the values for Number1 and Number2 : ");


scanf("%d %d", &int1, &int2);
if (int1 == int2)
printf("Number1 and Number2 are equal\n");
else
printf("Number1 and Number2 are not equal\n");
}

2. Write a C program to check whether a given number is even or odd.

Test Data : 15

Expected Output :

15 is an odd integer

#include <stdio.h>

void main()
{
int num1, rem1;

printf("Input an integer : ");


scanf("%d", &num1);
rem1 = num1 % 2;
if (rem1 == 0)
printf("%d is an even integer\n", num1);
else
printf("%d is an odd integer\n", num1);
}

3. Write a C program to check whether a given number is positive or negative.

Test Data : 14

Expected Output :
14 is a positive number

#include <stdio.h>

void main()
{
int num;

printf("Input a number :");


scanf("%d", &num);
if (num >= 0)
printf("%d is a positive number \n", num);
else
printf("%d is a negative number \n", num);
}

4. Write a C program to find whether a given year is a leap year or not.

Test Data : 2016

Expected Output :

2016 is a leap year.

#include <stdio.h>

void main()
{
int chk_year;

printf("Input a year :");


scanf("%d", &chk_year);
if ((chk_year % 400) == 0)
printf("%d is a leap year.\n", chk_year);
else if ((chk_year % 100) == 0)
printf("%d is a not leap year.\n", chk_year);
else if ((chk_year % 4) == 0)
printf("%d is a leap year.\n", chk_year);
else
printf("%d is not a leap year \n", 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 :

Congratulation! You are eligible for casting your vote.

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

printf("Input the age of the candidate : ");


scanf("%d",&vote_age);
if (vote_age<18)
{
printf("Sorry, You are not eligible to caste your vote.\
n");
printf("You would be able to caste your vote after %d
year.\n",18-vote_age);
}
else
printf("Congratulation! You are eligible for casting your
vote.\n");

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 ).

Test Data : 135


Expected Output :

The person is Dwarf.

#include <stdio.h>

void main()
{
float PerHeight;

printf("Input the height of the person (in centimetres) :");


scanf("%f", &PerHeight);
if (PerHeight < 150.0)
printf("The person is Dwarf. \n");
else if ((PerHeight >= 150.0) && (PerHeight <= 165.0))
printf("The person is average heighted. \n");
else if ((PerHeight >= 165.0) && (PerHeight <= 195.0))
printf("The person is taller. \n");
else
printf("Abnormal height.\n");
}

8. Write a C program to find the largest of three numbers.

Test Data : 12 25 52

Expected Output :

1st Number = 12, 2nd Number = 25, 3rd Number = 52

The 3rd Number is the greatest among three

#include <stdio.h>

void main()
{
int num1, num2, num3;

printf("Input the values of three numbers : ");


scanf("%d %d %d", &num1, &num2, &num3);
printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1,
num2, num3);
if (num1 > num2)
{
if (num1 > num3)
{
printf("The 1st Number is the greatest among three. \n");
}
else
{
printf("The 3rd Number is the greatest among three. \n");
}
}
else if (num2 > num3)
printf("The 2nd Number is the greatest among three \n");
else
printf("The 3rd Number is the greatest among three \n");
}

9. Write a C program to accept a coordinate point in a XY coordinate system and determine


in which quadrant the coordinate point lies.

Test Data : 7 9

Expected Output :

The coordinate point (7,9) lies in the First quadrant.

#include <stdio.h>

void main()
{
int co1,co2;

printf("Input the values for X and Y coordinate : ");


scanf("%d %d",&co1,&co2);

if( co1 > 0 && co2 > 0)


printf("The coordinate point (%d,%d) lies in the First quandrant.\
n",co1,co2);
else if( co1 < 0 && co2 > 0)
printf("The coordinate point (%d,%d) lies in the Second quandrant.\
n",co1,co2);
else if( co1 < 0 && co2 < 0)
printf("The coordinate point (%d, %d) lies in the Third quandrant.\
n",co1,co2);
else if( co1 > 0 && co2 < 0)
printf("The coordinate point (%d,%d) lies in the Fourth quandrant.\
n",co1,co2);
else if( co1 == 0 && co2 == 0)
printf("The coordinate point (%d,%d) lies at the origin.\
n",co1,co2);

}
}

10. Write a C program to read temperature in centigrade and display a suitable message
according to temperature state below :

Temp < 0 then Freezing weather

Temp 0-10 then Very Cold weather

Temp 10-20 then Cold weather


Temp 20-30 then Normal in Temp

Temp 30-40 then Its Hot

Temp >=40 then Its Very Hot

Test Data :

42

Expected Output :

Its very hot.

#include <stdio.h>

void main()
{
int tmp;

printf("Input days temperature : ");


scanf("%d",&tmp);
if(tmp<0)
printf("Freezing weather.\n");
else if(tmp<10)
printf("Very cold weather.\n");
else if(tmp<20)
printf("Cold weather.\n");
else if(tmp<30)
printf("Normal in temp.\n");
else if(tmp<40)
printf("Its Hot.\n");
else
printf("Its very hot.\n");

11. Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.

Test Data :

50 50 60

Expected Output :

This is an isosceles triangle.

#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);

if(sidea==sideb && sideb==sidec) //check whether all sides are equal


{
printf("This is an equilateral triangle.\n");
}
else if(sidea==sideb || sidea==sidec || sideb==sidec) //check whether two
sides are equal
{
printf("This is an isosceles triangle.\n");
}
else //check whether no sides are equal
{
printf("This is a scalene triangle.\n");
}

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 :

The triangle is not valid.

#include <stdio.h>

void main()
{
int anga, angb, angc, sum; //are three angles of a triangle

printf("Input three angles of triangle : ");


scanf("%d %d %d", &anga, &angb, &angc);

/* Calculate the sum of all angles of triangle */


sum = anga + angb + angc;

/* Check whether sum=180 then its a valid triangle otherwise not */


if(sum == 180)
{
printf("The triangle is valid.\n");
}
else
{
printf("The triangle is not valid.\n");
}

13. Write a C program to check whether a character is an alphabet, digit or special character.

Test Data :

Expected Output :

This is a special character.

#include <stdio.h>

void main()
{
char sing_ch;

printf("Input a character: ");


scanf("%c", &sing_ch);

/* Checks whether it is an alphabet */


if((sing_ch>='a' && sing_ch<='z') || (sing_ch>='A' && sing_ch<='Z'))
{
printf("This is an alphabet.\n");
}
else if(sing_ch>='0' && sing_ch<='9') /* whether it is digit */
{
printf("This is a digit.\n");
}
else /* Else special character */
{
printf("This is a special character.\n");
}
}

14. Write a C program to check whether an alphabet is a vowel or consonant.

Test Data :

Expected Output :

The alphabet is a consonant.

#include <stdio.h>

void main()
{
char sing_ch;

printf("Input any alphabet : ");


scanf("%c", &sing_ch);

if(sing_ch=='a' || sing_ch=='e' || sing_ch=='i' || sing_ch=='o' ||


sing_ch=='u' || sing_ch=='A' || sing_ch=='E' || sing_ch=='I' || sing_ch=='O'

|| 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");
}
}

15. Write a C program to calculate profit and loss on a transaction.

Test Data :

500 700

Expected Output :

You can booked your profit amount : 200

#include <stdio.h>

void main()
{
int cprice,sprice, plamt; //cprice is Cost Price and sprice is Selling
Price, plamt denotes total profit/loss

printf("Input Cost Price: ");


scanf("%d", &cprice);
printf("Input Selling Price: ");
scanf("%d", &sprice);

if(sprice>cprice) //calculate profit


{
plamt = sprice-cprice;
printf("\nYou can booked your profit amount : %d\n", plamt);
}
else if(cprice>sprice) //calculate loss
{
plamt = cprice-sprice;
printf("\nYou got a loss of amount : %d\n", plamt);
}
else //No Profit No Loss
{
printf("\nYou are running in no profit no loss condition.\n");
}
}

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 :

Input the grade :a

Expected Output :

You have chosen : Average

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main()
{

char grd;

printf("Input the grade :");


scanf("%c", &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;
}
}

18. Write a program in C to read any digit, display in the word.

Test Data :

Expected Output :

Four

#include <stdio.h>

void main()
{
int cdigit;

printf("Input Digit(0-9) : ");


scanf("%d",&cdigit);
switch(cdigit)
{
case 0:
printf("Zero\n");
break;

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 :

The area is : 78.500000

#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);
}

21. Write a program in C which is a Menu-Driven Program to perform a simple calculation.

Test Data :

10

Expected Output :

The Multiplication of 10 and 2 is: 20

#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);

printf("\nInput your option :\n");


printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-
Exit.\n");
scanf("%d",&opt);
switch(opt) {
case 1:
printf("The Addition of %d and %d is: %d\n",num1,num2,num1+num2);
break;

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;
}
}

You might also like