Computer Science
A bank intends to design a program to display the denomination of an input amount, up to 5 digits. The available denomination with the bank are of rupees 2000, 500, 200, 100, 50, 20, 10 and 1.
Design a program to accept the amount from the user and display the break-up in descending order of denominations. (i.e., preference should be given to the highest denomination available) along with the total number of notes.
[Note: Only the denomination used should be displayed].
Also print the amount in words according to the digits.
Example 1:
Input:
14836
Output:
One Four Eight Three Six
Denomination:
2000 * 7 = 14000
500 * 1 = 500
200 * 1 = 200
100 * 1 = 100
20 * 1 = 20
10 * 1 = 10
1 * 6 = 6
Example 2:
Input:
235001
Output:
Invalid Amount
Java
Java Arrays
12 Likes
Answer
import java.util.Scanner;
public class KboatDenominations
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the amount: ");
int amt = in.nextInt();
if (amt > 99999) {
System.out.println("Invalid Amount");
return;
}
String amtInWords = getAmtInWords(amt);
System.out.println(amtInWords);
System.out.println("Denomination:");
int notes[] = {2000, 500, 200, 100, 50, 20, 10, 1};
int t = amt;
for (int i = 0; i < notes.length; i++) {
int c = t / notes[i];
if (c != 0)
System.out.println(notes[i] + "\t*\t"
+ c + "\t=\t" + (c * notes[i]));
t = t % notes[i];
}
}
public static String getAmtInWords(int amt) {
StringBuffer sb = new StringBuffer();
while (amt != 0) {
int d = amt % 10;
amt /= 10;
switch (d) {
case 0:
sb.insert(0, "Zero ");
break;
case 1:
sb.insert(0, "One ");
break;
case 2:
sb.insert(0, "Two ");
break;
case 3:
sb.insert(0, "Three ");
break;
case 4:
sb.insert(0, "Four ");
break;
case 5:
sb.insert(0, "Five ");
break;
case 6:
sb.insert(0, "Six ");
break;
case 7:
sb.insert(0, "Seven ");
break;
case 8:
sb.insert(0, "Eight ");
break;
case 9:
sb.insert(0, "Nine ");
break;
default:
System.out.println("Invalid digit");
}
}
return sb.toString();
}
}
Output
Answered By
2 Likes
Related Questions
A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons used in descending order of capacity (i.e. preference should be given to the highest capacity available, and if boxes left are less than 6, an extra carton of capacity 6 should be used.)
Test your program with the following data and some random data:
Example 1
INPUT:
N = 726OUTPUT:
48 * 15 = 720
6 * 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16Example 2
INPUT:
N = 140OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6 * 1 = 6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6Example 3
INPUT:
N = 4296OUTPUT:
INVALID INPUTWrite a program to accept a set of n integers (where n > 0) in a single dimensional array. Arrange the elements of the array such that the lowest number appears in the centre of the array, next lower number in the right cell of the centre, next lower in the left cell of the centre and so on… . The process will stop when the highest number will set in its appropriate cell. Finally, display the array elements.(Pendulum Arrangement of Array)
Assume that the memory space is less. Hence, you don't need to create extra array for the aforesaid task.
Example:
Input: 1 2 3 4 5
Output: 5 3 1 2 4Input: 11 12 31 14 5
Output: 31 12 5 11 14The encryption of letters are to be done as follows:
A = 1
B = 2
C = 3 . . .
Z = 26The potential of a word is found by adding the encrypted value of the letters.
Example: KITE
Potential = 11 + 9 + 20 + 5 = 45Accept a sentence which is terminated by either " . " , " ? " or " ! ". Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ~~alphabetical~~ ~~order~~ increasing order of their potential. Output the result in the format given below:
Example 1
Input:
THE SKY IS THE LIMIT.Potential:
THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63Output:
IS THE THE SKY LIMITExample 2
Input:
LOOK BEFORE YOU LEAP.Potential:
LOOK = 53
BEFORE = 51
YOU = 61
LEAP = 34Output:
LEAP BEFORE LOOK YOUWrite a program to input and store n integers (n > 0) in a single subscripted variable and print each number with its frequency. The output should contain number and its frequency in two different columns.
Sample Input:
12 20 14 12 12 20 16 16 14 14 12 20 18 18 Sample Output:
Number Frequency 12 4 14 3 16 2 18 2 20 3