Muhammad Zain 502138 BEE16 (D) Lab#8
Muhammad Zain 502138 BEE16 (D) Lab#8
Lab Report # 8
Section: BEE-16(D)
Semester: 2st
Date: 10/Apr/2025
Lab 08: Complex Function
Lab Task#1:
Define a function called hypotenuse () that calculates the length of longest side of a
right- angle triangle when the other two sides are given. This function should take
two arguments of type double and return the hypotenuse as a double number. The
program should also display the value to the nearest whole number
CODE
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double side1, side2, hypotenuseLength;
cout<< "Enter the lengths of the two sides of the right triangle: ";
cin>> side1>>side2;
return 0;
}
CODE
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
void greet();
int main() {
greet();
return 0;
}
void greet() {
string name, birthPlace;
int age, weight;
do {
cout<< "Enter your name: ";
getline(cin, name);
if (name.empty()) {
cout<< "No name was provided! Please enter a valid name."<< endl;
continue;
}
srand(time(0));
int registrationID = rand() % 10000;
OUTPUT
Lab Task#3:
Computers are playing an increasing role in education. Write a program that will
help a primary school student learn multiplication. Use rand function to produce two
positives and one-digit integers. It should then type a question such as:
How much is 6times 7?
a) The student then types the answer, and your program checks the answer
b) If it is correct, print "Very good!" and then ask another multiplication question
c) If the answer is wrong, print "No. Please try again." and then let the student try
the same question again repeatedly until the student finally gets it right
d) Program will terminate upon negative value
CODE
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(static_cast<unsigned int>(time(0)));
int num1, num2, answer, studentAnswer;
while (true) {
num1 = rand() % 9 + 1;
num2 = rand() % 9 + 1;
answer = num1 * num2;
cout << "How much is " << num1 << " times " << num2 << "? (Enter negative
number to quit): ";
cin >> studentAnswer;
if (studentAnswer < 0) {
cout << "\nExiting the program. Thank you" << endl;
break;
}
while (studentAnswer != answer) {
cout << "Wrong Answer. Please try again." << endl;
cout << "\nHow much is " << num1 << " times " << num2 << "? (Enter
negative to quit): ";
cin >> studentAnswer;
if (studentAnswer < 0) {
cout << "\nExiting the program. Thank you" << endl;
return 0;
}
}
cout << "Very good" << endl;
}
return 0;
}
OUTPUT
Lab Task#4:
Write a modular program to check whether a given number is a strong number or
not. A number is considered a strong number if the sum of the factorials of its
individual digits is equal to the original number entered by the user.
You must implement a recursive function called factorial () which takes a number
as an input and returns its factorial. Your program should keep on asking the end-
user to enter ‘y’ to continue & q’ to quit the program
CODE
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
bool isStrongNumber(int number) {
int sum = 0;
int tempNumber = number;
while (tempNumber > 0) {
int digit = tempNumber % 10;
sum += factorial(digit);
tempNumber /= 10;
}
return (sum == number);
}
int main() {
char choice;
do {
int number;
cout<< "Enter a number to check if it is a strong number: ";
cin>> number;
if (isStrongNumber(number))
cout<< number << " is a Strong Number."<< endl;
else
cout<< number << " is not a Strong Number."<< endl;
cout<< "\nDo you want to continue? (y to continue, q to quit): ";
cin>> choice;