Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:40:53+00:00 2026-06-12T13:40:53+00:00

I am trying to make a sequence of code that calculates the volume of

  • 0

I am trying to make a sequence of code that calculates the volume of a cone based on a user input height and radius (see below).

My problem is that the answer comes out wrong (but not by a long way). Using the example of height = 5, radius = 10, I get an answer from the code of 500 (treating pi as exactly 3), but calculating manually I get ~523.

I’m assuming it has something to do with the variable type I am declaring pi under (double then converting to long) but I have struggled to make any other combination of variable types work.

What is the correct version of this below that will store pi properly (to at least 5 or 6 dec places)?

                double piDouble = Math.PI;

                long height = Long.parseLong(heightString);
                long pi = (new Double(piDouble)).longValue();
                long radius = Long.parseLong(radiusString);

                long volumeBase = (pi*(radius*radius)*height) / 3;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:40:55+00:00Added an answer on June 12, 2026 at 1:40 pm

    long pi = (new Double(piDouble)).longValue(); is a long, so it is equal to 3, exactly.

    If you need more precision, use double all the way, and round at the end only. This should give you the result you expect:

    double piDouble = Math.PI;
    long height = Long.parseLong(heightString);
    long radius = Long.parseLong(radiusString);
    double volumeBase = (piDouble * (radius * radius) * height) / 3;
    

    You can then round the result if you need to:

    long roundedDownVolume = (long) volumeBase; //round down
    long roundedVolume = Math.round(volumeBase); //or round to the closest long
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a code that does the following: Multiplying the digits
I'm trying to make video output (sequence of frames) to any qt visible widget.
I'm trying make a login window where a user is prompted to enter their
I'm trying make an entity with doctrine that has three associations with other entities
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to make an xna game, where the user needs to tap to stop
I'm trying to make a Procedure that will duplicate multiple rows of a table
I'm trying to create a source filter that makes a live video stream based
I'm in trouble trying to make this code to work. What I'm trying to
So I'm trying to make a class that extends list, with the extra ability

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.