Write a program to give output as a ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a program to give output as a ?

12345 *2345 **345 ***45 ****5

9th Sep 2017, 1:34 AM
Raj Kumar
Raj Kumar - avatar
3 Answers
+ 6
Java: public class Program { public static void main(String[] args) { for(int i = 1; i < 6; ++i) { int j = 1; for(; j < i; ++j) { System.out.print("*"); } for(; j < 6; ++j) { System.out.print(j); } System.out.println(); } } } Python: for i in range(1, 6): for j in range(i-1): print("*", end="") for k in range(6-i): print(k+i, end="") print() Not very elegant, but hopefully easy to understand.
9th Sep 2017, 2:00 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Less easy to understand than @ChaoticDawg's, added just for thinking: maxx = 5 stars = "*" * maxx nums = "".join( [str(c+1) for c in range(maxx)] ) for n in range(maxx): print(stars[:n] + nums[n:])
9th Sep 2017, 2:55 AM
Kirk Schafer
Kirk Schafer - avatar
9th Sep 2017, 3:28 AM
sayan chandra
sayan chandra - avatar