C PROGRAMMING EXERCISE – 1 (PROGRAMS WITH SOURCE CODE)
1. Check Even or Odd Number
#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;
}
2. Check Positive or Negative Number
#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;
}
3. Find Largest of Three Numbers
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a >c) {
printf("%d is the largest number.\n", a);
} else if (b >a && b >c) {
printf("%d is the largest number.\n", b);
} else {
printf("%d is the largest number.\n", c);
}
return 0;
}
1
4. Check Leap Year or Not
#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;
}
5. Check Voting Eligibility
#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;
}
6. Find Grade Based on Marks
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 80) {
printf("Grade B\n");
} else if (marks >= 70) {
printf("Grade C\n");
} else if (marks >= 60) {
printf("Grade D\n");
} else {
printf("Grade F\n");
}
return 0;
}
2
7. Check the input number Divisibility by 3 and 5
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 3 == 0 && num % 5 == 0) {
printf("%d is divisible by both 3 and 5.\n", num);
} else {
printf("%d is not divisible by both 3 and 5.\n", num);
}
return 0;
}
8. Find the Maximum of Two Numbers
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b) {
printf("%d is greater.\n", a);
} else {
printf("%d is greater.\n", b);
}
return 0;
}
9. Check if Character is Alphabet or not
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("%c is an alphabet.\n", ch);
} else {
printf("%c is not an alphabet.\n", ch);
}
return 0;
}
10. Basic operation of a Simple Calculator (Addition, Subtraction, Multiplication, Division)
#include <stdio.h>
int main() {
int num1, num2;
char operator;
printf("Enter operator (+, -, *, /): ");
scanf("%c", &operator);
3
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (operator == '+') {
printf("%d %c %d = %d\n", num1, operator, num2, num1 + num2);
} else if (operator == '-') {
printf("%d %c %d = %d\n", num1, operator, num2, num1 - num2);
} else if (operator == '*') {
printf("%d %c %d = %d\n", num1, operator, num2, num1 * num2);
} else if (operator == '/') {
if (num2 != 0) {
printf("%d %c %d = %.2f\n", num1, operator, num2, (float) num1 / num2);
} else {
printf("Division by zero is not allowed.\n");
}
} else {
printf("Invalid operator.\n");
}
return 0;
}
11. Check Whether a Year is a Century Year or Not
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 100 == 0) {
printf("%d is a century year.\n", year);
} else {
printf("%d is not a century year.\n", year);
}
return 0;
}
12. Check if a Character is a Vowel or Consonant
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
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);
}
return 0;
}