In this comprehensive coding tutorial, we'll guide you through the process of creating a program to calculate and display the area and circumference of a circle. As we delve into this programming journey, we'll provide implementations in four popular languages: C, C++, Java, and Python.
Problem Statement:
Develop an efficient program that prompts users for the radius of a circle, calculating and presenting both the area and circumference based on the input.
Sample Input/Output:
Algorithm:
Start
Prompt user for the circle radius
Read and store the radius value
Calculate the area using the formula: Area = π * radius * radius
Calculate the circumference using the formula: Circumference = 2 * π * radius
Display the calculated area and circumference
End
C Programming:
Objective: In this C programming tutorial, we harness the provided radius input to calculate and showcase both the area and circumference of a circle. This implementation ensures precision and efficiency.
#include <stdio.h>
#define PI 3.14159265
// Function to calculate circle properties
void calculateCircleProperties(float radius) {
float area = PI * radius * radius;
float circumference = 2 * PI * radius;
// Display the calculated area and circumference
printf("Area: %.2f\n", area);
printf("Circumference: %.2f\n", circumference);
}
// Main program
int main() {
float radius;
// Prompt user for circle radius
printf("Enter Circle Radius: ");
scanf("%f", &radius);
// Call the function to calculate and display properties
calculateCircleProperties(radius);
return 0;
}
Input/Output:
Enter Circle Radius: 5
Area: 78.54
Circumference: 31.42
C++ Programming:
Objective: In this C++ programming tutorial, we leverage the provided radius to calculate and present the area and circumference of a circle. The implementation emphasizes readability and functionality.
#include <iostream>
#include <cmath>
#define PI 3.14159265
// Function to calculate circle properties
void calculateCircleProperties(float radius) {
float area = PI * pow(radius, 2);
float circumference = 2 * PI * radius;
// Display the calculated area and circumference
std::cout << "Area: " << area << std::endl;
std::cout << "Circumference: " << circumference << std::endl;
}
// Main program
int main() {
float radius;
// Prompt user for circle radius
std::cout << "Enter Circle Radius: ";
std::cin >> radius;
// Call the function to calculate and display properties
calculateCircleProperties(radius);
return 0;
}
Input/Output:
Enter Circle Radius: 10
Area: 314.16
Circumference: 62.83
Java Programming:
Objective: This Java programming tutorial utilizes the input radius to calculate and present both the area and circumference of a circle. The implementation focuses on efficiency and adheres to Java's readability standards.
import java.util.Scanner;
public class CircleProperties {
final static double PI = 3.14159265;
// Function to calculate circle properties
static void calculateCircleProperties(double radius) {
double area = PI * Math.pow(radius, 2);
double circumference = 2 * PI * radius;
// Display the calculated area and circumference
System.out.println("Area: " + area);
System.out.println("Circumference: " + circumference);
}
// Main program
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for circle radius
System.out.print("Enter Circle Radius: ");
double radius = scanner.nextDouble();
// Call the function to calculate and display properties
calculateCircleProperties(radius);
}
}
Input/Output:
Enter Circle Radius: 7
Area: 153.94
Circumference: 43.98
Python Programming:
Objective: In this Python programming tutorial, we utilize the user-input radius to calculate and showcase both the area and circumference of a circle. The implementation emphasizes simplicity and readability.
import math
# Function to calculate circle properties
def calculateCircleProperties(radius):
area = math.pi * radius**2
circumference = 2 * math.pi * radius
# Display the calculated area and circumference
print(f"Area: {area:.2f}")
print(f"Circumference: {circumference:.2f}")
# Main program
if __name__ == "__main__":
radius = float(input("Enter Circle Radius: "))
# Call the function to calculate and display properties
calculateCircleProperties(radius)
Input/Output:
Enter Circle Radius: 8
Area: 201.06
Circumference: 50.27
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box