Wap in java to get an output
1
12
123
1234
Answers
Answered by
1
Easiest program:
package com.company;
public class Main {
public static void main(String[] args) {
int s = 0;
for (int i = 1; i <= 4 ; i++) {
s = s*10+i;
System.out.println(s);
}
}
}
Output:
1
12
123
1234
Answered by
3
public class Main {
public static void main(String [] args) {
for (int i = 1; i <= 4; i++) {
for (int n = 1; n <= i; n++) {
System.out.print(n);
}
System.out.print("\n");
}
}
}
- The class name is declared as "i"
- Then comes the main method where all the legal code goes in.
- We executed a loop for the variable "i" from 1 to 4.
- There is one more same loop but with variable "n" nested inside the 'i loop'.
- Then we print the variable "n" inside the loop.
- Outside this loop, we print the newline character to print the series accurately.
- And we finished it...
Hope it helps..♪
Similar questions
Math,
6 months ago
English,
6 months ago
Math,
1 year ago
Physics,
1 year ago
Computer Science,
1 year ago