print circle stars patters using java code, Exercises of Computer science

this exercise is to print a pattern made of stars in the shape of a circle using loops

Typology: Exercises

2022/2023

Uploaded on 12/19/2023

amr-musharrafa
amr-musharrafa 🇪🇬

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1

Partial preview of the text

Download print circle stars patters using java code and more Exercises Computer science in PDF only on Docsity!

Powered by AI Bing Here is the updated Java code that prints a circle pattern using stars and its time complexity analysis: class GFG { static void printPattern (int radius) { double dist; for (int i = @; i <= 2 * radius; i++) { for (int j = 0; j <= 2 * radius; j++) { dist = Math.sqrt ( (i - radius) * (i - radius) + (j - radius) * (j - radius)); if (dist > radius - @.5 && dist < radius + @.5) System.out.print ("*"); else System.out.print (" "); t System.out.print ("\n"); } public static void main (String [] args) { int radius = 6; printPattern (radius); The above code uses the printPattern function to print a circle pattern using stars. The function takes an integer radius as input and prints the circle pattern using stars. The Math. sqrt function is used to calculate the distance between the center of the circle and each point on the circle. If the distance is within a certain range, a star is printed, otherwise a space is printed. The output of the above code is: seer ** +k ee 4 a +e *K + ** + *k + *k +k ak a ae +e ** ak soo The time complexity of the above code is O(n*2), where n is the radius of the circle. This is because the code uses two nested loops to iterate over all the points in the circle. The outer loop runs from 0 to 2 * radius, and the inner loop also runs from 0 to 2 * radius. Therefore, the total number of iterations is (2 * radius + 1) * (2 * radius + 1), which is equal to (4 * radius*2 + 4 * radius + 1). Asymptotically, this is equivalent to O(n’2). Thope this helps! Let me know if you have any other questions.