Posted on by Kalkicode
Code Geometric

Find the surface area of a torus

A torus is a three-dimensional geometric shape that resembles a donut or an inner tube. Finding the surface area of a torus involves calculating the total area of the curved surface that makes up its boundaries. This calculation is important in mathematics, engineering, and architecture, where tori are used to model various objects, such as pipes and jewelry.

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 surface area. The formula for finding the surface area of a torus in terms of its radii 'r' and 'R' is:

Surface Area = 4 * π² * R * r

Example Scenario

Imagine you are a designer working on creating a decorative bracelet in the shape of a torus. To determine the amount of material needed for the bracelet's outer surface, you need to calculate the surface area of the toroidal shape. This calculation helps you estimate the cost of materials and production.

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 surface area of the torus.
  3. Display the calculated surface area.

Pseudocode

function torus_surface_area(r, R):
    area = 4 * π² * R * r
    return area

main:
    r1 = 3
    R1 = 9
    
    r2 = 5
    R2 = 8
    
    r3 = 6.2
    R3 = 8.3
    
    area1 = torus_surface_area(r1, R1)
    area2 = torus_surface_area(r2, R2)
    area3 = torus_surface_area(r3, R3)
    
    print("Torus [ r :", r1, "R :", R1, "]")
    print("Surface Area :", area1)
    
    print("Torus [ r :", r2, "R :", R2, "]")
    print("Surface Area :", area2)
    
    print("Torus [ r :", r3, "R :", R3, "]")
    print("Surface Area :", area3)

Algorithm Explanation

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

Code Solution

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

#include <math.h>

//Calculate surface area of a torus by given small and large radius
void torus_surface_area(double r, double R)
{
	// Formula of torus area
	// 4 × π² × 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 surface area of torus
	double area = 4 * (M_PI * M_PI) * R * r;
	//Display surface area
	printf("\nSurface area of torus : %lf\n", area);
}
int main()
{
	//Simple Case
	torus_surface_area(3, 9);
	torus_surface_area(5, 8);
	torus_surface_area(6.2, 8.3);
	return 0;
}

Output

Given Radius of r: 3.000000 Radius of R : 9.000000
Surface area of torus : 1065.917275

Given Radius of r: 5.000000 Radius of R : 8.000000
Surface area of torus : 1579.136704

Given Radius of r: 6.200000 Radius of R : 8.300000
Surface area of torus : 2031.559370
// Java Program
// Find the surface area of a torus
class Torus
{
	//Calculate surface area of a torus by given small and large radius
	public void surface_area(double r, double R)
	{
		System.out.print("\nGiven Radius of r: " + r + " Radius of R : " + R + " ");
		// Formula of torus area
		// 4 × π² × R × r
		// here r is radius of small circle
		// R is radius of large circle
		//Calculate surface area of torus
		double area = 4 * (Math.PI * Math.PI) * R * r;
		System.out.print("\nSurface area of torus : " + area + "\n");
	}
	public static void main(String[] args)
	{
		Torus torus = new Torus();
		//Simple Case
		torus.surface_area(3, 9);
		torus.surface_area(5, 8);
		torus.surface_area(6.2, 8.3);
	}
}

Output

Given Radius of r: 3.0 Radius of R : 9.0
Surface area of torus : 1065.9172753176508

Given Radius of r: 5.0 Radius of R : 8.0
Surface area of torus : 1579.1367041742974

Given Radius of r: 6.2 Radius of R : 8.3
Surface area of torus : 2031.5593699202338
// C++ Program
// Find the surface area of a torus
#include<iostream>
#include<math.h>

using namespace std;
class Torus
{
	public:
		//Calculate surface area of a torus by given small and large radius
		void surface_area(double r, double R)
		{
			cout << "\nGiven Radius of r: " << r << " Radius of R : " << R << " ";
			// Formula of torus area
			// 4 × π² × R × r
			// here r is radius of small circle
			// R is radius of large circle
			//Calculate surface area of torus
			double area = 4 * (M_PI * M_PI) * R * r;
			cout << "\nSurface area of torus : " << area << "\n";
		}
};
int main()
{
	Torus torus;
	//Simple Case
	torus.surface_area(3, 9);
	torus.surface_area(5, 8);
	torus.surface_area(6.2, 8.3);
	return 0;
}

Output

Given Radius of r: 3 Radius of R : 9
Surface area of torus : 1065.92

Given Radius of r: 5 Radius of R : 8
Surface area of torus : 1579.14

Given Radius of r: 6.2 Radius of R : 8.3
Surface area of torus : 2031.56
// C# Program
// Find the surface area of a torus
using System;
class Torus
{
	//Calculate surface area of a torus by given small and large radius
	public void surface_area(double r, double R)
	{
		Console.Write("\nGiven Radius of r: " + r + " Radius of R : " + R + " ");
		// Formula of torus area
		// 4 × π² × R × r
		// here r is radius of small circle
		// R is radius of large circle
		//Calculate surface area of torus
		double area = 4 * (Math.PI * Math.PI) * R * r;
		Console.Write("\nSurface area of torus : " + area + "\n");
	}
	public static void Main(String[] args)
	{
		Torus torus = new Torus();
		//Simple Case
		torus.surface_area(3, 9);
		torus.surface_area(5, 8);
		torus.surface_area(6.2, 8.3);
	}
}

