Java Program to Print Exponentially Increasing Pattern

Program to Print Exponentially Increasing Pattern

In this article we are going to see how to print the exponentially increasing star pattern.

Example-1

If no of row = 4
*
**
****
********
Example-2

If no of row = 5
*
**
****
********
****************

Approach:

  • Enter total row and store it in an integer variable say row.
  • Take an outer for loop to print the rows.
  • Take an inner loop to print the column values.
  • Then go on printing the star symbols according to the pattern.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
    int r,c,row;
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter no of rows = ");
    row=sc.nextInt();
     
    // iteration for no of row
    for(r = 0; r < row; r++)
    {
      // print the column values
      // each column will print the values with power of r
      // Math.pow() function gives power of a number       
      for(c = 0; c < Math.pow(2,r); c++)
       {
           System.out.print("*");
       }
       // move to next line/row
       System.out.println("");
    }
    }
}

Output:

Enter no of rows = 5
*
**
****
********
****************

Method-2 : User Input Character

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
    int r,c,row;
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter no of rows = ");
    row=sc.nextInt();
    
    System.out.print("Enter any character = ");
    char s=sc.next().charAt(0);
     
    // iteration for no of row
    for(r = 0; r < row; r++)
    {
      // print the column values
      // each column will print the values with power of r
      // Math.pow() function gives power of a number       
      for(c = 0; c < Math.pow(2,r); c++)
       {
           System.out.print(s);
       }
       // move to next line/row
       System.out.println("");
    }
    }
}

Output:

Enter no of rows = 5
Enter any character = @
@
@@
@@@@
@@@@@@@@
@@@@@@@@@@@@@@@@

Explanation:

Let’s understand the program with detailed explanation.

Let we have taken row as 5.

Iteration-I

r=0 (passed through first for loop condition) which will execute till r<row.

It will enter inner loop and execute it from c=0 till power of r^2 i.e. 1 time and print the values. It will print 1 star symbol.

*

Iteration-II

r=1 (passed through first for loop condition) which will execute till r<row.

It will enter inner loop and execute it from c=0 till power of r^2 i.e. 2 times and print the values. It will print 2 star symbols.

**

Iteration-III

r=2 (passed through first for loop condition) which will execute till r<row.

It will enter inner loop and execute it from c=0 till power of r^2 i.e. 4 times and print the values. It will print 4 star symbols.

****

Iteration-IV

r=3 (passed through first for loop condition) which will execute till r<row.

It will enter inner loop and execute it from c=0 till power of r^2 i.e. 8 times and print the values. It will print 8 star symbols.

********

Iteration-V

r=4 (passed through first for loop condition) which will execute till r<row.

It will enter inner loop and execute it from c=0 till power of r^2 i.e. 16 times and print the values. It will print 16 star symbols.

****************

Now i=5, so first for loop condition fails i.e. r<row. At no more for loop will be executed. At last we will see a pattern like this on output screen.

*
**
****
********
****************

C Code:

#include<stdio.h> 
#include<math.h>
 
int main(){
    int r,c,row;
    printf("Enter no of rows = ");
    scanf("%d", &row);
    for(r = 0; r < row; r++){
       for(c = 0; c < pow(2,r); c++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}
Output:

Enter no of rows = 5
*
**
****
********
****************

C++ Code:

#include <iostream>
#include <cmath>

using namespace std;

int main(){
    int r,c,row;
    cout<<"Enter no of rows = ";
    cin>>row;
    for(r = 0; r < row; r++)
    {
       for(c = 0; c < pow(2,r); c++)
       {
           cout<<"*";
       }
       cout<<"\n";
    }
    return 0;
}
Output:

Enter no of rows = 5
*
**
****
********
****************

Related Java Star Pattern Programs: