Open In App

Program to find diameter with the given radius of a circle.

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given the radius of a circle, write a program to find the diameter of a circle.

Examples:

Input: r=4
Output: 8

Input: 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:

C++




#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;
}


Java




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();
    }
}


Python3




# 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)


C#




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);
    }
}


Javascript




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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads