Programming Examples

Java program to check entered word is happy word or not


One of the system of calculating word value is Hebrew Numerology, in which corresponding numeric value of letter (1 to 26 for A to Z) are joined together. A “Happy Word” is a word whose eventual sum of square of the digits of its word value is equal to 1. Write a program to input a word and check whether it is a Happy Word or not. The program displays a message accordingly.

Sample Input: VAT

Place value V=22, A=1, T=20

[Hint A=1,b=2,.............................Z=26]

Solution: 22120 => 2^2+2^2+1^2+2^2+0=13 =>1^2+3^2=10 => 1^2+0=1

Sample Output: A Happy Word

Solution

import java.util.*;
class HappyWord
{
	static int sumDigit(int n)
	{
		int sum=0,digit;
		while(n>0)
		{
			digit=n%10;
			sum=sum+digit*digit;
			n=n/10;
		}
		return(sum);
	}
	public static void main(String arr[])
	{
		Scanner sc=new Scanner (System.in);
		System.out.println("Enter Any String");
		String str=sc.nextLine();
		String pos="";
		int n;
		str=str.toUpperCase();
		for(int a=0;a<str.length();a++)
		{
			n=((int)str.charAt(a))-64; // Find the position value of each Character
			pos=pos+Integer.toString(n); // concatenate the position value by converting it in string
		}
		
		n=Integer.valueOf(pos); // Conver the value String to Integer
		int sum=HappyWord.sumDigit(n); 
		while(sum>9)
		{
			sum=HappyWord.sumDigit(sum);
		}
		if(sum==1)
		{
			System.out.println("String is HAPPY WORD");
		}
		else
		{
			System.out.println("String is not HAPPY WORD");
		}
	}
}
Output

Enter Any String

RAJ

18110

String is not HAPPY WORD

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz