Printing a format in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Input: 5
Output:
*
**
***
****
*****
Comments
Post a Comment