# C++ Programming/Exercises/Functions/Pages

Jump to navigation Jump to search
1. include<iostearm.h>
2. include<conio.h>
```void add();
void mul();
void main()
```

{

```  int a,b;
clrscr();
cout<<"enter 2 num";
cin>>a>>b;
add(a,b);
mul(a,b);
gatch();
```

}

```void add (inta,intb);
{
intc;
c=a+b;
cout<<c<<endl;
}
void mul(intx,inty);
{
int c;
c=x*y;
cout<<c<<endl;
}
```

## Contents

### EXERCISE 1

.Write a program that performs arithmetic division. The program will use two integers, a and b (obtained by the user) and will perform the division a/b, store the result in another integer c and show the result of the division using cout. In a similar way, extend the program to add, subtract, multiply, do modulo and power using integers a and b. Modify your program so that when it starts, it asks the user which type of calculation it should do, then asks for the 2 integers, then runs the user selected calculation and outputs the result in a user friendly formatted manner.

Sample run:

```Enter two numbers: 3 12
The sum is 15.
```
Solution

Solution #1

```#include <iostream>
using namespace std;

int AddTwo (int addend1, int addend2)  {
return addend1 + addend2;
}

int main ()  {
int number1, number2, sum;

cout << "Enter two integers:\n";
cin >> number1 >> number2;
sum = AddTwo(number1, number2);
cout << "\nThe sum is " << sum << ".";

}
```

Solution #2

```//by blazzer12
//Input two values. Call a function that returns the sum of the values.

#include<iostream>

using namespace std;

int getSum(int, int);

int main()
{
int number1, number2, sum;

//get values
cout<<"Enter two integers to add"<<endl;

cout<<"Number1 :";
cin>>number1;
cout<<"Number2 :";
cin>>number2;

//call getSum() and store result in sum
sum = getSum(number1, number2);

//print result
cout<<number1<<" + "<<number2<<" = "<<sum;
}

int getSum(int addend1, int addend2)
{
return addend1 + addend2;
}
```

The solution in C.

```#include <stdio.h>

int add(int, int);

int main()
{
int a, b, sum;

printf("A: ");
scanf("%d", &a);

printf("B: ");
scanf("%d", &b);

sum = add(a, b);

printf("The sum of %d and %d is %d.\n", a, b, sum);

return 0;
}

int add(int a, int b)
{
return a + b;
}
```

```// Another solution:
# include <iostream>

using namespace std;

int sum (int number1, int number2);

int number1;
int number2;

int main()
{

cout<<"Give me a number amigo: ";
cin>>number1;

cout<<"Give me another number dude: ";
cin>>number2;

cout<<"The sum of "<<number1<<" and "<<number2<<" is: "<<sum(number1,number2)<<"."<<endl;

return 0;

}

int sum (int number1, int number2)
{
return number1+number2;
}

// by neuroalchemist
```

### EXERCISE 2

Basically the same as exercise 1, but this time, the function that adds the numbers should be void, and takes a third, pass by reference parameter; then puts the sum in that.

Solution

Solution #1

```#include <iostream>
using namespace std;

void AddTwo (int addend1, int addend2, int &sum)  {
sum = addend1 + addend2;
}

int main ()  {
int number1, number2, sum;

cout << "Enter two integers:\n";
cin >> number1 >> number2;
AddTwo(number1, number2, sum);
cout << "\nThe sum is " << sum << ".";

return 0;
}
```

Solution #2

```//by blazzer12
//adds two integers using a "pass by reference" type function call.

#include <iostream>
using namespace std;

int addNum(int, int, int&);

int main()
{
int number1,number2,sum;

//get values;
cout<<"Enter two integers to add"<<endl;

cout<<"Enter Number 1: ";
cin>>number1;
cout<<"Enter Number 2: ";
cin>>number2;

//call addNum to add the numbers
addNum(number1, number2, sum);

//print sum
cout<<number1<<" + "<<number2<<" = "<<sum;
return 0;
}
int addNum(int addend1, int addend2, int &sum)
{
sum = addend1 + addend2;
}
```

Solution #3

```#include<iostream>

using namespace std;

void add(int,int);

int sum=0;

int main(){
cout<<"Please input the first number you wanna add:"<<endl;
int num1;
cin>>num1;
cout<<"Please input the second number you wanna ad"<<endl;
int num2;
cin>>num2;
add(num1,num2);
cout<<"The sum of these two number:"<<num1<<"&"<<num2<<" is:"<<sum<<endl;
return 0;
}

void add(int a,int b){
sum=a+b;
}
```

### EXERCISE 3

Write a recursive function that finds the #n integer of the Fibonacci sequence. Then build a minimal program to test it. For reference see Wikipedia:Fibonacci number.

