0% found this document useful (0 votes)
3 views9 pages

Muhammad Zain 502138 BEE16 (D) Lab#8

The document is a lab report for a computer programming course at NUST, detailing four programming tasks. Each task includes a description and corresponding C++ code: calculating the hypotenuse of a triangle, creating a gym registration program, a multiplication quiz for students, and checking if a number is a strong number using recursion. The report is submitted by a student named Muhammad Zain and includes various coding exercises aimed at enhancing programming skills.

Uploaded by

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

Muhammad Zain 502138 BEE16 (D) Lab#8

The document is a lab report for a computer programming course at NUST, detailing four programming tasks. Each task includes a description and corresponding C++ code: calculating the hypotenuse of a triangle, creating a gym registration program, a multiplication quiz for students, and checking if a number is a strong number using recursion. The report is submitted by a student named Muhammad Zain and includes various coding exercises aimed at enhancing programming skills.

Uploaded by

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

National University of Sciences & Technology - NUST

School of Electrical Engineering and Computer Science


- SEECS Department of Electrical Engineering

DEPARTMENT OF ELECTRICAL ENGINEERING

CS107 Computer Programming

Lab Report # 8

Lab Instructor: Sir Moeed Ahmed

Submitted by: Muhammad Zain

CMS ID: 502138

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;

double hypotenuse(double a, double b);

int main() {
double side1, side2, hypotenuseLength;
cout<< "Enter the lengths of the two sides of the right triangle: ";
cin>> side1>>side2;

hypotenuseLength = hypotenuse(side1, side2);

cout<< "\nThe length of the hypotenuse is: "<< fixed


<< setprecision(2) << hypotenuseLength<< endl;

cout<< "\nRounded to the nearest whole number: "


<< static_cast<int>(round(hypotenuseLength))<< endl;

return 0;
}

double hypotenuse(double a,double b) {


return sqrt(pow(a, 2) +pow(b, 2));
}
OUTPUT
Lab Task#2:
Write a program with a function greet() to print a welcome message to the user
after signing up on the registration web form at a gym. The program will take
details like name, age, weight and birth place as input.
a) The function should assign an registration id to each user using random function
and display in output b) The function must print output if no name was provided
c) The function should print date and time of joining
d) The program will ask again for the input if age is below 18

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;
}

cout<<"Enter your age: ";


cin>> age;

cout<< "Enter your weight in kg: ";


cin>> weight;

cout<< "Enter your birthplace: ";


cin.ignore();
getline(cin, birthPlace);

if (age < 18) {


cout<< "You must be at least 18 years old to register. Please provide
the details again."<< endl;
continue;
}

srand(time(0));
int registrationID = rand() % 10000;

time_t now = time(0);


char* dt = ctime(&now);

cout<< "\nWelcome " << name << "!" << endl;


cout<< "Your registration ID is: "<< registrationID <<endl;
cout<< "Date and time of joining: " << dt;
cout << "Weight: " << weight << " kg"<< endl;
cout<< "Birthplace: "<< birthPlace<< endl;

} while (age < 18);


}

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;

} while (choice == 'y' || choice == 'Y');


return 0;
}
OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy