I need to make a program in C to obtain the number series of the Kaprekar operation. The first integer must be requested from the standard entry and must be positive with four significant digits.
1. You do homework for the purpose that you learn. You won't learn (much) if you don't do it.
2. What is "Kaprekar operation"? You have to list the required steps with detail. That list becomes algorithm. Algorithm turns into code.
3. "use of arrangements"? No idea what that is.
You could start from:
read value (where 999<value<10000)
old = value
new = 0
iteration = 0
ready = false
DO
print "(iteration) old"
compute new from old
IF we are at the end of series
THEN ready = true
old = new
++iteration
WHILE not ready
The way to write a program is to break the problem down into smaller problems. They you break those down into smaller problems. Eventually you get to steps that can be translated to code.
I look at Kaprekar's operation and see the following sub-problems:
- break a number up into it's digits.
- Sort the digits.
- Create the largest number with the 4 digits: (largest digit) (next digit) (next digit) (smallest digit)
- Create the smallest number from the 4 digits: (smallest digit) (next digit) (next digit) (largest digit)
If you can write code to do these 4 things then the creating the program is trivial.
Have you learned about functions? You could make each one of these a function. Write it. Test it. Then write the next. When the functions work, use them in the main() program to solve the problem.
Yep, that's exactly the strategy I would use in Ruby. This sort of problem reminds me of some of the instances when I'd take a string and split(//) it, which kinda uses regex to quickly convert it to an array. I tried to use less variables but had trouble with the comparison =S
You have a small bug because you didn't 0-pad the next string. "332" or "1000" quickly goes to 0 because of the lost digit. I ran into this too until I processed it against "%03d" or "%04d", respectively.
print "Enter a 3- or 4-digit number with at least two distinct digits: "
n = gets.chomp
sz = n.size
11.times {|x|
puts "(#{x}) #{n}"
ar = n.split(//)
m = ar.sort.join
n = "%0#{sz}d" % (m.reverse.to_i - m.to_i)
breakif ar.join == n
}
Enter a 3- or 4-digit number with at least two distinct digits: 332
(0) 332
(1) 099
(2) 891
(3) 792
(4) 693
(5) 594
(6) 495
Enter a 3- or 4-digit number with at least two distinct digits: 5200
(0) 5200
(1) 5175
(2) 5994
(3) 5355
(4) 1998
(5) 8082
(6) 8532
(7) 6174