The following Code is to get the possible number of notes within a particular amount.
Code:-
import java.util.*;
public class NoteNumber{
Scanner scan=new Scanner(System.in);
int amount,amt, r1, r2, r5, r10, r20, r50, r100, r500, r1000;
public void input(){
try{
System.out.println("Enter the Total Amount: ");
amount=scan.nextInt();
}
catch(NumberFormatException e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
r1000=r500=r100=r50=r20=r10=r5=r2=r1=0;
amt=amount;
r1000=amount/1000;
amount%=1000;
r500=amount/500;
amount%=500;
r100=amount/100;
amount%=100;
r50=amount/50;
amount%=50;
r20=amount/20;
amount%=20;
r10=amount/10;
amount%=10;
r5=amount/5;
amount%=5;
r2=amount/2;
amount%=2;
r1=amount/1;
}
public void notes(){
System.out.println("Total Amount: "+amt);
System.out.println("Notes of 1000: "+r1000);
System.out.println("Notes of 500: "+r500);
System.out.println("Notes of 100: "+r100);
System.out.println("Notes of 50: "+r50);
System.out.println("Notes of 20: "+r20);
System.out.println("Notes of 10: "+r10);
System.out.println("Notes of 5: "+r5);
System.out.println("Notes of 2: "+r2);
System.out.println("Notes of 1: "+r1);
}
public static void main(String[] args){
NoteNumber note=new NoteNumber();
note.input();
note.compute();
note.notes();
}
}
Output:-
Enter the Total Amount:
29187
Total Amount: 29187
Notes of 1000: 29
Notes of 500: 0
Notes of 100: 1
Notes of 50: 1
Notes of 20: 1
Notes of 10: 1
Notes of 5: 1
Notes of 2: 1
Notes of 1: 0
Code:-
import java.util.*;
public class NoteNumber{
Scanner scan=new Scanner(System.in);
int amount,amt, r1, r2, r5, r10, r20, r50, r100, r500, r1000;
public void input(){
try{
System.out.println("Enter the Total Amount: ");
amount=scan.nextInt();
}
catch(NumberFormatException e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
r1000=r500=r100=r50=r20=r10=r5=r2=r1=0;
amt=amount;
r1000=amount/1000;
amount%=1000;
r500=amount/500;
amount%=500;
r100=amount/100;
amount%=100;
r50=amount/50;
amount%=50;
r20=amount/20;
amount%=20;
r10=amount/10;
amount%=10;
r5=amount/5;
amount%=5;
r2=amount/2;
amount%=2;
r1=amount/1;
}
public void notes(){
System.out.println("Total Amount: "+amt);
System.out.println("Notes of 1000: "+r1000);
System.out.println("Notes of 500: "+r500);
System.out.println("Notes of 100: "+r100);
System.out.println("Notes of 50: "+r50);
System.out.println("Notes of 20: "+r20);
System.out.println("Notes of 10: "+r10);
System.out.println("Notes of 5: "+r5);
System.out.println("Notes of 2: "+r2);
System.out.println("Notes of 1: "+r1);
}
public static void main(String[] args){
NoteNumber note=new NoteNumber();
note.input();
note.compute();
note.notes();
}
}
Output:-
Enter the Total Amount:
29187
Total Amount: 29187
Notes of 1000: 29
Notes of 500: 0
Notes of 100: 1
Notes of 50: 1
Notes of 20: 1
Notes of 10: 1
Notes of 5: 1
Notes of 2: 1
Notes of 1: 0
Comments