Computer Science, asked by shilamondal51, 6 hours ago

2,22,222,2222 ,22222 solve in while loop java​

Answers

Answered by anindyaadhikari13
2

\texttt{\textsf{\large{\underline{Solution}:}}}

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;

       }

   }

}

\texttt{\textsf{\large{\underline{Logic}:}}}

  • 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