Programs Day 1 and 2
Write a Program to: 1: Read in two numbers and output the largest 2: Read in an exam mark and output the appropriate grade according to the following scheme. 85 and over - A 70 to 84 - B C D E otherwise 3: Write a program which reads in the three sides of a triangle and detemines whether it is Equilateral, Isoceles or Scalene
Larger of Two Numbers #Include main() { int a,b; printf(“enter first number \n”); scanf(“%d”,&a); printf(“enter second number \n”); scanf(“%d”,&b ); If (a > b ) printf(“%d is greater than %d ”,a,b); Else printf(“%d is greater than %d ”,b,a); }
Output the Grade #Include main() { int grade; printf(“Enter Exam Mark \n”); scanf(“%d”,&grade); if(grade >= 85) printf("A"); else if((grade >= 70)&&(grade < 85)) printf("B"); else if((grade >= 55)&&(grade < 70)) printf("C"); else if((grade >= 40)&&(grade < 55)) printf("D"); else printf("F"); }
Triangles #Include main() { int a,b; printf(“enter first side \n”); scanf(“%d”,&a); printf(“enter second side \n”); scanf(“%d”,&b ); printf(“enter third side \n”); scanf(“%d”,&c ); If((a==b)||(b==c)||(a==c)) { If ((a==b)&&(b==c)) printf(“Equilateral”); Else {printf( “ Isosceles”)};} Else {printf(“Scalene”)}; }
More Programs 4: Write a program which reads in a year and determines whether or not it is a leap year. Conditions for a leap year a) It is divisible by 4 b) If it is divisible by 100 then it is leap only if it is also divisible by 400 So 1600 and 2000 are leap years but 1900 isn't. 5: Write a program to read in a month and year and output how many days are in that month. 6: Read in the number which represents a day of the week, 01 is Sunday 02 is Monday etc and output in words what the next day is.
Leap Year #Include main() { int year; printf(“Enter Year \n”); scanf(“%d”,&year); if(year % 4 ==0 && (year % 100 != 0 ||year % 400 == 0)) printf( "The year is leap) else printf("The year is not leap”); }
No of Days in a month #Include main() { int days,month,year; printf(“Enter Month \n”); scanf(“%d”,&month); printf(“Enter Year \n”); scanf(“%d”,&year); days = 0; If (month == 2) { if(year % 4 ==0 && (year % 100 != 0 ||year % 400 == 0)) days = 29 else days = 28; } Else if((month == 4) ||(month == 6)||(month == 9)||(month == 11)) {days = 30;} Else {days = 31;}; printf (“the number of days in the month is %d”,days);}
Output the Following Day Note 1 = Sunday #Include main() { int day; printf(“Enter day \n”); scanf(“%d”,&day); if(day == 1) printf(“Monday”); else if(day == 2) printf(“Tuesday"); else if(day == 3) printf(“Wednesday"); else if(day == 4) printf(“Thursday"); else if(day == 5) printf(“Friday"); else if(day == 6) printf(“Saturday"); else printf(“Sunday"); }
Output the Following Day Note 1 = Sunday #Include main() { int day; printf(“Enter day \n”); scanf(“%d”,&day); if(day <= 4) {if (day <= 2) {if (day == 1) prinf(“Monday”) else printf(“Tuesday"); } else {if(day == 3) printf(“Wednesday"); else printf(“Thursday"); }} else { if(day < 6) printf(“Friday"); else if(day == 6) printf(“Saturday"); else printf(“Sunday"); }}