Posted on by Kalkicode
Code Geometric

# Find the volume of a torus

A torus is a three-dimensional geometric shape resembling a donut with a hole in the center. Finding the volume of a torus involves calculating the amount of space enclosed within its boundaries. This calculation is relevant in various fields, such as mathematics, physics, and engineering, where tori are used to model objects like pipes, tires, and certain parts of machinery.

## Problem Statement

Given the radii of the small circle (r) and the large circle (R) that make up the torus, the task is to calculate its volume. The formula for finding the volume of a torus in terms of its radii 'r' and 'R' is:

Volume = 2 * π² * R * r²

## Example Scenario

Imagine you are an engineer designing a water pipe system for a new building. To estimate the amount of water the pipes can hold, you need to calculate the volume of the toroidal pipe shape. This calculation helps ensure that the pipe system can handle the required water flow.

## Idea to Solve the Problem

To solve this problem, we can follow these steps:

1. Accept the radii 'r' and 'R' of the small and large circles as inputs.
2. Use the formula to calculate the volume of the torus.
3. Display the calculated volume.

## Pseudocode

``````function torus_volume(r, R):
volume = 2 * π² * R * (r * r)
return volume

main:
r1 = 3
R1 = 7

r2 = 4
R2 = 8

r3 = 6.2
R3 = 8.3

volume1 = torus_volume(r1, R1)
volume2 = torus_volume(r2, R2)
volume3 = torus_volume(r3, R3)

print("Torus [ r :", r1, "R :", R1, "]")
print("Volume :", volume1)

print("Torus [ r :", r2, "R :", R2, "]")
print("Volume :", volume2)

print("Torus [ r :", r3, "R :", R3, "]")
print("Volume :", volume3)``````

## Algorithm Explanation

1. Define a function `torus_volume` that takes the radii 'r' and 'R' as inputs.
2. Inside the function, use the provided formula to calculate the volume of the torus.
3. In the `main` function, set three test cases with different values for the radii 'r' and 'R'.
4. Calculate the volumes for each test case by calling the `torus_volume` function.
5. Display the calculated volumes along with their respective radii values.

## Code Solution

``````/*
C Program
Find the volume of a torus
*/
#include <stdio.h>

#include <math.h>

//Calculate volume of a torus by given small and large radius
void torus_volume(double r, double R)
{
// Formula of torus volume
// 2 × π² × R × r²
// here r is radius of small circle
// R is radius of large circle
//Display given inputs
printf("\nGiven Radius of r: %lf Radius of R : %lf", r, R);
//Calculate volume of torus
double volume = 2 * (M_PI * M_PI) * R * (r * r);
//Display volume
printf("\nVolume of torus : %lf\n", volume);
}
int main()
{
//Simple Case
torus_volume(3, 7);
torus_volume(4, 8);
torus_volume(6.2, 8.3);
return 0;
}``````

#### Output

``````Given Radius of r: 3.000000 Radius of R : 7.000000
Volume of torus : 1243.570155

Volume of torus : 2526.618727

Volume of torus : 6297.834047``````
``````// Java Program
// Find the volume of a torus
class Torus
{
//Calculate volume of a torus by given small and large radius
public void volume(double r, double R)
{
// Formula of torus volume
// 2 × π² × R × r²
// here r is radius of small circle
// R is radius of large circle
System.out.print("\nGiven Radius of small r: " + r + " Radius of large R : " + R + "");
//Calculate volume of torus
double volume = 2 * (Math.PI * Math.PI) * R * (r * r);
System.out.print("\nVolume of torus : " + volume + "\n");
}
public static void main(String[] args)
{
Torus torus = new Torus();
//Simple Case
torus.volume(3, 7);
torus.volume(4, 8);
torus.volume(6.2, 8.3);
}
}``````

#### Output

``````Given Radius of small r: 3.0 Radius of large R : 7.0
Volume of torus : 1243.570154537259

Given Radius of small r: 4.0 Radius of large R : 8.0
Volume of torus : 2526.6187266788756

