Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Conditional Statement Programs

1. Write a program to check whether the entered number is less than 10? if yes,
display the same.
#include <stdio.h> Output:
int main() Enter n value3
{ Value is less than 10
int n; --------------------------------
printf("\n\t Enter n value");
Output:
scanf("%d", &n);
Enter n value22
if(n<10)
--------------------------------
printf("\n\t Value is less than 10");
return 0;
}

2. Write a C program to check Least Significant Bit (LSB) of a number is set or not.
#include <stdio.h> Output:
int main() Enter a value6
{ Significant Bit (LSB) Not Set
int n; --------------------------------
printf("\n\t Enter a value");
scanf("%d", &n); Output:
if(n&1==1) Enter a value5
printf("\n\t Significant Bit (LSB) is Set"); Significant Bit (LSB) is Set
else --------------------------------
printf("\n\t Significant Bit (LSB) Not Set");
return 0;
}

3. Write a C program that accepts 4 integers p, q, r, s from the user where r and s
are positive and p is even. If q is greater than r and s is greater than p and if the
sum of r and s is greater than the sum of p and q print "Correct values",
otherwise print "Wrong values".
#include <stdio.h> Output:
#include <stdlib.h> Enter 4 values 25 35 15 46
int main() Wrong values
{ --------------------------------
int p,q,r,s;
printf("\n\t Enter 4 values");
scanf("%d%d%d%d", &p,&q,&r,&s);
if((r>0)&&(s>0)&&(p%2==0)&&(q>r)&&(s>p)&&(r+s > p+q))
printf("\n\t Correct values"); Output:
else Enter 4 values
printf("\n\t Wrong values"); Correct values
return 0; --------------------------------
}

Dr.Mohammed Ali. Shaik


1-8 Asst. Prof. School of CS&AI, SR University
Conditional Statement Programs

4. Write a C program to check a given integer is positive even, negative even,


positive odd or negative odd. Print even if the number is 0.
#include <stdio.h>
int main() Output:
{ Enter a values22
int n; Positive Even
printf("\n\t Enter a values"); --------------------------------
scanf("%d", &n);
if(n==0) Output:
printf("\n\t Even"); Enter a values-10
else Negative Even
{ --------------------------------
if(n>0)
Output:
printf("\n\t Positive");
Enter a values23
else
Positive Odd
printf("\n\t negative");
--------------------------------
if(n%2==0)
printf(" Even"); Output:
else Enter a values-7
printf(" Odd"); Negative Odd
} --------------------------------
return 0;
}

5. Write a C program to check whether the given year is leap or not.


#include <stdio.h>
int main() Output:
{ Enter year2020
int year; Leap Year
printf("\n\t Enter year"); --------------------------------
scanf("%d", &year);
if(year%400==0) Output:
printf("\n\t Leap Year"); Enter year2022
else if(year%100==0) Not Leap Year
printf("\n\t Not Leap Year"); --------------------------------
else if(year%4==0)
printf("\n\t Leap Year");
else
printf("\n\t Not Leap Year");
return 0;
}

Dr.Mohammed Ali. Shaik


2-8 Asst. Prof. School of CS&AI, SR University
Conditional Statement Programs

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


#include <stdio.h>
int main() Output:
{ Enter a value5
int val; 5 is Odd
printf("\n\t Enter a value"); --------------------------------
scanf("%d", &val);
if(val%2==0) Output:
printf("\n\t %d is Even",val); Enter a value6
else 6 is Even
printf("\n\t %d is Odd",val); --------------------------------
return 0;
}

7. If cost price and selling price of an item is input through the keyboard, write a
program to determine whether the seller has made profit or incurred loss. Also
determine how much profit he made or loss he incurred.
#include <stdio.h> Output:
int main() Enter Cost Price2000
{ Enter Selling Price4000
float c_price, s_price; Profit
printf("\n\t Enter Cost Price"); --------------------------------
scanf("%f", &c_price);
printf("\n\t Enter Selling Price");
scanf("%f", &s_price); Output:
if(s_price>c_price) Enter Cost Price5000
printf("\n\t Profit"); Enter Selling Price4000
else Loss
printf("\n\t Loss"); --------------------------------
return 0;
}

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


