#percentage calculator in java!!

1 messages · Page 1 of 1 (latest)

ashen radish
#

well i dont get correct answers and i dont know why pls edit my code and tell me about the error?

import java.util.Scanner;
public class percentage_calculator {
    public static void main(String[] args){
        System.out.println("Percentage Calculator");
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the total amount: ");
        int tamt = sc.nextInt();
        System.out.print("Enter the amount given: ");
        int amt = sc.nextInt();
        int perc = (amt/tamt)*100;
        System.out.print("The percentage is: " +perc+"%");
    }
}
misty loomBOT
#

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

misty loomBOT
#

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.

paper bison
deft pebble
#

Read the sections on integers and floating point numbers

#

but this one explains why you aren't getting the right result

ashen radish
ashen radish
#

bruh

#

yall shouldve just told me to replace int with float

slim nacelle
#

they guided u to where to look

ashen radish
#

it works

slim nacelle
#

also u dont even need a variable to begin with

#

calculation inside the print

ashen radish
#

i just used my own logic

ashen radish
misty loomBOT
#

Closed the thread.

paper bison
#

I would have continued if you said yes/no

deft pebble
#

at least not as deeply

paper bison
#

also no need getting mad getting help lol

hidden coral
#

The answer is correct in your program but not accurate since in cases where the answer comes in decimals because the entire program is in int.You can use double or float to correct this.Change the 12 no line as

#

double perc = (amt/tamt)*100.0;

#

Hope it works.If it isn't working ,knock me.

paper bison
#

that wont fix the issue though

#

if amt and tamt are still integers

#

it will still do integer division (amt/tamt)

hidden coral
#

then do it using decimal types only.The case here happening is integer/integer = integer and it adds just a zero.

slim nacelle
#

op closed the thread ages ago 😉