Given Radius of small r: 6.2 Radius of large R : 8.3
Volume of torus : 6297.834046752725``````
``````// C++ Program
// Find the volume of a torus
#include<iostream>
#include<math.h>

using namespace std;
class Torus
{
public:
//Calculate volume of a torus by given small and large radius
void volume(double r, double R)
{
cout << "\nGiven Radius of small r: " << r << " Radius of large R : " << R << "";
//Calculate volume of torus
double volume = 2 * (M_PI * M_PI) * R * (r * r);
cout << "\nVolume of torus : " << volume << "\n";
}
};
int main()
{
Torus torus ;
//Simple Case
torus.volume(3, 7);
torus.volume(4, 8);
torus.volume(6.2, 8.3);
return 0;
}``````

#### Output

``````Given Radius of small r: 3 Radius of large R : 7
Volume of torus : 1243.57

Given Radius of small r: 4 Radius of large R : 8
Volume of torus : 2526.62

Given Radius of small r: 6.2 Radius of large R : 8.3
Volume of torus : 6297.83``````
``````// C# Program
// Find the volume of a torus
using System;
class Torus
{
//Calculate volume of a torus by given small and large radius
public void volume(double r, double R)
{
Console.Write("\nGiven Radius of small r: " + r + " Radius of large R : " + R + "");
//Calculate volume of torus
double volume = 2 * (Math.PI * Math.PI) * R * (r * r);
Console.Write("\nVolume of torus : " + volume + "\n");
}
public static void Main(String[] args)
{
Torus torus = new Torus();
//Simple Case
torus.volume(3, 7);
torus.volume(4, 8);
torus.volume(6.2, 8.3);
}
}``````

#### Output

``````Given Radius of small r: 3 Radius of large R : 7
Volume of torus : 1243.57015453726

Given Radius of small r: 4 Radius of large R : 8
Volume of torus : 2526.61872667888

Given Radius of small r: 6.2 Radius of large R : 8.3
Volume of torus : 6297.83404675273``````
``````<?php
// Php Program
// Find the volume of a torus
class Torus
{
//Calculate volume of a torus by given small and large radius
public  function volume(\$r, \$R)
{
echo "\nGiven Radius of small r: ". \$r ." Radius of large R : ". \$R ."";
//Calculate volume of torus
\$volume = 2 * (M_PI * M_PI) * \$R * (\$r * \$r);
echo "\nVolume of torus : ". \$volume ."\n";
}
}

function main()
{
\$torus = new Torus();
//Simple Case
\$torus->volume(3, 7);
\$torus->volume(4, 8);
\$torus->volume(6.2, 8.3);
}
main();``````

#### Output

``````Given Radius of small r: 3 Radius of large R : 7
Volume of torus : 1243.5701545373

Given Radius of small r: 4 Radius of large R : 8
Volume of torus : 2526.6187266789

Given Radius of small r: 6.2 Radius of large R : 8.3
Volume of torus : 6297.8340467527``````
``````// Node Js Program
// Find the volume of a torus
class Torus
{
//Calculate volume of a torus by given small and large radius
volume(r, R)
{
process.stdout.write("\nGiven Radius of small r: " + r + " Radius of large R : " + R + "");
//Calculate volume of torus
var volume = 2 * (Math.PI * Math.PI) * R * (r * r);
process.stdout.write("\nVolume of torus : " + volume + "\n");
}
}

function main()
{
var torus = new Torus();
//Simple Case
torus.volume(3, 7);
torus.volume(4, 8);
torus.volume(6.2, 8.3);
}
main();``````

#### Output

``````Given Radius of small r: 3 Radius of large R : 7
Volume of torus : 1243.570154537259

Given Radius of small r: 4 Radius of large R : 8
Volume of torus : 2526.6187266788756

Given Radius of small r: 6.2 Radius of large R : 8.3
Volume of torus : 6297.834046752725``````
``````#  Python 3 Program
#  Find the volume of a torus
import math
class Torus :
# Calculate volume of a torus by given small and large radius
def volume(self, r, R) :
print("\nGiven Radius of small r: ", r ," Radius of large R : ", R ,"", end = "")
# Calculate volume of torus
volume = 2 * (math.pi * math.pi) * R * (r * r)
print("\nVolume of torus : ", volume ,"\n", end = "")