Output

Given Radius of r: 3 Radius of R : 9
Surface area of torus : 1065.91727531765

Given Radius of r: 5 Radius of R : 8
Surface area of torus : 1579.1367041743

Given Radius of r: 6.2 Radius of R : 8.3
Surface area of torus : 2031.55936992023
<?php
// Php Program
// Find the surface area of a torus
class Torus
{
	//Calculate surface area of a torus by given small and large radius
	public	function surface_area($r, $R)
	{
		echo "\nGiven Radius of r: ". $r ." Radius of R : ". $R ." ";
		// Formula of torus area
		// 4 × π² × R × r
		// here r is radius of small circle
		// R is radius of large circle
		//Calculate surface area of torus
		$area = 4 * (M_PI * M_PI) * $R * $r;
		echo "\nSurface area of torus : ". $area ."\n";
	}
}

function main()
{
	$torus = new Torus();
	//Simple Case
	$torus->surface_area(3, 9);
	$torus->surface_area(5, 8);
	$torus->surface_area(6.2, 8.3);
}
main();

Output

Given Radius of r: 3 Radius of R : 9
Surface area of torus : 1065.9172753177

Given Radius of r: 5 Radius of R : 8
Surface area of torus : 1579.1367041743

Given Radius of r: 6.2 Radius of R : 8.3
Surface area of torus : 2031.5593699202
#  Python 3 Program
#  Find the surface area of a torus
import math
class Torus :
	# Calculate surface area of a torus by given small and large radius
	def surface_area(self, r, R) :
		print("\nGiven Radius of r: ", r ," Radius of R : ", R ," ", end = "")
		#  Formula of torus area
		#  4 × π² × R × r
		#  here r is radius of small circle
		#  R is radius of large circle
		# Calculate surface area of torus
		area = 4 * (math.pi * math.pi) * R * r
		print("\nSurface area of torus : ", area ,"\n", end = "")
	

def main() :
	torus = Torus()
	# Simple Case
	torus.surface_area(3, 9)
	torus.surface_area(5, 8)
	torus.surface_area(6.2, 8.3)

if __name__ == "__main__": main()

Output

Given Radius of r:  3  Radius of R :  9
Surface area of torus :  1065.9172753176508

Given Radius of r:  5  Radius of R :  8
Surface area of torus :  1579.1367041742974

Given Radius of r:  6.2  Radius of R :  8.3
Surface area of torus :  2031.5593699202338
#  Ruby Program
#  Find the surface area of a torus
class Torus

	# Calculate surface area of a torus by given small and large radius
	def surface_area(s_r, l_r)
	
		print("\nGiven Radius of small r: ", s_r ," Radius of large R : ", l_r ," ")
		#  Formula of torus area
		#  4 × π² × R × r
		#  here r is radius of small circle
		#  R is radius of large circle
		# Calculate surface area of torus
		area = 4 * (Math::PI * Math::PI) * l_r * s_r
		print("\nSurface area of torus : ", area ,"\n")
	end
end
def main()

	torus = Torus.new()
	# Simple Case
	torus.surface_area(3, 9)
	torus.surface_area(5, 8)
	torus.surface_area(6.2, 8.3)
end
main()

Output

Given Radius of small r: 3 Radius of large R : 9 
Surface area of torus : 1065.9172753176508

Given Radius of small r: 5 Radius of large R : 8 
Surface area of torus : 1579.1367041742974

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

Output

Given Radius of r: 3.0 Radius of R : 9.0
Surface area of torus : 1065.9172753176508

Given Radius of r: 5.0 Radius of R : 8.0
Surface area of torus : 1579.1367041742974

Given Radius of r: 6.2 Radius of R : 8.3
Surface area of torus : 2031.5593699202338
import Foundation
// Swift Program
// Find the surface area of a torus
class Torus
{
	//Calculate surface area of a torus by given small and large radius
	func surface_area(_ r: Double, _ R: Double)
	{
		print("\nGiven Radius of r: ", r ," Radius of R : ", R ," ", terminator: "");
		// Formula of torus area
		// 4 × π² × R × r
		// here r is radius of small circle
		// R is radius of large circle
		//Calculate surface area of torus
		let area: Double = 4 * (Double.pi * Double.pi) * R * r;
		print("\nSurface area of torus : ", area ,"\n", terminator: "");
	}
}
func main()
{
	let torus: Torus? = Torus();
	//Simple Case
	torus!.surface_area(3, 9);
	torus!.surface_area(5, 8);
	torus!.surface_area(6.2, 8.3);
}
main();

Output

Given Radius of r:  3.0  Radius of R :  9.0
Surface area of torus :  1065.91727531765

Given Radius of r:  5.0  Radius of R :  8.0
Surface area of torus :  1579.1367041743

Given Radius of r:  6.2  Radius of R :  8.3
Surface area of torus :  2031.55936992023

Output Explanation

The code calculates the surface area for each test case and displays the results. For a torus with radii 'r' = 3 and 'R' = 9, the surface area is approximately 1065.917275. Similarly, for radii 'r' = 5 and 'R' = 8, the surface area is approximately 1579.136704. 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.

New Comment