Functions in C/C++ - examples
1. Replacement of the position
2. Is the number simple?
3. Printing the array
4. Extracting even elements of an array
5. Number of repetitions of a certain character
6. Daily earnings
Example:
Input:
12_2_2021 2045.33
23_8_2021 1034.66
15_4_2021 1567.99
Output:
Array 1:
12.02.2021
23.08.2021
15.04.2021
Array 2:
2045.31
1034.66
1567.99
7. Login
Example:
Input:
Enter your username and password:
"Tot"
"user1"
Re-enter incorrect password
"admin"
"user"
Exit:
The user is successfully logged in
8. Daily earnings
Example:
Input:
12_2_2021 2045.33
23_8_2021 1034.66
15_4_2021 1567.99
Output:
Array1:
12.02.2021
23.08.2021
15.04.2021
Array2:
2045.31
1034.66
1567.99
9. Login
Example:
Input:
Enter your username and password:
"Mika"
"user1"
Re-enter incorrect password
"admin"
"user"
Output:
The user is successfully logged in
10. Determination of the maximum
Enter two integers and determine their maximum using the previously defined function.
Create the function "maximum()", which receives two integers, a and b, as parameters, and then, inside the function, examine which of them is larger.
If "a" is greater, the function should return "a" as a return value, otherwise "b".
Inside the main function, enter two integers, then call the previously created maximum function to determine it. Print that value in the rest of the program.
/*Solution for C programming language*/
int maximum(int a, int b)
{
{
else
{
int main()
{
printf("a=?,b=?\n");
scanf("%d%d",&a,&b);
maxAB = maximum(a,b);
printf("Maximum is %d",maxAB);
return 0;
11. Determining whether a number is prime
Read more about prime numbers on the website: A prime numbers and factoring
/*Solution in the programming language C*/
int prime(int a)
{
if(a==1 || a==2){
int d=2;
while(d < a){
break;
d++;
int main()
{
printf("x=?\n");
scanf("%d",&x);
if(prime(x)){
else{
return 0;
12. Writing bits from left to right
Create a function "bits()", which receives an integer "a" as a parameter.
Inside the function, use a loop to create powers of 2, starting with 215, and then decrease the exponent through the cycles.
Check in each cycle, whether that power of two is less than the rest of the number, which is initially equal to the whole number "a" sent.
If yes, print 1 as bit, if not, print zero. Recalculate the remainder of the number that remains when we subtract the current power of 2 from the previous remainder.
In the main "main" function, call the previously created function to be executed.
/*Solution in the programming language C*/
int bits(int a)
{
for(int i = 15;i >= 0;i--){
if(a <= rem){
rem=rem % a; //a=4, rem =7%4=3; a=2, rem =3%2=1; a=1, rem =1%1=0
else{
printf("\n");
int main()
{
printf("x=?\n");
scanf("%d",&x);
bits(x);
return 0;
13. Rounding to K decimal places.
Enter three integers M,N and K. (eg 13,3,2)
Find the quotient M/N, (eg 13/3=4.333333), and choose double or float as the data type, in order to save the decimals.
Multiply the resulting number by 10K, and then convert that number into a whole number, so that it contains both the digits of the result and the digits that should be displayed after the decimal point (eg 433).
Extract the integer result from this number, by dividing by 10K, as well as the decimal part by looking for the remainder of the division by 10K.
Print the integer part first, then put "." and add the decimal part below.
/*Solution in programming language C*/
int main()
{
double resD;
printf("M=?,N=?,K=?\n");
scanf("%d%d%d",&M,&N,&K); //13,3,2
resD=(double)M/N; //4.333333
printf("res=%f\n",resD);
coef=(int)pow(10,K); //100
res=resD*coef; //433
resD=(double)res/coef; //4.3300000
printf("res=%g\n",resD);
int num,dec;
num=res/coef; //4
dec=res % coef; //33
printf("%d.%d\n",num,dec);
14. Combinatorics-Calculation of combinations.
for the given natural numbers n,k,p S=Cn k-Cn k+1 +..+(-1) p*Cn k+p,
according to the formula for calculating combinations
15. Bit in the given position.
16. Number in reverse order.
312 * 221 = 68952
213 * 122 = 25986
17. The closest number that satisfies the condition.
n+S(n)+S(S(n))+S(S(S(n)))+S(S(....S(n)....)=2003
Previous
|< Two dimensional arrays in C/C++ - examples |
Next
Algorithms examples >| |