Search⌘ K
AI Features

This Should Ring a Bell

Explore the logic behind a C programming puzzle by analyzing the given code and predicting its output. This lesson helps you strengthen your understanding of code behavior and prepares you for advanced puzzle discussions.

We'll cover the following...

Puzzle code

Read carefully the code given below:

C
#include <stdio.h>
int main()
{
const int limit = 1000;
int x;
float t;
/* divergent */
t = 0.0;
for(x=1; x<=limit; x++ ) t += 1.0 / (float)x;
printf("Divergent: %.4f\n", t);
/* convergent */
t = 0.0;
for(x=1; x<=limit; x*=2 ) t += 1.0 / (float)x;
printf("Convergent: %.4f\n", t);
return(0);
}

Your task: Guess the output

Attempt the following test to assess your understanding.

Technical Quiz
1.

What is the expected output of the above code?

A.

The program prints the sum of a divergent series as Divergent: 9.5432 and a convergent series as Convergent: 2.9980.

B.

The program prints the sum of a divergent series as Divergent: 7.4855 and a convergent series as Convergent: 10.5000.

C.

The program prints the sum of a divergent series as Divergent: 7.4855 and a convergent series as Convergent: 1.9980.

D.

The program results in an infinite loop because the convergent series never ends.


1 / 1

Let's discuss the code and output together in the next lesson.