[go: up one dir, main page]

0% found this document useful (0 votes)
21 views13 pages

C Programming Exam Solutions - 2021-2022

The document contains solutions to programming questions for a System Programming course at the Federal University of Technology, Akure. It includes C programs for calculating mortgage repayments, modifying values based on conditions, converting numbers to different bases, demonstrating relational and logical operators, awarding bursaries based on student levels, calculating averages, checking divisibility in arrays, summing numbers while ignoring certain conditions, and managing student records. Each question is accompanied by code snippets and explanations.

Uploaded by

freedagoomam8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

C Programming Exam Solutions - 2021-2022

The document contains solutions to programming questions for a System Programming course at the Federal University of Technology, Akure. It includes C programs for calculating mortgage repayments, modifying values based on conditions, converting numbers to different bases, demonstrating relational and logical operators, awarding bursaries based on student levels, calculating averages, checking divisibility in arrays, summing numbers while ignoring certain conditions, and managing student records. Each question is accompanied by code snippets and explanations.

Uploaded by

freedagoomam8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

System Programming with C

Federal University of Technology, Akure

2021/2022 First Semester Examination Solutions

Question 1a
Write a program to calculate annual mortgage repayment (R) given by the formula where A =
Amount borrowed(N), n = term of mortgage(years), r = interest rate (%).

#include <stdio.h>
#include <math.h>

int main() {
float A, r, R;
int n;

printf("Enter the amount borrowed (N): ");


scanf("%f", &A);

printf("Enter the term of mortgage (years): ");


scanf("%d", &n);

printf("Enter the interest rate (%%): ");


scanf("%f", &r);

// Convert interest rate to decimal and monthly rate


r = r / 100 / 12;

// Calculate monthly mortgage payment using the formula


R = (A * r * pow(1 + r, n * 12)) / (pow(1 + r, n * 12) - 1);

printf("Annual mortgage repayment: $%.2f\n", R * 12);

return 0;
}

Question 1b
Write a C program that accepts the value of a variable x from the keyboard and using a function
adds 100 to it if the value of x is greater than or equal to 10 (x ≥ 10) or subtract 50 if otherwise.

#include <stdio.h>

// Function to modify x based on condition


int modifyValue(int x) {
if (x >= 10) {
return x + 100;
} else {
return x - 50;
}
}

int main() {
int x, result;

printf("Enter the value of x: ");


scanf("%d", &x);

result = modifyValue(x);

printf("The result after applying the condition is: %d\n", result);

return 0;
}

Question 2
The algorithm below explains steps involved in converting an input value, Z, from base 10 to a
user-selectable base b (where 2 ≤ b ≤ 10). Working from left to right, producing the most-
significant digit first and the least significant digit last, write a program that repeatedly reads
value/base pairs, converts the value into that base, and prints the result.
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: %d\n", k);

printf("Result in base %d: ", base);

int i = k - 1;
while (i >= 0) {
// Calculate the current digit
int digit = value / pow(base, i);
printf("%d", digit);

// Update value
value = value - digit * pow(base, i);
i--;
}
printf("\n");
}

