forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FascinatingNumber.cpp
44 lines (36 loc) · 1.31 KB
/
FascinatingNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <set>
//A fascinating number is a number that, when multiplied by 2 and 3, and then both results are concatenated with the original number, contains all the digits from 1 to 9 exactly once
bool isFascinating(int num) {
// Multiply num by 2 and 3
int num2 = num * 2;
int num3 = num * 3;
// Concatenate the three numbers as strings
std::string concatenated = std::to_string(num) + std::to_string(num2) + std::to_string(num3);
// Create a set to store unique digits
std::set<char> uniqueDigits;
// Check each character in the concatenated string
for (char digit : concatenated) {
if (digit >= '1' && digit <= '9') {
// If it's a digit from 1 to 9, add it to the set
uniqueDigits.insert(digit);
} else {
// If any character is not a digit from 1 to 9, it's not fascinating
return false;
}
}
// Check if the set contains all digits from 1 to 9
return uniqueDigits.size() == 9;
}
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
if (isFascinating(num)) {
std::cout << num << " is a fascinating number!" << std::endl;
} else {
std::cout << num << " is not a fascinating number." << std::endl;
}
return 0;
}