Search⌘ K
AI Features

More Simple

Explore C puzzle code to practice guessing outputs and deepen your comprehension of basic programming logic and code flow. This lesson helps you sharpen your analytical skills and prepares you for more complex coding challenges.

We'll cover the following...

Puzzle code

Read carefully the code given below:

C
#include <stdio.h>
int main()
{
int n, d, larger, smaller, diff;
printf("Enter a fraction (nn/nn): ");
scanf("%d/%d", &n, &d);
if (d == 0)
{
printf("Error: Denominator cannot be zero.\n");
return 1;
}
printf("%d/%d = ", n, d);
larger = n > d ? n : d;
smaller = n < d ? n : d;
diff = larger - smaller;
while (diff != larger)
{
larger = smaller > diff ? smaller : diff;
smaller = smaller == larger ? diff : smaller;
diff = larger - smaller;
}
if (diff > 1)
printf("%d/%d\n", n / diff, d / diff);
else
printf("%d/%d\n", n, d);
return 0;
}

Your task: Guess the output

Attempt the following test to assess your understanding.

Technical Quiz
1.

What will be the output of the following code if the user inputs 21/63?

A.

21/63 = 7/21

B.

21/63 = 1/3

C.

21/63 = 21/63

D.

21/63 = 3/7


1 / 1

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