Program to Compute Volume and Surface Area of a Cone

Program to Compute Volume and Surface Area of a Cone

  • Write a program to Compute Volume and Surface Area of a Cone in C
  • Write a program to Compute Volume and Surface Area of a Cone in C++
  • Write a program to Compute Volume and Surface Area of a Cone in Python
  • Write a program to Compute Volume and Surface Area of a Cone in PHP
  • Write a program to Compute Volume and Surface Area of a Cone in Java
  • Write a program to Compute Volume and Surface Area of a Cone in Java Script
  • Write a program to Compute Volume and Surface Area of a Cone in C#

Explanation:

A cone is a three-dimensional geometric shape with a circular base that tapers smoothly to a point called the apex.

Formulas:

Volume (V): 1/3 x PI x r2 x h

Surface Area (SA): Surface Area = PI x r (r + l)

r = Radius of the base

h = Height of the cone

l = Slant height of the cone, calculated as: l = sqrt(r2 + l2)

Program to Compute Volume and Surface Area of a Cone

#include <stdio.h>
#include <math.h>

int main() {
    float radius, height, slantHeight, volume, surfaceArea;

    printf("Enter the radius of the cone: ");
    scanf("%f", &radius);
    printf("Enter the height of the cone: ");
    scanf("%f", &height);

    slantHeight = sqrt(radius * radius + height * height);
    volume = (1.0 / 3.0) * M_PI * radius * radius * height;
    surfaceArea = M_PI * radius * (radius + slantHeight);

    printf("Volume: %.2f\n", volume);
    printf("Surface Area: %.2f\n", surfaceArea);

    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    float radius, height, slantHeight, volume, surfaceArea;

    cout << "Enter the radius of the cone: ";
    cin >> radius;
    cout << "Enter the height of the cone: ";
    cin >> height;

    slantHeight = sqrt(radius * radius + height * height);
    volume = (1.0 / 3.0) * M_PI * radius * radius * height;
    surfaceArea = M_PI * radius * (radius + slantHeight);

    cout << "Volume: " << volume << endl;
    cout << "Surface Area: " << surfaceArea << endl;

    return 0;
}

import math

radius = float(input("Enter the radius of the cone: "))
height = float(input("Enter the height of the cone: "))

slant_height = math.sqrt(radius**2 + height**2)
volume = (1 / 3) * math.pi * radius**2 * height
surface_area = math.pi * radius * (radius + slant_height)

print(f"Volume: {volume}")
print(f"Surface Area: {surface_area}")

<?php
$radius = (float)readline("Enter the radius of the cone: ");
$height = (float)readline("Enter the height of the cone: ");

$slantHeight = sqrt($radius**2 + $height**2);
$volume = (1 / 3) * pi() * $radius**2 * $height;
$surfaceArea = pi() * $radius * ($radius + $slantHeight);

echo "Volume: $volume\n";
echo "Surface Area: $surfaceArea\n";
?>

import java.util.Scanner;

public class Cone {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the radius of the cone: ");
        double radius = sc.nextDouble();
        System.out.print("Enter the height of the cone: ");
        double height = sc.nextDouble();

        double slantHeight = Math.sqrt(radius * radius + height * height);
        double volume = (1.0 / 3.0) * Math.PI * radius * radius * height;
        double surfaceArea = Math.PI * radius * (radius + slantHeight);

        System.out.println("Volume: " + volume);
        System.out.println("Surface Area: " + surfaceArea);
    }
}

const radius = parseFloat(prompt("Enter the radius of the cone: "));
const height = parseFloat(prompt("Enter the height of the cone: "));

const slantHeight = Math.sqrt(radius ** 2 + height ** 2);
const volume = (1 / 3) * Math.PI * radius ** 2 * height;
const surfaceArea = Math.PI * radius * (radius + slantHeight);

console.log(`Volume: ${volume}`);
console.log(`Surface Area: ${surfaceArea}`);

using System;

class Program {
    static void Main() {
        Console.Write("Enter the radius of the cone: ");
        double radius = double.Parse(Console.ReadLine());
        Console.Write("Enter the height of the cone: ");
        double height = double.Parse(Console.ReadLine());

        double slantHeight = Math.Sqrt(radius * radius + height * height);
        double volume = (1.0 / 3.0) * Math.PI * radius * radius * height;
        double surfaceArea = Math.PI * radius * (radius + slantHeight);

        Console.WriteLine("Volume: " + volume);
        Console.WriteLine("Surface Area: " + surfaceArea);
    }
}

List of All Programs