2,22,222,2222 ,22222 solve in while loop java
Answers
Answered by
2
The given problem is solved using while loop in Java.
import java.util.*;
public class Series{
public static void main(String args[]){
int i=0,n;
String s="2 ";
System.out.print("Enter limit - ");
n=(new Scanner(System.in)).nextInt();
while(i++<n){
System.out.print(s);
s="2"+s;
}
}
}
- Accept the limit from the user.
- Initialise s = "2 ".
- Repeat for I = 1 to limit ->
- Display s.
- Replace s with value - "2" + s.
See attachment for output.
Attachments:
Similar questions