Program for print number from 1 to 100 without using loop
Printing the numbers from 1 to 100 without using any loop. This is one of the common questions asked in test logic of recursion. We can solve this problem without using loops only by using of recursion.
Input num = 100
OutPut :
---------------------------------------
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// Java program for
// print number from 1 to 100 without using loop
class Example
{
public void printNumber(int num)
{
// Base condition
if (num > 0)
{
// Execute function recursively
printNumber(num - 1);
// Display value
System.out.print(" " + num);
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
System.out.println();
}
}
}
public static void main(String[] args)
{
Example task = new Example();
// Print number
task.printNumber(100);
}
}
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// Include namespace system
using System;
// Csharp program for
// print number from 1 to 100 without using loop
public class Example
{
public void printNumber(int num)
{
// Base condition
if (num > 0)
{
// Execute function recursively
this.printNumber(num - 1);
// Display value
Console.Write(" " + num.ToString());
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
Console.WriteLine();
}
}
}
public static void Main(String[] args)
{
var task = new Example();
// Print number
task.printNumber(100);
}
}
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// print number from 1 to 100 without using loop
class Example
{
public: void printNumber(int num)
{
// Base condition
if (num > 0)
{
// Execute function recursively
this->printNumber(num - 1);
// Display value
cout << " " << num;
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
cout << endl;
}
}
}
};
int main()
{
Example *task = new Example();
// Print number
task->printNumber(100);
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// C program for
// print number from 1 to 100 without using loop
#include <stdio.h>
void printNumber(int num)
{
// Base condition
if (num > 0)
{
// Execute function recursively
printNumber(num - 1);
// Display value
printf(" %d", num);
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
printf("\n");
}
}
}
int main()
{
// Print number
printNumber(100);
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
package main
import "fmt"
// Go program for
// print number from 1 to 100 without using loop
func printNumber(num int) {
// Base condition
if num > 0 {
// Execute function recursively
printNumber(num - 1)
// Display value
fmt.Print(" ", num)
// Separate result by new line
if num % 10 == 0 {
// Add new line
fmt.Println()
}
}
}
func main() {
// Print number
printNumber(100)
}
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
<?php
// Php program for
// print number from 1 to 100 without using loop
class Example
{
public function printNumber($num)
{
// Base condition
if ($num > 0)
{
// Execute function recursively
$this->printNumber($num - 1);
// Display value
printf("%s", " ".strval($num));
// Separate result by new line
if ($num % 10 == 0)
{
// Add new line
print("\n");
}
}
}
public static function main($args)
{
$task = new Example();
// Print number
$task->printNumber(100);
}
}
Example::main(array());
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// Node JS program for
// print number from 1 to 100 without using loop
class Example
{
printNumber(num)
{
// Base condition
if (num > 0)
{
// Execute function recursively
this.printNumber(num - 1);
// Display value
process.stdout.write(" " + num);
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
console.log();
}
}
}
}
function main()
{
var task = new Example();
// Print number
task.printNumber(100);
}
// Start program execution
main();
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
# Python 3 program for
# print number from 1 to 100 without using loop
class Example :
def printNumber(self, num) :
# Base condition
if (num > 0) :
# Execute function recursively
self.printNumber(num - 1)
# Display value
print(" ", num, end = "")
# Separate result by new line
if (num % 10 == 0) :
# Add new line
print()
def main() :
task = Example()
# Print number
task.printNumber(100)
if __name__ == "__main__": main()
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
# Ruby program for
# print number from 1 to 100 without using loop
class Example
def printNumber(num)
# Base condition
if (num > 0)
# Execute function recursively
self.printNumber(num - 1)
# Display value
print(" ", num)
# Separate result by new line
if (num % 10 == 0)
# Add new line
print("\n")
end
end
end
end
def main()
task = Example.new()
# Print number
task.printNumber(100)
end
main()
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// Scala program for
// print number from 1 to 100 without using loop
class Example()
{
def printNumber(num: Int): Unit = {
// Base condition
if (num > 0)
{
// Execute function recursively
printNumber(num - 1);
// Display value
print(" " + num);
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
println();
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Example = new Example();
// Print number
task.printNumber(100);
}
}
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// Swift 4 program for
// print number from 1 to 100 without using loop
class Example
{
func printNumber(_ num: Int)
{
// Base condition
if (num > 0)
{
// Execute function recursively
self.printNumber(num - 1);
// Display value
print(" ", num, terminator: "");
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
print();
}
}
}
}
func main()
{
let task: Example = Example();
// Print number
task.printNumber(100);
}
main();
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
// Kotlin program for
// print number from 1 to 100 without using loop
class Example
{
fun printNumber(num: Int): Unit
{
// Base condition
if (num > 0)
{
// Execute function recursively
this.printNumber(num - 1);
// Display value
print(" " + num);
// Separate result by new line
if (num % 10 == 0)
{
// Add new line
println();
}
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Example = Example();
// Print number
task.printNumber(100);
}
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment