type name(lists_of_arguments) {
function body
}
type - return type of function
name - function name
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);
name - function name
lists_of_arguments - the list of arguments has the right arguments that if more are separated by a stop
Write a program in which a cube n3 is calculated for a natural number n.
#include <iostream>
using namespace std;
double cube(int x) {
return x * x * x;
}
int main() {
int n;
<< "Enter a natural number: ";
cout >> n;
cin double res = cube(n);
<< "Cube of " << n << " is " << res << endl;
cout return 0;
}
cmath
libraryThere is a standard mathematical library cmath
containing many finished mathematical functions.
To be used, it should be included with:
#include <cmath>
All functions of the standard math.h library receive double arguments and return values of the same type.
Function | Explanation |
---|---|
sqrt(x) |
square root of x |
exp(x) |
exponential function e^x |
log(x) |
natural logarithm of x (base e) |
log10(x) |
logarithm of x with base 10 |
fabs(x) |
absolute value of x |
ceil(x) |
rounds x to the smallest integer not less than x |
floor(x) |
rounds x to the largest integer not greater than x |
pow(x, y) |
x raised to the power y |
fmod(x, y) |
remainder of x/y as a floating-point number |
sin(x) |
sine of x (in radians) |
cos(x) |
cosine of x (in radians) |
tan(x) |
tangent of x (in radians) |
cmath
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;
<< "Enter a natural number: ";
cout >> n;
cin double res = pow(n, 3);
<< "Cube of " << n << " is " << res << endl;
cout return 0;
}
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
double diameter(double radius);
double perimeter(double radius);
double area(double radius);
int main() {
double radius, D, L, P;
std::cout << "Enter the radius of the circle: ";
std::cin >> radius;
= diameter(radius);
D = perimeter(radius);
L = area(radius);
P
std::cout << "Diameter of the circle = " << D << std::endl;
std::cout << "Perimeter of the circle = " << L << std::endl;
std::cout << "Area of the circle = " << P << std::endl;
return 0;
}
double diameter(double radius) {
return 2 * radius;
}
double perimeter(double radius) {
return 2 * radius * PI;
}
double area(double radius) {
return radius * radius * PI;
}
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:
3417 is divisible with 34 + 17
5265 is divisible with 52 + 65
6578 is divisible with 65 + 78
#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) {
<< i << endl;
cout ++count;
}
}
<< "Total: " << count;
cout return 0;
}
Write a program that calculates for a given natural number the difference between the nearest prime number and that number itself.
Example: For input of 573
, The program should
output577 – 573 = 4
#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;
}
int main() {
int n;
>> n;
cin int largerPrime = firstLargerPrime(n);
<< largerPrime << " " << largerPrime - n;
cout return 0;
}
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) {
+= n % 10;
sum /= 10;
n }
return sum;
}
int main() {
int i, count = 0;
for(i = 2; i <= 9999; ++i) {
if(is_prime(i) && is_prime(sum_digits(i))) {
<< i << endl;
cout ++count;
}
}
<< "Total: " << count << endl;
cout return 0;
}
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)) {
<< "(" << i << ", " << i+2 <<")" << endl;
cout ++count;
}
}
<< "Total: " << count << endl;
cout return 0;
}
Calculate the following sum:
1! + (1 + 2)! + (1 + 2 + 3)! + … + (1 + 2 + … + n)!
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) {
+= i;
s }
return s;
}
int factorial(int n) {
int result = 1;
int i;
for(i = 1; i <= n; ++i) {
*= i;
result }
return result;
}
int main() {
int n;
>> n;
cin if(n > 0) {
int i;
int result = 0;
int s;
for(i = 1; i < n; ++i) {
= sum(i);
s += faktoriel(s);
result << s << "! + ";
cout }
= sum(n);
s += factorial(s);
result << s << "! = " << result;
cout } else {
<< "Invalid number" << endl;
cout }
return 0;
}