Beruflich Dokumente
Kultur Dokumente
sum. The program should ask the user for two numbers, then call the function with the numbers as
arguments, and tell the user the sum.
#include<iostream>
int main()
{
int number1, number2, sum;
//get values
cout<<"Enter two integers to add"<<endl;
cout<<"Number1 :";
cin>>number1;
cout<<"Number2 :";
cin>>number2;
//print result
cout<<number1<<" + "<<number2<<" = "<<sum;
}
EXERCISE 2[edit]
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.
#include <iostream>
using namespace std;
int main () {
int number1, number2, sum;
return 0;
}
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
#include <iostream>
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;
EXERCISE 4
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.
#include <iostream>
#include <cmath>
int main()
{
int x,y,sum;
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;
}
C++ program to add two integers. Make a function add() to add integers and
display sum in main() function.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cout<<"Enters two numbers to add: ";
cin >> num1 >> num2;
// Function call
sum = add(num1, num2);
cout << "Sum = " << sum;
return 0;
}
// Function definition
int add(int a, int b)
{
int add;
add = a + b;
// Return statement
return add;
}
Example: Prime Numbers Between two
Intervals
#include <iostream>
using namespace std;
int checkPrimeNumber(int);
int main()
{
int n1, n2;
bool flag;
cout << "Prime numbers between " << n1 << " and " << n2 << " are: ";
if(flag == false)
cout << i << " ";
}
return 0;
}
// user-defined function to check prime number
int checkPrimeNumber(int n)
{
bool flag = true;
int checkPrimeNumber(int);
int main()
{
int n;
To call a function, you simply need to pass the required parameters along
with function name, and if function returns a value, then you can store
returned value. For example:
#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main () {
// local variable declaration:
int a = 100;
int b = 200;
int ret;
return 0;
}
return result;
}
Default Values for Parameters
When you define a function, you can specify a default value for each of the
last parameters. This value will be used if the corresponding argument is
left blank when calling to the function.
This is done by using the assignment operator and assigning values for the
arguments in the function definition. If a value for that parameter is not
passed when the function is called, the default given value is used, but if a
value is specified, this default value is ignored and the passed value is used
instead. Consider the following example:
#include <iostream>
using namespace std;
result = a + b;
return (result);
}
int main () {
// local variable declaration:
int a = 100;
int b = 200;
int result;
return 0;
}
a=20 b=30
a) 20
b) 30
c) 10
d) 5