Interview Question

Java Developer Interview

-

Oracle

Write a program to print * * * * * * * * * * * * * * * * * * * * * * * * * * * * * with minimum number of variable use

AnswerAdd Tags

Interview Answers

12 Answers

5

class StarPrint { public static void main (String[] args) throws java.lang.Exception { int i = 0, j = 0; for( i = 5; i > 0; i-- ){ for( j = 0; j < 5 - i; j++) System.out.print(" "); for( j = 0; j < i; j++ ) System.out.print("* "); System.out.println();} for( i = 3; i <= 6; i++ ){ for( j = 0; j <= 5 - i; j++) System.out.print(" "); for( j = 1; j < i; j++ ) System.out.print("* "); System.out.println(); } } }

Srichakradhar - print sand clock with stars on

0

public class Check { public static void main(String[] args) { int limit = 5; // Specify the number of lines as limit and check output int i=0,j=0; for(i=0;ilimit-i;j--)System.out.print(" "); for(j=limit;j>i;j--)System.out.print("* "); System.out.println(); } j=limit; for(;limit>1;limit--){ for(i=2;ilimit-2;i--)System.out.print("* "); System.out.println(); } } }

Santanu Naskar on

0

In the previous solution, the LESS THAN and GREATER THAN symbols have nit been parsed. public class Check { public static void main(String[] args) { int limit = 9; int i=0,j=0; for(i=0;i limit-i;j--)System.out.print(" "); for(j=limit;j > i;j--)System.out.print("* "); System.out.println(); } j=limit; for(;limit > 1;limit--){ for(i=2;i limit-2;i--)System.out.print("* "); System.out.println(); } } }

Santanu Naskar on

0

public static void printStars(){ int k = 0; for(int i=5;i>=1;i--){ int j = i; k++; System.out.printf("%"+k+"s"," "); while(j > 0){ System.out.print( " * "); j--; } System.out.println(); } for(int i=1;i<=5;i++){ int j = 1; System.out.printf("%"+k+"s"," "); while(j <= i){ System.out.print(" * "); j++; } k--; System.out.println(); } }

amidala siva kumar on

0

package learning.concepts.corejava; import org.apache.commons.lang.StringUtils; public class TestStars { public static void main(String[] args) { if(args.length < 1) { System.out.println("Please provide an input having *s separated by space"); return; } String stars = args[0]; int starCount = StringUtils.countMatches(stars, "*")-1; if(starCount < 0 || (stars.length() % 2) == 0) { System.out.println("Please ensure that input string starts with a * and ends with a *"); return; } int len = stars.length(); String[] starArray = new String[len]; StringBuilder member = new StringBuilder(); for(int count=0; count <= starCount; count++) { member.setLength(0); if(count==starCount) { starArray[count] = member.append(getSpace(count)).append(stars.charAt(0)).toString(); } else { member.append(getSpace(count)).append(stars.substring(0,(len-(count*2)))); starArray[count] = starArray[len-count-1] = member.toString(); } } for(String mem: starArray) { System.out.println(mem); } } private static String getSpace(int count) { StringBuilder spaces = new StringBuilder(); for(int temp=1; temp <=count; temp++) { spaces.append(" "); } return spaces.toString(); } }

mandar on

0

public static void main(String[] args) { final int N = 5; for (int i=-N;i<=N;i++) { if(i==-1||i==0) continue; for(int j=0;j

Anonymous on

0

public static void main(String[] args) { final int N = 5; for (int i=-N;i<=N;i++) { if(i==-1||i==0) continue; for(int j=0;j

Anonymous on

0

int k=-1; for(int i=N;i<=N;i+=k) { for(int j=0;j

Anonymous on

0

public class HelloWorld{ public static void main(String []args){ int i=0,j=0; for(i=-5;i<6;i++){ if(Math.abs(i)<1 || i == -1) continue; for(j=0;j<6 -Math.abs(i);j++){ System.out.print(" "); } for(j=0;j

dawid debinski on

0

public class HelloWorld{ public static void main(String []args){ int i=0,j=0; for(i=-5;i<6;i++){ if(Math.abs(i)<1 || i == -1) continue; for(j=0;j<6 -Math.abs(i);j++){ System.out.print(" "); } for(j=0;j

dawid debinski on

0

public class HelloWorld{ public static void main(String []args){ int i=0,j=0; for(i=-5;i<6;i++){ if(Math.abs(i)<1 || i == -1) continue; for(j=0;j<6 -Math.abs(i);j++){ System.out.print(" "); } for(j=0;j

dawid debinski on

1

i have used 2 variables only --------------------------------- package algorithm; public class Pyramid { public static void main(String[] args) { generatePyramid(4); } static void generatePyramidNew(int base) { int i,j; for (i = 0; i 0; i--) { for (k = 1; k < i; k++) { System.out.print(" "); } for (k = 0; k

Deep on

Add Answers or Comments

To comment on this, Sign In or Sign Up.