1. Member
Join Date
Jan 2011
Posts
7
Rep Power
0

## Please need help asap

I need to make a program that can count how much money you get back in different money notes, i get this error: Exception in thread "main" java.lang.ArithmeticException: / by zero

can somebody please help me i dont have that much time :(

Java Code:
```import java.util.Scanner;
public class Uppgift222 {
public static void main(String args[]){

Scanner räkna = new Scanner(System.in);
System.out.println("Var snäll och ange summan på varan: ");
summa = räkna.nextInt();
System.out.println("Summan: " + summa);
System.out.println("Hur mycket betalade du: ");
fåtillbaka = betalade - summa;
System.out.println("Att få tillbaka : " + fåtillbaka);

int hundralapp = 0;//100 kr
int femtiolapp = 0;// 50 kr
int tjugor = 0;//20 kr
int tior = 0;//10 kr
int femmor = 0;//5 kr
int enkronor = 0;//1 kr

hundralapp=fåtillbaka/100;
femtiolapp = (fåtillbaka%(hundralapp*100))/50;
tjugor = (fåtillbaka%(femtiolapp*50))/20;
tior = (fåtillbaka%(tjugor*20))/10;
femmor = (fåtillbaka%(tior*50))/5;
enkronor = (fåtillbaka%(femmor*5))/1;

System.out.println("så här många hundralappar " + hundralapp);
System.out.println("så här många femtiolappar " + femtiolapp);
System.out.println("så här många tjugolappar " + tjugor);
System.out.println("så här många tior " + tior);
System.out.println("så här många femmor " + femmor);
System.out.println("så här många enkronor " + enkronor);
}
}```

2. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
7
Java Code:
`femtiolapp = (fåtillbaka%(hundralapp*100))/50;`
Whenever hundralapp is 0, this will cause a runtime error. You want either an if statement or a ?: operation to make sure it does the right thing with a denominator of zero.

3. Originally Posted by Tamtome
I need to make a program that can count how much money you get back in different money notes, i get this error: Exception in thread "main" java.lang.ArithmeticException: / by zero

can somebody please help me i dont have that much time :(
I don't have much time either so you should print the value of each variable before you do any calculation with it and see which one equals zero.

kind regards,

Jos

4. in java, when you do something like that

System.out.println(100 % 0);

you will get the exception java.lang.ArithmeticException: / by zero. so check if one of the reusult of your terms is 0.

5. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
thanks for the replies! j2me64, how do i check that?

6. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
this line is giving me error: tjugor = (fåtillbaka%(femtiolapp*50))/20;

7. Originally Posted by Tamtome
this line is giving me error: tjugor = (fåtillbaka%(femtiolapp*50))/20;
If it was an Arithmetic error, variable 'femtiolapp' was zero. Did you implement my suggestion and print all variables? If so, you could've seen what was wrong.

kind regards,

Jos

8. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
jos, im trying to use this: if (!(hundralapp<= 0)) {

but now im having problems with they money notes, do u know how i can fix it?

Java Code:
```import java.util.Scanner;
public class Test222 {
public static void main(String args[]){

Scanner räkna = new Scanner(System.in);
System.out.println("Var snäll och ange summan på varan: ");
summa = räkna.nextInt();
System.out.println("Summan: " + summa);
System.out.println("Hur mycket betalade du: ");
fåtillbaka = betalade - summa;
System.out.println("Att få tillbaka : " + fåtillbaka);

int hundralapp = 0;
int femtiolapp = 0;
int tjugor = 0;
int tior = 0;
int femmor = 0;
int enkronor = 0;

if (!(hundralapp<= 0)) {
hundralapp=fåtillbaka/100;

femtiolapp = (fåtillbaka%(hundralapp*100))/50;
tjugor = (fåtillbaka%(femtiolapp*50))/20;
tior = (fåtillbaka%(tjugor*20))/10;
femmor = (fåtillbaka%(tior*50))/5;
enkronor = (fåtillbaka%(femmor*5))/1;
}
System.out.println("så här många hundralappar " + hundralapp);
System.out.println("så här många femtiolappar " + femtiolapp);
System.out.println("så här många tjugolappar " + tjugor);
System.out.println("så här många tior " + tior);
System.out.println("så här många femmor " + femmor);
System.out.println("så här många enkronor " + enkronor);

}

}```

9. Originally Posted by Tamtome
jos, im trying to use this: if (!(hundralapp<= 0)) {

but now im having problems with they money notes, do u know how i can fix it?
What are those 'problems'? Please be more explicit and don't make us guess.

kind regards,

Jos

10. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
im sorry i didnt mean to! im gonna try to explain, now it only shows how much money you get back but not how many money notes, instead of showing how much change u get it only shows zero. it prints like this

Var snäll och ange summan på varan:
300
Summan: 300
Hur mycket betalade du:
500
Att få tillbaka : 200
så här många hundralappar 0
så här många femtiolappar 0
så här många tjugolappar 0
så här många tior 0
så här många femmor 0
så här många enkronor 0

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•