Interview Question

Java Developer Interview

-

WebKul

Write a program for odd input the pattern output will as below n=1 *-* n=3 ***** *** *---* *** *****

AnswerAdd Tags

Interview Answers

12 Answers

11

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

Anonymous on

2

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

Sarthak Tomar on

1

import java.util.Scanner; public class webu { 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 = n; i >1; i--) { for(int j=1;j1){ for (int l =1 ; l <=2*i-1; l++) { System.out.print("*"); } System.out.println(" "); } } } }

Rohit Kumar on

0

import java.util.Scanner; public class Pattern { public static void main(String ... args){ Scanner sc =new Scanner(System.in); int n=sc.nextInt(); for(int i=0;i

Anonymous on

0

import java.util.Scanner; class Pattern2 { public static void main(String a[]) { System.out.print("Enter the Number "); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=0;i0;j--) { System.out.print("*"); } System.out.println(); } for(int i=0;i

Alam on

0

for swift answer func designPattern6(number:Int){ for i in 1...((number * 2) - 1){ if i%2 == 0{ print("") }else{ for j in 1...((number * 2) - 1){ if j >= (((number * 2) + 1) - i)/2 && j <= ((number * 2) - i){ print("*", terminator: "") }else{ print(" ", terminator: "") } } } print("") } }

unknown on

0

n=int(input()) for i in range(n): if(n==1): print("* "*(n+1)) elif(n==3): print( " "*i+"*"*(2*n-1-2*i)) else: print(" ") for i in range(n): if(n==3): print(""*(i+1)+"*"*(n+2*i-2))

vikash kumar gupta on

0

mport java.util.Scanner; public class Pattern{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); for(int i=n;i>=2;i--){ for(int j=1;j<=2*n-(i-1);j++){ System.out.print(" "); } for(int k=1;k<=2*i-1;k++){ System.out.print("*"); } System.out.println(); } for(int i=1;i<=n-1;i++){ System.out.print(" "); } System.out.print("*"); for(int i=1;i<=n;i++){ System.out.print("-"); } System.out.print("*"); System.out.println(); for(int i=2;i<=n;i++){ for(int j=1;j<=n-i;j++){ System.out.print(" "); } for(int k=1;k<=2*i-1;k++){ System.out.print("*"); } System.out.println(); } } }

Anonymous on

0

import java.util.Scanner; public class Webu { 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 = n; i >1; i--) { for(int j=1;j1){ for (int l =1 ; l <=2*i-1; l++) { System.out.print("*"); } System.out.println(" "); } } } }

Rohit Kumar on

0

import java.util.Scanner; public class webu { 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 = n; i >1; i--) { for(int j=1;j1){ for (int l =1 ; l <=2*i-1; l++) { System.out.print("*"); } System.out.println(" "); } } } }

Rohit Mishra on

0

code for c language #include #include int main() { int n=10,i,j,k,l,m,o; for(i=1;i<=n;i++) { for(j=1;j<=n+i;j++) { if(i==n) { for(k=1;k<=n-1;k++) printf(" "); printf("*"); for(m=1;m<=n;m++) printf(" "); // printf("*"); break; } printf(" "); } for(o=1;o<=2*(n-i)+1;o++) printf("*"); printf("\n"); } for(i=1;i

shubham singh on

0

import java.util.*; class Pattern { public static void main(String arr[]) { Scanner input=new Scanner(System.in); System.out.print("enter the "); int n=input.nextInt(); int p=n; for(int i=1;i=1;j--) { System.out.print("*"); } System.out.print(" "); p--; } for(int k=1;k<=n;k++) { System.out.print("_"); } System.out.print(" "); for(int m=1;m<=n;m++) { for(int l=1;l<=2*m-1;l++) { System.out.print("*"); } System.out.print(" "); } } }

Interview Answer on

Add Answers or Comments

To comment on this, Sign In or Sign Up.