Java Program to Print Greater Than Symbol Number Pattern

Print Greater Than Symbol Number Pattern

In the previous article, we have discussed Java Program to Print Less Than Symbol Number Pattern

In this article we are going to see how to print the greater than symbol number pattern.

Example-1 

When size value=7
1        
   2          
      3        
        4      
      3        
   2          
1
Example-2

When the Size : 5

1       
   2      
      3    
   2      
1

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

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach:

  • Enter size of the pattern and store it in an integer variable size.
  • Take one outer for loop to iterate the rows.
  • Take one inner for loops, to iterate the columns and based on condition print the column values.
  • After each iteration print a new line.

Java Code to Print Greater Than Symbol Number Pattern

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
    int size, r, c;
    //Taking size as input from user
    System.out.print("Size : ");
    Scanner scan = new Scanner(System.in);
    size = scan.nextInt();
    int d = 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == d)
                System.out.print(c);
            else
                System.out.print("  ");
        }
        //Prints a newline
        System.out.println();
        //Adjusting the d value
        if (r <= size / 2)
            d++;
        else
            d--;
    }
    }
}
Size : 5
1        
    2      
       3    
    2      
1

C Code to Print Greater Than Symbol Number Pattern

#include <stdio.h>
int main()
{
    int size, r, c;
    //Taking size as input from user
    printf("Size : ");
    scanf("%d", &size);
    int d = 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == d)
                printf("%d",c);
            else
                printf(" ");
        }
        //Prints a newline
        printf("\n");
        //Adjusting the d value
        if (r <= size / 2)
            d++;
        else
            d--;
    }
    return 0;
}
Size : 5

1        
   2      
      3    
   2      
1

C++ Code to Print Greater Than Symbol Number Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int size, r, c;
    //Taking size as input from user
    cout << "Size : ";
    cin >> size;
    int d = 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= d; c++)
        {
            if (c == d)
                cout << c;
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
        //Adjusting the d value
        if (r <= size / 2)
            d++;
        else
            d--;
    }
    return 0;
}
Output:

Size : 5
 1        
    2      
       3    
    2      
1

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: