Question:

What is the time complexity of the following algorithm? 
int func(int n) { 
       for (int i = 1; i < = n; i++) { 
              for (int j = 1; j < = n; j++) { 
                  printf("Hello"); 
               } 
            } 
        } 

Show Hint

To determine the time complexity of an algorithm, consider the number of iterations of each loop. The time complexity for nested loops is the product of the iterations.

Updated On: Feb 14, 2025
  • O(n)

  • O(n²) 

  • O(n³)

  • O(log n)

Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Understanding the Algorithm 
- The outer loop runs n times. 
- The inner loop also runs n times for each iteration of the outer loop. 
- The total number of executions of the `printf` statement is \( n \times n = n^2 \). 

Thus, the time complexity of the algorithm is O(n²).

Was this answer helpful?
0
0

Top Questions on Programming in C

View More Questions