C Programming Questions and Answers
2. Implement a C program that checks whether a number is even or odd.
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("%d is even\n", number);
} else {
printf("%d is odd\n", number);
}
return 0;
}
3. Implement a C program that finds the 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\n", a);
} else if (b >= a && b >= c) {
printf("%d is the largest\n", b);
} else {
printf("%d is the largest\n", c);
}
return 0;
}
4. Implement a C program to calculate the factorial of a number using a loop.
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i = 1; i <= n; ++i) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
return 0;
}