Program to find diameter with the given radius of a circle.
Last Updated :
29 Jan, 2024
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;
cout << "The Given radius of the circle is: " << radius
<< endl;
diameter = 2 * radius;
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;
System.out.println( "The Given radius of the circle is: " + radius);
diameter = 2 * radius;
System.out.println( "The diameter of the circle is: " + diameter);
scanner.close();
}
}
|
Python3
radius = 4
print ( "The Given radius of the circle is:" , radius)
diameter = 2 * radius
print ( "The diameter of the circle is:" , diameter)
|
C#
using System;
public class GFG {
static public void Main()
{
int radius = 4, diameter;
Console.WriteLine(
"The Given radius of the circle is: " + radius);
diameter = 2 * radius;
Console.WriteLine( "The diameter of the circle is : "
+ diameter);
}
}
|
Javascript
let radius = 4;
let diameter;
console.log( "The Given radius of the circle is: " + radius);
diameter = 2 * radius;
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.
Share your thoughts in the comments
Please Login to comment...