1. Write a C program that calculates the area of a triangle.
#include <stdio.h>
int main() {
float base, height, area;
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
// Area formula: 1/2 * base * height
area = 0.5 * base * height;
printf("The area of the triangle is: %.2f\n", area);
return 0;
}
2. Write a C program that converts a temperature from Celsius to Fahrenheit.
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// Conversion formula: F = (C × 9/5) + 32
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
3. Write a C program that takes a year as input from the user and determines whether it is a leap year or
not.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Leap year condition using logical operators
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
return 0;
4. Write a C program that takes an integer as input from the user and determines whether the number is
odd or even.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is Even.\n", num);
} else {
printf("%d is Odd.\n", num);
}
return 0;
5. Write a C program that takes three numbers as input from the user and determines which one
is the largest.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("The largest number is %d\n", num1);
else if (num2 >= num1 && num2 >= num3) {
printf("The largest number is %d\n", num2);
else {
printf("The largest number is %d\n", num3);
}
return 0;
6. Write a C program that takes an integer as input from the user and determines whether the
number is:
• Positive (greater than 0)
• Negative (less than 0)
• Zero (equal to 0)
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is Positive.\n", num);
else if (num < 0) {
printf("%d is Negative.\n", num);
else {
printf("The number is Zero.\n");
}
return 0;
7. Write a C program to check whether a given character is a vowel or a consonant. If the input is not an
alphabet, display an error message.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
// Check if alphabet
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
// Check vowel
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
} else {
printf("Invalid input! Please enter an alphabet.\n");
}
return 0;
8. Write a C program that takes a student’s marks (0–100) as input and displays whether the student has
passed or failed.
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks (0-100): ");
scanf("%d", &marks);
if (marks >= 33 && marks <= 100) {
printf("Result: Pass\n");
else if (marks >= 0 && marks < 33) {
printf("Result: Fail\n");
else {
printf("Invalid input! Please enter marks between 0 and 100.\n");
}
return 0;
9. Write a C program that takes the marks of a student (0–100) as input and displays the
corresponding letter grade according to the grading system below:
• 80–100 → A+
• 70–79 → A
• 60–69 → A−
• 50–59 → B
• 40–49 → C
• 33–39 → D
• 0–32 → F
If the input is less than 0 or greater than 100, display an invalid marks message.
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks (0-100): ");
scanf("%d", &marks);
if (marks >= 80 && marks <= 100) {
printf("Grade: A+\n");
} else if (marks >= 70) {
printf("Grade: A\n");
} else if (marks >= 60) {
printf("Grade: A-\n");
} else if (marks >= 50) {
printf("Grade: B\n");
} else if (marks >= 40) {
printf("Grade: C\n");
} else if (marks >= 33) {
printf("Grade: D\n");
} else if (marks >= 0) {
printf("Grade: F\n");
} else {
printf("Invalid marks! Please enter between 0 and 100.\n");
return 0;