/*Program to calculate absolute value of an integer*/ #include <stdio.h> main() { int number; /*Enter number*/ printf("Enter a number: "); scanf("%d",&number); /*test if negative*/ if (number < 0) number = -number; /*output results*/ printf("The absolute value is %d\n",number); }
/*Program to test if number is even or odd*/ #include <stdio.h> main() { int number,remainder; printf("Enter a number: "); scanf("%d",&number); remainder = number % 2; if (remainder ==0) printf("The number is even\n"); else printf("The number is odd\n"); }
/*Program to illustrate the use of nested if statements*/ #include <stdio.h> main() { int number; printf("Enter a number between 1 and 99: "); scanf("%d",&number); if (number > 0 && number < 100 ) { if (number < 10) printf("One digit number\n"); else printf("Two digit number\n"); } else printf("Number not in range\n"); }
#include <stdio.h> main() { int number, n_digits = 0; printf("Enter a number: "); scanf("%d",&number); if (number >= 100) n_digits = 3; else if (number >=10) n_digits = 2; else if (number >=0) n_digits = 1; else printf("Value out of range\n"); if (n_digits > 0) printf("The number has %d digits\n", n_digits); }
#include <stdio.h> main() { char day; printf("Which day?"); day = getchar(); switch(day){ case 's': printf("Weekend\n"); break; case 'm': printf("Week day\n"); break; case 't': printf("Week day\n"); break; case 'w': printf("Week day\n"); break; case 'f': printf("Week day\n"); break; default: printf("Not a day\n"); } }
#include <stdio.h> main() { char day; printf("Which day?"); day = getchar(); switch(day){ case 's': case 'S': printf("Weekend\n"); break; case 'm': case 'M': printf("Week day\n"); break; case 't': case 'T': printf("Week day\n"); break; case 'w': case 'W': printf("Week day\n"); break; case 'f': case 'F': printf("Week day\n"); break; default: printf("Not a day\n"); } }
#include <stdio.h> main() { int input_value; printf("Which day?"); scanf("%d",&input_value); switch (input_value){ case 1: printf("The day is Monday\n"); break; case 2: printf("The day is Tuesday\n"); break; case 3: printf("The day is Wednesday\n"); break; case 4: printf("The day is Thursday\n"); break; case 5: printf("The day is Friday\n"); break; case 6: printf("The day is Saturday\n"); break; case 7: printf("The day is Sunday\n"); break; default : printf("Invalid Input\n"); break; } }
Back to Main Page for Lecture 3 |