Professional Documents
Culture Documents
Selection
Selection
Selection
If one way If-else 2 way Is else if multiple way Switch multiple way
If -else
Problems if -else
P1 . Largest of three #include <stdio.h> int main() { int a, b, c, max; printf("\nEnter 3 numbers"); scanf("%d %d %d", &a, &b, &c); max=a; if(b>max) max=b; if(c>max) max=c; printf("Largest No is %d", max); return 0; }
Problems if -else
P2.Two nos equal #include <stdio.h> int main() { int a=2, b=3; if(a == b) printf("EQUAL"); else printf("UNEQUAL"); return 0; }
Problems if -else
P3. Leap year #include<stdio.h> int main() { int year, rem_4,rem_100,rem_400; printf("Enter the year to be tested:"); scanf("%d", &year); rem_4 = year % 4; rem_100 = year % 100; rem_400 = year % 400; if( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0) printf("It is a leap year.\n"); else printf("It is not a leap year.\n"); return 0; }
Problems if -else
ProblemsMultiple if -else
Problems if else if
P4. No is +, - or zero #include <stdio.h> int main() { int x; printf("\n ENTER THE NUMBER:"); scanf("%d", &x); if(x > 0) printf("x is positive \n"); else if(x == 0) printf("x is zero \n"); else printf("x is negative \n"); return 0; }
Problems if -else
p.5 grades :A, B,C,D,F #include <stdio.h> int main() { int score; char grade; printf("\n ENTER SCORE : "); scanf("%d", &score); if(score >= 90) grade = 'A'; else if(score >= 80) grade = 'B'; else if(score >= 70) grade = 'C'; else if(score >= 60) grade = 'D'; else grade = 'F'; printf("GRADE IS: %c", grade); return 0; }
Case study
Problem statement: Get the lengths of three sides of a triangle. Check whether the triangle can be formed or not. If possible then classify the triangle as equilateral, isosceles or scalene. Otherwise, if the triangle cannot be formed give the user a chance to re-enter the lengths of the sides or terminate the program.
while(ans=='y' ||ans=='Y') {
printf("\n Enter th elengths of threesides of a triangle:"); scanf("%d %d %d",&a,&b,&c);
if(a+b>c && b+c>a && a+c>b) { printf("\n Triangle can be drawn"); if(a==b && b==c) printf("\n It is a Equilateral Triangle"); else if(a==b|| b==c ||a==c) printf("\n It is a Isosceles Triangle"); else printf("\n It is a scalene Triangle"); ans='n'; } else { printf("\n Triangle cannot be drawn"); printf("\n Do you want to reenter the lengths again(y/n)? "); scanf("%c",&ans);
Switch
No two case labels have same value Case labels can be in any order Default is optional case constants can be integers or constants Break is optional Switch can be replaced by else-if
P6 #include <stdio.h> int main() { int n; printf("\n Enter the number:"); scanf("%d", &n);
P7. Write a menu driven program in C to carry out the arithmetic operations addition, subtraction, multiplication, and division between two variables
#include <stdio.h> void main() { char c; printf("Enter a character: "); scanf("%c", &c); switch(c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("%c is always a vowel!\n", c); break; case 'y': case 'Y': printf("%c is sometimes a vowel!\n", c); break; default: printf("%c is not a vowel!\n", c); }}
vowels
Write a program to display the month name by accepting the month number form user.
O/P IF USER ENTERS 4 OUTPUT SHOULD BE APRIL
Write a program using switch case to display class according to the marks scored by student Marks class 70-100 D 60-69 F 50-59 S 40-49 p 0-39 f
Break