Chapter 11 Exercise Solved Programs
Question 8: write a program that Compute the area of a square or a triangle ( Ask user to enter S for
square and T for Triangle)
#include <stdio.h>
int main() {
char figureType;
printf("Enter the first character of the figure name (S or T): ");
scanf(" %c", &figureType);
if (figureType == 'S' || figureType == 's') {
float side, area;
printf("Enter the side length of the square: ");
scanf("%f", &side);
area = side * side;
printf("Area of the square: %f\n", area);
} else if (figureType == 'T' || figureType == 't') {
float base, height, area;
printf("Enter the base length of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = 0.5 * base * height;
printf("Area of the triangle: %f\n", area);
} else {
printf("Invalid figure type.\n");
}
// Question 9: Check if a year is a leap year
#include <stdio.h>
int main() {
int year;
printf("\nEnter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("Leap year\n");
} else {
printf("Not a leap year\n");
}
}
// Question 10: Display a message based on temperature
#include <stdio.h>
int main() {
float temperature;
printf("\nEnter temperature: ");
scanf("%f", &temperature);
if (temperature > 35) {
printf("Hot day\n");
} else if (temperature >= 25 && temperature <= 35) {
printf("Pleasant day\n");
} else {
printf("Cool day\n");
}
}
// Question 11: Calculate percentage and display grade
#include <stdio.h>
int main() {
int obtainedMarks;
printf("\nEnter obtained marks (out of 1100): ");
scanf("%d", &obtainedMarks);
float percentage = (float)obtainedMarks / 1100 * 100;
if (percentage >= 80) {
printf("Grade: A+\n");
} else if (percentage >= 70) {
printf("Grade: A\n");
} else if (percentage >= 60) {
printf("Grade: B\n");
} else if (percentage >= 50) {
printf("Grade: C\n");
} else if (percentage >= 40) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
}
// Question 12: Perform arithmetic operation based on user choice
#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
printf("\nEnter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Enter your choice (1: Sum, 2: Subtract, 3: Multiply, 4: Divide): ");
scanf("%d", &choice);
switch (choice) {
case 1:
result = num1 + num2;
printf("Sum: %f\n", result);
break;
case 2:
result = num1 - num2;
printf("Difference: %f\n", result);
break;
case 3:
result = num1 * num2;
printf("Product: %f\n", result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
printf("Quotient: %f\n", result);
} else {
printf("Error: Division by zero\n");
}
break;
default:
printf("Wrong choice\n");
}