int main() {
int value, base;
char choice;

do {
printf("Enter a value (in base 10): ");
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 simple program to explain the usage of: (i) relational operators in C language. (ii) logical
operators in C language.
c

#include <stdio.h>

int main() {
int a = 10, b = 5;

// Demonstration of relational operators


printf("Demonstration of Relational Operators:\n");
printf("a = %d, b = %d\n", a, b);
printf("a == b : %d (Equal to)\n", a == b);
printf("a != b : %d (Not equal to)\n", a != b);
printf("a > b : %d (Greater than)\n", a > b);
printf("a < b : %d (Less than)\n", a < b);
printf("a >= b : %d (Greater than or equal to)\n", a >= b);
printf("a <= b : %d (Less than or equal to)\n", a <= b);

// Demonstration of logical operators


printf("\nDemonstration of Logical Operators:\n");
int x = 1, y = 0; // Using as boolean true(1) and false(0)
printf("x = %d (true), y = %d (false)\n", x, y);
printf("x && y : %d (Logical AND - true if both operands are true)\n", x && y);
printf("x || y : %d (Logical OR - true if at least one operand is true)\n", x || y);
printf("!x : %d (Logical NOT - inverts the state of operand)\n", !x);
printf("!y : %d (Logical NOT - inverts the state of operand)\n", !y);

// Practical example combining operators


printf("\nPractical Example:\n");
int age = 20;
int hasID = 1; // 1 means true
printf("Age = %d, Has ID = %d\n", age, hasID);
printf("Can buy alcohol (age >= 21 && hasID): %d\n", (age >= 21 && hasID));

return 0;
}

Question 3b
Write a program that checks the level of study of FUTA students and award bursary based on the
level using switch case statement.
c

#include <stdio.h>

int main() {
int level;
float bursary = 0.0;

printf("Enter student level (100, 200, 300, 400, or 500): ");


scanf("%d", &level);

switch(level) {
case 100:
bursary = 5000.0;
printf("First year student: Bursary awarded is N%.2f\n", bursary);
break;
case 200:
bursary = 7500.0;
printf("Second year student: Bursary awarded is N%.2f\n", bursary);
break;
case 300:
bursary = 10000.0;
printf("Third year student: Bursary awarded is N%.2f\n", bursary);
break;
case 400:
bursary = 12500.0;
printf("Fourth year student: Bursary awarded is N%.2f\n", bursary);
break;
case 500:
bursary = 15000.0;
printf("Fifth year student: Bursary awarded is N%.2f\n", bursary);
break;
default:
printf("Invalid level entered. Please enter 100, 200, 300, 400, or 500.\n");
}

return 0;
}

Question 4a
Write a function in C that reads 5 numbers from the console. The function should return the
average of those numbers. Show how the function can be called from the main function.
c

#include <stdio.h>

// Function to calculate average of 5 numbers


float calculateAverage() {
float sum = 0.0;
float num;

printf("Enter 5 numbers:\n");

for(int i = 0; i < 5; i++) {


printf("Number %d: ", i+1);
scanf("%f", &num);
sum += num;
}

return sum / 5;
}

int main() {
float average;

// Call the function and store its return value


average = calculateAverage();

printf("The average of the 5 numbers is: %.2f\n", average);

return 0;
}

Question 4b
Write a function in C that takes two arguments, an array of 500 integers and an integer. Your
function should print out all of the elements of the array that are exactly divisible by 2nd
argument. The function should check whether the 2nd argument is zero and print an appropriate
message if it is.
c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to check for divisibility


void checkDivisibility(int arr[], int size, int divisor) {
// Check if divisor is zero
if (divisor == 0) {
printf("Error: Division by zero is not allowed.\n");
return;
}

printf("Elements exactly divisible by %d:\n", divisor);


int count = 0;

for (int i = 0; i < size; i++) {


if (arr[i] % divisor == 0) {
printf("%d ", arr[i]);
count++;

// Print 10 numbers per line for better readability


if (count % 10 == 0) {
printf("\n");
}
}
}

if (count == 0) {
printf("No elements are exactly divisible by %d.\n", divisor);
} else {
printf("\nTotal %d elements found that are divisible by %d.\n", count, divisor);
}
}

int main() {
int arr[500];
int divisor;

// Fill array with random numbers (for demonstration)


srand(time(NULL));
for (int i = 0; i < 500; i++) {
arr[i] = rand() % 1000 + 1; // Random numbers between 1 and 1000
}
printf("Enter a divisor: ");
scanf("%d", &divisor);

// Call the function


checkDivisibility(arr, 500, divisor);

return 0;
}

Question 5a
Write a program in C that will sum all the numbers from 1 to 1000 while ignoring all numbers
divisible by 3 and 5.

#include <stdio.h>

int main() {
int sum = 0;

for (int i = 1; i <= 1000; i++) {


// Skip numbers divisible by both 3 and 5
if (i % 3 != 0 && i % 5 != 0) {
sum += i;
}
}

printf("The sum of all numbers from 1 to 1000 (excluding those divisible by 3 and 5) is: %d

return 0;
}

Question 5b
Write a C program that reads names of fifty (50) students, their Matric number and displays a list
containing the names of students and their Matric number.
c

#include <stdio.h>
#include <string.h>

#define MAX_STUDENTS 50
#define MAX_NAME_LENGTH 50
#define MAX_MATRIC_LENGTH 20

struct Student {
char name[MAX_NAME_LENGTH];
char matricNumber[MAX_MATRIC_LENGTH];
};

int main() {
struct Student students[MAX_STUDENTS];
int i;

printf("Enter information for %d students:\n", MAX_STUDENTS);

// Input student information


for (i = 0; i < MAX_STUDENTS; i++) {
printf("\nStudent %d:\n", i+1);

printf("Name: ");
scanf(" %[^\n]s", students[i].name);

printf("Matric Number: ");


scanf(" %[^\n]s", students[i].matricNumber);
}

// Display student information


printf("\n\nStudent Records:\n");
printf("%-5s %-30s %-20s\n", "No.", "Name", "Matric Number");
printf("------------------------------------------------\n");

for (i = 0; i < MAX_STUDENTS; i++) {


printf("%-5d %-30s %-20s\n", i+1, students[i].name, students[i].matricNumber);
}

return 0;
}

You might also like