Computer >> Computer tutorials >  >> Programming >> C programming

How to print a name multiple times without loop statement using C language?


Problem

Try to print a name 10 times without using any loop or goto statement in C programming language.

Solution

Generally, looping statements are used to repeat the block of code until condition is false.

Example1

In this program, we are trying to print a name 10 times without using loop or goto statements.

#include <stdio.h>
void printname(char* name,int count){
   printf("%03d : %s\n",count+1,name);
   count+=1;
   if(count<10)
      printname(name,count);
}
int main(){
   char name[50];
   printf("\nEnter you name :");
   scanf("%s",name);
   printname(name,0);
   return 0;
}

Output

Enter you name :tutorialspoint
001 : tutorialspoint
002 : tutorialspoint
003 : tutorialspoint
004 : tutorialspoint
005 : tutorialspoint
006 : tutorialspoint
007 : tutorialspoint
008 : tutorialspoint
009 : tutorialspoint
010 : tutorialspoint

Example 2

Below is the program to print your name 10 times using any loop or goto statement −

#include <stdio.h>
int main(){
   char name[50],i;
   printf("\nEnter you name :");
   scanf("%s",name);
   for(i=0;i<10;i++){
      printf("%s\n",name);
   }
   return 0;
}

Output

Enter you name :TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint