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

Katikilo Soln

Uploaded by

eddydeleibniz01
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 views6 pages

Katikilo Soln

Uploaded by

eddydeleibniz01
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/ 6

MBEYA UNIVERSITY OF SCIENCE AND TECHNOLOGY

Special Test: Programming Solutions

Question: 1. Write a program to accept the total scores for students in subjects CS 8204 and

CS 8200. The program should process data for 510 students, determine the highest score,

and display it.

#include <iostream>

using namespace std;

int main() {

const int numStudents = 510;

int scores[numStudents];

int highestScore = 0;

cout << "Enter the scores for " << numStudents << " students:\n";

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

cin >> scores[i];

if (scores[i] > highestScore) {

highestScore = scores[i];

cout << "The highest score is: " << highestScore << endl;

return 0;

Question: 2. Develop a program that converts between kilometers and meters based on the
user's choice. Include a main() function and two helper functions: kilometerToMeter()

(converts kilometers to meters) and meterToKilometer() (converts meters to kilometers). The

program should display options to the user, accept the necessary input, and show the output

accordingly.

#include <iostream>

using namespace std;

void kilometerToMeter(double kilometers) {

cout << kilometers << " kilometers = " << kilometers * 1000 << " meters\n";

void meterToKilometer(double meters) {

cout << meters << " meters = " << meters / 1000 << " kilometers\n";

int main() {

int choice;

double value;

cout << "Choose an option:\n1. Convert kilometers to meters\n2. Convert meters to

kilometers\n";

cin >> choice;

if (choice == 1) {

cout << "Enter value in kilometers: ";

cin >> value;

kilometerToMeter(value);
} else if (choice == 2) {

cout << "Enter value in meters: ";

cin >> value;

meterToKilometer(value);

} else {

cout << "Invalid choice!\n";

return 0;

Question: 3. Create a program that prompts the user to input an integer and calculates its

factorial.

#include <iostream>

using namespace std;

int main() {

int number;

long long factorial = 1;

cout << "Enter an integer: ";

cin >> number;

if (number < 0) {

cout << "Factorial of a negative number doesn't exist.\n";

} else {

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


factorial *= i;

cout << "Factorial of " << number << " is: " << factorial << endl;

return 0;

Question: 4. Write a program to extract and display the initials of a user's first name, middle

name, and last name.

#include <iostream>

#include <string>

using namespace std;

int main() {

string firstName, middleName, lastName;

cout << "Enter your first name: ";

cin >> firstName;

cout << "Enter your middle name: ";

cin >> middleName;

cout << "Enter your last name: ";

cin >> lastName;

cout << "Your initials are: "

<< firstName[0] << middleName[0] << lastName[0] << endl;


return 0;

Question: 5. Write a program to display the following 3x3 matrix and determine the greatest

number within it:

6 9 7

8 5 12

4 6 16

#include <iostream>

using namespace std;

int main() {

int matrix[3][3] = {

{6, 9, 7},

{8, 5, 12},

{4, 6, 16}

};

int greatest = matrix[0][0];

cout << "Matrix:\n";

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

for (int j = 0; j < 3; j++) {

cout << matrix[i][j] << " ";

if (matrix[i][j] > greatest) {

greatest = matrix[i][j];

}
cout << endl;

cout << "The greatest number in the matrix is: " << greatest << endl;

return 0;

You might also like