Natural Number:- Replace the Number by the sum of the squares of its digits, and repeat the process. In the end, If the Number is equaled to 1 then it is a Happy Number.
Example:-
Given Number=31
31⇒3²+1²=1010⇒1²+0²=1
31 is Happy Number.
Given Number=11
11⇒1²+1²=2
2⇒2²=4
11 is an Unhappy Number.
Problem statement:- Program to Check whether a number is a Happy Number or Not.
Sample Input/Output:-
Sample Input First: 13 Sample Output First: It is a Happy Number.
Sample Input Second: 50 Sample Output Second: It is an Unhappy Number.
Explanation:-
For output 1st: 13⇒1²+3²=10 10⇒1²+0²=1
For output 2nd: 50⇒5²+0²=25 25⇒2²+5²=29 ............
Data requirement:-
Input Data:- num
Output Data:- String output
Additional Data:- sum, rem
Input Data:- num
Output Data:- String output
Additional Data:- sum, rem
Program in C
Here is the source code of the C Program to check whether the number is Happy Number or Not.
Code:
//Happy Number Or Not
#include<stdio.h>
int main()
{
int num;
printf("Enter the number:");
scanf("%d",&num);
int sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum+=(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1)
printf("It is a Happy Number.");
else
printf("It is an Unhappy Number.");
}
#include<stdio.h>
int main()
{
int num;
printf("Enter the number:");
scanf("%d",&num);
int sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum+=(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1)
printf("It is a Happy Number.");
else
printf("It is an Unhappy Number.");
}
Input/Output:
Enter the number:31
It is a Happy Number.
Program in C++
Here is the source code of the C++ Program to check whether the number is Happy Number or Not.
It is a Happy Number.
Program in C++
Here is the source code of the C++ Program to check whether the number is Happy Number or Not.
Code:
#include <iostream>
using namespace std;
int main()
{
int num,i;
cout<<"Enter the number:";
cin>>num;
int sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum+=(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1)
cout<<"It is a Happy Number.";
else
cout<<"It is an Unhappy Number.";
}
using namespace std;
int main()
{
int num,i;
cout<<"Enter the number:";
cin>>num;
int sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum+=(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1)
cout<<"It is a Happy Number.";
else
cout<<"It is an Unhappy Number.";
}
Input/Output:
Enter the number:11
It is an Unhappy Number.
Program in Java
It is an Unhappy Number.
Program in Java
Here is the source code of the Java Program to check whether the number is Happy Number or Not.
Code:
import java.util.Scanner;
public class HappyNumberOrNot {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
System.out.println("Enter a number:");
num=cs.nextInt();
int sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum+=(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1)
System.out.println("It is a Happy Number.");
else
System.out.println("It is an Unhappy Number.");
cs.close();
}
}
Input/Output:public class HappyNumberOrNot {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
System.out.println("Enter a number:");
num=cs.nextInt();
int sum=0,rem;
while(sum!=1 && sum!=4)
{
sum=0;
while(num!=0)
{
rem=num%10;
sum+=(rem*rem);
num/=10;
}
num=sum;
}
if(sum==1)
System.out.println("It is a Happy Number.");
else
System.out.println("It is an Unhappy Number.");
cs.close();
}
}
Enter a number:
13
It is a Happy Number.
13
It is a Happy Number.
Program in Python
Here is the source code of the Program to check whether the number is Happy Number or Not.
Code:
num=int(input("Enter a number:"))
sum=0
while sum != 1 and sum != 4:
sum=0
while num!=0:
rem = num % 10
sum += (rem*rem)
num //= 10
num=sum
if sum==1:
print("It is a Happy Number.")
else:
print("It is an Unhappy Number.")
sum=0
while sum != 1 and sum != 4:
sum=0
while num!=0:
rem = num % 10
sum += (rem*rem)
num //= 10
num=sum
if sum==1:
print("It is a Happy Number.")
else:
print("It is an Unhappy Number.")
Input/Output:
Enter a number:50
It is an Unhappy Number.
It is an Unhappy Number.
Most Recommend Questions:-
More Questions:-
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box