Auditory Exercises 06 Functions
Auditory Exercises 06 Functions
Auditory Exercises 06 Functions
Structural Programming
Exercise 6 - Functions
type name(lists_of_arguments) {
function body
}
lists_of_arguments - the formal argument list contains the arguments together with their types,
separated by a stop
function_body - the body of function contains the same elements as the ‘main()’ function itself
name(lista_na_argumenti);
lists_of_arguments - the list of arguments has the right arguments that if more are separated by
a stop
https://finki-mk.github.io/SP_2023/en/6.html 1/9
11/30/23, 9:58 AM 6
#include <iostream>
using namespace std;
double cube(int x) {
return x * x * x;
}
int main() {
int n;
cout << "Enter a natural number: ";
cin >> n;
double res = cube(n);
cout << "Cube of " << n << " is " << res << endl;
return 0;
}
All functions of the standard math.h library receive double arguments and return values of the
same type.
3. Example functions
Function Explanation
https://finki-mk.github.io/SP_2023/en/6.html 2/9
11/30/23, 9:58 AM 6
Function Explanation
Write a program in which a separate function calculates the cube n3 of a natural number n.
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n;
cout << "Enter a natural number: ";
cin >> n;
double res = pow(n, 3);
cout << "Cube of " << n << " is " << res << endl;
return 0;
}
4. Exercise
4.1. Exercise 1
Write appropriate functions for calculating a diameter, perimeter, and square of a circle whose
radius is entered as an argument. Then write a program in which these functions will be called for
R (entered from the keyboard) to calculate the diameter, perimeter, and surface of the
corresponding circle.
#include <iostream>
#define PI 3.14
https://finki-mk.github.io/SP_2023/en/6.html 3/9
11/30/23, 9:58 AM 6
int main() {
double radius, D, L, P;
std::cout << "Enter the radius of the circle: ";
std::cin >> radius;
D = diameter(radius);
L = perimeter(radius);
P = area(radius);
4.2. Exercise 2
Write a program that will print all four-digit natural numbers that are divided by the sum of the two
numbers composed of the first two digits and the last two digits of the four-digit number. It should
eventually print out how many such numbers have been found.
Example:
https://finki-mk.github.io/SP_2023/en/6.html 4/9
11/30/23, 9:58 AM 6
#include <iostream>
using namespace std;
int sum(int n) {
return n % 100 + n / 100;
}
int main() {
int i;
int count = 0;
for(i = 1000; i <= 9999; ++i) {
if(i % sum(i) == 0) {
cout << i << endl;
++count;
}
}
cout << "Total: " << count;
return 0;
}
4.3. Exercise 3
Write a program that calculates for a given natural number the difference between the nearest
prime number and that number itself.
#include <iostream>
using namespace std;
int prime(int n) {
int i;
for(i = 2; i * i <= n; ++i) {
if(n % i == 0) {
return 0;
}
}
return 1;
}
int firstLargerPrime(int n) {
do{
++n;
}while(!prime(n));
return n;
https://finki-mk.github.io/SP_2023/en/6.html 5/9
11/30/23, 9:58 AM 6
int main() {
int n;
cin >> n;
int largerPrime = firstLargerPrime(n);
cout << largerPrime << " " << largerPrime - n;
return 0;
}
4.4. Exercise 4
Write a program that will print all prime numbers less than 10000, whose sum of digits is also a
prime number. At the end, print how many such numbers are found.
#include <iostream>
using namespace std;
int is_prime(int n) {
if(n < 4) return 1;
else {
if(n % 2 == 0) return 0;
else {
int i;
for(i = 3; i * i <= n; i += 2) {
if(n % i == 0) {
return 0;
}
}
}
}
return 1;
}
int sum_digits(int n) {
int sum = 0;
while(n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
int main() {
int i, count = 0;
for(i = 2; i <= 9999; ++i) {
https://finki-mk.github.io/SP_2023/en/6.html 6/9
11/30/23, 9:58 AM 6
4.5. Exercise 5
Write a program that will print all pairs of prime numbers less than 1000 that differ between them
by 2. Finally, print the number of such pairs.
#include <iostream>
using namespace std;
int prime(int n) {
int i;
for(i = 2; i * i <= n; ++i) {
if(n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int i, count = 0;
for(i = 1; i < 998; ++i) {
if(prime(i) && prime(i + 2)) {
cout << "(" << i << ", " << i+2 <<")" << endl;
++count;
}
}
cout << "Total: " << count << endl;
return 0;
}
4.6. Exercise 6
https://finki-mk.github.io/SP_2023/en/6.html 7/9
11/30/23, 9:58 AM 6
NOTE: Use a function to calculate the sum of the first k natural numbers. Use a factorial
calculation function to one natural number k
#include <iostream>
using namespace std;
int sum(int n) {
int i;
int s = 0;
for(i = 1; i <= n; ++i) {
s += i;
}
return s;
}
int factorial(int n) {
int result = 1;
int i;
for(i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
int main() {
int n;
cin >> n;
if(n > 0) {
int i;
int result = 0;
int s;
for(i = 1; i < n; ++i) {
s = sum(i);
result += faktoriel(s);
cout << s << "! + ";
}
s = sum(n);
result += factorial(s);
cout << s << "! = " << result;
} else {
cout << "Invalid number" << endl;
}
https://finki-mk.github.io/SP_2023/en/6.html 8/9
11/30/23, 9:58 AM 6
return 0;
}
https://finki-mk.github.io/SP_2023/en/6.html 9/9