Lập trình C++

How will you print numbers from 1 to 100 without using loop?

Here is a solution that prints numbers using recursion.

Other alternatives for loop statements are recursion and goto statement, but use of goto is not suggestible as a general programming practice as goto statement changes the normal program execution sequence and makes it difficult to undestand and maintain.

#include <stdio.h>

/* Prints numbers from 1 to n */
void printNos(unsigned int n)
{
if(n > 0)
{
	printNos(n-1);
	printf("%d ", n);
}
return;
}

/* Driver program to test printNos */
int main()
{
printNos(100);
getchar();
return 0;
}

Demo online here

Published by

tuanitpro

Tôi là Lê Thanh Tuấn, và tôi chia sẻ những điều mình cho rằng nó là thú vị, hay giúp ích cho bạn!

Leave a Reply