Lap 2
Lap 2
Exercise1:
#include <stdio.h>
int main () {
printf("Please enter your grade: ");
char grade;
scanf("%s", &grade);
if (grade == 'A') {
printf("Excellent!\n");
} else if (grade == 'B') {
printf("Very good!\n");
} else if (grade == 'C') {
printf("Good!\n");
} else if (grade == 'D') {
printf("Fair!\n");
} else if (grade == 'F') {
printf("Failed!\n");
} else {
printf("Invalid grade!\n");
}
return 0;
}
Exercise2:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
char sex;
printf("Enter your sex (M or F): ");
scanf(" %c", &sex); // Notice the space before %c
return 0;
}
scanf(" %c", &sex);: See the space before %c? That little space tells the computer
to ignore any extra spaces or "enter" keys that might be hanging around. This makes
sure the gender input works correctly.
Exercise3:
#include <stdio.h>
int main () {
int a;
float b;
printf("Enter your total photocopy:");
scanf("%d", &a);
if (a <= 30) {
b = a * 0.05;
printf("Your total photocopy is: %.2f", b);
} else if (a < 100) {
b = a * 0.025;
printf("Your total photocopy is: %.2f", b);
} else if (a >= 100) {
b = a * 0.01;
printf("Your total photocopy is: %.2f", b);
return 0;
}
}
Exercise4:
#include <stdio.h>
int main() {
char letter;
switch (letter) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("It's a vowel!\n");
break;
default:
if (letter >= 'a' && letter <= 'z') {
printf("It's a consonant!\n");
} else {
printf("That's not a letter!\n");
}
break;
}
return 0;
}
Exercise5:
#include <stdio.h>
int main() {
float num1, num2;
char operator;
float result;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
switch(operator) {
case '+':
result = num1 + num2;
printf("The sum is : %f\n", result);
break;
case '-':
result = num1 - num2;
printf("The difference is : %f\n", result);
break;
case '*':
result = num1 * num2;
printf("The product is : %f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("The quotient is : %f\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
break;
}
default:
printf("Error: Invalid operator.\n");
break;
}
return 0;
}
Exercise6:
#include <stdio.h>
int main() {
int score;
printf("Enter your score: ");
scanf("%d", &score);
return 0;
}