Conditional operators pgms with solns.pptx

GayathriM270621 24 views 52 slides Jun 11, 2024
Slide 1
Slide 1 of 52
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52

About This Presentation

conditional operators


Slide Content

CONDITIONAL STATEMENT C PROGRAMS: 14/03/2024 1) Write a C program to accept two integers and check whether they are equal or not.

#include < stdio.h > int main() { int a,b ; scanf ("% d",&a ); scanf ("% d",&b ); // accept two integers from user if(a==b)//for checking 2 integers whether equal or not { printf ("%d and %d are equal", a,b ); } else { printf ("%d and %d are not equal", a,b ); } return 0; } QUESTION 1

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

#include < stdio.h > int main() { int a; scanf ("% d",&a ); if(a%2==0)//condition for checking even number { printf ("%d is a even number",a ); } else { printf ("%d is a odd number",a ); } return 0; } Question 2

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

#include< stdio.h > int main() { int a; scanf ("% d",&a ); if(a>=0)//condition for positive number { printf ("%d is a positive number",a ); } else { printf ("%d is a negative number",a ); } return 0; } QUESTION 3

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

#include < stdio.h > int main() { int year; printf ("enter the leap year:"); scanf ("%d", &year); //condition for checking given year is 4 digit number or not if(year>=1000 && year<= 9999) { //condition for checking leap year if(year%4==0 || year%400==0) { printf ("\ n%d is a leap year", year); } else { printf ("\ n%d is not a leap year", year); } } else { printf ("\ ngiven year is invalid"); } return 0; } QUESTION 4

5) Write a C program to read the age of a candidate and determine whether it is eligible for casting his/her own vote.

#include < stdio.h > int main() { int age; printf ("enter the age:"); scanf ("% d",&age ); if(age>=18) { printf ("candidate is eligible to vote"); } else { printf ("candidate is not eligible to vote"); } return 0; } QUESTION 5

CONDITIONAL STATEMENT C PROGRAMS: 15/3/2024 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.

CODE: #include< stdio.h > i nt main() { printf (“enter the number:”); //read the value of an integer scanf (“% d”,&m ); if(m>0) printf (“n is 1”); else if(m==0) printf (“n is 0”); else printf (“n is -1”); return 0; } QUESTION 6

7) Write a C program to accept the height of a person in centimeter and categorize the person according to their height.

CODE: #include< stdio.h > int main() { int height; printf ("enter the height of a person in centimeter:"); scanf ("% d",&height ); if(height<=150) printf ("The person is dwarf"); else if(height>=151 && height<=165) printf ("The person is average"); else printf ("The person is tall"); return 0; } QUESTION 7

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

CODE: #include<stdio.h> int main() { int n1,n2,n3; printf("enter the numbers:"); scanf("%d\n %d\n %d",&n1,&n2,&n3); if(n1>=n2 && n1>=n3) printf("%d is largest number",n1); else if(n2>=n1 && n2>=n3) printf("%d is largest number",n2); else printf("%d is largest number",n3); return 0; } QUESTION 8

9) Write a C program to accept a coordinate point in a XY coordinate system and determine in which quadrant the coordinate point lies.

#include<stdio.h> void main() { int x,y; while(1) { printf("\n enter the x coordinate: "); scanf("%d",&x); printf("\n enter the y coordinate: "); scanf("%d",&y); if(x>0 && y>0) printf("\n%d and %d lies in 1st quadrant",x,y); else if(x<0 && y>0) printf("\n%d and %d lies in 2nd quadrant",x,y); else if(x<0 && y<0) printf("\n%d and %d lies in 3rd quadrant",x,y); else if(x>0 && y<0) printf("\n%d and %d lies in 4th quadrant",x,y); else if(x==0 && y==0) printf("\n%d and %d lies in origin",x,y); printf("\n); } } QUESTION 9

10) Write a C program to find the eligibility of admission for a professional course based on the following criteria: Eligibility Criteria : Marks in Maths >=65 and Marks in Phy >=55 and Marks in Chem>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140 ---Input the marks obtained in Physics :65 Input the marks obtained in Chemistry : 51 Input the marks obtained in Mathematics :72 Total marks of Maths, Physics and Chemistry : 188 Total marks of Maths and Physics : 137 - The candidate is not eligible.

#include<stdio.h> int main() { //eligibility of admission for a professional course int math,phy,chem,total1,total2; printf("enter the marks of each subject:"); printf("\nMaths mark:"); scanf("%d",&math); printf("\nPhysics mark:"); scanf("%d",&phy); printf("\nChemistry mark:"); scanf("%d",&chem); total1 = math+phy+chem; total2 = math+phy; printf("\nTotal1 : %d\n",(math+phy+chem)); if(math>=65 && phy>=55 && chem>=50 && total1>=190 || total2>=140) printf("candidate is eligible for the course"); else printf("candidate is not eligible for the course"); return 0; } QUSTION 10

