Java Program to Print Asterisk Character Pattern

Program to Print Asterisk Character Pattern

Asterisk in java: In the previous article, we have discussed Java Program to Print Flag Character Pattern

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

Example-1

Characters : 5

A   E    I
 B  E  H 
  C E G  
   DEF   
     E    
   DEF   
  C E G  
 B  E  H 
A   E    I
Example-2

Characters : 10

A        J         S
 B       J        R 
  C      J       Q  
   D     J      P   
    E    J     O    
     F   J    N     
      G  J  M      
       H J L       
         IJK        
          J         
         IJK        
       H J L       
      G  J  M      
     F   J    N     
    E    J     O    
   D     J      P   
  C      J       Q  
 B       J        R 
A        J         S

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 character.
  • After each iteration print a new line.

Java Code to Print Asterisk Character Pattern

import java.util.Scanner;
class Main
{
    public static void main(String[] args) 
    {
        // int ascii 64 
        int ascii=64;
        Scanner scan = new Scanner(System.in);
        System.out.print("Characters : ");
        // 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((char)(c+ascii));
                else
                        System.out.print(" ");  
            }
            //Prints a newline
            System.out.println( );  
        }
    }
}
Output:

Characters : 5
 
A   E    I
 B  E  H 
  C E G  
   DEF   
     E    
   DEF   
  C E G  
 B  E  H 
A   E    I

C Code to Print Asterisk Character Pattern

#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
    printf("Characters : ");
    //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("%c",(c+ascii));
            else
                printf(" ");
        }
        printf("\n");
        //Prints a newline
    }
    return 0;
}
Characters : 5

A   E    I
 B  E  H 
  C E G  
   DEF   
     E    
   DEF   
  C E G  
 B  E  H 
A   E    I

C++ Code to Print Asterisk Character 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;
    int ascii=64;
    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 << (char)(c+ascii);
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}
Output:

Characters : 5

A   E    I
 B  E  H 
  C E G  
   DEF   
     E    
   DEF   
  C E G  
 B  E  H 
A   E    I

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 Character Pattern Programs: