Print Series 1, 12, 123, 1234, …………n in Java.



import java.util.Scanner;
public class Series 
{
 public static void main(String args[]) 
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int s = 0, c;                       // s for terms of series, c for counter to generate n terms
  for (c = 1; c <= n; c++) {
   s = s * 10 + c;
   System.out.print(s + " ");
  }
 }
}


Output:
Enter the number of terms: 6
1,  12,  123,  1234,  12345,  123456....... 
BUILD SUCCESSFUL (total time: 3 seconds)


1 comment:

  1. import java.util.Scanner;
    //1 12 123 1234 - eg. Output for number input number 4

    public class Series{

    public static void main(String []args){
    Scanner myObj = new Scanner(System.in);
    System.out.print("Enter the number:");
    int n = myObj.nextInt();
    for(int i=1;i<=n;i++){
    System.out.print(i);
    System.out.print(" ");
    for(int j=1;j<=i;j++)
    System.out.print(j);
    }

    }
    }

    ReplyDelete