# Find out all Happy numbers present within a given range

Happy 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 then it is a Happy Number.

Example:

Given Number=10
10⇒1²+0²=1
10 is Happy Number.

Given Number=22
22⇒2²+2²=8
22 is an Unhappy Number.

Problem statement:-  Program to Find out all Happy numbers present within a given range.

Data requirement:-

Input Data:- range1, range2

Output Data:- i, range1, range2

Additional Data:- num, sum, rem

Program in C

Here is the source code of the C Program to Find out all Happy numbers present within a given range.

Code:

//Happy Number in range
#include<stdio.h>
int main()
{
int range1,range2,i;
printf("Enter a range:");
scanf("%d %d",&range1,&range2);
printf("Happy Numbers between %d and %d are: ",range1,range2);
for(i=range1;i<=range2;i++)
{
int num=i,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("%d ",i);
}
}

Input/Output:
Enter a range:1
100
Happy Numbers between 1 and 100 are: 1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100

Program in C++

Here is the source code of the C++ Program to Find out all Happy numbers present within a given range.

Code:

#include<iostream>
using namespace std;
int main()
{
int range1,range2,i;
cout<<"Enter a range:";
cin>>range1;
cin>>range2;
cout<<"Happy numbers between "<<range1<<" and "<<range2<<" are: ";
for(i=range1;i<=range2;i++)
{
int num=i,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<<i<<" ";
}
}

Input/Output:
Enter a range:100
200
Happy numbers between 100 and 200 are: 100 103 109 129 130 133 139 167 176 188 190 192 193

Program in Java

Here is the source code of the Java Program to Find out all Happy numbers present within a given range.

Code:

import java.util.Scanner;
public class FindHappyNumbersInRange {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int range1,range2;
System.out.println("Enter a range:");
range1=cs.nextInt();
range2=cs.nextInt();
System.out.println("Spy numbers between "+range1+" and "+range2+" are: ");
for(int i=range1;i<=range2;i++)
{
int num=i,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.print(i+" ");
}
cs.close();
}
}

Input/Output:
Enter a range:
300
350
Spy numbers between 300 and 350 are:
301 302 310 313 319 320 326 329 331 338

Program in Python

Here is the source code of the Program to Find out all Happy numbers present within a given range.

Code:

print("Enter a range:")
range1=int(input())
range2=int(input())
print("Happy numbers between ",range1," and ",range2," are: ")
for i in range(range1,range2+1):
num=i
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(i,end=" ")

Input/Output:
Enter a range:
1000
1025
Happy numbers between  1000  and  1025  are:
1000 1003 1009

More:-