Happy Number Program Java
__________________________________CLASS_____________________________
public class GetNumber // This class is used to convert a string into digits and each digit is
squared and added
{
public long getNumber(String str) // this function take string and return a number that is
squared of each digit
{
long number=0;
char[] data=str.toCharArray(); // convert string into char array
for(int i=0;i seenSet = new HashSet(); // Create a Set to store the previous cases of invalid
squared sum
GetNumber gn=new GetNumber();
String input_data=\"\";
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println(\"Enter a Number String: \");
input_data = reader.nextLine(); // Scans the next token of the input as an int.
if(cs.check_String_Value(input_data)==true) // Check string vailidity
{
String happy_data=input_data;
long happy_check1=gn.getNumber(happy_data); // get squared sum number of digits
long happy_check=happy_check1;
happy_data=\"\"+happy_check;
seenSet.add(happy_check1); // adding first squared sum number of digits
if(happy_check==1)
{
System.out.println(input_data+\" is a \"+\"HAPPY Number\");
}
else
{
while(happy_check!=1) // check until the correct happy number
{
happy_check=gn.getNumber(happy_data);
if(seenSet.contains(happy_check)) // if the current squared sum number exists in set
then it is a invalid happy number
{
System.out.println(input_data+\" is a \"+\"UNHAPPY Number\");
break;
}
else
{
seenSet.add(happy_check); // add squared sum number into set
}
if(happy_check==1)
{
System.out.println(input_data+\" is a \"+\"HAPPY Number\");
}
if(happy_check==happy_check1)
{
System.out.println(input_data+\" is a \"+\"UNHAPPY Number\");
break;
}
happy_data=\"\"+happy_check;
}
}
}
else
{
System.out.println(\"The Entered String Contains Alphabets or Some Special Charaters
Please Enter a Correct String\");
System.exit(1);
}
}
}
___________________TEST CASES______________________
--------------------Configuration: --------------------
Enter a Number String:
2345678901234567899999
2345678901234567899999 is a UNHAPPY Number
Process completed.
--------------------Configuration: --------------------
Enter a Number String:
19
19 is a HAPPY Number
Process completed.
____________________________MAIN CLASS 2_____________________________
import java.util.*;
public class Happy{
public static void main(String args[])
{
for(int cases=1;cases<10000;cases++)
{
Set seenSet = new HashSet();
check_String cs=new check_String();
GetNumber gn=new GetNumber();
String input_data=\"\";
input_data = input_data+cases; // Scans the next token of the input as an int.
if(cs.check_String_Value(input_data)==true)
{
String happy_data=input_data;
long happy_check1=gn.getNumber(happy_data);
long happy_check=happy_check1;
happy_data=\"\"+happy_check;
seenSet.add(happy_check1);
if(happy_check==1)
{
System.out.println(input_data);
}
else
{
while(happy_check!=1)
{
happy_check=gn.getNumber(happy_data);
if(seenSet.contains(happy_check))
{
//Sys.
NaOH is a strong electrolyte i.e. it is highly so.pdf
Happy Number Program Java __________________________________CLASS_.pdf
1. Happy Number Program Java
__________________________________CLASS_____________________________
public class GetNumber // This class is used to convert a string into digits and each digit is
squared and added
{
public long getNumber(String str) // this function take string and return a number that is
squared of each digit
{
long number=0;
char[] data=str.toCharArray(); // convert string into char array
for(int i=0;i seenSet = new HashSet(); // Create a Set to store the previous cases of invalid
squared sum
GetNumber gn=new GetNumber();
String input_data="";
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a Number String: ");
input_data = reader.nextLine(); // Scans the next token of the input as an int.
if(cs.check_String_Value(input_data)==true) // Check string vailidity
{
String happy_data=input_data;
long happy_check1=gn.getNumber(happy_data); // get squared sum number of digits
long happy_check=happy_check1;
happy_data=""+happy_check;
seenSet.add(happy_check1); // adding first squared sum number of digits
if(happy_check==1)
{
System.out.println(input_data+" is a "+"HAPPY Number");
}
else
{
while(happy_check!=1) // check until the correct happy number
{
happy_check=gn.getNumber(happy_data);
if(seenSet.contains(happy_check)) // if the current squared sum number exists in set
then it is a invalid happy number
2. {
System.out.println(input_data+" is a "+"UNHAPPY Number");
break;
}
else
{
seenSet.add(happy_check); // add squared sum number into set
}
if(happy_check==1)
{
System.out.println(input_data+" is a "+"HAPPY Number");
}
if(happy_check==happy_check1)
{
System.out.println(input_data+" is a "+"UNHAPPY Number");
break;
}
happy_data=""+happy_check;
}
}
}
else
{
System.out.println("The Entered String Contains Alphabets or Some Special Charaters
Please Enter a Correct String");
System.exit(1);
}
}
}
___________________TEST CASES______________________
--------------------Configuration: --------------------
Enter a Number String:
2345678901234567899999
2345678901234567899999 is a UNHAPPY Number
Process completed.
--------------------Configuration: --------------------
3. Enter a Number String:
19
19 is a HAPPY Number
Process completed.
____________________________MAIN CLASS 2_____________________________
import java.util.*;
public class Happy{
public static void main(String args[])
{
for(int cases=1;cases<10000;cases++)
{
Set seenSet = new HashSet();
check_String cs=new check_String();
GetNumber gn=new GetNumber();
String input_data="";
input_data = input_data+cases; // Scans the next token of the input as an int.
if(cs.check_String_Value(input_data)==true)
{
String happy_data=input_data;
long happy_check1=gn.getNumber(happy_data);
long happy_check=happy_check1;
happy_data=""+happy_check;
seenSet.add(happy_check1);
if(happy_check==1)
{
System.out.println(input_data);
}
else
{
while(happy_check!=1)
{
happy_check=gn.getNumber(happy_data);
if(seenSet.contains(happy_check))
{
//System.out.println(input_data+" is a "+"UNHAPPY Number");
break;
45. public class GetNumber // This class is used to convert a string into digits and each digit is
squared and added
{
public long getNumber(String str) // this function take string and return a number that is
squared of each digit
{
long number=0;
char[] data=str.toCharArray(); // convert string into char array
for(int i=0;i seenSet = new HashSet(); // Create a Set to store the previous cases of invalid
squared sum
GetNumber gn=new GetNumber();
String input_data="";
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a Number String: ");
input_data = reader.nextLine(); // Scans the next token of the input as an int.
if(cs.check_String_Value(input_data)==true) // Check string vailidity
{
String happy_data=input_data;
long happy_check1=gn.getNumber(happy_data); // get squared sum number of digits
long happy_check=happy_check1;
happy_data=""+happy_check;
seenSet.add(happy_check1); // adding first squared sum number of digits
if(happy_check==1)
{
System.out.println(input_data+" is a "+"HAPPY Number");
}
else
{
while(happy_check!=1) // check until the correct happy number
{
happy_check=gn.getNumber(happy_data);
if(seenSet.contains(happy_check)) // if the current squared sum number exists in set
then it is a invalid happy number
{
System.out.println(input_data+" is a "+"UNHAPPY Number");
break;
46. }
else
{
seenSet.add(happy_check); // add squared sum number into set
}
if(happy_check==1)
{
System.out.println(input_data+" is a "+"HAPPY Number");
}
if(happy_check==happy_check1)
{
System.out.println(input_data+" is a "+"UNHAPPY Number");
break;
}
happy_data=""+happy_check;
}
}
}
else
{
System.out.println("The Entered String Contains Alphabets or Some Special Charaters
Please Enter a Correct String");
System.exit(1);
}
}
}
___________________TEST CASES______________________
--------------------Configuration: --------------------
Enter a Number String:
2345678901234567899999
2345678901234567899999 is a UNHAPPY Number
Process completed.
--------------------Configuration: --------------------
Enter a Number String:
19
19 is a HAPPY Number
47. Process completed.
____________________________MAIN CLASS 2_____________________________
import java.util.*;
public class Happy{
public static void main(String args[])
{
for(int cases=1;cases<10000;cases++)
{
Set seenSet = new HashSet();
check_String cs=new check_String();
GetNumber gn=new GetNumber();
String input_data="";
input_data = input_data+cases; // Scans the next token of the input as an int.
if(cs.check_String_Value(input_data)==true)
{
String happy_data=input_data;
long happy_check1=gn.getNumber(happy_data);
long happy_check=happy_check1;
happy_data=""+happy_check;
seenSet.add(happy_check1);
if(happy_check==1)
{
System.out.println(input_data);
}
else
{
while(happy_check!=1)
{
happy_check=gn.getNumber(happy_data);
if(seenSet.contains(happy_check))
{
//System.out.println(input_data+" is a "+"UNHAPPY Number");
break;
}
else
{