25. A “Happy Word’ is defined as:
Take a word and calculate the word’s value based on position of the letters in English alphabet. On the basis of word’s value, find the sum of the squares of its digits. Repeat the process with the resultant number until the number equals 1 (one). If the number ends with 1 then it is called a ‘Happy Word’. Write a program to input a word and check whether it a ‘Happy Word’ or not. The program displays a message accordingly.
Sample Input:
VAT
Place value of V=22, A=1, T=20
[Hint: A=1, B=2,……….Z=26]
Solution: 22120 = 2^2+2^2+1^2+2^2+0^2 = 13 = 1^2 + 3^2 = 10 = 1^2+0^2 = 1
Sample Output : A Happy Word
Code :
import java.util.*;
class V25 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter String :");
String str = in.nextLine().toUpperCase();
String sum = "";
for(int i=0;i9) {
int sumd=0;
while(sumn!=0) {
int d = sumn%10;
sumd+=d*d;
sumn/=10;
}
sumn=sumd;
}
if(sumn==1) {
System.out.println("Happy String.");
}
else {
System.out.println("Not a Happy String.");
}
}
}