Given the radius of a circle, write a program to find the diameter of a circle.
Examples:
Input: r=4
Output: 8Input: r=5
Output:10
Approach:
The diameter of a circle is simply twice the radius.
Here's how you could express this formula in a mathematical sense:
D= 2* r
r is radius and D is diameter of a circle
Below is the implementation of the code:
#include <iostream>
using namespace std;
int main()
{
int radius = 4, diameter;
// Get user input for the radius
cout << "The Given radius of the circle is: " << radius
<< endl;
// Calculate the diameter using the formula: diameter =
// 2 * radius
diameter = 2 * radius;
// Display the result
cout << "The diameter of the circle is : " << diameter
<< endl;
return 0;
}
import java.util.Scanner;
public class CircleDiameter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int radius = 4;
int diameter;
// Display the given radius of the circle
System.out.println("The Given radius of the circle is: " + radius);
// Calculate the diameter using the formula: diameter = 2 * radius
diameter = 2 * radius;
// Display the result
System.out.println("The diameter of the circle is: " + diameter);
scanner.close();
}
}
# Get the radius of the circle
radius = 4
# Display the given radius of the circle
print("The Given radius of the circle is:", radius)
# Calculate the diameter using the formula: diameter = 2 * radius
diameter = 2 * radius
# Display the result
print("The diameter of the circle is:", diameter)
using System;
public class GFG {
static public void Main()
{
int radius = 4, diameter;
// Get user input for the radius
Console.WriteLine(
"The Given radius of the circle is: " + radius);
// Calculate the diameter using the formula:
// diameter = 2 * radius
diameter = 2 * radius;
// Display the result
Console.WriteLine("The diameter of the circle is : "
+ diameter);
}
}
let radius = 4;
let diameter;
// Display the given radius
// of the circle
console.log("The Given radius of the circle is: " + radius);
// Calculate the diameter using
// the formula: diameter = 2 * radius
diameter = 2 * radius;
// Display the calculated diameter
console.log("The diameter of the circle is : " + diameter);
Output
The Given radius of the circle is: 4 The diameter of the circle is : 8
Time Complexity: O(1), since there is one multiplication operation which takes constant time.
Auxiliary Space: O(1), since no extra space has been taken.