Printing a format in java


Print the format, you are given with an Integer, based on that integer print * sequential order as shown below
import java.util.*;
public class HackerRank {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
String format="*";
String dollar="$";
/* for(int i=0;i<input;i++){
for(int j=i+1;j<input;j++){
System.out.print(format);
}
System.out.println(format);
}
input: 5
output:
*****
****
***
**
*
*/
for(int i=0;i<input;i++){
for(int j=0;j<=i;j++){
if(j==i){
System.out.println(format); //This condition is used to avoid extra spaces at the ending
}else
System.out.print(format+" ");
}
}
/*
Input 5
output:
*
**
***
****
*****
*/
sc.close();
}
}
view raw PrintingFormat hosted with ❤ by GitHub



Input: 5

Output:
*
**
***
****
*****

Comments

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series