Java Program to Print Less Than Symbol Number Pattern

Print Less Than Symbol Number Pattern

In the previous article, we have discussed Java Program to Print Upward Arrow Mark Symbol Star Pattern

In this article we are going to see how to print the less than symbol Number pattern.

Output:

When size value= 7

         4      
      3        
   2          
1            
   2          
      3        
         4
Example-2

When size value= 5  

      3    
   2      
1        
   2      
      3

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

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

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

Java Code to Print Less 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();
    //Taking middle of the pattern
    int mid = size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == mid)
                System.out.print(c);
            else
                System.out.print("  ");
        }
        //Prints a newline
        System.out.println();
        //Adjusting the mid value
        if (r <= size / 2)
            mid--;
        else
            mid++;
    }
  }
}
Output:

Size :  5
   
      3    
   2      
1        
   2      
     3

C Code to Print Less Than Symbol Number Pattern

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

Size :   5

      3    
   2       
1        
   2      
      3

C++ Code to Print Less 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;
    //Taking middle of the pattern
    int mid = size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= mid; c++)
        {
            if (c == mid)
                cout << c;
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
        //Adjusting the mid value5
        if (r <= size / 2)
            mid--;
        else
            mid++;
    }
    return 0;
}
Output:

Size :    5

      3    
   2      
1        
   2      
      3

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: