☞ Write a program to print following pattern.
Solution in JAVA
Solution in Python
Solution in C
Sample Input
253
Sample Output
2
2 5
2 5 3
5
5 3
3
253
Sample Output
2
2 5
2 5 3
5
5 3
3
Solution in JAVA
import java.util.Scanner;
public class PatternProgram {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = String.valueOf(n);
for(int i=0;i<s.length();i++){
for(int j=i;j<s.length();j++){
for(int k=i;k<=j;k++){
System.out.print(s.charAt(k));
}
System.out.println();
}
System.out.println();
}
}
}
public class PatternProgram {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String s = String.valueOf(n);
for(int i=0;i<s.length();i++){
for(int j=i;j<s.length();j++){
for(int k=i;k<=j;k++){
System.out.print(s.charAt(k));
}
System.out.println();
}
System.out.println();
}
}
}
Solution in Python
n = int(input())
s = str(n)
for i in range(len(s)):
for j in range(i,len(s)):
print(s[i:j+1])
print()
s = str(n)
for i in range(len(s)):
for j in range(i,len(s)):
print(s[i:j+1])
print()
Solution in C
#include<stdio.h>
int main(){
int n,nrev=0,temp,i,j,c=0,temp2;
scanf("%d",&n);
temp=n;
while(temp!=0){
nrev=nrev*10+temp%10;
temp/=10;
c++;
}
while(nrev!=0){
temp=nrev;
for(i=1;i<=c;i++){
temp2=temp;
for(j=1;j<=i;j++,temp2/=10){
printf("%d%c",temp2%10,32);
}
printf("\n");
}
printf("\n");
nrev/=10;
c--;
}
}
int main(){
int n,nrev=0,temp,i,j,c=0,temp2;
scanf("%d",&n);
temp=n;
while(temp!=0){
nrev=nrev*10+temp%10;
temp/=10;
c++;
}
while(nrev!=0){
temp=nrev;
for(i=1;i<=c;i++){
temp2=temp;
for(j=1;j<=i;j++,temp2/=10){
printf("%d%c",temp2%10,32);
}
printf("\n");
}
printf("\n");
nrev/=10;
c--;
}
}