#include <stdio.h>
int main() Output:
{ Enter three numbers: 3
int A, B, C; 4
printf("\n\t Enter three numbers: "); 5
scanf("%d %d %d", &A, &B, &C); 5 is the largest number.
if (A >= B) { if (A >= C) --------------------------------
printf("\n\t %d is the largest number.", A);
else
printf("\n\t %d is the largest number.", C);
}
else { if (B >= C)
printf("\n\t %d is the largest number.", B);
else
printf("\n\t %d is the largest number.", C);
} return 0;
}

Dr.Mohammed Ali. Shaik


3-8 Asst. Prof. School of CS&AI, SR University
Conditional Statement Programs

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


character.
#include <stdio.h> Output:
int main() Enter any character: a
{ a is alphabet.
char ch; --------------------------------
printf("\n\tEnter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("\n\t %c is alphabet.", ch); Output:
} Enter any character: 5
else if(ch >= '0' && ch <= '9') 5 is digit.
{ --------------------------------
printf("\n\t %c is digit.", ch);
} Output:
else Enter any character: $
{ $ is special character.
printf("\n\t %c is special character.", ch); --------------------------------
}
return 0;
}

10. 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.
#include <stdio.h>
Output:
int main()
Enter an integer value: 22
{
value of n=1.
int m,n;
--------------------------------
printf("\n\t Enter an integer value: ");
scanf("%d", &m); Output:
if(m>0) Enter an integer value: 0
n=1; value of n=0.
else if(m==0) --------------------------------
n=0; Output:
else Enter an integer value: -12
n=-1; value of n=-1.
printf("\n\t value of n=%d.", n); --------------------------------
return 0;
}

Dr.Mohammed Ali. Shaik


4-8 Asst. Prof. School of CS&AI, SR University
Conditional Statement Programs

11. Write a C program to find the eligibility of admission for a professional course
based on the following criteria: Marks in Maths>=65, Marks in Phy>=55,
Marks in Chem>=50, Total in all three subject >=180
#include <stdio.h> Output:
int main() Enter Maths, Physics, Chemistry marks: 60 70 66
{ Eligible for Admission.
int m,p,c; --------------------------------
printf("Enter Maths, Physics, Chemistry marks: ");
scanf("%d%d%d", &m,&p,&c);
if(m>=65 && p>=55 && c>=50 && (m+p+c)>=180)
printf("Eligible for Admission.");
else
printf("Not Eligible for Admission.");
return 0;
}

12. Write a program to check whether the candidate's age is greater than 17 or not.
If yes,display message "Eligible for Voting".
#include <stdio.h>
int main() Output:
{ Enter age: 15
int age; Not Eligible for Voting.
printf("Enter age: "); --------------------------------
scanf("%d", &age); Output:
if(age>17) Enter age: 35
printf("Eligible for Voting."); Eligible for Voting.
else --------------------------------
printf("Not Eligible for Voting.");
return 0;
}

13. Read the values of a,b,c through the Keyboard. Add them and after addition
check if it is in the range of 100 & 200 or not. Print separate message for each.
#include <stdio.h>
int main() Output:
{ Enter three values: 15 4 6
int a,b,c; Values are not in the range.
printf("Enter three values: "); --------------------------------
scanf("%d%d%d", &a,&b,&c); Output:
if((a+b+c)>=200 && (a+b+c)<=100) Enter three values: 55 44 66
printf("Values are in the range."); Values are in the range.
else --------------------------------
printf("Values are not in the range.");
return 0;
}

Dr.Mohammed Ali. Shaik


5-8 Asst. Prof. School of CS&AI, SR University
Conditional Statement Programs

14. Write a program to find the roots of a quadratic equation by using if else
condition. When roots are real print roots, otherwise print Roots are imaginary.
#include <stdio.h>
#include <math.h> Output:
int main() Enter a,b,c values: 10 30 20
{ root1 = -1.000000 and root2 = -2.000000
float a,b,c,r1,r2,discriminant; --------------------------------
printf("Enter a,b,c values: "); Output:
scanf("%f%f%f", &a,&b,&c); Enter a,b,c values: 2.3 4 5
discriminant = b * b - 4 * a * c; Roots are Imaginary.
if (discriminant > 0) --------------------------------
{
r1 = (-b + sqrt(discriminant)) / (2 * a);
r2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %f and root2 = %f", r1, r2);
}
else
printf("Roots are Imaginary.");
return 0;
}

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


#include <stdio.h> Output:
int main() Enter any character: a
{ a is Vowel.
char ch; --------------------------------
printf("\n\t Enter any character: ");
scanf("%c", &ch);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("\n\t %c is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("\n\t %c is Consonant.", ch); Output:
} Enter any character: b
else b is Consonant.
{ --------------------------------
printf("\n\t %c is not an alphabet.", ch); Output:
} Enter any character: 9
return 0; 9 is not an alphabet.
} --------------------------------

Dr.Mohammed Ali. Shaik


6-8 Asst. Prof. School of CS&AI, SR University
Conditional Statement Programs

16. Write a C program to input electricity unit charge and calculate the total
electricity bill according to the given condition: For first 50 units Rs. 0.50/unit,
For next 100 units Rs. 0.75/unit, For next 100 units Rs. 1.20/unit, For unit above
250 Rs. 1.50/unit, An additional surcharge of 20% is added to the bill.
#include <stdio.h>
int main() Output:
{ Enter total units consumed: 450
int unit; Electricity Bill = Rs. 624.00
float amt, total_amt, sur_charge; --------------------------------
printf("\n\t Enter total units consumed: ");
scanf("%d", &unit);
if(unit <= 50)
amt = unit * 0.50;
else if(unit <= 150)
amt = 25 + ((unit-50) * 0.75);
else if(unit <= 250)
amt = 100 + ((unit-150) * 1.20);
else
amt = 220 + ((unit-250) * 1.50);
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
printf("\n\t Electricity Bill = Rs. %.2f", total_amt);
return 0;
}

17. Write a C program to print day of week name using switch case.
#include <stdio.h>
int main()
Output:
{
Enter week number(1-7): 2
int week;
Tuesday
printf("Enter week number(1-7): ");
--------------------------------
scanf("%d", &week);
switch(week)
{
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thursday"); break;
case 5: printf("Friday"); break;
case 6: printf("Saturday"); break;
case 7: printf("Sunday"); break;
default: printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
}

Dr.Mohammed Ali. Shaik


7-8 Asst. Prof. School of CS&AI, SR University
Conditional Statement Programs

18. Write a program to convert years into 1. Minutes 2. Hours 3. Days 4. Months 5.
Seconds using switch () statement.
#include <stdio.h> Output:
int main() Enter Option: 1. Minutes, 2. Hours, 3.
{ Days, 4. Months,5. Seconds1
int years, op; Minutes=1051200
printf("\n\t Enter Number of years"); --------------------------------
scanf("%d", &years);
printf("\n\t Enter Option: 1. Minutes, 2. Hours, 3. Days, 4. Months,5. Seconds") ;
scanf("%d",&op);
switch(op)
{
case 1: printf("\n\t Minutes=%d",years*365*24*60); break;
case 2: printf("\n\t Hours =%d",years*365*24); break;
case 3: printf("\n\t Days =%d",years*365); break;
case 4: printf("\n\t Months =%d",years*12); break;
case 5: printf("\n\t Seconds =%d",years*365*24*60*60); break;
default: printf("\n\t Wrong value entered...");
}
return 0;
}

19. Write a program in C which is a Menu-Driven Program to compute the area of


the various geometrical shape
#include <stdio.h>
int main ()
{
int choice,r,l,w,b,h;
float area;
printf("\n\t Enter 1 for area of circle \n\t Enter 2 for area of rectangle");
printf("\n\t Enter 3 for area of triangle \n\t Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n\t Enter radius of the circle : ");
scanf("%d",&r);
area=3.14*r*r; break;
case 2: printf("\n\t Enter length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w; break;
case 3: printf("\n\t Enter the base and height of the triangle :");
scanf("%d%d",&b,&h);
Output:
area=.5*b*h;
Enter 1 for area of circle
break;
Enter 2 for area of rectangle
}
Enter 3 for area of triangle
printf("\n\t The area is : %f\n",area);
Enter your choice: 2
return 0;
Enter length and width of the rectangle: 60 30
}
The area is : 1800.000000
--------------------------------

Dr.Mohammed Ali. Shaik


8-8 Asst. Prof. School of CS&AI, SR University

You might also like