From 8dedaa00d0d12f62e6992eaafb06701a1b5a9fb4 Mon Sep 17 00:00:00 2001 From: Arghadip Chatterjee <114013720+Arghadip-Chatterjee@users.noreply.github.com> Date: Sat, 22 Oct 2022 13:33:28 +0530 Subject: [PATCH] Happy Number A number is called happy if it leads to 1 after a sequence of steps wherein each step number is replaced by the sum of squares of its digit that is if we start with Happy Number and keep replacing it with digits square sum, we reach 1. --- happy_number.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 happy_number.java diff --git a/happy_number.java b/happy_number.java new file mode 100644 index 0000000..3e26208 --- /dev/null +++ b/happy_number.java @@ -0,0 +1,59 @@ +import java.util.*; +class happy_number +{ + int y,n; + happy_number() + { + y=0; + n=0; + } + void input() + { + Scanner sc=new Scanner(System.in); + System.out.println("Enter the Number"); + n=sc.nextInt(); + } + int sum_of_digit(int z) + { + if(z==0) + { + return 0; + } + else + { + int d=z%10; + return d*d+sum_of_digit(z/10); + } + } + + int happy(int x) + { + while(x>9) + { + y=sum_of_digit(x); + x=y; + } + return y; + } + + void display() + { + int q=happy(n); + if(q==1) + { + System.out.println("Happy Number"); + } + else + { + System.out.println("Not Happy Number"); + } + } + + public static void main() + { + happy_number ob=new happy_number(); + ob.input(); + ob.display(); + } +} +