Question: The question is Happy number , ( see the image) .expected outcome 1= happy number, 0= unhappy number I have coded in python . I
The question is Happy number , ( see the image) .expected outcome 1= happy number, 0= unhappy number
I have coded in python . I have passed 2 test cases which are happy numbers 1 and 7 both resulting in output :1 but I am not able to pass the third test case which is 22 a unhappy number. it should give the output as :0 .
I don't want a code where the input needs to be entered in the output console like = Enter the number
I want code where the input is read directly from the program and displays the results in console
You can either correct my code , or code in java or java script or C# or python


Happy Numbers Programming challenge description: A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers. Input: Your program should read lines of text from standard input. Each une contains a single positive integer, N. Output: If the number is a happy number, print 1 to standard output. Otherwise, print 0. For the curious, here's why 7 is a happy number: 749+9713010+1. Here's why 22 is NOT a happy number: 22+8+64 +52 +29785-189145- >4220416-37-5889... Test 1 Test Input 1 Expected Output 1 Test 2 Test Input bers Iption: is return sq 3. 1 def sumDigitSquare(n): 2 sq = @ 3 while (n): 4 digit=n%10 5 sq=sq=digit+digit n=n//10 7 8 9 def isHappy(n): 10 while(1): 11 if(n==1): 12 return True 13 n=sumDigitSquare(n) 14 if (n==22): 15 return false 16 return false 17 n=1 18 if (isHappy(n)): 19 print("1") 20 else : 21 print("") per by quares repeat the y), or it na s not I I ch this 1 are while et end >_ Test Case Output Running test cases... Done Test 1 w Passed ? Expand hould ct from Each single -N. Test 2 win Passed # Expand Test 3 X Failed oription: er la >_ Test Cane Output -888. Running test cases... Done any | Test 1 Passed & Expand Test 2 Passed Expand Test 3 X Failed umber by squares nd repeat until the s 1 stay), or it sly in a does not se which this 3 in 1 are ers, while not end ppy Test Input: 22 Expected Output: 0 Your Output: m should f text from
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
