Sei sulla pagina 1di 30

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

Program for lcm and gcd

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.

#include <stdio.h> void main() { int a,b,c; char ans='y';

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

Write a program to display the factors of a user entered number.

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

Program using switch for even or odd


P6 #include <stdio.h> int main() { int n; printf("\n Enter the number:"); scanf("%d", &n);

switch(n%2) { case 0: printf("\n EVEN"); break; case 1: printf("\n ODD"); break; }


return 0;

Write a program to display single digit no. in words

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

Potrebbero piacerti anche