Question

Kaprekar C++ program a. Write a c++  code that takes any number between 0 and 9999 as...

Kaprekar C++ program

a. Write a c++  code that takes any number between 0 and 9999 as an input value, and returns the number of iterations it takes that number to reach 6174. Also, it needs to indicate if the number does not converge to 6174. Run the program with three different numbers of your choosing to show that it works.

b. Write a second c++ code using the first. However, this new program will calculate how many iterations it takes each number from 0 to 9999 to converge to 6174. The output will be (1) the number of values that do not converge within 8 iterations, and (2) a table with the first column being iterations from 1 through 8, and the second column being the number of values that took that many times to converge. For example, the number of values that converge after three calculations is 2400.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

CODE

//C program to check if a number is Kaprekar number or not

#include<bits/stdc++.h>

using namespace std;

// Returns true if n is a Kaprekar number, else false

bool iskaprekar(int n)

{

  if (n == 1)

  return true;

  // Count number of digits in square

  int sq_n = n * n;

  int count_digits = 0;

  while (sq_n)

  {

    count_digits++;

    sq_n /= 10;

  }

  sq_n = n*n; // Recompute square as it was changed

  // Split the square at different poitns and see if sum

  // of any pair of splitted numbers is equal to n.

  for (int r_digits=1; r_digits<count_digits; r_digits++)

  {

    int eq_parts = pow(10, r_digits);

    // To avoid numbers like 10, 100, 1000 (These are not

    // Karprekar numbers

    if (eq_parts == n)

      continue;

    // Find sum of current parts and compare with n

    int sum = sq_n/eq_parts + sq_n % eq_parts;

    if (sum == n)

    return true;

  }

  // compare with original number

  return false;

}

// Driver code

int main()

{

int n;

cout << "Enter a number between 0 and 9999: ";

cin >> n;

int count = 0;

while(n <= 6174) {

if (n == 6174) {

cout << "Number of iterations: " << count;

return 0;

}

n = iskaprekar(n);

count ++;

}

  return 0;

}

NOTE: As per Chegg policy, I am allowed to answer only 1 coding question (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
Kaprekar C++ program a. Write a c++  code that takes any number between 0 and 9999 as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ function that takes any number between 0 and 9999 as an input value, and returns the ...

    Write a C++ function that takes any number between 0 and 9999 as an input value, and returns the number of iterations it takes that number to reach 6174. Also, it needs to indicate if the number does not converge to 6174. Run the program with three different numbers of your choosing to show that it works.

  • Write a python code that takes in an number 0-9 and prints out the word of...

    Write a python code that takes in an number 0-9 and prints out the word of the number. For example 1 would print out one. Below is the skeleton of the code that needs to be filled in. def num2string(num): """ Takes as input a number, num, and returns the corresponding name as a string. Examples: num2string(0) returns "zero", num2string(1)returns "one" Assumes that input is an integer ranging from 0 to 9 """ numString = "" ################################### ### FILL IN...

  • Write the following code in C. Write a program that takes a positive integer and prints...

    Write the following code in C. Write a program that takes a positive integer and prints that many stars These are sample outputs. Enter the number of stars: 9 Enter the number of stars: 4 Enter the number of stars: -3 Invalid input!

  • *Please write in code in C* First, write a function called prime_check(int x) that takes an...

    *Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes...

    d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes as parame- ters any three integers and returns the largest of the three integers. Your output should have the same format and should work for any integers a user enters Desired output: Enter three integers: 6 15 8 The largest integer is: 15 7.3 Recursive Functions Write a program that uses a function sum(int) that takes as an argument a positive integer n and...

  • Write a program C++ (Write comments for each code)Generate a number from 1 to 10, inclusive....

    Write a program C++ (Write comments for each code)Generate a number from 1 to 10, inclusive. Prompt the user to guess an integer number. If the guess is incorrect, indicate to the user whether it is too low or too high. Then, prompt the user to guess again. If the guess is correct, indicate the correct answer and how many tries it took the get the correct answer.

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • Write a C++ program that generates a random number from 0 to 9. It will then...

    Write a C++ program that generates a random number from 0 to 9. It will then prompt the user to guess the random number it generated. Your program stops if the user guesses the right number. (Use the cout function to display the random number the computer generates so that you can determine if your code actually works). SAMPLE OUTPUT Random number = 8 Guess a number in the range [0,9]: 1 Too low Guess a number in the range...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT