-
Notifications
You must be signed in to change notification settings - Fork 72
/
fascinating-number
68 lines (59 loc) · 1.42 KB
/
fascinating-number
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// C++ program to implement
// fascinating number
#include <bits/stdc++.h>
using namespace std;
// function to check if number
// is fascinating or not
bool isFascinating(int num)
{
// frequency count array
// using 1 indexing
int freq[10] = {0};
// obtaining the resultant number
// using string concatenation
string val = "" + to_string(num) +
to_string(num * 2) +
to_string(num * 3);
// Traversing the string
// character by character
for (int i = 0; i < val.length(); i++)
{
// gives integer value of
// a character digit
int digit = val[i] - '0';
// To check if any digit has
// appeared multiple times
if (freq[digit] and digit != 0 > 0)
return false;
else
freq[digit]++;
}
// Traversing through freq array to
// check if any digit was missing
for (int i = 1; i < 10; i++)
{
if (freq[i] == 0)
return false;
}
return true;
}
// Driver code
int main()
{
// Input number
int num = 192;
// Not a valid number
if (num < 100)
cout << "No" << endl;
else
{
// Calling the function to
// check if input number
// is fascinating or not
bool ans = isFascinating(num);
if (ans)
cout << "Yes";
else
cout << "No";
}
}