0% found this document useful (0 votes)
12 views15 pages

Denomination and Kaprekar Number Programs

AI

Uploaded by

xagopen600
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Denomination and Kaprekar Number Programs

AI

Uploaded by

xagopen600
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 1

A bank intends to design a program to display the denomination of an input amount, up to 5 digits.
The available denomination with the banks are of rupees 1000, 500, 100, 50, 20, 10, 5, 2, and 1.

Design a program to accept the amount from the user and display the break-up in descending
order of denomination. (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:
14856
OUTPUT:
ONE FOUR EIGHT FIVE SIX
DENOMINATION:
1000 X 14 = 14000
500 X 1 = 500
100 X 3 = 300
50 X 1 = 50
5 X 1 = 5
1 X 1 = 1
TOTAL = 14856
TOTAL NUMBER OF NOTES = 21
Example 2
INPUT:
6043
OUTPUT:
SIX ZERO FOUR THREE
DENOMINATION:
1000 X 6 = 6000
20 X 2 = 40
2 X 1 = 2
1 X 1 = 1
TOTAL = 6043
TOTAL NUMBER OF NOTES = 10
Example 3
INPUT:
235001
OUTPUT:
INVALID AMOUNT
Program
import [Link].*;

class Denomination {
int a, t, c, temp;
String w[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};
int n[] = {1000, 500, 100, 50, 20, 10, 5, 2, 1};

Denomination(int amount) {
a = amount;
}

void check() {
String s = "" + a;
if ([Link]() > 5) {
[Link]("Invalid Input");
[Link](0);
}
}

void process() {
t = 0;
c = 0;
temp = a;
for (int i = 0; i < [Link]; i++) {
int d = temp / n[i];
if (d > 0) {
[Link](n[i] + "\tX\t" + d + "\t=\t" + (n[i] * d));
t += n[i] * d;
c += d;
temp %= n[i];
}
}
}

void toWords(int num) {


if (num == 0) {
return;
}
toWords(num / 10);
[Link](w[num % 10] + " ");
}

void display() {
toWords(a);
[Link]();
[Link]("DENOMINATION:");
process();
[Link]("TOTAL\t\t\t=\t" + t);
[Link]("TOTAL NUMBER OF NOTES\t=\t" + c);
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the amount: ");
int amount = [Link]();
Denomination obj = new Denomination(amount);
[Link]();
[Link]();
}
}
Algorithm
Step 1 : START
Step 2 : INITIALIZE string array w with number words from ZERO to NINE
Step 3 : INITIALIZE integer array n with denominations
Step 4 : In the main method, READ the amount
Step 5 : CONVERT amount to string s
Step 6 : IF length of s is greater than 5 THEN
Step 7 : DISPLAY "INVALID AMOUNT"
Step 8 : ELSE
Step 9 : CREATE an object of the class and pass the amount to the constructor
Step 10 : CALL the display method
Step 11 : ENDIF
Step 12 : In the constructor, assign the received amount to the global variable a
Step 13 : In the display method, CALL the toWords method with a
Step 14 : In the toWords method, IF the number is 0 THEN RETURN
Step 15 : CALL toWords recursively with number/10
Step 16 : DISPLAY the word for number%10
Step 17 : Back in the display method, CALL the process method
Step 18 : In the process method, INITIALIZE total t = 0 and total count c = 0
Step 19 : CREATE a temporary variable temp = a
Step 20 : DISPLAY "DENOMINATION:"
Step 21 : FOR i from 0 to 8
Step 22 : CALCULATE d = temp / n[i]
Step 23 : IF d > 0 THEN
Step 24 : DISPLAY denomination, count and product
Step 25 : UPDATE t = t + (n[i] * d)
Step 26 : UPDATE c = c + d
Step 27 : UPDATE temp = temp % n[i]
Step 28 : ENDIF
Step 29 : ENDFOR
Step 30 : DISPLAY total amount t
Step 31 : DISPLAY total number of notes c
Step 32 : STOP
Variable Description
Data Type Variable Name Description

int a Stores the user-entered amount.

String[] w Array to store number names from 'ZERO'

int[] n Array to store the available denominations

int t Stores the total calculated amount from de

int c Stores the total number of notes.

int d Stores the count of notes for a specific den

int i Loop control variable.

Denomination obj Object of the Denomination class.

Input/Output
Test 1
INPUT:
14856
OUTPUT:
ONE FOUR EIGHT FIVE SIX
DENOMINATION:
1000 X 14 = 14000
500 X 1 = 500
100 X 3 = 300
50 X 1 = 50
5 X 1 = 5
1 X 1 = 1
TOTAL = 14856
TOTAL NUMBER OF NOTES = 21
Test 2
INPUT:
6043
OUTPUT:
SIX ZERO FOUR THREE
DENOMINATION:
1000 X 6 = 6000
20 X 2 = 40
2 X 1 = 2
1 X 1 = 1
TOTAL = 6043
TOTAL NUMBER OF NOTES = 10
Test 3
INPUT:
235001
OUTPUT:
INVALID AMOUNT
Question 2
A positive whole number 'n' that has 'd' number of digits is squared and split into two pieces, a
right-hand piece that has 'd' digits and a left-hand piece that has remaining 'd' or 'd-1' digits. If the
sum of the two pieces is equal to the number, then 'n' is a Kaprekar number.

The first few Kaprekar numbers are: 9, 45, 297

Given the two positive integers p and q, where p<q, write a program to determine how many
Kaprekar numbers are there in the range between p and q (both inclusive) and output them.

The input contains two positive integers p and q. Assume p < 5000 and q < 5000. You are to
output the number of Kaprekar numbers in the specified range along with their values in the format
specified below:

Example 1:
9
9^2 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number.
SAMPLE DATA:
INPUT:
p=1
q = 1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:-
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8
Program
import [Link].*;

class Kaprekar {
int p, q, c;
String r;

Kaprekar(int lower, int upper) {


p = lower;
q = upper;
}

void check() {
if (p > q || p > 5000 || q > 5000 || p <= 0) {
[Link]("Invalid Input");
[Link](0);
}
}

boolean isKaprekar(int n) {
if (n == 1) {
return true;
}
long s = (long) n * n;
String ns = "" + n;
int d = [Link]();
String ss = "" + s;
int len = [Link]();
if (len < d * 2 - 1) {
return false;
}
int right = [Link]([Link](len - d));
int left = [Link]([Link](0, len - d));
return (left + right == n);
}

void process() {
c = 0;
r = "";
for (int i = p; i <= q; i++) {
if (isKaprekar(i)) {
if (c > 0) {
r += ", ";
}
r += i;
c++;
}
}
}

void display() {
[Link]("THE KAPREKAR NUMBERS ARE:-");
[Link](r);
[Link]("FREQUENCY OF KAPREKAR NUMBERS IS: " + c);
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the lower limit: ");
int lower = [Link]();
[Link]("Enter the upper limit: ");
int upper = [Link]();
Kaprekar obj = new Kaprekar(lower, upper);
[Link]();
[Link]();
[Link]();
}
}
Algorithm
Step 1 : START
Step 2 : In the main method, READ the lower limit p and upper limit q
Step 3 : IF p > q OR p > 5000 OR q > 5000 OR p <= 0 THEN
Step 4 : DISPLAY "INVALID INPUT"
Step 5 : ELSE
Step 6 : CREATE an object of the class and pass p and q to the constructor
Step 7 : CALL the process method
Step 8 : ENDIF
Step 9 : In the constructor, assign received limits to global variables p and q
Step 10 : In the process method, INITIALIZE count c = 0 and result string
Step 11 : DISPLAY "THE KAPREKAR NUMBERS ARE:-"
Step 12 : FOR i from p to q
Step 13 : CALL isKaprekar method with i
Step 14 : SET k = value returned from isKaprekar(i)
Step 15 : IF k is true THEN
Step 16 : APPEND i to the result string, with a comma if not the first number
Step 17 : INCREMENT c
Step 18 : ENDIF
Step 19 : ENDFOR
Step 20 : DISPLAY the result string
Step 21 : DISPLAY the frequency c
Step 22 : In the isKaprekar method with argument n,
Step 23 : CALCULATE square s = n * n
Step 24 : CALCULATE number of digits d in n
Step 25 : CALCULATE the right part r from the last d digits of s
Step 26 : CALCULATE the left part l from the remaining digits of s
Step 27 : IF l + r is equal to n THEN
Step 28 : RETURN true
Step 29 : ELSE
Step 30 : RETURN false
Step 31 : ENDIF
Step 32 : STOP
Variable Description
Data Type Variable Name Description

int p Stores the lower limit of the range.

int q Stores the upper limit of the range.

int n The number being checked in isKaprekar m

long s Stores the square of the number n.

int d Stores the number of digits in n.

int r Stores the right-hand part of the squared n

int l Stores the left-hand part of the squared nu

int c Stores the frequency of Kaprekar numbers

int i Loop control variable.

Kaprekar obj Object of the Kaprekar class.

Input/Output
Test 1
INPUT:
p=1
q = 1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:-
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8
Test 2
INPUT:
p = 400
q = 1500
OUTPUT:
THE KAPREKAR NUMBERS ARE:-
703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 2
Question 3
Input a paragraph containing 'n' number of sentences where (1 <= n < 4). The words are to be
separated with a single blank space and are in UPPERCASE. A sentence may be terminated
either with a full stop '.' Or a question mark '?' only. Any other character may be ignored. Perform
the following operations:

(i) Accept the number of sentences. If the number of sentences exceeds the limit, an
appropriate error message must be displayed.

(ii) Find the number of words in the whole paragraph

(iii) Display the words in ascending order of their frequency. Words with same frequency
may appear in any order.

Example 1
INPUT:
Enter number of sentences.
1
Enter sentences.
TO BE OR NOT TO BE.
OUTPUT:
Total number of words: 6
WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2
Program
import [Link].*;
import [Link].*;

class FrequencyWord {
String s;
String w[];
int f[];
int n;

FrequencyWord(String para) {
s = para;
n = 0;
}

void check(int num) {


if (num < 1 || num >= 4) {
[Link]("Invalid Input");
[Link](0);
}
}

void extractWords() {
int len = [Link]();
String temp = "";
int count = 0;

// Count total words and store unique words with frequency


for (int i = 0; i < len; i++) {
char ch = [Link](i);
if (ch != ' ' && ch != '.' && ch != '?') {
temp += ch;
} else {
if ([Link]() > 0) {
count++;
storeWord(temp);
temp = "";
}
}
}
if ([Link]() > 0) {
count++;
storeWord(temp);
}
[Link]("Total number of words: " + count);
}

void storeWord(String word) {


for (int i = 0; i < n; i++) {
if (w[i].equals(word)) {
f[i]++;
return;
}
}
w[n] = word;
f[n] = 1;
n++;
}

void sort() {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (f[j] > f[j + 1]) {
int tf = f[j];
f[j] = f[j + 1];
f[j + 1] = tf;
String ts = w[j];
w[j] = w[j + 1];
w[j + 1] = ts;
}
}
}
}

