Below is the program to print the Numbers from 1 to 100 in C++, C, Python3, C#, PHP and JavaScript.
C++
// C++ program to How will you print
// numbers from 1 to 100 without using loop?
#include <iostream>
using namespace std;
class gfg
{
// Prints numbers from 1 to n
public:
void printNos(unsigned int n)
{
if(n > 0)
{
printNos(n - 1);
cout << n << " ";
}
return;
}
};
// Driver code
int main()
{
gfg g;
g.printNos(100);
return 0;
}
C
#include <stdio.h>
void printNos(unsigned int n)
{
if(n > 0)
{
printNos(n - 1);
printf("%d ", n);
}
return;
}
// Driver code
int main()
{
printNos(100);
getchar();
return 0;
}
C#
using System;
class GFG
{
// Prints numbers from 1 to n
static void printNos(int n)
{
if(n > 0)
{
printNos(n - 1);
Console.Write(n + " ");
}
return;
}
// Driver Code
public static void Main()
{
printNos(100);
}
}
Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class GFG
{
// Prints numbers from 1 to n
static void printNos(int n)
{
if(n > 0)
{
printNos(n - 1);
System.out.print(n + " ");
}
return;
}
// Driver Code
public static void main(String[] args)
{
printNos(100);
}
}
PHP
<?php
// PHP program print numbers
// from 1 to 100 without
// using loop
// Prints numbers from 1 to n
function printNos($n)
{
if($n > 0)
{
printNos($n - 1);
echo $n, " ";
}
return;
}
// Driver code
printNos(100);
?>
Python 3
def printNos(n):
if n > 0:
printNos(n - 1)
print(n, end = ' ')
# Driver code
printNos(100)
JavaScript
<script>
function printNos(n)
{
if(n > 0)
{
printNos(n - 1);
document.write(n + " ");
}
return;
}
printNos(100);
</script>
Output:

Source: geeksofgeeks
The Tech Platform
Comments