0% found this document useful (0 votes)
7 views

Assignment 5

Uploaded by

myr7hcxt5g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment 5

Uploaded by

myr7hcxt5g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ASSIGNMENT NO.

05

Submitted by:
Ahmad Sameet Bin Manzar

Roll No:
SU92-BIOTM-S24-015

PROGRAMMING FUNDAMENTALS

THE SUPERIOR UNIVERSITY, LAHORE


Department Of Information & Technology
Section: BSIOT-1A

Submitted to:
Sir Nadeem Sarfraz

June 11, 2024


1. A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number:
#include <iostream>
using namespace std;
int sumOfDigits(int number) {
int sum = 0, digit;

while (number > 0) {


digit = number % 10;
sum += digit;
number /= 10;
}

return sum;
}
int main() {
int number;

cout << "Enter a 5-digit positive integer: ";


cin >> number;
int sum = sumOfDigits(number);

cout << "Sum of the digits: " << sum << endl;

return 0;
}

2. A 5-digit positive integer is entered through the keyboard, write a function to


calculate sum of digits of the 5-digit number:
#include <iostream>
#include <vector>
using namespace std;

bool isPrime(int number) {


if (number <= 1) return false;
for (int i = 2; i <= number / 2; ++i) {
if (number % i == 0) return false;
}
return true;
}
vector<int> primeFunction(int number) {
vector<int> factors;

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


while (number % i == 0) {
if (isPrime(i)) {
factors.push_back(i);
number /= i;
}
}
}

return factors;
}

int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;

if (num <= 0) {
cout << "Please enter a positive integer." << endl;
return 1;
}

vector<int> factors = primeFunction(num);

cout << "Prime factors: ";


for (int factor : factors) {
cout << factor << " ";
}
cout << endl;

return 0;
}
3. Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence.
In a Fibonacci sequence the sum of two successive terms gives the third term.
Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21
34 55 89...
#include <iostream>
using namespace std;

int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
void printFibonacciSequence(int count) {
for (int i = 0; i < count; ++i) {
cout << fibonacci(i) << " ";
}
cout << endl;
}
int main() {
int count = 25;
cout << "The first " << count << " numbers of the Fibonacci sequence are:" << endl;
printFibonacciSequence(count);
return 0;
}
4. A positive integer is entered through the keyboard, write a function to find the
binary equivalent of this number using recursion
#include <iostream>
using namespace std;
void printBinary(int number) {
if (number > 1) {
printBinary(number / 2);
}
cout << (number % 2);
}
int main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;

if (number < 0) {
cout << "Please enter a positive integer." << endl;
return 1;
}
cout << "Binary equivalent: ";
printBinary(number);
cout << endl;

return 0;
}
5. Write a function to compute the distance between two points and use it to develop
another function that will compute the area of the triangle whose vertices are A(x1,
y1), B(x2, y2), and C(x3, y3). Use these functions to develop a function which returns
a value 1 if the point (x, y) lines inside the triangle ABC, otherwise a value 0.
#include <iostream>
using namespace std;

double areaOfTriangle(double x1, double y1, double x2, double y2, double x3, double
y3) {
double abs((x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)) / 2.0);
return abs;}
bool isInsideTriangle(double x1, double y1, double x2, double y2, double x3, double y3,
double x, double y) {

double totalArea = areaOfTriangle(x1, y1, x2, y2, x3, y3);

double area1 = areaOfTriangle(x, y, x1, y1, x2, y2);


double area2 = areaOfTriangle(x, y, x2, y2, x3, y3);
double area3 = areaOfTriangle(x, y, x3, y3, x1, y1);

return totalArea == (area1 + area2 + area3);


}
int main() {

double x1 = 0, y1 = 0;
double x2 = 4, y2 = 0;
double x3 = 2, y3 = 3;

double x = 2, y = 1.5;
bool inside = isInsideTriangle(x1, y1, x2, y2, x3, y3, x, y);
if (inside) {
cout << "The point (" << x << ", " << y << ") lies inside the triangle ABC." << endl;
} else {
cout << "The point (" << x << ", " << y << ") does not lie inside the triangle ABC." <<
endl;
}

return 0;
}
6. During the tax season, every Friday, the J&J accounting firm provides assistance to
people who prepare their own tax returns. Their charges are as follows:
a. If a person has low income (,5 25,000) and the consulting time is less than or
equal to 30 minutes, there are no charges; otherwise, the service charges
are 40% of the regular hourly rate for the time over 30 minutes.
b. For others, if the consulting time is less than or equal to 20 minutes, there
are no service charges; otherwise, service charges are 70% of the regular
hourly rate for the time over 20 minutes.
(For example, suppose that a person has low income and spent 1 hour and 15
minutes, and the hourly rate is $70.00. Then the billing amount is 70.00 *40* (45/60)
= $21.00)
#include <iostream>
using namespace std;

double calculateBillingAmount(double hourlyRate, int consultingTime, bool lowIncome)


{
double serviceCharge = 0.0;

if (lowIncome) {
if (consultingTime > 30) {
serviceCharge = hourlyRate * 0.4 * ((double)(consultingTime - 30) / 60);
}
} else {
if (consultingTime > 20) {
serviceCharge = hourlyRate * 0.7 * ((double)(consultingTime - 20) / 60);
}
}

return serviceCharge;
}

int main() {
double hourlyRate;
int consultingTime;
bool lowIncome;

cout << "Enter the hourly rate: $";


cin >> hourlyRate;

cout << "Enter the total consulting time (in minutes): ";
cin >> consultingTime;

cout << "Is the person low income? (1 for Yes, 0 for No): ";
cin >> lowIncome;
double billingAmount = calculateBillingAmount(hourlyRate, consultingTime,
lowIncome);

cout << "The billing amount is: $" << billingAmount << endl;

return 0;
}
Write a program that prompts the user to enter the hourly rate, the total consulting
time, and whether the person has low income. The program should output the
billing amount. Your program must contain a function that takes as input the hourly
rate, the total consulting time, and a value indicating whether the person has low
income. The function should return the billing amount. Your program may prompt
the user to enter the consulting time in minutes.

7. Write a program that prints the day number of the year, given the date in the form
month-day-year. For example, if the input is 1-1-2006, the day number is 1; if the
input is 12-25-2006, the day number is 359. The program should check for a leap
year. A year is a leap year if it is divisible by 4, but not divisible by 100. For example,
1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a
leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by
400. However, 1800 is not a leap year because 1800 is not divisible by 400.
#include <iostream>
using namespace std;

bool isLeapYear(int year) {


return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int dayOfYear(int month, int day, int year) {
int daysInMonth[] = {31, 28 + isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dayNumber = day;
for (int i = 0; i < month - 1; ++i) {
dayNumber += daysInMonth[i];
}
return dayNumber;
}
int main() {
int month, day, year;

cout << "Enter the date in the format month-day-year: ";


cin >> month >> day >> year;

if (month < 1 || month > 12 || day < 1 || day > 31 || year < 1) {
cout << "Invalid date!" << endl;
return 1;
}
cout << "Day number of the year: " << dayOfYear(month, day, year) << endl;

return 0;
}
8. A positive integer is entered through the keyboard. Write a function to obtain the
prime factors of this number.
#include <iostream>
#include <vector>
using namespace std;

void primeFactors(int n) {
vector<int> factors;

for (int i = 2; i * i <= n; ++i) {


while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 1) {
factors.push_back(n);
}
cout << "Prime factors of the number: ";
for (int factor : factors) {
cout << factor << " ";
}
cout << endl;
}
int main() {
int num;

cout << "Enter a positive integer: ";


cin >> num;

if (num <= 0) {
cout << "Please enter a positive integer." << endl;
return 1;
}

primeFactors(num);

return 0;
}
9. Write a function to calculate the factorial value of any integer entered through the
keyboard.
#include <iostream>
using namespace std;

unsigned factorial(int n) {

if (n == 0) {
return 1;
}
unsigned long long fact = 1;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
return fact;
}
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;

cout << "Factorial of " << num << " is: " << factorial(num) << endl;

return 0;
}
1.

You might also like