6174 is known as Kaprekar's constant after the Indian mathematician D. R. Kaprekar.
This number is notable for the following rule:
1. Take any four-digit number, using at least two different
digits.
2. Arrange the digits in descending and then in ascending order to
get two four-digit numbers, adding leading zeros if
necessary.
3. Subtract the smaller number from the bigger number.
4. Go back to step 2 and repeat.
The above process, known as Kaprekar's routine, will always
reach its fixed point, 6174, in at most 7 iterations.
Once 6174 is reached, the process will continue yielding 7641 –
1467 = 6174.
Example (8692) | number of steps counted |
9862-2689=7173 | Step 1 |
7731-1377=6354 | Step 2 |
6543-3456=3087 | Step 3 |
8730-378=8352 | Step 4 |
8532-2358=6174 | Step 5 |
Example (6174) | number of steps counted |
7641-1467=6174 | Step 1 |
The only four-digit numbers for which Kaprekar's routine does not reach 6174 are repdigits such as 1111, which give the result 0000 after a single subtraction.
Note: Kaprekar's constant is not unique. There are families of Kaprekar's constants for different bases or different number of digits. For example, the Kaprekar constant is 495 for 3-digit integer. In this program, we only consider Kaprekar's constant 6174 for 4-digit integer.
*Note:*
1. Add code ONLY in the correponding "TODO" area.
2. DO NOT change the function header of the given
functions.
3. DO NOT add additional libraries.
4. DO NOT use array, otherwise ZERO marks for the
assignment.
5. You can add helper function(s) if needed.
Function 1: int selectDigit(int number, int index)
Example | Expected return value |
selectDigit(4853,0) | 3 |
selectDigit(4853,2) | 8 |
selectDigit(18694,3) | 8 |
selectDigit(18694,5) | -1 |
selectDigit(18694,-1) | -1 |
Function 2: bool isRepdigit(int number)
Example | Expected return value |
isRepdigit(4855) | false |
isRepdigit(5555) | true |
isRepdigit(18642) | false |
isRepdigit(1) | true |
Function 3: int sortDigit(int number, bool order)
Example | Expected return value |
sortDigit(4855,ASCENDING) | 4558 |
sortDigit(4855,DESCENDING) | 8554 |
sortDigit(89764,ASCENDING) | 46789 |
sortDigit(89764,DESCENDING) | 98764 |
sortDigit(20003,ASCENDING) | 23 |
sortDigit(20003,DESCENDING) | 32000 |
Function 4: int isKaprekar6174(int number, bool debug)
Example | Expected return value | Expected output |
isKaprekar6174(8692,false) | 5 (i.e. takes 5 steps to finish) | |
isKaprekar6174(8692,true) | 5 (i.e. takes 5 steps to finish) | 9862-2689=7173 7731-1377=6354 6543-3456=3087 8730-378=8352 8532-2358=6174 |
isKaprekar6174(865,false) | -1 (i.e. non 4-digit number) | |
isKaprekar6174(865,true) | -1 (i.e. non 4-digit number) | can't reach Kaprekar's constant 6174 |
isKaprekar6174(2222,false) | -1 (i.e. it is a repdigit) | |
isKaprekar6174(2222,true) | -1 (i.e. it is a repdigit) | can't reach Kaprekar's constant 6174 |
Function 5: void printStat(int m, int n)
Example | Expected output |
printStat(1000,2000) | 1* 2** 3***** 4*** 5**** 6**** 7**** -1* |
printStat(1000,9999) | 1******** 2*********** 3******************************************* 4*********************** 5**************************** 6******************************* 7**************************************** -1* |
Down below is the skeleton code for this program:
#include <iostream> #include <math.h> using namespace std; const bool ASCENDING = 0; const bool DESCENDING = 1; const bool DEBUG = true; /** * Task 1: selectDigit() returns one digit from a given index location of an arbitrary positive integer * The index starts from 0 at the rightmost digit * The function return -1 if index is invalid * Examples: selectDigit(123, 0) = 3, selectDigit(1234, 3) = 1, selectDigit(123, 3) = -1, selectDigit(123, -1) = -1 */ int selectDigit(int number, int index){ //TODO: start your coding here } /** * Task 2: isRepdigit() checks whether all digits in a positive integer are the same * It returns true if Yes, false otherwise * Examples: isRepdigit(222) = true, isRepdigit(212) = false, isRepdigit(2) = true */ bool isRepdigit(int number){ //TODO: start your coding here } /** * Task 3: sortDigit() takes an arbitrary positive integer number, * and returns a new integer with all digits in number arranged in ascending or descending order * Examples: sortDigit(16845, ASCENDING) = 14568, sortDigit(16845, DESCENDING) = 86541 * The function should be able to handle number consists 0s * Examples: sortDigit(500, ASCENDING) = 5, sortDigit(100250, DESCENDING) = 521000, sortDigit(100250, ASCENDING) = 125 */ int sortDigit(int number, bool order){ //TODO: start your coding here } /** * Task 4: isKaprekar6174() takes an arbitrary positive integer, * and returns the number of steps needed to reach Kaprekar's constant 6174 * The function returns -1 if the number can't reach 6174 * when DEBUG is true, calculation details needs to be printed out * Output format for the calculation details: Each row represents a subtraction calculation * There is no space between the number and minus sign. There is no space between the number and equal sign */ int isKaprekar6174(int number, bool debug){ //TODO: start your coding here } /** * Task 5: printStat() bincounts #steps to reach Kaprekar's constant 6174 for numbers in a given range from m to n (both inclusive) * Then print the bar chart (*) * For simplicity, you can assume that m and n are positive integers and there is always a valid range * 8 bins are used, which count the numbers with 1 to 7 steps to reach Kaprekar's constant 6174, or fail to do so * A * is printed for every 50 counted * Print a * if there is remainder * For example if bincount = 350, print 7 stars; bincount = 351, print 8 stars * Output format: For each row, starts with the bin number (i.e. 1 to 7) and immediately followed by the star. Do not leave any spaces between them * Output format: The last row (eighth row) of the output represents the failure case (started with -1 and immediately followed by the star, do not leave any spaces between them) */ void printStat(int m, int n){ //TODO: start your coding here } // This is the main function. It is already done. Please DO NOT make any modification. int main() { cout << "Task 1:" << endl; cout << "selectDigit(896543,0) = " << selectDigit(896543,0) << endl; cout << "selectDigit(896543,5) = " << selectDigit(896543,5) << endl; cout << "selectDigit(896543,-1) = " << selectDigit(896543,-1) << endl; cout << "selectDigit(896543,6) = " << selectDigit(896543,6) << endl; cout << endl << "Task 2:" << endl; cout << "isRepdigit(2999) " << boolalpha << isRepdigit(2999) << endl; cout << "isRepdigit(888888) " << boolalpha << isRepdigit(888888) << endl; cout << "isRepdigit(1) " << boolalpha << isRepdigit(1) << endl; cout << endl << "Task 3:" << endl; cout << "sortDigit(54321, ASCENDING) = " << sortDigit(54321, ASCENDING) << endl; cout << "sortDigit(794621, ASCENDING) = " << sortDigit(794621, ASCENDING) << endl; cout << "sortDigit(794621, DESCENDING) = " << sortDigit(794621, DESCENDING) << endl; cout << "sortDigit(100250, ASCENDING) = " << sortDigit(100250, ASCENDING) << endl; cout << "sortDigit(100250, DESCENDING) = " << sortDigit(100250, DESCENDING) << endl; cout << "sortDigit(500, ASCENDING) = " << sortDigit(500, ASCENDING) << endl; cout << endl << "Task 4:" << endl; cout << "isKaprekar6174(546, DEBUG) = " << isKaprekar6174(546, !DEBUG) << endl; isKaprekar6174(546, DEBUG); cout << "isKaprekar6174(18604, DEBUG) = " << isKaprekar6174(18604, !DEBUG)<< endl; isKaprekar6174(18604, DEBUG); cout << "isKaprekar6174(8888, DEBUG) = " << isKaprekar6174(8888, !DEBUG) << endl; isKaprekar6174(8888, DEBUG); cout << "isKaprekar6174(2894, DEBUG) = " << isKaprekar6174(2894, !DEBUG)<< endl; isKaprekar6174(2894, DEBUG); cout << "isKaprekar6174(6174, DEBUG) = " << isKaprekar6174(6174, !DEBUG)<< endl; isKaprekar6174(6174, DEBUG); cout << endl << "Task 5: " << endl; cout << "Statistic for range from 1000 to 9999" << endl; printStat(1000,9999); cout << "Statistic for range from 500 to 10500" << endl; printStat(500,10500); return 0; }
SOLUTION:-
Given that code
#include <iostream>
#include <math.h>
#include <cmath>
using namespace std;
const bool ASCENDING = 0;
const bool DESCENDING = 1;
const bool DEBUG = true;
/** Task 1: selectDigit() returns one digit from a given index
location of an arbitrary positive integer
*/
int selectDigit(int number, int index)
{
int d, currentIndex = 0;
while(number > 0) {
d = number % 10;
if(currentIndex == index){
return d;
}
currentIndex ++;
number = number / 10;
}
return -1;
}
/** Task 2: isRepdigit() checks whether all digits in a positive
integer are the same
*/
bool isRepdigit(int number)
{
int d=0,d1=0, flag = 0;
if(number <= 10) {
return true;
}
else{
while(number > 10) {
d = number % 10;
d1 = ((number % 100) - d)/ 10;
if(d != d1){
return false;
}
number = number/10;
}
return true;
}
}
/** Task 3: sortDigit() takes an arbitrary positive integer
number,
*/
int sortDigit(int number, bool order)
{ int i, p, n = 0, r;
if(order) {
//Descending
for(i=9;i>=0;i--)
{
p=number;
while(p!=0)
{
r = p%10;
if(r==i)
{
n = n * 10 + r;
}
p=p/10;
}
}
}
else
{
//Ascending
for(i=0;i<10;i++)
{
p=number;
while(p!=0)
{
r = p%10;
if(r==i)
{
n = n * 10 + r;
}
p=p/10;
}
}
}
return n;
}
/** Task 4: isKaprekar6174() takes an arbitrary positive
integer,
*/
int isKaprekar6174(int number, bool debug)
{ int maxCount = 7,i = 0, count = 0;
int n = number;
if(isRepdigit(number) || number < 1000 || number
>9999){
if(debug == true)
printf("Can't reach Kaprekar's constant 6174\n");
return -1;
}
else{
while(count <= maxCount)
{
count ++;
if(debug == true) {
cout<<n<<" = "<<sortDigit(n, true)<<" -
"<<sortDigit(n, false)<<endl;
}
n = sortDigit(n, true) - sortDigit(n, false);
if(n == 6174){
return count;
}
}
return count;
}
}
/** Task 5: printStat() bincounts
*/
void printStat(int m, int n)
{
int c0=0, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0, c7=0, i;
int star;
for (i = m; i <= n; i ++) {
if(isKaprekar6174(i,0) == 1) {
c1++;
}
else if(isKaprekar6174(i,0) == 2) {
c2++;
}
else if(isKaprekar6174(i,0) == 3) {
c3++;
}
else if(isKaprekar6174(i,0) == 4) {
c4++;
}
else if(isKaprekar6174(i,0) == 5) {
c5++;
}
else if(isKaprekar6174(i,0) == 6) {
c6++;
}
else if(isKaprekar6174(i,0) == 7) {
c7++;
}
else if(isKaprekar6174(i,0) == -1){
c0++;
}
}
for(i = 1; i <= 7; i ++){
if(i == 1) {
star = c1 / 50;
if(c1 % 50 != 0) {
star ++;
}
}
else if(i ==2) {
star = c2 / 50;
if(c2 % 50 != 0) {
star ++;
}
}
else if(i == 3) {
star = c3 / 50;
if(c3 % 50 != 0) {
star ++;
}
}
else if(i == 4) {
star = c4 / 50;
if(c4 % 50 != 0) {
star ++;
}
}
else if(i == 5) {
star = c5 / 50;
if(c5 % 50 != 0) {
star ++;
}
}
else if(i == 6) {
star = c6 / 50;
if(c6 % 50 != 0) {
star ++;
}
}
else if(i == 7) {
star = c7 / 50;
if(c7 % 50 != 0) {
star ++;
}
}
cout<<i;
for(int j = 1; j <= star; j ++) {
cout<<"*";
}
cout<<endl;
}
star = c0/50;
if(c0 % 50 != 0) {
star ++;
}
cout<<"-1";
for(int j = 1; j <= star; j ++) {
cout<<"*";
}
cout<<endl;
}
// This is the main function. It is already done. Please DO NOT
make any modification.
int main()
{
cout << "Task 1:" << endl;
cout << "selectDigit(896543,0) = " <<
selectDigit(896543,0) << endl;
cout << "selectDigit(896543,5) = " <<
selectDigit(896543,5) << endl;
cout << "selectDigit(896543,-1) = " <<
selectDigit(896543,-1) << endl;
cout << "selectDigit(896543,6) = " <<
selectDigit(896543,6) << endl;
cout << endl << "Task 2:" << endl;
cout << "isRepdigit(2999) " << boolalpha <<
isRepdigit(2999) << endl;
cout << "isRepdigit(888888) " << boolalpha <<
isRepdigit(888888) << endl;
cout << "isRepdigit(1) " << boolalpha <<
isRepdigit(1) << endl;
cout << endl << "Task 3:" << endl; cout <<
"sortDigit(54321, ASCENDING) = " << sortDigit(54321,
ASCENDING) << endl;
cout << "sortDigit(794621, ASCENDING) = " <<
sortDigit(794621, ASCENDING) << endl;
cout << "sortDigit(794621, DESCENDING) = " <<
sortDigit(794621, DESCENDING) << endl;
cout << "sortDigit(100250, ASCENDING) = " <<
sortDigit(100250, ASCENDING) << endl;
cout << "sortDigit(100250, DESCENDING) = " <<
sortDigit(100250, DESCENDING) << endl;
cout << "sortDigit(500, ASCENDING) = " <<
sortDigit(500, ASCENDING) << endl;
cout << endl << "Task 4:" << endl;
cout << "isKaprekar6174(546, DEBUG) = " <<
isKaprekar6174(546, !DEBUG) << endl;
isKaprekar6174(546, DEBUG);
cout << "isKaprekar6174(18604, DEBUG) = " <<
isKaprekar6174(18604, !DEBUG)<< endl;
isKaprekar6174(18604, DEBUG);
cout << "isKaprekar6174(8888, DEBUG) = " <<
isKaprekar6174(8888, !DEBUG) << endl;
isKaprekar6174(8888, DEBUG);
cout << "isKaprekar6174(2894, DEBUG) = " <<
isKaprekar6174(2894, !DEBUG)<< endl;
isKaprekar6174(2894, DEBUG);
cout << "isKaprekar6174(6174, DEBUG) = " <<
isKaprekar6174(6174, !DEBUG)<< endl;
isKaprekar6174(6174, DEBUG);
cout << endl << "Task 5: " << endl;
cout << "Statistic for range from 1000 to 9999" <<
endl;
printStat(1000,9999);
cout << "Statistic for range from 500 to 10500" <<
endl;
printStat(500,10500);
return 0;
}
THANK YOU, if any queries please leave your valuable comment on comment box..........
6174 is known as Kaprekar's constant after the Indian mathematician D. R. Kaprekar. This number is...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...
I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...
/* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...
* This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0 sudoku(int g[][9]); //constructor //Postcondition: grid = g void initializeSudokuGrid(); //Function to prompt the user to specify the numbers of the //partially filled grid. //Postcondition: grid is initialized to the numbers // specified by the user. void initializeSudokuGrid(int g[][9]); //Function to initialize grid to g //Postcondition: grid = g; void printSudokuGrid(); //Function to print the sudoku grid. bool solveSudoku(); //Funtion to solve the sudoku problem....
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...
c++
Some pieces of the following code are missing. Fill in the blanks of the following program. Each answer must fit on a single line. include <iostream> #include <cstdib #include <ctimes using namespace std; randoml_event return 2 (rand() 5 random2 intre + rand 90 return ret: random1_1000X rand) intret 100 return ret: fint 2) return nt; return n2 is oder Is_odot if (n%2 0) return return true; int sum3 (int n, int n, int n3 X int sum= n1 +...
Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) { int uniqueDigitCount = 0; int storeDigit = 0; int digit = 0; while (input > 0) { digit = 1 << (input % 10); if (!(storeDigit & digit)) { storeDigit |= digit; ...
DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted. Task 1 – Reverse array (3 pts) Given an integer array like: const...