11. Write a C program to calculate the root of a Quadratic Equation

#include <stdio.h> #include <math.h> int main() { //ax^2+bx+c=0 is a quadratic equation double a,b,c,discriminant,x1,x2; printf("enter the coefficient of x^2:"); scanf("%lf",&a); printf("enter the coefficient of x:"); scanf("%lf",&b); printf("enter the constant:"); scanf("%lf",&c); //to calculate the root of quadratic equation discriminant = (b * b) - (4 * a * c); //case 1 roots are real and distinct if(discriminant >0) { x1=(-b+sqrt(discriminant))/(2*a); x2=(-b-sqrt(discriminant))/(2*a); printf("Roots are real and distinct.\n"); printf("Root 1 = %.2lf\n", x1); printf("Root 2 = %.2lf\n", x2); } //case 2 roots are real and same else if (discriminant == 0) { x1 = -b / (2 * a); printf("Roots are real and same.\n"); printf("Root 1 = Root 2 = %.2lf\n", x1); } //case 3 roots are complex and different else { double realPart = -b / (2 * a); double imaginaryPart = sqrt(-discriminant) / (2 * a); printf("Roots are complex and different.\n"); printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart); printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart); } return 0; }

12. Write a C program to read roll no, name and marks of three subjects and calculate the total, percentage and division

//division of grades if(percentage<35) printf("Division for your marks : FAIL"); else if(percentage>=35 && percentage<50) printf("Division for your marks : PASS"); else if(percentage>=50 && percentage<70) printf("Division for your marks : GOOD"); else if(percentage>=70 && percentage<90) printf("Division for your marks : VERY GOOD"); else if(percentage>=90 && percentage<=100) printf("Division for your marks : EXCELLENT"); return 0; } #include<stdio.h> int main() { double roll_no, sub1, sub2, sub3, total, percentage; char name; printf("enter the roll no :"); scanf("%lf",&roll_no); printf("enter the name :"); scanf("%s",&name); printf("enter the marks in subject 1:"); scanf("%lf",&sub1); printf("enter the marks in subject 2:"); scanf("%lf",&sub2); printf("enter the marks in subject 3:"); scanf("%lf",&sub3); total=sub1+sub2+sub3; printf("The total marks obtained : %lf\n",total); percentage=((sub1+sub2+sub3)/3); printf("Overall percentage obtained : %0.2lf\n",percentage);

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

#include<stdio.h> int main() { int temp; printf("enter the temperature in centigrade:"); scanf("%d",&temp); if(temp<0) printf("Freezing weather"); else if(temp>=0 && temp<10) printf("very cold weather"); else if(temp>=10 && temp<20) printf("cold weather"); else if(temp>=20 && temp<30) printf("normal in temperature"); else if(temp>=30 && temp<40) printf("It's hot"); else if(temp>=40) printf("it's' very hot"); return 0; }

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

#include <stdio.h> int main() { int sidea, sideb, sidec; printf("Input three sides of triangle: "); scanf("%d %d %d", &sidea, &sideb, &sidec); if(sidea==sideb && sideb==sidec) // Check if all sides are equal. printf(" This is an equilateral triangle.\n"); else if(sidea==sideb || sidea==sidec || sideb==sidec) // Check if two sides are equal. printf(" This is an isosceles triangle.\n"); else // If no sides are equal. printf(" This is a scalene triangle.\n"); return 0; }

15) Write a C program to check whether a triangle can be formed by the given value for the angles.

#include<stdio.h> int main() { int ang1,ang2,ang3; printf("enter the 3 angles for the triangle:"); scanf("%d %d %d",&ang1,&ang2,&ang3); printf("The total angle: %d\n",ang1+ang2+ang3); if((ang1+ang2+ang3)==180) printf("The triangle can form"); else printf("The triangle can't form"); return 0; }

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

#include <stdio.h> int main() { char ch; printf("Enter any character: "); scanf("%c", &ch); if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90)) printf("'%c' is alphabet.", ch); else if(ch >= 48 && ch <= 57) printf("'%c' is digit.", ch); else printf("'%c' is special character.", ch); return 0; }

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

#include<stdio.h> int main() { char ch; printf("enter the alphabet:"); 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("The given alphabet is an vowel"); else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')) printf("The given alphabet is consonant"); else printf("The given character is not an alphabet"); return 0; }

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

#include <stdio.h> int main() { int cp, sp, profit_amt, loss_amt; printf("Input Cost Price: "); scanf("%d", &cp); printf("Input Selling Price: "); scanf("%d", &sp); if(sp > cp) { profit_amt = sp - cp; printf("\nYou can book your profit amount : %d\n", profit_amt); } else if(cp> sp) { loss_amt = cp - sp; printf("\nYou have a loss of amount : %d\n", loss_amt); } else { printf("\nYou are in a no profit, no loss condition.\n"); } return 0; }

19. Write a program in C to calculate and print the Electricity bill of a given customer. The customer id., name and unit consumed by the user should be taken from the keyboard and display the total amount to pay to the customer. The charge are as follow : Unit Charge/unit upto 199 @1.20 200 and above but less than 400 @1.50 400 and above but less than 600 @1.80 600 and above @2.00 If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum bill should be of Rs. 100/-

#include<stdio.h> int main() { double unit,id,charge,total_amt,surchg,netamt; printf("enter the customer id:"); scanf("%lf",&id); printf("enter the units consumed by consumer:"); scanf("%lf",&unit); if(unit<200) charge=1.20; else if(unit>=200 && unit<400) charge=1.50; else if(unit>=400 && unit<600) charge=1.80; else if(unit>=600) charge=2.00; total_amt = unit * charge; if (total_amt > 400) { surchg = total_amt * 15 / 100.0; netamt = total_amt + surchg; } else if (total_amt < 100) netamt = 100; netamt=total_amt; printf("\nElectricity Bill\n"); printf("Customer IDNO :%lf\n", id); printf("unit Consumed :%lf\n", unit); printf("Amount Charges @Rs. %lf per unit :%lf\n", charge, total_amt); printf("Surchage Amount :%lf\n", surchg); printf("Net Amount Paid By the Customer :%lf\n", netamt); return 0; }

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

#include<stdio.h> int main() { char grade; printf("enter the grade :"); scanf("%c",&grade); switch(grade) { case'E': printf("EXCELLENT"); break; case'V': printf("VERY GOOD"); break; case'G': printf("GOOD"); break; case'A': printf("AVERAGE"); break; case'F': printf("FAIL"); break; default: printf("Your grade is invalid"); } return 0; }

21. Write a program in C to read any day number in integer and display day name in the word.

#include<stdio.h> int main() { int day; printf("enter the day number:"); scanf("%d",&day); switch(day) { 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("entered invalid day number"); return 0; } }

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

#include <stdio.h> void main() { int digit; printf("Input Digit(0-9) : "); scanf("%d",&digit); switch(digit) { 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"); } }

23. Write a program in C to read any Month Number in integer and display Month name in the word

#include <stdio.h> void main() { int month_no; printf("Input Month No : "); scanf("%d",&month_no); switch(month_no) { 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"); } }

24. Write a program in C to read any Month Number in integer and display the number of days for this month.

#include <stdio.h> void main() { int month_no; printf("Input Month No : "); scanf("%d",&month_no); switch(month_no) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("Month have 31 days"); break; case 2: printf("The 2nd month is a February and have 28 days"); printf("in leap year The February month Have 29 days"); break; case 4: case 6: case 9: case 11: printf("Month have 30 days"); break; default: printf("Invalid Month number"); } }

25. Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape.

void main () { int choice,r,l,w,b,h,s; 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 4 for area of square\n"); printf("Input your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Input radius of the circle : "); scanf("%d",&r); area=3.14*r*r; printf("The area of circle is : %f\n",area); break; case 2: printf("Input length and width of the rectangle : "); scanf("%d %d",&l,&w); area=l*w; printf("The area rectangle is : %f\n",area); break; case 3: printf("Input the base and hight of the triangle :"); scanf("%d%d",&b,&h); area=.5*b*h; printf("The area of triangle is : %f\n",area); break; case 4: printf("Input the side of the square :"); scanf("%d",&s); area=s*s; printf("The area of square is : %f\n",area); break; default: printf("please choose from the given options."); } }

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

#include <stdio.h> void main () { int option, num1, num2; printf("\nInput your option :\n"); printf("1-Addition \n"); printf("2-Substraction \n"); printf("3-Multiplication \n"); printf("4-Division \n"); scanf("%d",&option); printf("Enter the first Integer : "); scanf("%d",&num1); printf("Enter the second Integer : "); scanf("%d",&num2); switch(option) { 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 cannot be zero. \n"); else printf("The Division of %d and %d is : %d\n",num1,num2,num1/num2); break; default: printf("plz select correct option next time\n"); } }
Tags