C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to print all happy numbers between 1 and 100ExplanationIn this program, we need to display all happy numbers between 1 and 100. Happy number The happy number can be defined as a number which will yield 1 when it is replaced by the sum of the square of its digits repeatedly. If this process results in an endless cycle of numbers containing 4, then the number is called an unhappy number. For example, 32 is a happy number as the process yields 1 as follows Formula32 + 22 = 13 12 + 32 = 10 12 + 02 = 1 Some of the other examples of happy numbers are 7, 28, 100, 320 and so on. Unhappy number will result into a cycle of 4, 16, 37, 58, 89, 145, 42, 20, 4, ... To find whether a given number is happy or not, calculate the square of each digit present in number and add it to a variable sum. If resulting sum is equal to 1 then, given number is a happy number. If the sum is equal to 4 then, the number is an unhappy number. Else, replace the number with the sum of the square of digits. Algorithm
SolutionPython#isHappyNumber() will determine whether a number is happy or not def isHappyNumber(num): rem = sum = 0; #Calculates the sum of squares of digits while(num > 0): rem = num%10; sum = sum + (rem*rem); num = num//10; return sum; #Displays all happy numbers between 1 and 100 print("List of happy numbers between 1 and 100: "); for i in range(1, 101): result = i; #Happy number always ends with 1 and #unhappy number ends in a cycle of repeating numbers which contains 4 while(result != 1 and result != 4): result = isHappyNumber(result); if(result == 1): print(i), print(" "), Output: List of happy numbers between 1 and 100: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 C#include <stdio.h> //isHappyNumber() will determine whether a number is happy or not int isHappyNumber(int num){ int rem = 0, sum = 0; //Calculates the sum of squares of digits while(num > 0){ rem = num%10; sum = sum + (rem*rem); num = num/10; } return sum; } int main() { //Displays all happy numbers between 1 and 100 printf("List of happy numbers between 1 and 100: \n"); for(int i = 1; i <= 100; i++){ int result = i; //Happy number always ends with 1 and //unhappy number ends in a cycle of repeating numbers which contains 4 while(result != 1 && result != 4){ result = isHappyNumber(result); } if(result == 1) printf("%d ", i); } return 0; } Output: List of happy numbers between 1 and 100: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 JAVApublic class HappyNumbers { //isHappyNumber() will determine whether a number is happy or not public static int isHappyNumber(int num){ int rem = 0, sum = 0; //Calculates the sum of squares of digits while(num > 0){ rem = num%10; sum = sum + (rem*rem); num = num/10; } return sum; } public static void main(String[] args) { //Displays all happy numbers between 1 and 100 System.out.println("List of happy numbers between 1 and 100: "); for(int i = 1; i <= 100; i++){ int result = i; //Happy number always ends with 1 and //unhappy number ends in a cycle of repeating numbers which contains 4 while(result != 1 && result != 4){ result = isHappyNumber(result); } if(result == 1) System.out.print(i + " "); } } } Output: List of happy numbers between 1 and 100: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 C#using System; public class HappyNumbers { //isHappyNumber() will determine whether a number is happy or not public static int isHappyNumber(int num){ int rem = 0, sum = 0; //Calculates the sum of squares of digits while(num > 0){ rem = num%10; sum = sum + (rem*rem); num = num/10; } return sum; } public static void Main() { //Displays all happy numbers between 1 and 100 Console.WriteLine("List of happy numbers between 1 and 100: "); for(int i = 1; i <= 100; i++){ int result = i; //Happy number always ends with 1 and //unhappy number ends in a cycle of repeating numbers which contains 4 while(result != 1 && result != 4){ result = isHappyNumber(result); } if(result == 1) Console.Write(i + " "); } } } Output: List of happy numbers between 1 and 100: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 PHP<!DOCTYPE html> <html> <body> <?php //isHappyNumber() will determine whether a number is happy or not function isHappyNumber($num){ $rem = $sum = 0; //Calculates the sum of squares of digits while($num > 0){ $rem = $num%10; $sum = $sum + ($rem*$rem); $num = intval($num/10); } return $sum; } //Displays all happy numbers between 1 and 100 print("List of happy numbers between 1 and 100: <br>"); for($i = 1; $i <= 100; $i++){ $result = $i; //Happy number always ends with 1 and //unhappy number ends in a cycle of repeating numbers which contains 4 while($result != 1 && $result != 4){ $result = isHappyNumber($result); } if($result == 1) print($i . " "); } ?> </body> </html> Output: List of happy numbers between 1 and 100: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100
Next Topic#
|