How do I print numbers like this: 1 21 321 in the shape of a pyramid | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do I print numbers like this: 1 21 321 in the shape of a pyramid

16th Oct 2017, 9:45 PM
Anthony
5 Answers
+ 3
I think like this you want on java public class Program { public static void main(String[] args) { String sum = ""; for(int i = 1; i < 10; i++){ for(int j = i; j > 0; j--){ sum += j+""; System.out.print(j); } System.out.println(); } } } or one loop public class Program { public static void main(String[] args) { String sum = ""; for(int i = 1; i <= 10; i++){ sum += i+""; System.out.println(sum); } } }
16th Oct 2017, 10:38 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 3
oh the question was for java ... sorry Ferhats code looks good although there is one for loop to much
16th Oct 2017, 10:45 PM
Chrizzhigh
Chrizzhigh - avatar
+ 2
nums="" for i in range(1,10): nums += str(i) print(nums) this should work... i am sure there is a even shorter or better way but you get the idea edit: i changed 100 to 10 because sololearn has a time limit
16th Oct 2017, 10:04 PM
Chrizzhigh
Chrizzhigh - avatar
+ 2
Yes, Python is very easy for loop, you are done with one line or two lines
16th Oct 2017, 10:48 PM
Ferhat Sevim
Ferhat Sevim - avatar
16th Oct 2017, 11:38 PM
Luiz Augusto Ventura
Luiz Augusto Ventura - avatar