#Output is wrong

1 messages · Page 1 of 1 (latest)

fossil pumice
#

I was trying to calculate the area of circle and volume of sphere.I entered the radius and it showed the wrong answer. I had also typed the syntax correct than to also it was showing wrong. Pls someone help me

tribal daggerBOT
#

<@&987246399047479336> please have a look, thanks.

tribal daggerBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

fossil pumice
#
System.out.println(" Area of circle = " + carea); ```
#

This is for area of circle

scarlet estuary
#

It looks like the 22/7 is integer division, so the decimal there is being lost. Is r also an int by chance?

#

When you divide ints in Java, the result is also an int (Java just scraps the decimal if there is one).

fossil pumice
#

I have declared r as an double

scarlet estuary
#

Oh okay.

fossil pumice
#

And the 22/7 is the value of π

#

The formula is πr²

scarlet estuary
#

I'm not sure what the current output is but I'm assuming the issue is with the 22/7. Can you try making one of those a double value to see if it keeps the decimal? Like this:java carea=(22d/7)*r*r; System.out.println(" Area of circle = " + carea);(The d suffix makes the number 22 a double value instead of just an int.)

scarlet estuary
fossil pumice
#

It means I need to initialise d=22;

fossil pumice
scarlet estuary
fossil pumice
#

I am actually having my dinner

#

Can I send you later?

scarlet estuary
#

That's okay.

scarlet estuary
fossil pumice
#

Ok

fossil pumice
#

Here is the code

true ruin
#

You didn't use the discussed double cast. I dont think you're following what he meant by integer division.
Also you're told to use 22/7 and not the predefined Math.PI? (3.1415926....)

fossil pumice
#

I used 22/7 only

#

Help me

#

I have to complete this by tomm

fossil pumice
tidal fulcrum
fossil pumice
#

I can't

#

I am supposed to use the given value

#

The value given is 22/7

ruby marlin
#

is the output still wrong ?

true ruin
#

Its definitely wrong because 22/7 = 3

#

Integer division truncates any decimals (which would be obtained with modulus (%) )

#

You need to cast it to a double or use a double literal such as 22.0

ruby marlin
#

or 22d

true ruin
#

22.0 / 7 or 22d / 7 will convert both operands to doubles and perform 22.0 / 7.0 which gives a floating point answer

fossil pumice
ruby marlin
#

no if u place a d behind an int, it will be treated as a double

true ruin
#

? d is the literal syntax to denote a double

fossil pumice
#

I will try

#

Can I use import java.math.*;

#

For int calculations

true ruin
#

What int calculation do u need from that?

#

like Math.pow()?

fossil pumice
#

No

#

At the starting

true ruin
#

if you're referring to the top of the file, that is where imports go, yes, but why do you need the math library

#

Also java.math.* are math classes like BigInteger. java.lang.Math* is the math constants

#

int/double division examples:

        double r = 4;
        //Incorrect calculations
        System.out.println(22/7 * r * r); //wrong
        System.out.println((double)(22/7) * r * r); //wrong
        // The following are correct division
        System.out.println(22d/7 * r * r);
        System.out.println(22.0/7 * r * r);
        System.out.println(22/7d * r * r);
        System.out.println(22/7.0 * r * r);
        System.out.println((double) 22/7 * r * r);
        System.out.println(22/(double)7 * r * r);
        System.out.println((double) 22/7 * r * r);```
fossil pumice
#

Ok I will try any of these

#

Thank you

#

For helping

#

If still prblm appears what to do ?

fossil pumice
true ruin
#

Yeah

#

Its just the pi part thats messed up

#

That r=4 is a placeholder

fossil pumice
#

Thank you

fossil pumice
tribal daggerBOT
#

Closed the thread.

fossil pumice
#

Hello

#

My output is again coming wrong for another question

#

Not the area & volume one

#

My program is to calculate relative velocity and time taken to cross

#

The output coming was velocity as 0.0 and time as infinity

#

Here is the code

keen roost
#

@fossil pumice use the snipping tool to take screenshots

#

not your phone

fossil pumice
#

Ye

#

But now help me

tulip timber
#

the image is not readable on mobile for example

fossil pumice
#

Ok

scarlet estuary
#

It looks like the default label does not apply to any statement.

fossil pumice
#

Lemme type

fossil pumice
scarlet estuary
#

Ah, nevermind. It's not.

fossil pumice
#

It's not a compilation error

scarlet estuary
#

I suppose there's special syntax for switch blocks that lets you write a default or case label at the very end.

fossil pumice
scarlet estuary
fossil pumice
#

In my 2 yr experience I have only studied switch(variable),case, default,break syntax

#

For switch case prgms

#

In other prgms it's showing correct output by adding default:

#
{
    case '1' :
    speed=Math.abs(s1-s2);
    break;
    case '2' :
    speed=s1+s2;
    break;
    default:
} ```
fossil pumice
#

Actually I have taken choice as integer

#

Should I take string for choice ?

true ruin
#

U shouldnt compare an int to a char. '1' is a char and will be converted to the ascii value

fossil pumice
#

Ok

austere marten
fossil pumice
#

The latest one

austere marten
fossil pumice
#

Can you give me the another switch

austere marten
fossil pumice
#

I put speed as 50

#

For train 1

#

And for train 2 it's 40

#

Should I give question

#

?

austere marten
#
switch(choice){
    case '1' -> speed = Math.abs(s1-s2);
    case '2' -> speed = s1+s2;
    default -> {}
}

Or better :

speed = switch(choice){
    case '1' -> Math.abs(s1-s2);
    case '2' -> s1+s2;
    default -> speed;
}
#

@fossil pumice

fossil pumice
#

I am back

#

I got my prblm

#

I have resolved

#

Thank you for helping me

#

Actually I had declared choice as an integer

tribal daggerBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