0% found this document useful (0 votes)
21 views8 pages

Function C+++

The document contains multiple C++ programs that demonstrate various mathematical functions and operations. These include calculating the cube of a number, the area of geometric shapes (triangle, rectangle), determining line intersections, computing factorials and Fibonacci numbers, approximating Euler's number, checking for prime numbers, and performing basic arithmetic operations. Each program includes a main function that prompts the user for input and displays the results.

Uploaded by

sisaypagei
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)
21 views8 pages

Function C+++

The document contains multiple C++ programs that demonstrate various mathematical functions and operations. These include calculating the cube of a number, the area of geometric shapes (triangle, rectangle), determining line intersections, computing factorials and Fibonacci numbers, approximating Euler's number, checking for prime numbers, and performing basic arithmetic operations. Each program includes a main function that prompts the user for input and displays the results.

Uploaded by

sisaypagei
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

1 #include <iostream>

using namespace std;

int cube(int number) {

return number * number * number;

int main() {

int x = 3;

cout << "Cube of " << x << " is " << cube(x) << endl;

return 0; }

2 #include <iostream>

using namespace std;

float triangle(float h, float w) {

return 0.5 * w * h;

int main() {

float height = 5.0, base = 10.0;

cout << "Area of triangle: " << triangle(height, base) << endl;

return 0;

3 #include <iostream>

using namespace std;

float rectangle(float h, float w) {

return h * w;

int main() {
float height = 4.0, width = 6.0;

cout << "Area of rectangle: " << rectangle(height, width) << endl;

return 0; }

4 #include <iostream>

using namespace std;

float Line(float m, float b, float x) {

return m * x + b;}

int main() {

float slope = 2.0, intercept = 3.0, x_value = 4.0;

cout << "Y-coordinate: " << Line(slope, intercept, x_value) << endl;

return 0; }

5 #include <iostream>

using namespace std;

int Intersect(float m1, float b1, float m2, float b2) {

if (m1 != m2) {

return 1;

} else {

return 0; }

int main() {

float m1 = 2.0, b1 = 3.0, m2 = 2.0, b2 = 5.0;

if (Intersect(m1, b1, m2, b2)) {

cout << "The lines intersect." << endl;

} else {
cout << "The lines do not intersect." << endl; }

return 0; }

6 #include <iostream>

using namespace std;

// Recursive function to calculate factorial

int factorial(int n) {

if (n <= 1)

return 1;

else

return n * factorial(n - 1); }

int main() {

int number;

cout << "Enter a positive integer: ";

cin >> number;

if (number < 0) {

cout << "Invalid input. Please enter a positive integer." << endl;

} else {

cout << "Factorial of " << number << " is " << factorial(number) << endl;

return 0; }

7 #include <iostream>

using namespace std;

// Recursive function to calculate Fibonacci value

int fibonacci(int n) {

if (n == 0)
return 0;

else if (n == 1)

return 1;

else

return fibonacci(n - 1) + fibonacci(n - 2); }

int main() {

int number;

cout << "Enter a non-negative integer: ";

cin >> number;

if (number < 0) {

cout << "Invalid input. Please enter a non-negative integer." << endl;

} else {

cout << "Fibonacci value at position " << number << " is " << fibonacci(number) << endl;

return 0;

8 #include <iostream>

using namespace std;

// Recursive function to calculate factorial

int factorial(int n) {

if (n <= 1)

return 1;

else

return n * factorial(n - 1); }


// Function to approximate Euler's number

double approximateE(int terms) {

double e = 1.0; // Start with the first term: 1

for (int i = 1; i <= terms; ++i) {

e += 1.0 / factorial(i); }

return e;

int main() {

int n;

cout << "Enter the number of terms to approximate e: ";

cin >> n;

if (n < 0) {

cout << "Please enter a non-negative integer." << endl;

} else {

double e_approx = approximateE(n);

cout << "Approximation of e using " << n << " terms is: " << e_approx << endl;

return 0;

9 #include <iostream>

using namespace std;

// Function to check if a number is prime

bool isPrime(int n) {

if (n <= 1)

return false; // 0 and 1 are not prime

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


if (n % i == 0)

return false; }

return true;

10 #include<iostreame>

using namespace std;

int main() {

int number;

cout << "Enter an integer: ";

cin >> number;

if (isEven(number))

cout << number << " is even." << endl;

else

cout << number << " is odd." << endl;

return 0;

11 #include <iostream>

using namespace std;

// Function for addition

float sum(float a, float b) {

return a + b;

// Function for subtraction

float difference(float a, float b) {

return a - b; }
// Function for multiplication

float product(float a, float b) {

return a * b;

// Function for division

float quotient(float a, float b) {

if (b != 0)

return a / b;

else {

cout << "Error: Division by zero!" << endl;

return 0;

int main() {

float num1, num2;

char op;

cout << "Enter first number: ";

cin >> num1;

cout << "Enter operator (+, -, *, /): ";

cin >> op;

cout << "Enter second number: ";

cin >> num2;

switch (op) {

case '+':

cout << "Result: " << sum(num1, num2) << endl;


break;

case '-':

cout << "Result: " << difference(num1, num2) << endl;

break;

case '*':

cout << "Result: " << product(num1, num2) << endl;

break;

case '/':

cout << "Result: " << quotient(num1, num2) << endl;

break;

default:

cout << "Invalid operator!" << endl;

return 0; }

You might also like