SET A
1) Program to accept an integer and check if it is even or odd
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is Even.\n", num);
else
printf("%d is Odd.\n", num);
return 0;
}
Sample Output:
Enter an integer: 7
7 is Odd.
2) Program to accept a number and check whether it is positive, negative, or zero
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("Number is Positive.\n");
else if (num < 0)
printf("Number is Negative.\n");
else
printf("Number is Zero.\n");
return 0;
}
Sample Output:
Enter a number: -15
Number is Negative.
3) Program to find the maximum of three numbers using the conditional
operator
#include <stdio.h>
int main() {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("Maximum number is: %d\n", max);
return 0;
}
Sample Output:
Enter three numbers: 12 45 33
Maximum number is: 45
4) Program to check whether user’s age is eligible for voting
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
printf("You are eligible to vote.\n");
else
printf("You are not eligible to vote.\n");
return 0;
}
Sample Output:
Enter your age: 16
You are not eligible to vote.
5) Program to check whether a lowercase character is a vowel or consonant
#include <stdio.h>
int main() {
char ch;
printf("Enter a lowercase character: ");
scanf(" %c", &ch);
if (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);
return 0;
}
Sample Output:
Enter a lowercase character: e
e is a Vowel.
SET B
1) Accept a single digit and display it in words
#include <stdio.h>
int main() {
int digit;
printf("Enter a single digit (0-9): ");
scanf("%d", &digit);
switch (digit) {
case 0: printf("Zero\n"); break;
case 1: printf("One\n"); break;
case 2: printf("Two\n"); break;
case 3: printf("Three\n"); break;
case 4: printf("Four\n"); break;
case 5: printf("Five\n"); break;
case 6: printf("Six\n"); break;
case 7: printf("Seven\n"); break;
case 8: printf("Eight\n"); break;
case 9: printf("Nine\n"); break;
default: printf("Invalid input! Please enter 0-9.\n");
}
return 0;
}
Sample Output:
Enter a single digit (0-9): 9
Nine
2) Simple Calculator using switch case
#include <stdio.h>
int main() {
double num1, num2;
char op;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter second number: ");
scanf("%f", &num2);
switch (op) {
case '+': printf("Result: %f\n", num1 + num2); break;
case '-': printf("Result: %f\n", num1 - num2); break;
case '*': printf("Result: %f\n", num1 * num2); break;
case '/':
if (num2!= 0)
printf("Result: %f\n", num1 / num2);
else
printf("Error! Division by zero.\n");
break;
default: printf("Invalid operator.\n");
}
return 0;
}
Sample Output:
Enter first number: 12
Enter an operator (+, -, *, /): *
Enter second number: 4
Result: 48.00
3) Check if given time is valid
#include <stdio.h>
int main() {
int hour, minute, second;
printf("Enter time (hour minute second): ");
scanf("%d %d %d", &hour, &minute, &second);
if ((hour >= 0 && hour < 24) &&
(minute >= 0 && minute < 60) &&
(second >= 0 && second < 60)) {
printf("Valid time.\n");
} else {
printf("Invalid time.\n");
}
return 0;
}
Sample Output:
Enter time (hour minute second): 13 45 20
Valid time.
4) Check whether a year is leap year
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
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;
}
Sample Output:
Enter a year: 2024
2024 is a Leap Year.
5) Profit or Loss Calculation
#include <stdio.h>
int main() {
float costPrice, sellingPrice, profitOrLoss;
printf("Enter Cost Price: ");
scanf("%f", &costPrice);
printf("Enter Selling Price: ");
scanf("%f", &sellingPrice);
profitOrLoss = sellingPrice - costPrice;
if (profitOrLoss > 0)
printf("Profit of Rs. %f\n", profitOrLoss);
else if (profitOrLoss < 0)
printf("Loss of Rs. %f\n", -profitOrLoss);
else
printf("No Profit No Loss.\n");
return 0;
}
Sample Output:
Enter Cost Price: 500
Enter Selling Price: 650
Profit of Rs. 150.00
6) Menu-driven program for area calculation
#include<stdio.h>
int main() {
int choice;
float length, breadth, base, height, area;
printf("Menu:\n");
printf("1. Area of Square\n");
printf("2. Area of Rectangle\n");
printf("3. Area of Triangle\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter length of square: ");
scanf("%f", &length);
area = length * length;
printf("Area of Square = %.2f\n", area);
break;
case 2:
printf("Enter length and breadth of rectangle: ");
scanf("%f %f", &length, &breadth);
area = length * breadth;
printf("Area of Rectangle = %.2f\n", area);
break;
case 3:
printf("Enter base and height of triangle: ");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of Triangle = %.2f\n", area);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Sample Output:
Menu:
1. Area of Square
2. Area of Rectangle
3. Area of Triangle
Enter your choice (1-3): 2
Enter length and breadth of rectangle: 5 4
Area of Rectangle = 20.00
SET C: 1) Write a C program to read marks from keyboard and your program
should display equivalent grade. (Distinction, First Class, Second Class, Pass
Class and Fail)
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks >= 75 && marks <= 100)
printf("Grade: Distinction\n");
else if (marks >= 60 && marks < 75)
printf("Grade: First Class\n");
else if (marks >= 50 && marks < 60)
printf("Grade: Second Class\n");
else if (marks >= 40 && marks < 50)
printf("Grade: Pass Class\n");
else if (marks >= 0 && marks < 40)
printf("Grade: Fail\n");
else
printf("Invalid marks entered!\n");
return 0;
}
Sample Output 1:
Enter marks (0-100): 82
Grade: Distinction
Sample Output 2:
Enter marks (0-100): 45
Grade: Pass Class