print the following series: 1,11,111,1111,11111
Answers
Answer:
This is done in JAVA
Code:-
int i,s=1;
for (I=1;I<=5;I++)
{
System.out.print(s+",");
s=s*10+1;
}
#Shortest_Code_Possible
Output:-
1,11,111,1111,11111
Question:-
➡ Write a program to display the following series.
1 11 111 1111 11111
Program:-
This can be solved using 2 ways.
- Using 1 loop.
- Without using loop.
Using 1 loop
import java.util.*;
class Series
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n=sc.nextInt();
int a=1;
for(int i=1;i<=n;i++, a=a*10+1)
System.out.print(a+" ");
}
}
Without using loop.
import java.util.*;
class Series
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n=sc.nextInt();
printPattern(1,n,1);
}
static void printPattern(int initial, int final, int a)
{
if(initial<=final)
{
System.out.print(a+" ");
a=a*10+1;
printPattern(++initial, final, a);
}
}
}