0% found this document useful (0 votes)
5 views3 pages

Lap 2

The document contains six exercises in C programming that cover various topics including grade evaluation, voting eligibility based on age and sex, photocopy cost calculation, vowel and consonant identification, basic arithmetic operations, and score grading. Each exercise includes code snippets demonstrating the implementation of the respective functionalities. The exercises are designed to reinforce fundamental programming concepts and input handling in C.

Uploaded by

phanhacambodia22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Lap 2

The document contains six exercises in C programming that cover various topics including grade evaluation, voting eligibility based on age and sex, photocopy cost calculation, vowel and consonant identification, basic arithmetic operations, and score grading. Each exercise includes code snippets demonstrating the implementation of the respective functionalities. The exercises are designed to reinforce fundamental programming concepts and input handling in C.

Uploaded by

phanhacambodia22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lap2:

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

if (age > 20 && sex == 'M') {


printf("You are eligible to vote\n");
} else if (age > 18 && age < 35 && sex == 'F') {
printf("You are eligible to vote\n");
} else {
printf("You are not eligible to vote\n");
}

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;

printf("Enter a letter: ");


scanf(" %c", &letter); // The space is important!

// Make the letter lowercase so we don't have to check both cases


if (letter >= 'A' && letter <= 'Z') {
letter = letter + 32; // Convert to lowercase
}

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

if (score >= 90) {


printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else if (score >= 50) {
printf("Grade: E\n");
} else {
printf("Grade: F\n");
}

return 0;
}

You might also like