Assignment 04
Assignment 04
Task # 01:
Code:
#include<iostream>
using namespace std;
int fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
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 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()
{
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()
{
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 main()
{
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;
int main()
{
for (int x = 0; x <= 5; ++x)
{
double y = calculateY(x);
cout << "For x = " << x << ", y = " << y << endl;
}
return 0;
}
Output: