Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

Print Series 1, 11, 111, 1111……..n terms in Java.

Somdeb Burman | 11:45:00 AM | 17 comments

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 n terms
  for (c = 1; c <= n; c++)                          // To generate n terms
  {
   s = s * 10 + 1;
   System.out.print(s + " ");
  }                                                           //for  ends
 }
}


Output:
Enter the number of terms: 7
1, 11, 111, 1111, 11111, 111111, 1111111...... 
BUILD SUCCESSFUL (total time: 3 seconds)

Java Resources are available for Download from below

Category: , , , ,

17 comments:

  1. Thnx (tomorrow I have no exam!😜)

    ReplyDelete
  2. Please do the reverse of the program

    ReplyDelete
  3. Every step will be same. Except
    for (c=n; c>=1; c--)
    {
    s = s / 10; // / will give only the quotient so if n = 11111 s will be 1111.
    S.o.p(s + " ");
    }

    ReplyDelete
  4. It's not printing the in that output format. I want that if n =5 then the output must be like:-
    1,11,111,1111,11111.
    A comma and a full stop must also be printed. Pls help.

    ReplyDelete
    Replies
    1. s = s * 10 + 1;
      System.out.print(s + " ");

      Change the last line to
      s = s * 10 + 1;
      System.out.print(s + " , ");

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Java Program......
      int i=1;
      int sum=0;
      int j=1;
      while (i<=5) {
      sum=sum+j;
      j=j*10;
      i++;
      }
      System.out.println(sum+" + ");

      Delete
  5. It's not printing the in that output format. I want that if n =5 then the output must be like:-
    1,11,111,1111,11111.
    A comma and a full stop must also be printed. Pls help.

    ReplyDelete
  6. How will I get it's sum? Can you please help?

    ReplyDelete
    Replies
    1. s is variable where its value is 0.
      Its is not sum

      Delete
  7. can you solve this one by using while loop

    ReplyDelete
  8. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. 333 angel number

    ReplyDelete