```For any possible natural number "n", the following applies
fib(n+2) = fib(n+1) + fib(n)
Also, the following are predefined
fib(0) = 0
fib(1) = 1
```
Solution

Solution #1

```#include <iostream>

using namespace std;

unsigned fib(unsigned n);

int main()
{
// Printing the first 20 Fibonacci sequence values
for (unsigned i = 0; i < 20; i++){
cout << "fib(" << i << ") = " << fib(i) << endl;
}
}

unsigned fib(unsigned n)
{
if (n < 2)
return n;

return fib(n-2) + fib(n-1);
}
```

### EXERCISE 4

Basically the same as exercise 3, although this time you mustn't use recursion.

Solution

Solution #1

```#include <iostream>

using namespace std;

unsigned fib(unsigned n);

int main()
{
// Printing the first 20 Fibonacci sequence values
for (unsigned i = 0; i < 20; i++){
cout << "fib(" << i << ") = " << fib(i) << endl;
}
}

unsigned fib(unsigned n)
{
if (n < 2)
return n;

unsigned prev1 = 0;
unsigned prev2 = 1;

for (unsigned i = 0; i <= n-2; i++){
unsigned temp = prev1 + prev2;
// Just doing a rotation of values, since only the last two are needed
prev1 = prev2;
prev2 = temp;
}

return prev2;
}
```

For extra exercise, give a big number( like 1000000 ) to both exercise 3 and 4 solutions and compare the execution times. Ponder on the results ;)

### EXERCISE 5

Create a calculator that takes a number, a basic math operator (+,-,*,/,^), and a second number all from user input, and have it print the result of the mathematical operation. The mathematical operations should be wrapped inside of functions.

Solution

Solution #1

Hammad city university

```#include<iostream>
using namespace std;
void calculator(int num, int num2, int result);
void calculator(int num,int num2,int result)
{
char op;
cout<<"\n     Calculator:-  \nEnter Number: " ;
cin>>num;
cout<<"Enter operator +,-,*,/,^  : ";
cin >>op;
cout<<"Enter second number: " ;
cin>>num2;
if(op=='+')result=num+num2;
if(op=='-')result=num-num2;
if(op=='*')result=num*num2;
if(op=='/')result=num/num2;
if(op=='^')result=num^num2;
cout<<"result: "<<result<<"\n";
}
main()
{
int a,b,c;
calculator(a,b,c);
return 0;
}

}
```

Solution #2

```#include <iostream>
float add(float a, float b)
{
return a + b;
}
float sub(float a, float b)
{
return a - b;
}
float mul(float a, float b)
{
return a * b;
}
float div(float a, float b)
{
if(b != 0)
{
return a / b;

}
std::cout << "Error: division by zero.\n";
return 0;
}
float pow(float a, float b)
{
return pow(a, b);
}
float mod(float a, float b)
{
return fmod(a, b);
}
void calc()
{
float a, b;
char op;
std::cout << "Enter a #: ";
std::cin >> a;
std::cout << "Enter an operator (+, -, *, /, ^, %): ";
std::cin >> op;
std::cout << "Enter a second #: ";
std::cin >> b;
switch(op)
{
case '+':
std::cout << "Result: " << add(a, b);
break;
case '-':
std::cout << "Result: " << sub(a, b);
break;
case '*':
std::cout << "Result: " << mul(a, b);
break;
case '/':
std::cout << "Result: " << div(a, b);
break;
case '^':
std::cout << "Result: " << pow(a, b);
break;
case '%':
std::cout << "Result: " << mod(a, b);
break;
default:
std::cout << "Error: operator not valid.\n";
}
}
int main()
{
calc();
return 0;
}
```

Soln #3

```// This program will do simple calculations involving + - * / and ^ //

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int x,y,sum;

cout << "Enter first number ";
cin >> x;
cout << endl;

cout << "Enter second number ";
cin >> y;
cout << endl;

cout << 1 << " +" << endl;
cout << 2 << " -" << endl;
cout << 3 << " *" << endl;
cout << 4 << " /" << endl;
cout << 5 << " ^" << endl << endl;
cout << "What math would you like to do? ";
cin >> sum;
cout << endl;

switch (sum){
case 1:
sum = x + y;
cout << "The answer to your addition is " << sum << endl;
break;

case 2:
sum = x - y;
cout << "The answer to your subtraction is " << sum << endl;
break;

case 3:
sum = x * y;
cout << "The answer to your multiplication is " << sum<< endl;
break;

case 4:
sum = x / y;
cout << "The answer to your division is " << sum << endl;
break;

case 5:
sum = pow(x,y);
cout << "The answer to your power function is " << sum << endl;
break;

default:
cout << "You have entered an invalid option " << endl;
break;
}

return 0;
}
```