void display() {
[Link]("WORD\tFREQUENCY");
for (int i = 0; i < n; i++) {
[Link](w[i] + "\t" + f[i]);
}
}

public static void main(String[] args) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter number of sentences: ");
int num = [Link]([Link]());
[Link]("Enter sentences.");
String para = [Link]().toUpperCase();
FrequencyWord obj = new FrequencyWord(para);
obj.w = new String[[Link]()];
obj.f = new int[[Link]()];
[Link](num);
[Link]();
[Link]();
[Link]();
}
}
Algorithm
Step 1 : START
Step 2 : In the main method, READ the number of sentences num
Step 3 : IF num < 1 OR num >= 4 THEN
Step 4 : DISPLAY "INVALID ENTRY"
Step 5 : ELSE
Step 6 : READ the paragraph string s
Step 7 : CREATE an object of the class, passing s to the constructor
Step 8 : CALL the extractWords method
Step 9 : CALL the sort method
Step 10 : CALL the display method
Step 11 : ENDIF
Step 12 : In the constructor, assign the received paragraph to global variable s
Step 13 : In the extractWords method,
Step 14 : SPLIT the paragraph s into a temporary array of words
Step 15 : DISPLAY the total number of words
Step 16 : FOR each word in the temporary array
Step 17 : SEARCH for the word in the unique words array w
Step 18 : IF found THEN
Step 19 : INCREMENT its corresponding frequency in array f
Step 20 : ELSE
Step 21 : ADD the word to the unique words array w
Step 22 : SET its frequency to 1 in array f
Step 23 : INCREMENT unique word count n
Step 24 : ENDIF
Step 25 : ENDFOR
Step 26 : In the sort method,
Step 27 : FOR i from 0 to n-2
Step 28 : FOR j from 0 to n-i-2
Step 29 : IF frequency at j > frequency at j+1 THEN
Step 30 : SWAP frequencies at j and j+1
Step 31 : SWAP words at j and j+1
Step 32 : ENDIF
Step 33 : ENDFOR
Step 34 : ENDFOR
Step 35 : In the display method,
Step 36 : DISPLAY header "WORD FREQUENCY"
Step 37 : FOR i from 0 to n-1
Step 38 : DISPLAY the word w[i] and its frequency f[i]
Step 39 : ENDFOR
Step 40 : STOP
Variable Description
Data Type Variable Name Description

