0% found this document useful (0 votes)
28 views

Isc Computer Project #3

Uploaded by

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

Isc Computer Project #3

Uploaded by

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

Program 3:-

Question: A Fascinating number is one which when multiplied by 2 and 3 and then, after the
results are concatenated with the original number, the new number contains all the digits from
1 to 9 exactly once. There can be any number of zeroes and are to be ignored.

Write a program to display all Fascinating numbers that are in the range between m and n
(both inclusive) and output them along with the frequency.

Algorithm:

Step 1: START

Step 2: Create a static method check(String s) which returns a boolean value

Sub-Steps:
2.1: Set chk = true;
2.2: Set a loop for char i=’1’ till i<=9, i++. if s.indexOf(i)= -1
or s.indexOf(i) != s.lastIndexOf(i), set chk = false and
break
2.3: Return chk

Step 3: In main method, input lower and higher range values


from user, i.e., m and n

Step 4: Set f=0


Step 5: Print “All fascinating numbers within range”
Step 6: Start a loop for int i=m to i<=n, I increased by 1 each
iteration

Sub-Steps:
6.1: If check(i + "" + (i * 2) + "" + (i * 3)) = true, go to step
6.2 else go to step 7
6.2: Print i and increase f by 1

Step 7: Print “Frequency” along with f


Step 8: END

Source Code:

import java.util.Scanner;
public class Fascinating {

static boolean check(String s) {

boolean chk = true;

for (char i = '1'; i <= '9'; i++)


if (s.indexOf(i) == -1 || s.indexOf(i) != s.lastIndexOf(i)) {
chk = false; //checks whether each digit is present once
break;
}
return chk;

public static void main(String[] args)


{

Scanner sc = new Scanner(System.in);


System.out.println("Enter the range:-");
System.out.print("Starting at: ");
long m = sc.nextLong();
System.out.print("Ending at: ");
long n = sc.nextLong();

long f = 0;

System.out.println("\nAll Fascinating numbers within " + m +


“and " + n + " (inclusive) are:-");

for (long i = m; i <= n; i++)

if (check(i + "" + (i * 2) + "" + (i * 3)) ) { //concatenates 3 no.


System.out.println(i);
f++; // calculate frequency
}

System.out.println("\nFrequency:" + f);

} //end of main

} //end of class
Variable Description:

Variable Name Data Type Description


m long Stores lower range
n long Stores higher range
f long Stores frequency
long(in main), char(in
i Used to operate for loop
method)
chk boolean Stores true/false
Receives string in parameter
s String
list

Sample Input / Output:

You might also like