Question

Write a java code prompting the user to input the number of balls they want to...

Write a java code prompting the user to input the number of balls they want to buy and also input those ball's radius. Once the input has been prompted, print out their volumes and surface areas.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

import java.text.DecimalFormat;
import java.util.Scanner;

public class VolAndSurA {

public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("####0.00"); // round upto 2 decimal places
int count;
double pi = 3.14;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of balls : ");
count = s.nextInt();
float[] r = new float[count];
double[] area = new double[count];
double[] volume = new double[count];

for (int i = 0; i < count; i++) {
System.out.print("Enter the radius of sphere:" + (i + 1) + "# ");
r[i] = s.nextFloat();
area[i] = 3 * pi * r[i] * r[i];
volume[i] = 1.333 * pi * r[i] * r[i] * r[i];
}
System.out.println("");
for (int i = 0; i < count; i++) {
System.out.println("Surface Area of sphere of radius : " + r[i] + " is : " + df.format(area[i]));
System.out.println("Volume of sphere of radius : " + r[i] + " is : " + df.format(volume[i]));
System.out.println("");
}
  
}
}

Output:

Enter number of balls2 Enter the radius f sphere: 1# 25.25 Enter the radius f sphere: 2# 68.25 Surface Area of sphere of radi

Add a comment
Know the answer?
Add Answer to:
Write a java code prompting the user to input the number of balls they want to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT