...

/

Solution: Nested Loop with Multiplication (Pro)

Solution: Nested Loop with Multiplication (Pro)

This review provides a detailed analysis of the different ways to solve the Nested Loop with Multiplication (Pro) challenge.

We'll cover the following...

Solution #

Press + to interact
int main(){
int n =10; // you can change the value of n
int sum = 0;
int j = 1;
float pie = 3.14;
for (int i = 0; i < n; i++){
cout << pie << endl;
while (j < i){
sum+=1;
j*=2;
}
}
cout << sum << endl;
}

The outer loop in the main function has n iterations as it iterates on i from 0 to n-1. If the condition j < i is true, the inner loop is entered. However, immediately, j is doubled. Note that j is not reset to 1 in the code since the j is immediately doubled, therefore inner while loop will run O(log2(n) ...

Access this course and 1400+ top-rated courses and projects.