21. Write a function to check if a number (17) is prime.
Required Input:
17
Expected Output:
17 is a prime number
Code In C
#include
int isPrime(int num) {
// Your code here
}
int main() {
int num = 17;
if (isPrime(num)) {
printf("%d is a prime number\n", num);
} else {
printf("%d is not a prime number\n", num);
}
return 0;
}
Output
Click Run Button to view compiled output
22. Find the factorial of each number in an array {4, 3, 2, 5}.
Required Input:
4 3 2 5
Expected Output:
24 6 2 120
Code In C
#include
int factorial(int num) {
// Your code here
}
int main() {
int arr[] = {4, 3, 2, 5};
for (int i = 0; i < 4; i++) {
printf("%d ", factorial(arr[i]));
}
printf("\n");
return 0;
}
Output
Click Run Button to view compiled output
23. Write a function to find the sum of digits of a number (456).
Required Input:
456
Expected Output:
Sum of digits: 15
Code In C
#include
int sumOfDigits(int num) {
// Your code here
}
int main() {
int num = 456;
printf("Sum of digits: %d\n", sumOfDigits(num));
return 0;
}
Output
Click Run Button to view compiled output
24. Write a function to reverse the digits of a number (12345).
Required Input:
12345
Expected Output:
Reversed number: 54321
Code In C
#include
int reverseNumber(int num) {
// Your code here
}
int main() {
int num = 12345;
printf("Reversed number: %d\n", reverseNumber(num));
return 0;
}
Output
Click Run Button to view compiled output
25. Check if a string "level" is a palindrome.
Required Input:
level
Expected Output:
level is a palindrome
Code In C
#include
#include
int isPalindrome(char str[]) {
// Your code here
}
int main() {
char str[] = "level";
if (isPalindrome(str)) {
printf("%s is a palindrome\n", str);
} else {
printf("%s is not a palindrome\n", str);
}
return 0;
}
Output
Click Run Button to view compiled output
26. Convert a binary number 1010 to its decimal equivalent.
Required Input:
1010
Expected Output:
Decimal: 10
Code In C
#include
int binaryToDecimal(int binary) {
// Your code here
}
int main() {
int binary = 1010;
printf("Decimal: %d\n", binaryToDecimal(binary));
return 0;
}
Output
Click Run Button to view compiled output
27. Find the sum of elements above the main diagonal in a 3x3 matrix {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}.
Required Input:
Matrix:
1 2 3
4 5 6
7 8 9
Expected Output:
Sum above main diagonal: 11
Code In C
#include
int sumAboveDiagonal(int matrix[3][3]) {
// Your code here
}
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Sum above main diagonal: %d\n", sumAboveDiagonal(matrix));
return 0;
}
Output
Click Run Button to view compiled output
28. Find the largest number in an array {10, 25, 17, 35, 20}.
Required Input:
10 25 17 35 20
Expected Output:
Largest number: 35
Code In C
#include
int findLargest(int arr[], int n) {
// Your code here
}
int main() {
int arr[] = {10, 25, 17, 35, 20};
printf("Largest number: %d\n", findLargest(arr, 5));
return 0;
}
Output
Click Run Button to view compiled output
29. Convert a decimal number (15) to binary.
Required Input:
15
Expected Output:
Binary: 1111
Code In C
#include
void decimalToBinary(int num) {
// Your code here
}
int main() {
int num = 15;
printf("Binary: ");
decimalToBinary(num);
printf("\n");
return 0;
}
Output
Click Run Button to view compiled output
30. Find the sum of prime numbers in an array {5, 10, 17, 23, 4, 9}.
Required Input:
5 10 17 23 4 9
Expected Output:
Sum of primes: 45
Code In C
#include
int isPrime(int num) {
// Prime check code
}
int sumOfPrimes(int arr[], int n) {
// Sum of primes code
}
int main() {
int arr[] = {5, 10, 17, 23, 4, 9};
printf("Sum of primes: %d\n", sumOfPrimes(arr, 6));
return 0;
}
Output
Click Run Button to view compiled output