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.



Similar Reads

Program to find the side length of a square given its area
Write a program to find the side length of a square with given area. Examples: Input: Area = 16 square units Output: Side length = 4 units Input: Area = 25 square meters Output: Side length = 5 meters Approach: To solve the problem, follow the below idea: The side length of a square can be calculated using the formula: Side length=sqrt(Area)‚Äč. Step
2 min read
Program to find the perimeter of a square given its area
Write a program to find the perimeter of a square given its area. Examples: Input: Area = 36 square unitsOutput: Perimeter = 24 unitsExplanation: Since the area of the square is 36 square units, so the side of square will be 6 units. Therefore, the perimeter of the square will be 4 * 6 = 24 units. Input: Area = 25 square unitsOutput: Perimeter = 20
3 min read
Program to find the number of days in a given month
Write a program to find the number of days in a given month. We are given name of a month in lowercase letters, so print the number of days in the month. Note: For February, consider the number of days as 28. Examples: Input: month = "january"Output: 31Explanation: January has 31 days. Input: month = "february"Output: 28Explanation: February has 28
3 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
Program to find Minkowski Distance
Given two arrays A[] and B[] as position vector of two points in n-dimensional space along with an integer p, the task is to calculate Minkowski Distance between these two points. The Minkowski distance is a generalization of other distance measures, such as Euclidean and Manhattan distances, and is defined as: [Tex]D(A, B) = \left( \sum_{i=1}^{n}
4 min read
Program to find the sum and difference of two numbers
Given two Integers A and B, your task is to find the sum and difference of two numbers. Examples: Input: A = 4, B = 9Output: Sum = 13 and Difference = -5Input: A = -3, B = -2Output: Sum = -5 and Difference = -1 Approach: The problem can be solved using arithmetic operators. Operations on Numbers: Addition (+): This operation is used to add the inpu
3 min read
Program to find the area of different shapes | Menu Driven
Write a Menu-driven program to calculate the area of different shapes based on the user choice. Menu to calculate area of different shapes: Enter 1 to calculate the area of a circle.Enter 2 to calculate the area of a triangle. Enter 3 to calculate the area of a square. Enter 4 to calculate the area of a rectangle.Enter 5 to terminate the program. F
9 min read
Program to find the average of two numbers
Given two Integers A and B, your task is to find the average of two numbers where the average is given by the formula:average(A, B) = (A+B)/2 Examples: Input: A=5, B=3Output: 4Explanation: Average(A,B) = (A+B)/2 = 8/2 =4 Input: A=10, B=5Output: 7.5Explanation: Average(10, 5) = (10 + 5)/2 = 15/2 = 7.5 Approach: To solve the problem, follow the below
3 min read
Program to count the number of months between given two years
Given a start year and end year. Write a program to count number of months between given two years (both inclusive). Examples: Input: startYear = 1991, endYear = 2023Output: 396Explanation: The number of years between 1991 and 2023 (both inclusive) are: 33. So, total number of months = 396. Input: startYear = 2010, endYear = 2023Output: 168Explanat
4 min read
Program to print the number of days in a given year
Write a program to print the number of days in a given year. Examples: Input: 2023Output: 365Explanation: The year 2023 is not a leap year, therefore the number of days are 365. Input: 2024Output: 366Explanation: The year 2024 is a leap year, therefore the number of days are 366. Approach: To solve the problem, follow the below idea: To determine t
4 min read
Article Tags :