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

Assignment 04

Uploaded by

musclesgrowth99
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)
18 views

Assignment 04

Uploaded by

musclesgrowth99
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/ 13

Assignment # 04

Name: Muhammad Zammad


Roll No: BS-23-LB-100303
Section: B

Task # 01:
Code:
#include<iostream>
using namespace std;
int fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

void printFibonacci(int terms)


{
for (int i = 0; i < terms; ++i)
{
std::cout << fibonacci(i) << " ";
}
}
int main() {
int terms;

cout << "Enter the number of terms in the Fibonacci sequence: ";
cin >> terms;

cout << "Fibonacci Sequence up to " << terms << " terms: ";
printFibonacci(terms);

return 0;
}

Output:

Task # 02:
Code:
#include <iostream>
#include <cmath>
using namespace std;

double rectangle(double length, double width) {


return length * width;
}
double triangle(double base, double height) {
return 0.5 * base * height;
}

double circle(double r) {
return 3.14 * r * r;
}

int main() {
double length, width, base, height, radius;

// Rectangle
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
double rectangleArea = rectangle(length, width);
cout << "Area of rectangle: " << rectangleArea << endl;

// Triangle
cout << "Enter the base of the triangle: ";
cin >> base;
cout << "Enter the height of the triangle: ";
cin >> height;
double triangleArea = triangle(base, height);
cout << "Area of triangle: " << triangleArea << endl;
// Circle
cout << "Enter the radius of the circle: ";
cin >> radius;
double circleArea = circle(radius);
cout << "Area of circle: " << circleArea << endl;

return 0;
}

Output:

Task # 03:
Code:
#include <iostream>
using namespace std;
double power(double base, int exponent)
{
if (exponent == 0)
{
return 1;
} else
{
return base * power(base, exponent - 1);
}
}

int main()
{

double base;
int exponent;
cout << "Enter the base: ";
cin >> base;
cout << "Enter the exponent: ";
cin >> exponent;
cout << base << " raised to the power " << exponent << " is: " << power(base, exponent) <<
endl;

return 0;
}

Output:

Task # 04:
Code:
#include <iostream>
using namespace std;
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int main()
{

int num1, num2;


cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;

cout << "Before swapping - Num1: " << num1 << ", Num2: " << num2 << endl;
swap(&num1, &num2);
cout << "After swapping - Num1: " << num1 << ", Num2: " << num2 << endl;

return 0;
}

Output:
Task # 05:
Code:
#include <iostream>
using namespace std;
void Cal_Cost(int& Length, int& Width, int& Total_Cost)
{
const int cost_per_sq_ft = 150;
Total_Cost = Length * Width * cost_per_sq_ft;
}

int main()
{

int Length, Width, Total_Cost;


cout << "Enter the length of the house in feet: ";
cin >> Length;
cout << "Enter the width of the house in feet: ";
cin >> Width;
Cal_Cost(Length, Width, Total_Cost);
cout << "The total cost of the house is: Rs " << Total_Cost << endl;

return 0;
}

Output:
Task # 06:
Code:
#include <iostream>
using namespace std;
double pow(double a, int x)
{
double result = 1;
for (int i = 1; i <= x; i++)
{
result =result* x;
}
return result;
}
int fact(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * fact(n - 1);
}
}
double calculateSum(double x, int n)
{
double sum = 0.0;
for (int i = 0; i <= n; i += 2)
{
sum =sum+ pow(x, i) / fact(i + 1);
}
return sum;
}
int main()
{
double x;
int n;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the value of n: ";
cin >> n;
double sum = calculateSum(x, n);
cout << "The calculated sum is: " << sum << endl;
return 0;
}

Output:

Task # 07:
Code:
#include <iostream>
using namespace std;

int add(int* a, int* b)


{
return (*a) + (*b);
}
int subtract(int* a, int* b)
{
return (*a) - (*b);
}
int multiply(int* a, int* b)
{
return (*a) * (*b);
}
int divide(int* a, int* b)
{
return (*a) / (*b);
}

int main()
{

int value1, value2;


char choice;
cout << "Enter the first integer: ";
cin >> value1;
cout << "Enter the second integer: ";
cin >> value2;

cout << "Select the operation:" << endl;


cout << "1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Divide" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice)
{
case '1':
cout << "Result of addition: " << add(&value1, &value2) << endl;
break;
case '2':
cout << "Result of subtraction: " << subtract(&value1, &value2) << endl;
break;
case '3':
cout << "Result of multiplication: " << multiply(&value1, &value2) << endl;
break;
case '4':
if (value2 != 0)
{
cout << "Result of division: " << divide(&value1, &value2) << endl;
} else
{
cout << "Cannot divide by zero." << endl;
}
break;
default:
cout << "Invalid choice." << endl;
}

return 0;
}

Output:
Multiplication:

Addition:

Task # 08:
Code:
#include <iostream>
using namespace std;

double calculateY(double x, double m = 2, double c = 1) {


return m * x + c;
}

int main()
{
for (int x = 0; x <= 5; ++x)
{
double y = calculateY(x);
cout << "For x = " << x << ", y = " << y << endl;
}

return 0;
}

Output:

You might also like