DSA/Lab_2/Lab_2_loops.c

50 lines
1.2 KiB
C
Raw Normal View History

2024-03-09 17:32:59 +02:00
#include <stdio.h>
#include <math.h>
int main() {
int operation_counter = 0;
int n;
printf("Input n (must be a natural number): ");
scanf("%d", &n);
double result = 0;
operation_counter += 1; // result = 0
operation_counter += 2; // for loop
for (int i = 1; i <= n; i++)
{
double denominator = 1;
operation_counter += 1; // denominator = 1
operation_counter += 2; // for loop
for (int j = 1; j <= i; j++)
{
denominator *= j + cos(j);
operation_counter += 5; // denominator *= j + cos(j), j <= i, j++
}
double power_of_four = 1;
operation_counter += 1; // power_of_four = 1
operation_counter += 2; // for loop
for (int k = 1; k <= i; k++)
{
power_of_four *= 4;
operation_counter += 3; // power_of_four *= 4, k <= i, k++
}
result += (power_of_four - i) / denominator;
operation_counter += 5; // result += (power_of_four - i) / denominator, i <= n, i++
}
printf("Result = %.7f\n", result);
printf("Number of operations = %d\n", operation_counter);
return 0;
}