+ 3

How to print 1 2 3 3 4 5 4 5 6 7 pattern in java?

31st Oct 2017, 12:05 PM
Ganesh pandurang apune
Ganesh pandurang apune - avatar
11 Answers
+ 23
//input 4 import java.util.*; public class Program { public static void main(String[] args) { Scanner inp = new Scanner(System.in); int lng = inp.nextInt(); int i = 1; while (i <= lng) { int ii = 0; while(ii < i) { System.out.print((ii+i)+" "); ii++; } System.out.print("\n"); i++; } } }
31st Oct 2017, 12:29 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 20
//input 4 import java.util.*; public class Program { public static void main(String[] args) { Scanner inp = new Scanner(System.in); int lng = inp.nextInt(); for (int i = 1; i <= lng; i++) { for (int ii = 0; ii < i; ii++) { System.out.print((ii+i)+" "); } System.out.print("\n"); } } }
31st Oct 2017, 12:24 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 19
//Well... If you only need this specific output... ^_^ System.out.println("1\n2 3\n3 4 5\n4 5 6 7");
31st Oct 2017, 12:15 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 18
"While" loop?!?! Why didn't you say it earlier?!?! Your question was incomplete... ~_~
31st Oct 2017, 12:26 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 16
Can't figure out the algorithm... /;
31st Oct 2017, 12:11 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
public class Program { public static void main(String[] args) { int k = 1; for(int i = 1; i <= 5; i++){ for(int j = 1; j < i; j++){ System.out.print(k); k++; } System.out.println(); } } }
31st Oct 2017, 12:29 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 3
You cannot write a code to solve this for other numbers, it seems the numbers do not follow a specific order, so: String [] nums = {"1", "23", "345", "4567"}; for (String s : nums){ System.out.println(s); }
31st Oct 2017, 12:21 PM
Jonas Schröter
Jonas Schröter - avatar
+ 2
What do you mean?
31st Oct 2017, 12:09 PM
Jonas Schröter
Jonas Schröter - avatar
+ 2
1 2 3 3 4 5 4 5 6 7 I want this output
31st Oct 2017, 12:11 PM
Ganesh pandurang apune
Ganesh pandurang apune - avatar
+ 1
//Well... If you only need this specific output... ^_^ System.out.println("1\n2 3\n3 4 5\n4 5 6 7"); thnks but using while loop print this pattern
31st Oct 2017, 12:17 PM
Ganesh pandurang apune
Ganesh pandurang apune - avatar
23rd Jun 2019, 8:51 AM
Himanshu Sharma
Himanshu Sharma - avatar