C Programming Exam Solutions - 2023-2024
C Programming Exam Solutions - 2023-2024
Question 1a
Write a C programming using array to find largest and smallest number from a list of 100 given
numbers.
c
#include <stdio.h>
#define SIZE 100
int main() {
int numbers[SIZE];
int i, largest, smallest;
// Input numbers
for(i = 0; i < SIZE; i++) {
printf("Enter number %d: ", i+1);
scanf("%d", &numbers[i]);
}
return 0;
}
Question 1b
Write program to read a one-dimensional array, sort the number in ascending and descending
order and display sorted number.
c
#include <stdio.h>
int main() {
int arr[SIZE];
int n, i, j, temp;
// Check if n is valid
if(n <= 0 || n > SIZE) {
printf("Invalid array size!\n");
return 1;
}
return 0;
}
Question 2a
The algorithm explains steps involved in converting an input value, Z, from base 10 to a user-
selectable base b (where 2 <= b <= 10).
c
#include <stdio.h>
#include <math.h>
int i = k - 1;
while (i >= 0) {
// Calculate the current digit
int digit = (int)(value / pow(base, i)) % base;
printf("%d", digit);
int main() {
int value, base;
char choice;
do {
printf("Enter a decimal value (Z): ");
scanf("%d", &value);
convertBase(value, base);
return 0;
}
Question 3a
Write a program to find a given character at the given array index of a given string. For example, if
the given string is "Claude", given character is 'u', and the given array index is 1, the resulting
string should be "Cuadle".
c
#include <stdio.h>
#include <string.h>
// Swap the character at the given index with the found character
char temp = str[index];
str[index] = ch;
}
int main() {
char str[100], ch;
int index;
return 0;
}
Question 3b (Part 1)
An electricity board charges according to the following rates:
#include <stdio.h>
int main() {
int units;
float totalCharge = 0.0;
const float METER_CHARGE = 150.0;
printf("\nElectricity Bill:\n");
printf("Units consumed: %d\n", units);
printf("Meter charge: N %.2f\n", METER_CHARGE);
printf("Consumption charge: N %.2f\n", totalCharge - METER_CHARGE);
printf("Total charge: N %.2f\n", totalCharge);
return 0;
}
Question 3b (Part 2)
Write a program to find the average of all the elements of m x n matrix.
c
#include <stdio.h>
int main() {
int m, n;
float sum = 0.0, average;
int matrix[m][n];
// Calculate average
average = sum / (m * n);
return 0;
}
Question 4a
Write a menu driven program having the given menu:
Main Menu
1. To print factorial of a number
2. To reverse an integer array
3. Exit
*** Please select your choice (1 to 3): ..................**
Note: The program should continue the execution till 3 is not selected*
c
#include <stdio.h>
return fact;
}
int main() {
int choice;
do {
// Display menu
printf("\nMain Menu\n");
printf("1. To print factorial of a number\n");
printf("2. To reverse an integer array\n");
printf("3. Exit\n");
printf("* Please select your choice (1 to 3): ");
scanf("%d", &choice);
switch(choice) {
case 1:
{
int num;
printf("\nEnter a number to calculate factorial: ");
scanf("%d", &num);
if(num < 0) {
printf("Factorial is not defined for negative numbers!\n");
} else if(num > 20) {
printf("Number too large, may cause overflow!\n");
} else {
printf("Factorial of %d = %llu\n", num, factorial(num));
}
}
break;
case 2:
{
int size, i;
if(size <= 0) {
printf("Invalid array size!\n");
break;
}
int arr[size];
reverseArray(arr, size);
case 3:
printf("\nExiting the program. Goodbye!\n");
break;
default:
printf("\nInvalid choice! Please select 1, 2, or 3.\n");
}
} while(choice != 3);
return 0;
}
Question 4b
Write a program to find the sum of all prime numbers in a given array. The main function of your
program should take the help of user defined function that tests, whether a given number is prime
or not.
c
#include <stdio.h>
#include <stdbool.h>
return true;
}
printf("\n");
return sum;
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
if(size <= 0) {
printf("Invalid array size!\n");
return 1;
}
int arr[size];
return 0;
}
Question 5b
Write a program to calculate the sum of given series up to the term given by user:
y = 1/2 + 2/3 + 3/4 + ...
c
#include <stdio.h>
int main() {
int n;
float sum = 0.0;
printf("Enter the number of terms to calculate in the series 1/2 + 2/3 + 3/4 + ...: ");
scanf("%d", &n);
if(n <= 0) {
printf("Please enter a positive number of terms!\n");
return 1;
}
printf("Series: ");
printf("%d/%d", i, i+1);
if(i < n) {
printf(" + ");
}
}
return 0;
}