Write a program to find the volume of the sphere.
Input: radius=5
Output: 392.5
Approach: Given the radius of the sphere.
Volume is PI*(4/3)*radius*radius*radius
Java
public class VolumeofSphere {static final double PI = 3.14;public static void main(String[] args) {double radius = 5;double volume = volumeSphere(radius);System.out.println("Volume of sphere is " + volume);}private static double volumeSphere(double radius) {return PI * (4 / 3) * radius * radius * radius;}}
C++
#include <bits/stdc++.h>using namespace std;#define PI 3.14//function to find the volume//of spheredouble volumeSphere(double radius){double volume=PI*(4/3)*radius*radius*radius;return volume;}int main(){double radius=5;double volume=volumeSphere(radius);cout<<"Volume of sphere is ";cout<<volume<<"\n";return 0;}
No comments:
Post a Comment