String s Stores the input paragraph.

String[] w Array to store unique words.

int[] f Array to store the frequency of unique word

int n Stores the count of unique words.

int num Stores the number of sentences entered b

int i, j Loop control variables.

FrequencyWord obj Object of the FrequencyWord class.

Input/Output
Test 1
INPUT:
Enter number of sentences.
1
Enter sentences.
TO BE OR NOT TO BE.
OUTPUT:
Total number of words: 6
WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2
Test 2
INPUT:
Enter number of sentences
3
Enter sentences.
THIS IS A STRING [Link] THIS EASY?YES, IT IS.
OUTPUT:
Total number of words: 11
WORD FREQUENCY
A 1
STRING 1
PROGRAM 1
EASY 1
YES 1
IT 1
THIS 2
IS 3

Common questions

Powered by AI

To determine if a number is a Kaprekar number, the algorithm first calculates the square of the number `n`. Then, it splits this square into two parts: the right-hand part consists of `d` digits, where `d` is the number of digits of `n`, and the left-hand part consists of the remaining digits. The two parts are summed, and if their sum equals the original number `n`, then `n` is a Kaprekar number. This process leverages string manipulation to accurately split and compute the parts of the squared number .

If no Kaprekar numbers are found in the given range, the program completes its loop without appending any numbers to the result string. As a result, the display output for Kaprekar numbers is empty, and the frequency is shown as zero. This is evident as the program's process function keeps a count `c` of how many Kaprekar numbers have been found, and this count remains zero if no numbers meet the criteria .

The program checks the validity of the input amount to ensure it does not exceed five digits. This constraint ensures the program operates within its defined limits, as denominations and calculations are designed to handle amounts of up to five digits only. Exceeding these limits may lead to improper handling or incorrect outputs, thus it is necessary to terminate the program with "Invalid Input" if this condition is breached .

If no words are found, meaning the paragraph contains only punctuation or is empty, the program's frequency and word arrays (initialized with possible size but not filled with actual data) remain unchanged. During the sorting phase, no operations are performed as there are no words to sort. Consequently, the display function outputs only the header "WORD FREQUENCY," followed by nothing, indicating no words were present for analysis .

The program checks the input number of sentences to ensure it lies within the specified range (1 to 3). If the input is less than 1 or greater than or equal to 4, it prints an "Invalid Input" message and terminates with `System.exit(0)`. This check ensures consistency with the program's constraints, which are designed to handle a small set of sentences for analysis .

Words are sorted by frequency using a bubble sort mechanism, which orders words in ascending order based on their occurrence counts. The program iterates through the words, swapping adjacent elements in the frequency array whenever an element is found to have a higher frequency than the next. This sorting ensures that words with lower frequencies appear first after sorting, although words with identical frequencies may appear in any order .

The recursive calls in the `toWords` function work to break down the input amount into its individual digits for conversion into words. The recursion processes the number digit by digit, starting from the most significant digit, allowing each digit to be translated into the corresponding word from the `w` array. This is achieved by calling the function recursively with `num/10` and printing the word equivalent of `num%10` as it unwinds, effectively spelling out the number from left to right .

During word extraction, the program processes the paragraph character by character to build words. It ignores any period '.' or question mark '?' punctuation, as well as any double spaces that might separate words. Only characters added to the current temporary word between spaces are considered valid. When a space, period, or question mark is encountered, the completed word is stored and the program resets its temporary storage for the next word. This ensures only valid words contribute to frequency analysis .

The program counts word frequency by first reading the input paragraph and splitting it into words that are stored without punctuation. As each word is encountered, the program checks if it already exists in an array of unique words. If it does, its corresponding frequency in an array is incremented; if not, the word is appended to the unique words array with an initial frequency of one. The program uses two arrays—one for words and another for their frequencies—to manage and track the count effectively .

The program initializes a temporary variable `temp` to the input amount and iteratively uses each available denomination starting from the highest. For each denomination, it calculates how many notes of that denomination can fit into the remaining amount (`temp/n[i]`). It then updates the total value `t` by adding the product of the denomination and its count to it (`t += n[i] * d`) and reduces the remaining amount (`temp %= n[i]`). This process ensures that the sum of all selected denominations equals the input amount .

You might also like