Program to check for a Happy Number.

Happy Number – Replace the number by the sum of the squares of its digits, and repeat the process. At the end, if the number is equals to 1 then it is a Happy Number, or it loops endlessly in a cycle that does not include 1 (if it is not a happy number then this process will end at 4).

For example:

23 is a Happy Number

23 :> 2^2 + 3^2 = 13
13 :> 1^2 + 3^2 = 10
10 :> 1^2 + 0 = 1

As we reached to 1, 23 is a Happy Number.

42 is a UnHappy Number

42 :> 4^2 + 2^2 = 20
20 :> 2^2 + 0 = 4

As we reached to 4, 42 is a UnHappy Number.

C

#include<stdio.h>

int main()
{ 
 int num,temp,sum=0;
 
 printf("Enter a Number : ");
 scanf("%d",&num);
 
 while(sum!=1 && sum!=4)
 {
  sum=0;
  
  while(num>0)
  {
   temp=num%10;
   sum+=(temp*temp);
   num=num/10;
  
  }
  num=sum;
 }
 
 if(sum==1)
  printf("Happy Number");
 else
  printf("UnHappy Number");

 return 0;
}

C++

#include<iostream.h>

int main()
{ 
 int num,temp,sum=0;
 
 cout<<"Enter a Number : ";
 cin>>num;
 
 while(sum!=1 && sum!=4)
 {
  sum=0;
  
  while(num>0)
  {
   temp=num%10;
   sum+=(temp*temp);
   num=num/10;
  
  }
   num=sum;
 }
 
 if(sum==1)
  cout<<"Happy Number";
 else
  cout<<"UnHappy Number";

 return 0;
}

Java

import java.util.Scanner;

class HappyNumber
{

    public static void main(String args[])
    {
     int num;
     int temp;
     int sum = 0;

     Scanner sc= new Scanner(System.in);


    System.out.print("Enter a number : ");
    num = sc.nextInt();

     while (sum != 1 && sum != 4)
     {
      sum = 0;

      while (num > 0)
      {
       temp = num % 10;
       sum += (temp * temp);
       num = num / 10;

      }
       num = sum;
     }

     if (sum == 1)
     {
      System.out.print("Happy Number");
     }
     else
     {
      System.out.print("UnHappy Number");
     }


    }
}

C#

class HappyNumber
{
 public static void Main()
   {
     int num;
     int temp;
     int sum = 0;


      Console.Write("Enter a number : ");
      num = Convert.ToInt32(Console.ReadLine());
        while (sum != 1 && sum != 4)
        {
          sum = 0;

           while (num > 0)
            {
              temp = num % 10;
              sum += (temp * temp);
              num = num / 10;

            }
            num = sum;
        }

        if (sum == 1)
        {
          Console.Write("Happy Number");
        }
        else
        {
          Console.Write("UnHappy Number");
        }
          Console.ReadKey(true); 
    }
}

Python

sum = 0

num = int(input("Enter the number: "))

while (sum != 1 and sum != 4):
    sum = 0
    while (num > 0):
        temp = num % 10
        sum += (temp * temp)
        num = num // 10
    num = sum

if (sum == 1):
    print("Happy Number")
else:
    print("UnHappy Number")
OUTPUT:

(1st Run)

Enter the number: 19
Happy Number

(2nd Run)

Enter the number: 165
UnHappy Number

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.