Now it\'s your turn. is hat the sum of the number of iterations required to reac
ID: 3816127 • Letter: N
Question
Now it's your turn. is hat the sum of the number of iterations required to reach a fixed point (6174 or 0000) using Kaprekar's routine for all four-digit numbers, from 0000 to 9999? To compute the sum, you should write a function which implements Kaprekar's routine. Your I function should take as input a nonnegative integer specifying the initial four-digit number (it may be less than four digits, but it is not allowed to be more than four digits) and should return the number of iterations required to reach a fixed point (either 6174 or 0000). The number of iterations to reach a fixed point from itself is 0. Once you have written and tested your function, you should write a simple loop that computes the sum of the output of the function for all four- digit numbers, from 0000 to 9999. F.A.Q 1. How do I count sum? a. See F.A.Q for Activity 1. 2. What's the answer supposed to be? a. Your instructor hopefully put up on sCampus a "test" that will tell you if you got it right or not. 3. How do I know how many 0s to pad? a. If you know the length of the string, and you know the string is supposed to have length 4, then you can use basic arithmetic to determine the number of os needed. 4. How do I sort in descending order? a. help sort 5. How do I check that a value is a nonnegative integer? a. See F.A.Q. for Activity 1.Explanation / Answer
num=input("enter a non-negative number");
count=0;
sum=0;
num1=0
if(num>0 && num<9999)
if(num>6174)
while(num==6174)
count=count+1
num--
end
else
while(num==6174)
count=count+1
num++
end
else
fprintf("enter positive number between 0 ,9999")
end
fprintf("no.of iterations: %d",count)
while(num1<=9999)
sum=sum+num1
num1++
end
fprintf("sum is :%d",sum)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.