Java Program to Print the Series 1 11 111 1111 … N

111 111 1111: In the previous article, we have discussed about Java Program to Find Sum of Series a^2 + a^2/2 + a^2/3 + …… + a^2/10

In this article we are going to see how to print the series  1 11 111 1111 … N using Java programming language. On addition of 1 11 111 1111…N we add from the beginners view some basic series 1 11 111 pattern in java, print pattern 1 11 111 in java, Draw print pattern in java, 1 11 111 series in java, How to print 1 11 111 in java, print pattern 11, 1111, 111111 in java, 1 11 111 1111 pattern in java and examples like:

For Example, Num= 4, The Pattern Should Be: 1 11 111 1111, For Example, If Num= 3, The Pattern Should Be: 11 1111 111111.

Also Read: Java 1 4 1 – Java Program to Print Series 1 4 9 16 25 36 …N

Java Program to Print the Series 1 11 111 1111 … N

In this series the it can be seen that numbers at each position i ‘1’ is  repeated i times.

For example:

If at 1st position there is only one ‘1’, at 5th position there are 5 ‘1’s.

Let’s see different ways to print the series.

Method-1: Java Program to Print the Series 1 11 111 1111 … N By Using User Input Value

Approach:

  • Create Scanner class object.
  • Prompt the user to enter a number.
  • Initialize a variable term to 1.
  • Run a for loop i=1 to n.
  • Inside the loop, print the term and the update term as term = term * 10 + 1

Program:

import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        // create a Scanner object
        Scanner sc = new Scanner(System.in);
        // prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        // initialize the variable term to 1
        int term = 1;
        // loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            System.out.print(term + " ");
            // update the value of term
            term = term * 10 + 1;
        }
    }
}
Output:

Enter the number of terms: 5
1 11 111 1111 11111

Method-2: Java Program to Print the Series 1 11 111 1111 … N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Prompt the user to enter a number.
  • Call the method to print the series.
  • In the method, Initialize a variable term to 1.
  • Run a for loop i=1 to n.
  • Inside the loop, print the term and the update term as term = term * 10 + 1

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        // create a Scanner object
        Scanner sc = new Scanner(System.in);
        // prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        // call the printSeries method
        printSeries(n);
    }
    // method to print the series
    private static void printSeries(int n)
    {
        // initialize the variable term to 1
        int term = 1;
        // loop to print the series
        for(int i = 1; i <= n; i++) 
        {
            System.out.print(term + " ");
            // update the value of term
            term = term * 10 + 1;
        }
    }
}

Output:

Enter the number of terms: 5
1 11 111 1111 11111

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: