System Programming with C
Federal University of Technology, Akure
2023/2024 First Semester Examination Solutions
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;
printf("Enter %d numbers:\n", SIZE);
// Input numbers
for(i = 0; i < SIZE; i++) {
printf("Enter number %d: ", i+1);
scanf("%d", &numbers[i]);
}
// Initialize largest and smallest with the first element
largest = smallest = numbers[0];
// Find largest and smallest
for(i = 1; i < SIZE; i++) {
if(numbers[i] > largest) {
largest = numbers[i];
}
if(numbers[i] < smallest) {
smallest = numbers[i];
}
}
printf("Largest number: %d\n", largest);
printf("Smallest number: %d\n", smallest);
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>
#define SIZE 100
// Function to print array
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[SIZE];
int n, i, j, temp;
printf("Enter the number of elements (max %d): ", SIZE);
scanf("%d", &n);
// Check if n is valid
if(n <= 0 || n > SIZE) {
printf("Invalid array size!\n");
return 1;
}
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
printf("Element %d: ", i+1);
scanf("%d", &arr[i]);
}
// Sort in ascending order using bubble sort
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
// Swap elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("\nArray sorted in ascending order:\n");
printArray(arr, n);
// Sort in descending order (reverse the sorted array)
for(i = 0; i < n/2; i++) {
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
printf("\nArray sorted in descending order:\n");
printArray(arr, n);
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>
void convertBase(int value, int base) {
if (base < 2 || base > 10) {
printf("Base must be between 2 and 10\n");
return;
}
// Calculate k = ⌊log_b Z⌋ + 1 (number of digits)
int k = floor(log(value) / log(base)) + 1;
printf("Number of digits (k): %d\n", k);
printf("Result in base %d: ", base);
int i = k - 1;
while (i >= 0) {
// Calculate the current digit
int digit = (int)(value / pow(base, i)) % base;
printf("%d", digit);
// Update value for next iteration
value = value - digit * pow(base, i);
i--;
}
printf("\n");
}
int main() {
int value, base;
char choice;
do {
printf("Enter a decimal value (Z): ");
scanf("%d", &value);
printf("Enter target base (2-10): ");
scanf("%d", &base);
convertBase(value, base);
printf("Do you want to convert another number? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
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>
void swapCharacterAtIndex(char str[], char ch, int index) {
int len = strlen(str);
// Check if index is valid
if(index < 0 || index >= len) {
printf("Invalid index! Index should be between 0 and %d\n", len-1);
return;
}
// Find position of the character in the string
int charPos = -1;
for(int i = 0; i < len; i++) {
if(str[i] == ch) {
charPos = i;
break;
}
}
// Check if character exists in the string
if(charPos == -1) {
printf("Character '%c' not found in the string!\n", ch);
return;
}
// Cannot swap with itself
if(charPos == index) {
printf("Character is already at the specified index!\n");
return;
}
// Swap the character at the given index with the found character
char temp = str[index];
// Shift characters if necessary
if(charPos > index) {
for(int i = charPos; i > index; i--) {
str[i] = str[i-1];
}
} else {
for(int i = charPos; i < index; i++) {
str[i] = str[i+1];
}
}
str[index] = ch;
}
int main() {
char str[100], ch;
int index;
printf("Enter a string: ");
scanf("%99s", str);
printf("Enter the character to find: ");
scanf(" %c", &ch);
printf("Enter the index to swap with: ");
scanf("%d", &index);
printf("Original string: %s\n", str);
swapCharacterAtIndex(str, ch, index);
printf("Modified string: %s\n", str);
return 0;
}
Question 3b (Part 1)
An electricity board charges according to the following rates:
For the first 100 units: N 150 per unit
For the next 200 units: N 200 per unit
Beyond 300 units: N 255 per unit All users are also charged 'meter charge', which is equal to N
150. Write a complete C program to read the number of units consumed and print out the
total charges.
c
#include <stdio.h>
int main() {
int units;
float totalCharge = 0.0;
const float METER_CHARGE = 150.0;
printf("Enter the number of units consumed: ");
scanf("%d", &units);
// Calculate charges based on consumption
if(units <= 100) {
totalCharge = units * 150.0;
} else if(units <= 300) {
totalCharge = (100 * 150.0) + ((units - 100) * 200.0);
} else {
totalCharge = (100 * 150.0) + (200 * 200.0) + ((units - 300) * 255.0);
}
// Add meter charge
totalCharge += METER_CHARGE;
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;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
if(m <= 0 || n <= 0) {
printf("Invalid matrix dimensions!\n");
return 1;
}
int matrix[m][n];
printf("Enter the elements of the %dx%d matrix:\n", m, n);
// Input matrix elements
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
printf("Enter element at position [%d][%d]: ", i+1, j+1);
scanf("%d", &matrix[i][j]);
sum += matrix[i][j];
}
}
// Calculate average
average = sum / (m * n);
// Display the matrix
printf("\nThe matrix is:\n");
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\nSum of all elements: %.2f\n", sum);
printf("Average of all elements: %.2f\n", average);
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>
// Function to calculate factorial
unsigned long long factorial(int n) {
if(n == 0 || n == 1) {
return 1;
}
unsigned long long fact = 1;
for(int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
// Function to reverse an array
void reverseArray(int arr[], int size) {
int temp;
for(int i = 0; i < size/2; i++) {
temp = arr[i];
arr[i] = arr[size-1-i];
arr[size-1-i] = temp;
}
}
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;
printf("\nEnter the size of the array: ");
scanf("%d", &size);
if(size <= 0) {
printf("Invalid array size!\n");
break;
}
int arr[size];
printf("Enter %d elements:\n", size);
for(i = 0; i < size; i++) {
printf("Element %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nOriginal array: ");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
reverseArray(arr, size);
printf("\nReversed array: ");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
break;
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>
// Function to check if a number is prime
bool isPrime(int num) {
if(num <= 1) {
return false;
}
if(num <= 3) {
return true;
}
if(num % 2 == 0 || num % 3 == 0) {
return false;
}
for(int i = 5; i * i <= num; i += 6) {
if(num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true;
}
// Function to find sum of prime numbers in array
int sumOfPrimes(int arr[], int size) {
int sum = 0;
printf("Prime numbers in the array: ");
for(int i = 0; i < size; i++) {
if(isPrime(arr[i])) {
printf("%d ", arr[i]);
sum += arr[i];
}
}
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];
printf("Enter %d elements:\n", size);
for(int i = 0; i < size; i++) {
printf("Element %d: ", i+1);
scanf("%d", &arr[i]);
}
int result = sumOfPrimes(arr, size);
printf("Sum of all prime numbers in the array: %d\n", result);
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: ");
for(int i = 1; i <= n; i++) {
float term = (float)i / (i + 1);
sum += term;
printf("%d/%d", i, i+1);
if(i < n) {
printf(" + ");
}
}
printf("\nSum of %d terms: %.6f\n", n, sum);
return 0;
}