Solving 50 Programming Questions with Answers for Grade 10, 11, and 12 | Computer | C Programming
Rohan Koirala - Fri Mar 22 2024
0
The difficulty level is in ascending order.
Write a program to find the sum of two numbers.
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// calculate sum
sum = num1 + num2;
// print the sum
printf("Sum: %d\n", sum);
return 0;
}
Write a program to calculate the average of three numbers.
#include <stdio.h>
int main() {
float num1, num2, num3, average;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
average = (num1 + num2 + num3) / 3;
printf("The average of the three numbers is: %.2f\n", average);
return 0;
}
Write a program to swap two numbers.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Write a program to find the maximum of two numbers.
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("%d is the maximum of %d and %d\n", num1, num1, num2);
} else if (num2 > num1) {
printf("%d is the maximum of %d and %d\n", num2, num1, num2);
} else {
printf("%d and %d are equal\n", num1, num2);
}
return 0;
}
Write a program to find the minimum 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 the minimum.", a);
} else {
printf("%d is the minimum.", b);
}
return 0;
}
Write a program to check if a number is even or odd.
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is even or odd
if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}
return 0;
}
Write a program to check if a number is positive, negative, or zero.
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
{
printf("%d is positive.", num);
}
else if (num < 0)
{
printf("%d is negative.", num);
}
else
{
printf("The number is zero.");
}
return 0;
}
Write a program to 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;
}
Write a program to calculate the factorial of a number.
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("The factorial of %d is %d.\n", number, factorial(number));
return 0;
}
Write a program to check if a number is prime.
#include <stdio.h>
#include <math.h>
int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
printf("%d is not a prime number.\n", n);
return 0;
}
}
printf("%d is a prime number.\n", n);
return 0;
}
Write a program to print the Fibonacci series up to a given number of terms.
#include <stdio.h>
int main() {
int n, i, a = 0, b = 1, c;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: ");
for (i = 1; i <= n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
return 0;
}
Write a program to find the sum of natural numbers up to a given number.
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("The sum of natural numbers up to %d is: %d\n", n, sum);
return 0;
}
Write a program to print the multiplication table of a given number.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
Write a program to find the GCD (Greatest Common Divisor) of two numbers.
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
return 0;
}
Write a program to find the LCM (Least Common Multiple) of two numbers.
#include <stdio.h>
int main()
{
int num1, num2, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
lcm = (num1 > num2) ? num1 : num2;
while(1)
{
if( lcm % num1 == 0 && lcm % num2 == 0 )
{
printf("The LCM of %d and %d is %d\n", num1, num2, lcm);
break;
}
++lcm;
}
return 0;
}
Write a program to reverse a number.
#include <stdio.h>
int main() {
int num, reversedNum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d", reversedNum);
return 0;
}
Write a program to check if a number is a palindrome.
#include <stdio.h>
int main() {
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
int originalNumber = n;
// reversedNumber initializes to 0, and the loop continues as long as n is greater than 0.
while (n != 0) {
remainder = n % 10;
reversedNumber = reversedNumber * 10 + remainder;
n /= 10;
}
// If originalNumber and the reversedNumber are the same, then the number is a palindrome.
if (originalNumber == reversedNumber) {
printf("%d is a palindrome.", originalNumber);
} else {
printf("%d is not a palindrome.", originalNumber);
}
return 0;
}
Write a program to check if a number is an Armstrong number.
#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, n = 0, result = 0;
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
// Find the number of digits in the number
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// Calculate the Armstrong number
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// Check if the number is an Armstrong number
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Write a program to find the sum of digits of a number.
#include <stdio.h>
int main() {
int num, sum = 0, temp;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
temp = num % 10;
sum += temp;
num /= 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}
Write a program to find the reverse of a string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len, i;
printf("Enter a string: ");
gets(str);
len = strlen(str);
printf("Reverse of the string: ");
for (i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
}
return 0;
}
Write a program to check if a string is a palindrome.
C
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i, j, palindrome;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
palindrome = 1;
for (i = 0, j = len - 1; i < len / 2; i++, j--) {
if (str[i] != str[j]) {
palindrome = 0;
break;
}
}
if (palindrome) {
printf("%s is a palindrome.", str);
} else {
printf("%s is not a palindrome.", str);
}
return 0;
}
Write a program to count the number of vowels in a string.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int len, i, count = 0;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = 0; i < len; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
count++;
}
}
printf("The number of vowels in the string is: %d\n", count);
return 0;
}
Write a program to count the number of words in a string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, wordCount = 0;
printf("Enter a string: ");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
wordCount++;
}
printf("The number of words in the string is: %d", wordCount + 1);
return 0;
}
Write a program to find the largest element in an array.
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
printf("The largest element in the array is %d", max);
return 0;
}
Write a program to find the smallest element in an array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, smallest;
int *arr;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
smallest = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] < smallest)
{
smallest = arr[i];
}
}
printf("The smallest element in the array is: %d\n", smallest);
free(arr);
return 0;
}
Write a program to sort an array in ascending order.
#include <stdio.h>
#include <stdlib.h>
void sort(int *arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
int arr[] = {5, 3, 1, 2, 4};
int size = sizeof(arr) / sizeof(arr[0]);
sort(arr, size);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Write a program to sort an array in descending order.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array in descending order
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// Print the sorted array
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Write a program to find the sum of elements in an array.
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
printf("The sum of the elements in the array is: %d\n", sum);
return 0;
}
Write a program to find the average of elements in an array.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
float sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
float average = sum / n;
printf("The average of the elements in the array is: %f\n", average);
return 0;
}
Write a program to merge two arrays.
#include <stdio.h>
int main()
{
int a[100], b[100], c[200], m, n, i, j, k;
printf("Enter the size of first array: ");
scanf("%d", &m);
printf("Enter elements of first array: ");
for (i = 0; i < m; i++)
scanf("%d", &a[i]);
printf("Enter the size of second array: ");
scanf("%d", &n);
printf("Enter elements of second array: ");
for (j = 0; j < n; j++)
scanf("%d", &b[j]);
i = 0;
j = 0;
k = 0;
while (i < m && j < n)
{
if (a[i] < b[j])
{
c[k] = a[i];
i++;
k++;
}
else
{
c[k] = b[j];
j++;
k++;
}
}
while (i < m)
{
c[k] = a[i];
i++;
k++;
}
while (j < n)
{
c[k] = b[j];
j++;
k++;
}
printf("Merged array: ");
for (i = 0; i < k; i++)
printf("%d ", c[i]);
return 0;
}
Write a program to reverse an array.
#include <stdio.h>
void reverseArray(int arr[], int size) {
int i, temp;
for (i = 0; i < size / 2; i++) {
temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, size);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Write a program to check if two arrays are equal.
#include <stdio.h>
int main()
{
int n1, n2, i;
int a[10], b[10];
printf("Enter the size of the first array: ");
scanf("%d", &n1);
printf("Enter the elements of the first array: ");
for (i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the size of the second array: ");
scanf("%d", &n2);
printf("Enter the elements of the second array: ");
for (i = 0; i < n2; i++)
scanf("%d", &b[i]);
if (n1 != n2)
{
printf("The arrays are not equal.\n");
}
else
{
for (i = 0; i < n1; i++)
{
if (a[i] != b[i])
{
printf("The arrays are not equal.\n");
return 0;
}
}
printf("The arrays are equal.\n");
}
return 0;
}
Write a program to check if a number is present in an array.
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int num = 3;
int found = 0;
for (int i = 0; i < size; i++) {
if (arr[i] == num) {
found = 1;
break;
}
}
if (found) {
printf("Number %d is present in the array.\n", num);
} else {
printf("Number %d is not present in the array.\n", num);
}
return 0;
}
Write a program to find the factorial of a number using recursion.
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("The factorial of %d is %d\n", n, factorial(n));
return 0;
}
Write a program to check if a string is a palindrome using recursion.
#include <stdio.h>
#include <string.h>
int is_palindrome_recursive(char *s, int start, int end) {
if (start >= end) {
return 1;
} else if (s[start] != s[end]) {
return 0;
} else {
return is_palindrome_recursive(s, start + 1, end - 1);
}
}
int main() {
char s[] = "racecar";
int start = 0;
int end = strlen(s) - 1;
if (is_palindrome_recursive(s, start, end)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
Write a program to find the sum of digits of a number using recursion.
#include <stdio.h>
int sumOfDigits(int num)
{
if (num == 0)
return 0;
return (num % 10 + sumOfDigits(num / 10));
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("The sum of digits of %d is %d\n", num, sumOfDigits(num));
return 0;
}
Write a program to find the GCD of two numbers using recursion.
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("The GCD of %d and %d is %d\n", a, b, gcd(a, b));
return 0;
}
Write a program to generate the first 'n' terms of the Fibonacci series using recursion.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("The first %d terms of the Fibonacci series are: ", n);
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
Write a program to find the power of a number using recursion.
#include <stdio.h>
int power(int base, int exponent)
{
if (exponent == 0)
{
return 1;
}
else
{
return base * power(base, exponent - 1);
}
}
int main() {
int base, exponent;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &exponent);
printf("%d^%d = %d\n", base, exponent, power(base, exponent));
return 0;
}
Write a program to calculate the average of marks of 'n' students.
#include <stdio.h>
int main() {
int n, i;
float marks[n], sum = 0, average;
printf("Enter the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the marks of student %d: ", i + 1);
scanf("%f", &marks[i]);
sum += marks[i];
}
average = sum / n;
printf("The average marks of the students is: %.2f\n", average);
return 0;
}
Write a program to find the smallest among three numbers.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
int smallest = num1;
if (num2 < smallest) {
smallest = num2;
}
if (num3 < smallest) {
smallest = num3;
}
printf("The smallest number is: %d\n", smallest);
return 0;
}
Write a program to check if a triangle is equilateral, isosceles, or scalene.
#include <stdio.h>
#include <math.h>
int main()
{
int a, b, c;
printf("Enter the lengths of the three sides of the triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a == b && b == c)
{
printf("The triangle is equilateral.\n");
}
else if (a == b || b == c || c == a)
{
printf("The triangle is isosceles.\n");
}
else
{
printf("The triangle is scalene.\n");
}
return 0;
}
Write a program to check if a number is a perfect number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, sum = 0, i;
printf("Enter a number: ");
scanf("%d", &num);
for(i = 1; i < num; i++)
{
if(num % i == 0)
{
sum += i;
}
}
if(sum == num)
{
printf("%d is a perfect number.\n", num);
}
else
{
printf("%d is not a perfect number.\n", num);
}
return 0;
}
Write a program to find the sum of digits of a number until it becomes a single-digit number.
#include <stdio.h>
int main()
{
int num, sum;
printf("Enter a number: ");
scanf("%d", &num);
while (num >= 10)
{
sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
num = sum;
}
printf("The sum of digits is: %d\n", num);
return 0;
}
Write a program to find the factorial of a number without using recursion.
#include <stdio.h>
int main()
{
int num, i, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
printf("The factorial of %d is: %d\n", num, fact);
return 0;
}
Write a program to check if a number is a strong number.
#include <stdio.h>
int main() {
int num, temp, sum = 0, i, remainder, fact;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (num > 0) {
remainder = num % 10;
fact = 1;
for (i = 1; i <= remainder; i++) {
fact = fact * i;
}
sum = sum + fact;
num = num / 10;
}
if (sum == temp)
printf("%d is a strong number.", temp);
else
printf("%d is not a strong number.", temp);
return 0;
}
Write a program to find the sum of natural numbers using recursion.
#include <stdio.h>
int sumOfNaturalNumbers(int n) {
if (n == 0) {
return 0;
} else {
return n + sumOfNaturalNumbers(n - 1);
}
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("The sum of the first %d natural numbers is %d\n", n, sumOfNaturalNumbers(n));
return 0;
}
Write a program to find the LCM of 'n' numbers.
#include <stdio.h>
int main() {
int n, i, j, lcm;
printf("Enter the number of integers: ");
scanf("%d", &n);
int arr[n];
printf("Enter the integers: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
lcm = arr[0];
for (i = 1; i < n; i++) {
lcm = lcm * arr[i] / gcd(lcm, arr[i]);
}
printf("The LCM of the given integers is: %d", lcm);
return 0;
}
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}