def main() :
torus = Torus()
# Simple Case
torus.volume(3, 7)
torus.volume(4, 8)
torus.volume(6.2, 8.3)

if __name__ == "__main__": main()``````

#### Output

``````Given Radius of small r:  3  Radius of large R :  7
Volume of torus :  1243.570154537259

Given Radius of small r:  4  Radius of large R :  8
Volume of torus :  2526.6187266788756

Given Radius of small r:  6.2  Radius of large R :  8.3
Volume of torus :  6297.834046752725``````
``````#  Ruby Program
#  Find the volume of a torus
class Torus

# Calculate volume of a torus by given small and large radius
def volume(s_r, l_r)

#  Formula of torus volume
#  2 × π² × R × r²
#  here r is radius of small circle
#  R is radius of large circle
print("\nGiven Radius of small r: ", s_r ," Radius of large R : ", l_r ,"")
# Calculate volume of torus
volume = 2 * (Math::PI * Math::PI) * l_r * (s_r * s_r)
print("\nVolume of torus : ", volume ,"\n")
end
end
def main()

torus = Torus.new()
# Simple Case
torus.volume(3, 7)
torus.volume(4, 8)
torus.volume(6.2, 8.3)
end
main()``````

#### Output

``````Given Radius of small r: 3 Radius of large R : 7
Volume of torus : 1243.570154537259

Given Radius of small r: 4 Radius of large R : 8
Volume of torus : 2526.6187266788756

Given Radius of small r: 6.2 Radius of large R : 8.3
Volume of torus : 6297.834046752725
``````
``````// Scala Program
// Find the volume of a torus
class Torus
{
//Calculate volume of a torus by given small and large radius
def volume(r: Double, R: Double): Unit = {
// Formula of torus volume
// 2 × π² × R × r²
// here r is radius of small circle
// R is radius of large circle
print("\nGiven Radius of small r: " + r + " Radius of large R : " + R + "");
//Calculate volume of torus
var volume: Double = 2 * (Math.PI * Math.PI) * R * (r * r);
print("\nVolume of torus : " + volume + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var torus: Torus = new Torus();
//Simple Case
torus.volume(3, 7);
torus.volume(4, 8);
torus.volume(6.2, 8.3);
}
}``````

#### Output

``````Given Radius of small r: 3.0 Radius of large R : 7.0
Volume of torus : 1243.570154537259

Given Radius of small r: 4.0 Radius of large R : 8.0
Volume of torus : 2526.6187266788756

Given Radius of small r: 6.2 Radius of large R : 8.3
Volume of torus : 6297.834046752725``````
``````// Swift Program
// Find the volume of a torus
import Foundation
class Torus
{
//Calculate volume of a torus by given small and large radius
func volume(_ r: Double, _ R: Double)
{
print("\nGiven Radius of small r: ", r ," Radius of large R : ", R ,"", terminator: "");
//Calculate volume of torus
let volume: Double = 2 * (Double.pi * Double.pi) * R * (r * r);
print("\nVolume of torus : ", volume ,"\n", terminator: "");
}
}
func main()
{
let torus: Torus? = Torus();
//Simple Case
torus!.volume(3, 7);
torus!.volume(4, 8);
torus!.volume(6.2, 8.3);
}
main();``````

#### Output

``````Given Radius of small r:  3.0  Radius of large R :  7.0
Volume of torus :  1243.57015453726

Given Radius of small r:  4.0  Radius of large R :  8.0
Volume of torus :  2526.61872667888

Given Radius of small r:  6.2  Radius of large R :  8.3
Volume of torus :  6297.83404675273``````

## Output Explanation

The code calculates the volume for each test case and displays the results. For a torus with radii 'r' = 3 and 'R' = 7, the volume is approximately 1243.570155. Similarly, for radii 'r' = 4 and 'R' = 8, the volume is approximately 2526.618727. The third test case follows the same pattern with a different set of radii values.

## Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations and the value of π², which are calculated in constant time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post