# Happy Number

Here given code implementation process.

``````//C Program
//Check if a given number is happy or not
#include <stdio.h>

//This method returns the sum of the digits of the given number.
long digit_square(long number)
{
long result = 0;

long digit = 0;

while (number!=0)
{
//Get last digit
digit = number % 10;

//Squre and sum of digit
result += digit * digit;

//Remove last digit
number /= 10;
}
return result;
}

//Method which is determine whether given number is happy number or not
void is_happy_no(long value)
{
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while(number!=4 && number!=1)
{
number=digit_square(number);
}

if(number==1)
{
printf(" %ld Is Happy Number\n",value);
}
else
{
printf(" %ld Is UnHappy Number\n",value);
}
}

int main() {

is_happy_no(31);
is_happy_no(21);
is_happy_no(44);
return 0;
}```
```

#### Output

`````` 31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````
``````/*
C++ Program
Check if a given number is happy or not
*/
#include<iostream>

using namespace std;

class MyNumber {
public:

//This method returns the sum of the digits of the given number.
long digit_square(long number) {
long result = 0;
long digit = 0;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit *digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
void is_happy_no(long value) {
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while (number != 4 && number != 1) {
number = this->digit_square(number);
}
if (number == 1) {
cout << value << " Is Happy Number\n";
} else {
cout << value << " Is UnHappy Number\n";
}
}
};
int main() {
MyNumber obj ;
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
return 0;
}```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````
``````/*
Java Program
Check if a given number is happy or not
*/

public class MyNumber {

//This method returns the sum of the digits of the given number.
public long digit_square(long number)
{
long result = 0;

long digit = 0;

while (number!=0)
{
//Get last digit
digit = number % 10;

//Squre and sum of digit
result += digit * digit;

//Remove last digit
number /= 10;
}
return result;
}

//Method which is determine whether given number is happy number or not
public void is_happy_no(long value)
{
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while(number!=4 && number!=1)
{
number=digit_square(number);
}

if(number==1)
{
System.out.print(value+" Is Happy Number\n");
}
else
{
System.out.print(value+" Is UnHappy Number\n");
}
}
public static void main(String[] args) {

MyNumber obj = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
}```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````
``````/*
C# Program
Check if a given number is happy or not
*/
using System;
public class MyNumber {

//This method returns the sum of the digits of the given number.
public long digit_square(long number) {
long result = 0;

long digit = 0;

while (number != 0) {
//Get last digit
digit = number % 10;

//Squre and sum of digit
result += digit * digit;

//Remove last digit
number /= 10;
}
return result;
}

//Method which is determine whether given number is happy number or not
public void is_happy_no(long value) {
long number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while (number != 4 && number != 1) {
number = digit_square(number);
}

if (number == 1) {
Console.Write(value + " Is Happy Number\n");
} else {
Console.Write(value + " Is UnHappy Number\n");
}
}
public static void Main(String[] args) {

MyNumber obj = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
}```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````
``````# Python 3 Program
# Check if a given number is happy or not
class MyNumber :
#This method returns the sum of the digits of the given number.
def digit_square(self, number) :
result = 0
digit = 0
while (number != 0) :
#Get last digit
digit = number % 10
#Squre and sum of digit
result += digit * digit
#Remove last digit
number = int(number / 10)

return result

#Method which is determine whether given number is happy number or not
def is_happy_no(self, value) :
number = value
# When number is 4 or digit square result is 4 that means no  is an not happy number

# Test Case when number is 1 that means sequece digit are valid
while (number != 4 and number != 1) :
number = self.digit_square(number)

if (number == 1) :
print(value ," Is Happy Number")
else :
print(value ," Is UnHappy Number")

def main() :
obj = MyNumber()
# Test Case
obj.is_happy_no(31)
obj.is_happy_no(21)
obj.is_happy_no(44)

if __name__ == "__main__":
main()```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````
``````# Ruby Program
# Check if a given number is happy or not
class MyNumber
#This method returns the sum of the digits of the given number.
def digit_square(number)
result = 0
digit = 0
while (number != 0)
#Get last digit
digit = number % 10
#Squre and sum of digit
result += digit * digit
#Remove last digit
number /= 10
end
return result
end
#Method which is determine whether given number is happy number or not
def is_happy_no(value)
number = value
# When number is 4 or digit square result is 4 that means no  is an not happy number

# Test Case when number is 1 that means sequece digit are valid
while (number != 4 and number != 1)
number = self.digit_square(number)
end
if (number == 1)
print(value ," Is Happy Number\n")
else
print(value ," Is UnHappy Number\n")
end
end
end
def main()
obj = MyNumber.new()
# Test Case
obj.is_happy_no(31)
obj.is_happy_no(21)
obj.is_happy_no(44)
end
main()```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number
``````
``````/*
Scala Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.
def digit_square(value: Long): Long = {
var result: Long = 0;
var digit: Long = 0;
var number: Long = value;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit * digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
def is_happy_no(value: Long): Unit = {
var number: Long = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while (number != 4 && number != 1) {
number = this.digit_square(number);
}
if (number == 1) {
print(s"\$value Is Happy Number\n");
} else {
print(s"\$value Is UnHappy Number\n");
}
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
}```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````
``````/*
Swift 4 Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.
func digit_square(_ value: Int) -> Int {
var result: Int = 0;
var digit: Int = 0;
var number: Int = value;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit * digit;
//Remove last digit
number /= 10;
}
return result;
}
//Method which is determine whether given number is happy number or not
func is_happy_no(_ value: Int) {
var number: Int = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while (number != 4 && number != 1) {
number = self.digit_square(number);
}
if (number == 1) {
print(value ," Is Happy Number");
} else {
print(value ," Is UnHappy Number");
}
}
}
func main() {
let obj: MyNumber = MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44);
}
main();```
```

#### Output

``````31  Is Happy Number
21  Is UnHappy Number
44  Is Happy Number``````
``````<?php
/*
Php Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.

public 	function digit_square(\$number) {
\$result = 0;
\$digit = 0;
while (\$number != 0) {
//Get last digit
\$digit = \$number % 10;
//Squre and sum of digit
\$result += \$digit *\$digit;
//Remove last digit
\$number = intval( \$number / 10);
}
return \$result;
}
//Method which is determine whether given number is happy number or not

public 	function is_happy_no(\$value) {
\$number = \$value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while (\$number != 4 && \$number != 1) {
\$number = \$this->digit_square(\$number);
}
if (\$number == 1) {
echo(\$value ." Is Happy Number\n");
} else {
echo(\$value ." Is UnHappy Number\n");
}
}
};

function main() {
\$obj = new MyNumber();
// Test Case

\$obj->is_happy_no(31);
\$obj->is_happy_no(21);
\$obj->is_happy_no(44);
}
main();```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````
``````/*
Node Js Program
Check if a given number is happy or not
*/
class MyNumber {
//This method returns the sum of the digits of the given number.
digit_square(number) {
var result = 0;
var digit = 0;
while (number != 0) {
//Get last digit
digit = number % 10;
//Squre and sum of digit
result += digit *digit;
//Remove last digit
number = parseInt(number / 10);
}
return result;
}
//Method which is determine whether given number is happy number or not
is_happy_no(value) {
var number = value;
// Test Case when number is 1 that means sequece digit are valid
// When number is 4 or digit square result is 4 that means no  is an not happy number
while (number != 4 && number != 1) {
number = this.digit_square(number);
}
if (number == 1) {
process.stdout.write(value + " Is Happy Number\n");
} else {
process.stdout.write(value + " Is UnHappy Number\n");
}
}
}

function main(args) {
var obj = new MyNumber();
// Test Case
obj.is_happy_no(31);
obj.is_happy_no(21);
obj.is_happy_no(44)
}
main();```
```

#### Output

``````31 Is Happy Number
21 Is UnHappy Number
44 Is Happy Number``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment 