Java Program to Print Circle Number Pattern

Printing Circle Number Pattern

In the previous article, we have discussed Java Program to Print Hollow Numerical Parallelogram

In this article we will see how to print circle number pattern.

Example-1

When size value=3

   333 
 3      3
3        3
3        3
3       3
 3     3
  333
Example-2:

When size value=4




      33333 
  33         33
33             33
3                  3
3                  3
3                  3
 33             33
  33          33
      33333

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

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach:

  • Enter total size and number store them in integer variables size.
  • Take a outer loop for horizontal movement .
  • Take a inner loop for vertical movement .
  • Calculate distance according to size & print according to it.
  • After each iteration print a newline.

Java Code to Print Circle Number Pattern

import java.util.Scanner;
class Main
{
    public static void main(String[] args)
    {
        // taking size of no. of iteration 
        //Row and column are the iterators
        int size , numberOfRows , numberOfColumns ,num ;
        // Create a new Scanner object
        Scanner scanner = new Scanner(System.in);
        // Get the number of rows from the user
        System.out.println("Enter the number of radius : ");
        size = scanner.nextInt();
        System.out.println("Enter the number of which you want to print : ");
        num = scanner.nextInt();
        // dist represents distance to the centre.
        double dist;
        // for horizontal movement
        for  ( numberOfRows = 0; numberOfRows <= 2 * size; numberOfRows++) 
        {
            // for vertical movement
            for (  numberOfColumns = 0; numberOfColumns <= 2 * size; numberOfColumns++)
            {
                dist = Math.sqrt((numberOfRows - size) * (numberOfRows - size) + (numberOfColumns - size) * (numberOfColumns - size));
                // dist should be in the range (size - 0.5)  and (size + 0.5) to print stars(*)
                if (dist > size - 0.5 && dist < size + 0.5)
                    System.out.print(num);
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
} 
Output :

Enter the radius : 
4
Enter the number you want to print  : 
3

     33333 
  33         33
33             33
3                  3
3                  3
3                  3
33             33
  33         33
     33333

C Code to Print Circle Number Pattern

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
       int size , numberOfRows , numberOfColumns,num;
       double dist;
       printf("Enter the radius : ");
       scanf ("%d",&size);
       printf("Enter the number you want to print  : ");
       scanf ("%d",&num);
        for  ( numberOfRows = 0; numberOfRows <= 2 * size; numberOfRows++) 
        {
            for (  numberOfColumns = 0; numberOfColumns <= 2 * size; numberOfColumns++)
            {
                dist = sqrt((numberOfRows - size) * (numberOfRows - size) + (numberOfColumns - size) * (numberOfColumns - size));
                if (dist > size - 0.5 && dist < size + 0.5)
                     printf("%d",num);
                else
                     printf(" ");
            }
             printf("\n");
        }
    return 0;
}

C++ Code to Print Circle Number Pattern

#include <bits/stdc++.h>
using namespace std;
int main()
{
    
       int size , numberOfRows , numberOfColumns,num;
       double dist;
       cout << "Enter the radius : " ;
       cin >> size;
       cout << "Enter the number you want to print : " ;
       cin >> num ;
      for  ( numberOfRows = 0; numberOfRows <= 2 * size; numberOfRows++) 
        {
            for (  numberOfColumns = 0; numberOfColumns <= 2 * size; numberOfColumns++)
            {
                dist = sqrt((numberOfRows - size) * (numberOfRows - size) + (numberOfColumns - size) * (numberOfColumns - size));
                if (dist > size - 0.5 && dist < size + 0.5)
                     cout << num ;
                else
                     cout << " ";
            }
             cout << "\n";
        }
    return 0;
}

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:

Leave a Comment