Basic C Programs with Code Examples
/*********************************************************************
1 Write a C program to check if a number is positive or negative.
2 *********************************************************************/
3 #include <stdio.h>
4
5 int main() {
6 int a;
7 printf("Enter the number: ");
8 scanf("%d", &a);
9
10 if (a == 0) {
11 printf("%d is zero\n", a);
12 }
13 else if (a < 0) {
14 printf("%d is a negative number\n", a);
15 }
16 else {
17 printf("%d is a positive number\n", a);
18 }
19
20 return 0;
21 }
22
23 /*********************************************************************
24
✅ TEST CASES
25
26
Input: 0
27
Output: 0 is zero
28
29
Input: 25
30
Output: 25 is a positive number
31
32
Input: -13
33
Output: -13 is a negative number
34
35
Input: 999
36
Output: 999 is a positive number
37
38
Input: -1
39
Output: -1 is a negative number
40
*********************************************************************/
41
1 /*********************************************************************
2 Write a C program to check if a number is even or odd.
3 *********************************************************************/
4 #include <stdio.h>
5
6 int main() {
7 int a;
8 printf("Enter the number: ");
9 scanf("%d", &a);
10
11 if (a == 0) {
12 printf("%d is zero\n", a);
13 }
14 else if (a % 2 == 0) {
15 printf("%d is an even number\n", a);
16 }
17 else {
18 printf("%d is an odd number\n", a);
19 }
20
21 return 0;
22 }
23
24 /*********************************************************************
25 ✅ TEST CASES
26
27 Input: 0
28 Output: 0 is zero
29
30 Input: 25
31 Output: 25 is an odd number
32
33 Input: -13
34 Output: -13 is an odd number
35
36 Input: 24
37 Output: 24 is an even number
38 *********************************************************************/
1 /*********************************************************************
2 Write a C program to find the larger of two numbers.
3 *********************************************************************/
4 #include <stdio.h>
5
6 int main() {
7 int a, b;
8 printf("Enter the numbers a and b: ");
9 scanf("%d %d", &a, &b);
10
11 if (a > b) {
12 printf("%d is the larger number\n", a);
13 }
14 else if (a == b) {
15 printf("Both numbers are equal\n");
16 }
17 else {
18 printf("%d is the larger number\n", b);
19 }
20
21 return 0;
22 }
23
24 /*********************************************************************
25 ✅ TEST CASES
26
27 Input: 35 35
28 Output: Both numbers are equal
29
30 Input: 12 6
31 Output: 12 is the largest number
32
33 Input: -13 2
34 Output: 2 is the largest number
35 *********************************************************************/
1 /*********************************************************************
2 Write a C program to find the smallest of two numbers.
3 *********************************************************************/
4 #include <stdio.h>
5
6 int main() {
7 int a, b;
8 printf("Enter the numbers a and b: ");
9 scanf("%d %d", &a, &b);
10
11 if (a < b) {
12 printf("%d is the smaller number\n", a);
13 }
14 else if (a == b) {
15 printf("Both numbers are equal\n");
16 }
17 else {
18 printf("%d is the smaller number\n", b);
19 }
20
21 return 0;
22 }
23
24 /*********************************************************************
25 ✅ TEST CASES
26
27 Input: 35 35
28 Output: Both numbers are equal
29
30 Input: 12 6
31 Output: 6 is the smaller number
32
33 Input: -13 2
34 Output: -13 is the smaller number
35 *********************************************************************/
/*********************************************************************
1 Write a C program to check if a person can vote (age ≥ 18)
2 *********************************************************************/
3 #include <stdio.h>
4
5 int main() {
6 int age;
7 printf("Enter the age: ");
8 scanf("%d", &age);
9
10 if (age >= 18) {
11 printf("The person can vote\n");
12 }
13 else {
14 printf("The person cannot vote\n");
15 }
16
17 return 0;
18 }
19
20 /*********************************************************************
21
✅ TEST CASES
22
23
Input: 35
24
Output: The person can vote
25
26
Input: 12
27
Output: The person cannot vote
28
29
*********************************************************************/
30
1 /*********************************************************************
2 Write a C program to find the largest of three numbers.
3 *********************************************************************/
4 #include <stdio.h>
5
6 int main() {
7 int a, b, c;
8 printf("Enter the numbers a, b and c: ");
9 scanf("%d %d %d", &a, &b, &c);
10
11 if ((a >= b) && (a >= c)) {
12 printf("%d is the largest number\n", a);
13 }
14 else if ((b >= a) && (b >= c)) {
15 printf("%d is the largest number\n", b);
16 }
17 else {
18 printf("%d is the largest number\n", c);
19 }
20
21 return 0;
22 }
23
24 /*********************************************************************
25 ✅ TEST CASES
26
27 Input: 35 12 6
28 Output: 35 is the largest number
29
30 Input: 10 45 13
31 Output: 45 is the largest number
32
33 Input: 15 23 67
34 Output: 67 is the largest number
35
36 Input: 25 25 10
37 Output: 25 is the largest number (handles equality too)
38 *********************************************************************/
1 /*********************************************************************
2 Write a C program to check if a number is divisible by 5.
3 *********************************************************************/
4 #include <stdio.h>
5
6 int main() {
7 int a;
8 printf("Enter the number a: ");
9 scanf("%d", &a);
10
11 if (a % 5 == 0) {
12 printf("%d is divisible by 5\n", a);
13 }
14 else {
15 printf("%d is not divisible by 5\n", a);
16 }
17
18 return 0;
19 }
20
21 /*********************************************************************
22 ✅ TEST CASES
23
24 Input: 25
25 Output: 25 is divisible by 5
26
27 Input: 13
28 Output: 13 is not divisible by 5
29
30 Input: 0
31 Output: 0 is divisible by 5
32 *********************************************************************/
/*********************************************************************
1
Write a C program to check if a number is divisible by 2 and 3.
2
*********************************************************************/
3
#include <stdio.h>
4
5
int main() {
6
int a;
7
printf("Enter the number a: ");
8
scanf("%d", &a);
9
10
if ((a % 2 == 0) && (a % 3 == 0)) {
11
printf("%d is divisible by both 2 and 3\n", a);
12
}
13
else {
14
printf("%d is not divisible by both 2 and 3\n", a);
15
}
16
17
return 0;
18
}
19
20
/*********************************************************************
21
✅ TEST CASES
22
23
Input: 25
24
Output: 25 is not divisible by both 2 and 3
25
26
Input: 12
27
Output: 12 is divisible by both 2 and 3
28
29
Input: 0
30
Output: 0 is divisible by both 2 and 3
31
*********************************************************************/
32
1 /*********************************************************************
2 Write a C program to check if a number is a multiple of 10.
3 *********************************************************************/
4 #include <stdio.h>
5
6 int main() {
7 int a;
8 printf("Enter the number a: ");
9 scanf("%d", &a);
10
11 if (a % 10 == 0) {
12 printf("%d is a multiple of 10\n", a);
13 }
14 else {
15 printf("%d is not a multiple of 10\n", a);
16 }
17
18 return 0;
19 }
20
21 /*********************************************************************
22 ✅ TEST CASES
23
24 Input: 25
25 Output: 25 is not a multiple of 10
26
27 Input: 20
28 Output: 20 is a multiple of 10
29
30 Input: 0
31 Output: 0 is a multiple of 10
32 *********************************************************************/
1 /*********************************************************************
2 Write a C program for a simple calculator (add or subtract)
3 *********************************************************************/
4 #include <stdio.h>
5
6 int main() {
7 int a, b, c;
8 char op;
9
10 printf("Enter two numbers a and b: ");
11 scanf("%d %d", &a, &b);
12
13 printf("Enter the operator (+ or -): ");
14 scanf(" %c", &op); // space before %c to skip newline
15
16 if (op == '+') {
17 c = a + b;
18 printf("Sum of %d and %d = %d\n", a, b, c);
19 }
20 else if (op == '-') {
21 c = a - b;
22 printf("Difference of %d and %d = %d\n", a, b, c);
23 }
24 else {
25 printf("Invalid operator\n");
26 }
27
28 return 0;
29 }
30
31 /*********************************************************************
32 ✅ TEST CASES
33
34 Input: 25 12 +
35 Output: Sum of 25 and 12 = 37
36
37 Input: 24 12 -
38 Output: Difference of 24 and 12 = 12
39
40 Input: 10 5 *
41 Output: Invalid operator
42 *********************************************************************/