Interview Question
Software Engineer Interview
-Bhubaneshwar
Josh Technology Groupwrite a program in c/c++/java to print the pattern 1 2*2 3*3*3 4*4*4*4 4*4*4*4 3*3*3 2*2 1
AnswerAdd Tags
Interview Answers
34 Answers
▲
1
▼
#include int main() { int i,j,k; k=0; for(i=1;i=1;i--) { for(j=(i+k);j>=1;j--) { if(j%2==0) printf("*"); else printf("%d",i); } k--; printf("\n"); } return 0; }
Ankur Mishra on
▲
0
▼
#include void print_pattern(int); int main() { int n; scanf("%d",&n); print_pattern(n); return 0; } void print_pattern(int n) { for(int i=1;i0;i--) { for(int j=1;j<=i;j++) { if(i==1) { printf("%d",i); } else { printf("%d",i); if(j!=i) { printf("*"); } } } printf("\n"); } }
this will print full answer!!! on
▲
0
▼
#include void print_pattern(int); int main() { int n; scanf("%d",&n); print_pattern(n); return 0; } void print_pattern(int n) { for(int i=1;i0;i--) { for(int j=1;j<=i;j++) { if(i==1) { printf("%d",i); } else { printf("%d",i); if(j!=i) { printf("*"); } } } printf("\n"); } }
param deol on
▲
3
▼
public class JavaApplication4 { public static void main(String[] args) { // TODO code application logic here for(int i = 1; i 0) { System.out.print("*"); } } System.out.println(); } for(int i = 4; i >= 1; i--){ for(int j =1; j 0) { System.out.print("*"); } } System.out.println(); } } }
Mohit Sharma on
▲
0
▼
there is a problem that only half of my code is getting displayed
Bhavana Johri on
▲
1
▼
#include int main(void) { int i,j; for(i=1;i=1;i--){ for(j=1;j<=(i*2)-1;j++){ if(j%2==0){ printf("*");} else{printf("%d",i);} } printf("\n"); } return 0; }
Harsh bhardwaj on
▲
0
▼
#include #include void main() { int i,j,k; for(i=1;i=i;j--) { printf(" "); } for(k=1;k<=i;k++) { if(k==1) { // Notice the space before %d printf(" %d",i); } //Notice the* before %d else(printf("*%d",i)); } printf("\n"); } }
Anonymous on
▲
0
▼
what is lt meaning
Anonymous on
▲
0
▼
C++ is way simpler than the rest of the code above #include using namespace std; int main() { int i,j,n; cout >n; for(i=1;i=1;n--&&i--)//starting from n till 1 { for(j=i;j>=1;j--){//starting from i till 1 cout<
Karan Sethi on
▲
0
▼
there is problem in the interface of this website, complete answer is not getting pasted
Nitesh Goyal on
▲
0
▼
#n is the maximum number which would occur in the pattern. n = int(input()) for i in range(1,n+1): s = [] for j in range(i-1): s.append(str(i)) s.append('*') s.append(str(i)) print(''.join(s)) for i in range(n, 0, -1): s = [] for j in range(i-1): s.append(str(i)) s.append('*') s.append(str(i)) print(''.join(s))
Devansh Singh on
▲
0
▼
#include #include void main() { int i,j,k; for(i=1;i=i;j--) { printf(" "); } for(k=1;k<=i;k++) { if(k==1) { printf(" %d",i); } else(printf("*%d",i)); } printf("\n"); } }
Anonymous on
▲
7
▼
#include int main() { int n; int i, j; for(i = 1;i = 1;i--) { for(j = 1; j <= i; j++ ) { printf("%d",i); if(j!=i) { printf("*"); } } printf("\n"); } }
Pavan Kumar on
▲
0
▼
*This code could be improved on several levels but the immediate answer could be this. Code : #include using namespace std; int main() { int i,j,n; cout >n; for(i=1;i=1;n--&&i--)//starting from n till 1 { for(j=i;j>=1;j--){//starting from i till 1 cout<
C++ Code with output on
▲
2
▼
int main() { int i, j, rows, k, x; printf("Enter number of rows: "); scanf("%d",&rows); for(i=1; i 0; i--) { k = i - 1; for(j = 1; j <= i; j++) { printf("%d", i); if(k != 0 && j != i) { printf("*"); } } printf("\n"); } return 0; }
Nitesh Goyal on
▲
0
▼
int main() { int i, j, rows, k, x; printf("Enter number of rows: "); scanf("%d",&rows); for(i=1; i 0; i--) { k = i - 1; for(j = 1; j <= i; j++) { printf("%d", i); if(k != 0 && j != i) { printf("*"); } } printf("\n"); } return 0; }
Nitesh Goyal on
▲
2
▼
import java.util.*; public class qus1 { public static void main(String argc[]) { int i,j; int n; Scanner sc=new Scanner(System.in); System.out.print("Enter value of n : "); n=sc.nextInt(); for(i=1;i=1;i--) { for(j=i;j>=1;j--) { if(j==1) System.out.print(i); else System.out.print(i+"*"); } System.out.println(); } } }
Uday Pratap Maurya on
▲
2
▼
import java.io.BufferedReader; import java.io.InputStreamReader; public class Solution { public static void main(String [] args) throws Exception{ int i , j , k=1 , n; BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(System.in)); n=Integer.parseInt(bufferedReader.readLine()); for (i=1;i<=n*2;i++){ if (i<=n) { for (j = 1; j <= i * 2 - 1; j++) { if (j % 2 == 0) { System.out.print("*"); } else { System.out.print("" + i); } } } else { for (j = 1; j <= 2*(i-k)-1; j++) { if (j % 2 == 0) { System.out.print("*"); } else { System.out.print("" + (i-k)); } } k+=2; } System.out.println(""); } } }
Sarang Tayde on
▲
1
▼
#include void printPattren(int n); int main(){ int n; scanf("%d", &n); printPattren(n); return 0; } void printPattren(int n){ int i, j; for(i = 1; i = 1; i--){ j = 1; printf("%d", i); while(j < i){ printf("*"); printf("%d", i); j++; } printf("\n"); } }
Dev Spirit on
▲
0
▼
this code is running successfully and giving the output #include int main(void) { // your code goes here int i,j,k=0,l; for(i=1;i=1;i--) { l=i; for(j=1;j<=i;j++) { printf("%d",i); if(k
Bhavana Johri on
▲
0
▼
#include int main(void) { // your code goes here int i,j,k=0,l; for(i=1;i=1;i--) { l=i; for(j=1;j<=i;j++) { printf("%d",i); if(k
bhavana johri on
▲
0
▼
#include int main() { // your code goes here int i,j,k=0,l; for(i=1;i1&&j=1;i--) { for(j=1;j1&&j
Sami on
▲
0
▼
#include //THIS CODE WORKS void main() { int i,n; for (n=1;n=1;n--) { for(i=1;i<=n;i++) { printf("%d",n); if(i!=n) printf("*"); else printf("\n"); }} }
PRAYATNA JAIN on
▲
0
▼
for(int i = 1;i0;i--) { for(int j=0;j
Manu Gupta on
▲
1
▼
#include int main() { int i,j,k; k=0; for(i=1;i=1;i--) { for(j=(i+k);j>=1;j--) { if(j%2==0) printf("*"); else printf("%d",i); } k--; printf("\n"); } return 0; }
Ankur Mishra on
▲
0
▼
//THIS IS VERIFIED AND CORRECT CODE import java.util.Scanner; public class MyClass { public static void main(String[] args) { int i,j; int n; Scanner sc=new Scanner(System.in); System.out.print("Enter value of n : "); n=sc.nextInt(); for(i=1;i=1;i--) { for(j=i;j>=1;j--) { while(j>1){ System.out.print(i+"*"); j--; } System.out.print(i); } System.out.println(); } }
Proteep Banerjee on
▲
0
▼
//THE SAME CODE IN C #include void main(){ int n; printf ("Enter the value of n"); scan ("%d",&n); void printPattern(n); } void printPattern(int k){ int i,j; for(i=1;i=1;i--) { for(j=i;j>=1;j--) { while(j>1) { printf ("%d",i); printf ("*"); j--; } printf ("%d",i); } printf ("\n"); }
Proteep Banerjee on
▲
0
▼
//Pattern Solution import java.util.*; import java.lang.*; import java.io.*; class Pattern { public static void main (String[] args) throws java.lang.Exception { for(int j=1;j=1;k--) { for(int l=1;l<=k;l++) { if(l==1) System.out.print(k); else System.out.print("*"+k); } System.out.println(); } }}
Siddharth Verma on
▲
1
▼
#include void printPattren(int n); int main(){ int n; scanf("%d", &n); printPattren(n); return 0; } void printPattren(int n){ int i, j; for(i = 1; i = 1; i--){ j = 1; printf("%d", i); while(j < i){ printf("*"); printf("%d", i); j++; } printf("\n"); } }
Anonymous on
▲
3
▼
#include int main() { int n; int i, j; for(i = 1;i = 1;i--) { for(j = 1; j <= i; j++ ) { printf("%d",i); if(j!=i) { printf("*"); } } printf("\n"); } }
Pavan Kumar on
▲
3
▼
hi atul your answer is wrong it only printing 1 infinite time. i was able to solve that problem in exam and my program was working for all the test cases.
sudhakar kumar on
▲
0
▼
All codes are giving wrong output *** this is the correct one***** #include int main(void) { int i,j; for(i=1;i=1;i--){ for(j=1;j<=(i*2)-1;j++){ if(j%2==0){ printf("*");} else{printf("%d",i);} } printf("\n"); } return 0; }
Harsh bhardwaj on
▲
13
▼
#include int main() { int i,j,n; scanf("%d",&n); for(i=1;i=1;i--) { for(j=1;j<=i;j++) { printf("%d",i); if(j
Atul Goel on
▲
7
▼
#include void printPattern(int n) { int i,j; for(i=1;i=1;i--) { j=1; if(i==0) printf("%d",i); else { printf("%d",i); while(j
sudhakar kumar on
Add Answers or Comments
To comment on this, Sign In or Sign Up.