Skip to content
Solved Examples · Example 28

Q.Write a C program to accept your name and print it for a specified number of times. The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include <stdio.h>  
void main()  
{  
char name [14];  
int ktimes, i;  
printf("what is your name?\n");  
gets(name);  
printf("How many times to print your name\n");  
scanf("%d", &ktimes);  
i=0;  
while(i<ktimes)  
{  
    puts(name);  
    i++;  
}  
}  
/* End of main()*/  
Sample output printed in the textbook:
What is your name?  
Benaka  
How many times to print your name?  
4  
Benaka  
Benaka  
Benaka  
Benaka  

Karnataka PUCTextbookLongImportance★★★★★est
83% · 128/154 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

[!TLDR]

A while loop with a counter prints the entered name ktimes times; Benaka with 4 prints Benaka four times.

The while loop is a pre-test loop: it checks its condition before each pass, so the body runs only while i < ktimes remains true. A counter variable is set up before the loop (i = 0), tested in the condition, and advanced inside the body (i++); getting all three parts right is what makes the loop run exactly the intended number of times. gets() reads a whole line into the name array and puts() writes a string followed by a newline. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.