Interview Question

Java Developer Interview

-

WebKul

The first question was to print pattern that is n=1 *** n=3 * * *** *** ************ *** *** * * n=5 * * *** *** ***** ***** ******************* ***** ***** *** *** * *

AnswerAdd Tags

Interview Answers

9 Answers

13

I don't understand the logic of mid line.... import java.util.Scanner; /* 1 *** 3 * * *** *** ************ *** *** * * 5 * * *** *** ***** ***** ******************* ***** ***** *** *** * * */ public class Pattern1 { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); if(n>1){ for(int i=1; i1){ for(int i=(n+1)/2; i>=1; i--){ for(int j=1; j<=(((n+1))/2)+1-i; j++){ System.out.print(" "); } int k=0; while(k != (2*i)-1){ System.out.print("*"); k++; }k=0; System.out.print(" "); while(k!=(2*i-1)){ System.out.print("*"); k++; }k=0; System.out.println(); } } sc.close(); } }

Umang Rastogi on

1

import java.util.Scanner; /* 1 *** 3 * * *** *** ************ *** *** * * 5 * * *** *** ***** ***** ******************* ***** ***** *** *** * * */ public class Pattern1 { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); if(n>1){ for(int i=1; i1){ for(int i=(n+1)/2; i>=1; i--){ for(int j=1; j<=(((n+1))/2)+1-i; j++){ System.out.print(" "); } int k=0; while(k != (2*i)-1){ System.out.print("*"); k++; }k=0; System.out.print(" "); while(k!=(2*i-1)){ System.out.print("*"); k++; }k=0; System.out.println(); } } sc.close(); } }

Anonymous on

1

package pattern; import java.util.Scanner; public class webu1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("enter the no"); int n=sc.nextInt(); for(int i=1;i=1; i--) { for (int j = 0; j < n-i; j++) { System.out.print(" "); } for(int k=0;k<(2*i-1);k++){ System.out.print("*"); } System.out.print(" "); for(int k=0;k<(2*i-1);k++){ System.out.print("*"); } System.out.println(""); } } }

Rohit Mishra on

0

class Codechef { public static void main (String[] args) throws java.lang.Exception { int n=3; int count=n-n/2; if(n==1) System.out.println("***"); else{ for (int i=1;i=1;i-=2){ count++; for (int l=1;l<=count;l++) System.out.print(" "); for (int j=1;j<=i;j++) System.out.print("*"); System.out.print(" "); for (int k=1;k<=i;k++) System.out.print("*"); System.out.println(); } } } }

Anonymous on

0

class Codechef { public static void main (String[] args) throws java.lang.Exception { int n=3; int count=n-n/2; if(n==1) System.out.println("***"); else{ for (int i=1;i=1;i-=2){ count++; for (int l=1;l<=count;l++) System.out.print(" "); for (int j=1;j<=i;j++) System.out.print("*"); System.out.print(" "); for (int k=1;k<=i;k++) System.out.print("*"); System.out.println(); } } } }

Anonymous on

0

ANSWER IN C LANGUAGE int i,n,k,left=1,right=1,k1,j; scanf("%d",&n); for(j=1;j<=n/2+1;j++) { for(k=1;k<=left;k++) { printf("*"); } printf(" "); for(k1=1;k1<=right;k1++) { printf("*"); } left=left+2; right=right+2; printf("\n"); } for(k=1;k<=n*4;k++) { printf("*"); } printf("\n"); left=left-2; right=right-2; for(i=n/2+3;i<=n+2;i++) { for(j=1;j<=n/2+1;j++) { for(k=1;k<=left;k++) { printf("*"); } printf(" "); for(k1=1;k1<=right;k1++) { printf("*"); } left=left-2; right=right-2; printf("\n"); } }

siya on

0

Glassdoor editor problem i am posting right code but it modify it. if(n>1){ for(int i=1; i<=(n+1)/2; i++){ for(int j=1; j<=(((n+1))/2)+1-i; j++){ System.out.print(" ");

Umang Rastogi on

0

import java.util.*; class Pat { public static void main(String ... args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); if(n==1) System.out.print("***"); else {for(int i=1;i=1;i--) { for(int j=1;j<=q;j++) System.out.print(" "); q++; for(int k=1;k<=2*i-1;k++) System.out.print("*"); System.out.print(" "); for(int k=1;k<=2*i-1;k++) System.out.print("*"); System.out.println(""); } } } }

Sarthak Tomar on

2

import java.util.Scanner; /* 1 *** 3 * * *** *** ************ *** *** * * 5 * * *** *** ***** ***** ******************* ***** ***** *** *** * * */ public class Pattern1 { public static void main(String[] args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); if(n>1){ for(int i=1; i1){ for(int i=(n+1)/2; i>=1; i--){ for(int j=1; j<=(((n+1))/2)+1-i; j++){ System.out.print(" "); } int k=0; while(k != (2*i)-1){ System.out.print("*"); k++; }k=0; System.out.print(" "); while(k!=(2*i-1)){ System.out.print("*"); k++; }k=0; System.out.println(); } } sc.close(); } }

Umang Rastogi on

Add Answers or Comments

To comment on this, Sign In or Sign Up.