Program to Print Hollow Circle Character Pattern
In the previous article, we have discussed Java Program to Print Circle Character Pattern
In this article we are going to see how to print the hollow circle character pattern.
- Java Code to Print Hollow Circle Character Pattern
- C Code to Print Hollow Circle Character Pattern
- C++ Code to Print Hollow Circle Character Pattern
Example-1 When radius value = 5 DEFGH C I B J A K A K A K A K A K B J C I DEFGH
Example-2 When radius value = 9 GHIJKLM EFG MNO DE OP C Q BC QR B R AB RS A S A S A S A S A S AB RS B R BC QR C Q DE OP EFG MNO GHIJKLM
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Approach:
- Enter the radius of the circle to print and store it in an integer variable
radius
. - Take first for loop to print all rows.
- Take inner for loop to print column values and one to print empty spaces.
- Then go on printing the character according to the iteration.
Java Code to Print Hollow Circle Character Pattern
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Radius : "); //Taking radius of the circle as input from user int radius = scan.nextInt(); int r, c; double d; // starting ASCII value taken 65 int ascii=65; for( r=0;r<=2 * radius; r++) {//Outer loop for(c=0; c<=2*radius; c++) {//Inner loop d = Math.sqrt((r-radius) * (r-radius) + (c-radius) * (c-radius));//Logic Part if(d > radius-0.5 && d < radius+0.5) //Print character or whitespace System.out.print((char)(c+ascii)); else System.out.print(" "); } System.out.println(); //Prints a newline } } }
Output: Radius : 5 DEFGH C I B J A K A K A K A K A K B J C I DEFGH
C Code to Print Hollow Circle Character Pattern
#include <stdio.h> #include <math.h> int main(int argc, char const *argv[]) { printf("Radius : "); //Taking radius as input from user int radius, r, c; // starting ASCII value taken 65 int ascii=65; float d; scanf("%d", &radius); for (r = 0; r <= 2 * radius; r++) { //Outer loop for (c = 0; c <= 2 * radius; c++) { //Inner loop d = sqrt((r - radius) * (r - radius) + (c - radius) * (c - radius)); //Logic Part if (d > radius - 0.5 && d < radius + 0.5) //Print star or whitespace printf("%c",(c+ascii)); else printf(" "); } printf("\n"); //Prints a newline } return 0; }
Output: Radius : 5 DEFGH C I B J A K A K A K A K A K B J C I DEFGH
C++ Code to Print Hollow Circle Character Pattern
#include <iostream> #include <math.h> using namespace std; int main(int argc, char const *argv[]) { cout << "Radius : "; //Taking radius as input from user int radius, r, c; // starting ASCII value taken 65 int ascii=65; cin >> radius; float d; for (r = 0; r <= 2 * radius; r++) { for (c = 0; c <= 2 * radius; c++) { d = sqrt((r - radius) * (r - radius) + (c - radius) * (c - radius)); //Logic Part //Logic Part if (d > radius - 0.5 && d < radius + 0.5) //Print character or whitespace cout << (char)(c+ascii); else cout << " "; } //Prints a newline cout << endl; } return 0; }
Output: Radius : 5 DEFGH C I B J A K A K A K A K A K B J C I DEFGH
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: