0% found this document useful (0 votes)
3 views5 pages

Algorithm Specimen

The document outlines a Java program that identifies and displays happy numbers within a user-defined range. It includes a class 'Happy' with methods to check if a number is happy and to display happy numbers in the specified range. The algorithm details the steps for initializing variables, processing input, and iterating through the range to find and print happy numbers.

Uploaded by

abhijit betal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Algorithm Specimen

The document outlines a Java program that identifies and displays happy numbers within a user-defined range. It includes a class 'Happy' with methods to check if a number is happy and to display happy numbers in the specified range. The algorithm details the steps for initializing variables, processing input, and iterating through the range to find and print happy numbers.

Uploaded by

abhijit betal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Algorithm specimen for the given code

Code:
//display happy numbers within given range

import java.util.*;

class Happy

private int l,u;

public Happy(int l,int u)//parameterised constructor

this.l=l;

this.u=u;

boolean isHappy(int n)

int p=n,s=0;

do

s=0;

while(p>0)

int r=p%10; //extracting digits from least positional value

p/=10; //extrcting the remaining digits not yet extracted

s=s+r*r;

Page 1 of 5
}

p=s;

}while(s>9);

if(s==1)

return true;

else

return false;

void display()

int f=0;

System.out.println("Happy numbers between "+l+" and "+u+":");

for(int i=l;i<=u;i++)

if(isHappy(i))

System.out.println(i);

f=1;

if(f==0)

System.out.println("No happy numbers within range");

public static void main(String args[])

Page 2 of 5
{

Scanner sc=new Scanner(System.in);

int low=0,up=0;

do

System.out.println("Enter lower range and upper range");

low=sc.nextInt();

up=sc.nextInt();

}while(low<=0 || up<=0 || low>up);

Happy h=new Happy(low,up);

h.display();

Algorithm:
Considering variable l and u to store lower and upper range value

Constructor Happy(int,int)

Step 1: Start

Step 2: Both the variable l and u are initialised with user- define values

Step 3: end of function

Method isHappy(int n):

Step 1: Start

Step 2: Assigning value n to variable p and initialising variable s with the value 0 after declraration.

Step 3: Introducing an outer loop which will iterate until the value of s becomes less or equal to 9.

Step 4: Assign 0 to variable s

Step 5: Introducing an inner nested loop structure which will iterate untill value of p becomes zero.

Page 3 of 5
Step 6: Store the value of remainder when p is divided by 10 in variable r

Step 7: Store the quotient when p is divided by 10 in variable p

Step 8: Multiply the value in r with r and add the result with the value in s and store the final result
in s.

Step 9: Inner loop closed.

Step 10: Assign value of s in variable p

Step 11: Outer loop closed.

Step 12: if the value in s is equal to 1 then method will return true otherwise it will return false.

Step 13: end of function

Method display():

Step 1: Start

Step 2: Declaring variable f and initialising with 0

Step 3: Introducing loop where the loop control variable i considers value in variable l as initial value
and value of i must be less or equal to u.

Step 4: if the value returned after calling isHappy() method is true then goto step 5 otherwise goto
step 6

Step 5: Display value stored in i and update the value in f by 1

Step 6: Increase value of i by 1

Step 7: End of loop

Step 8: if value in f equals to zero then display an appropriate message that no happy numbers
exists in the given range.

Step 9: end of function

Method main(String):

Step 1: start

Step 2: Take user defined values in the variables low and up considered as lower and upper range
respectively.

Step 3: Create object and call the method display()

Page 4 of 5
Step 4: end of function

Page 5 of 5

You might also like