Write a C program to print 1 to 100 numbers without using loop. In this tutorial, we going to learn how to write a C code to print 1 to 100 number without using loop (for, while etc).
How do we print numbers from 1 to 100 without using loop? Writing c program to print 1 to 100 numbers using loop is very simple. This problem can be solved using recursion.
MCQ on recursion for practice.
Let's write a code to print 1 to 100 numbers without using loop.
In the above program, we created a function printNumbers(int num) and passed 1 in an argument from main method.
Another way of writing this program.
C interview questions with answers
How do we print numbers from 1 to 100 without using loop? Writing c program to print 1 to 100 numbers using loop is very simple. This problem can be solved using recursion.
MCQ on recursion for practice.
C Program to Print 1 to 100 Numbers without using Loop
Let's write a code to print 1 to 100 numbers without using loop.
#include <stdio.h>
void printNumbers(int num) {
//If num is less than or equal to 100
if(num <= 100) {
//print number
printf("%d ", num);
//Recursively call
printNumbers(num+1);
}
}
int main(void) {
int n = 1;
//Call method to print number
printNumbers(n);
return 0;
}
In the above program, we created a function printNumbers(int num) and passed 1 in an argument from main method.
Another way of writing this program.
#include <stdio.h>
void printNumbers(int num) {
//If num is less greater than or equal to 1
if(num >= 1) {
printf("%d ", num);
/*Recursively call,
Decrement the value by 1
*/
printNumbers(num-1);
}
}
int main(void) {
//Call method to print number
printNumbers(100);
return 0;
}
C interview questions with answers
No comments:
Post a Comment