Computer Science, asked by 12457gsuhdhdzzt, 10 months ago

Fascinating number
Program in java to check whether a number is fascinating number or not

Answers

Answered by kumarianshika310
4

Answer:ascinating Number in Java. Write a program to input a positive integer and check if it is a fascinating number. 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

Answered by PushpendraVatsha
8

Answer: a a fascinating number is a number which when multiplied with 2 and then with 3 ,the products are contacted with the original number the result obtained contains all the digits from 1 to 9 appearing only once.

For example:in 192

"192"+"192*2"+"192*3"

"192"+"384"+"576"

192384576

this above number contains all digits from 1 to 9 appearing only once...

Program of the given no(fascinating)is below.......

import java.util.Scanner;

// Java program to check for fascinating numbers

public class CheckFascinatingNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Please enter a 3 digit number: ");

int number = scanner.nextInt();

if (number < 100 || number > 999) {

System.out.println(number + " is not a valid 3 digit number!");

} else {

if (isFascinatingNumber(number)) {

System.out.println(number + " is a fascinating number!");

} else {

System.out.println(number + " is NOT a fascinating number!");

}

}

scanner.close();

}

// Checks whether the input number is a fascinating number

public static boolean isFascinatingNumber(int input) {

String sVal = "" + input + input * 2 + input * 3;

// check existence of 1 to 9 exactly once!

for (int i = 1; i <= 9; i++) {

int pos = sVal.indexOf(i + "");

// is digit missing?

if (pos < 0) {

return false;

} else {

// Is there a duplicate digit?

if (sVal.substring(pos+1).indexOf(i + "") >= 0) {

return false;

}

}

}

System.out.println(sVal);

return true;

}

}

Following is the sample output from the above program,

java CheckFascinatingNumber

Please enter a 3 digit number: 273

273546819

273 is a fascinating number!

if it helped you plz follow me and mark me sa brainiest

Similar questions