Java Program to Print Asterisk Number Pattern

Program to Print Asterisk Number Pattern

In the previous article, we have discussed Java Program to Print Flag Number Pattern

In this article we are going to see how to print the asterisk number pattern.

Example-1

 Rows : 5 

  1   1   1 
   2  2  2 
    3 3 3 
     444 
       5 
     666 
    7 7 7 
   8  8  8 
  9   9   9

Now, let’s see the actual program to print it.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Approach:

  • Enter length of the line and store it in an integer variable size.
  • Take outer for loop to iterate the rows.
  • Take inner for loop, to print space and number.
  • After each iteration print a new line.

Java Code to Print Asterisk Number Pattern

import java.util.Scanner;
class Main
{
    public static void main(String[] args) 
    {
        
        Scanner scan = new Scanner(System.in);
        System.out.print("Rows : ");
        // Taking number of stars in a segment 
        // as input from user
        int stars=scan.nextInt(),r,c;
        //Outer Loop
        for(r=1;r<2*stars;r++)
        {
            //Inner loop
            for(c=1;c<2*stars;c++)
            {
                if(c==stars || r==c || r+c==2*stars)
                        System.out.print(r);
                else
                        System.out.print(" ");  
            }
            //Prints a newline
            System.out.println( );  
        }
    }
}
Output:

Rows : 5

1   1   1
 2  2  2 
  3 3 3  
   444  
     5    
   666   
  7 7 7  
 8  8  8 
9   9   9

C Code to Print Asterisk Number Pattern

#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
    printf("Rows : ");
    //Taking number of stars in a segment as input from user
    int stars, r, c;
    int ascii=64;
    float d;
    scanf("%d", &stars);
    //Outer loop
    for (r = 1; r < 2 * stars; r++)
    { 
        //Inner loop
        for (c = 1; c < 2 * stars; c++)
        { 
            if (c == stars || r == c || r + c == 2 * stars)
                printf("%d",r);
            else
                printf(" ");
        }
        printf("\n");
        //Prints a newline
    }
    return 0;
}
Output: 

Rows : 5 

1   1   1 
 2  2  2 
  3 3 3 
   444 
     5 
   666 
  7 7 7 
 8  8  8 
9   9   9

C++ Code to Print Asterisk Number Pattern

#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Characters : ";
    //Taking number of stars in a sefment as input from user
    int stars, r, c;
    cin >> stars;
    //Outer loop
    for (r = 1; r < 2 * stars; r++)
    { 
        //Inner loop
        for (c = 1; c < 2 * stars; c++)
        { 
            if (c == stars || r == c || r + c == 2 * stars)
                cout << r);
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}
Output: 

Rows : 5 

1   1   1 
 2  2  2 
  3 3 3 
   444 
     5 
   666 
  7 7 7 
 8  8  8 
9   9   9

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs: