एक नंबर के साथ दिया गया और कार्य यह जांचना है कि इनपुट नंबर एक भाग्यशाली संख्या है या नहीं और परिणाम प्रदर्शित करना है।
लकी नंबर क्या है
भाग्यशाली संख्या वह संख्या है जिसका प्रत्येक अंक अलग है और यदि संख्या से कम से कम एक अंक दोहरा रहा है तो उसे भाग्यशाली संख्या नहीं माना जाएगा।
उदाहरण
Input-: n = 1234 Output-: it is a lucky number Explanation-: As there is no repeating digit in a number n so it is a lucky number Input-: n = 3434 Output-: it is not a lucky number Explanation-: In the given number n, 3 and 4 are repeating twice so it is not a lucky number
दिए गए कार्यक्रम में हम जिस दृष्टिकोण का उपयोग कर रहे हैं वह इस प्रकार है -
- यह एक भाग्यशाली संख्या है या नहीं, यह जांचने के लिए उपयोगकर्ता से नंबर n इनपुट करें
- संपूर्ण अंक को एक संख्या के आकार तक पार करें
- प्रत्येक विज़िट पर देखे गए अंक को चिह्नित करें और जांचें कि यह पहले से ही मिला है या नहीं
- दिखाएं कि दी गई संख्या एक भाग्यशाली संख्या है या नहीं
एल्गोरिदम
Start
Step1-> declare function to check whether a given number is lucky or not
bool check_lucky(int size)
declare bool arr[10]
Loop For int i=0 and i<10 and i++
Set arr[i] = false
End
Loop While(size > 0)
declare int digit = size % 10
IF (arr[digit])
return false
End
set arr[digit] = true
Set size = size/10
End
return true
Step 2-> In main()
Declare int arr[] = {0,34,2345,1249,1232}
calculate int size = sizeof(arr)/sizeof(arr[0])
Loop For int i=0 and i<size and i++
check_lucky(arr[i])?
print is Lucky : print is not Lucky
End
Stop उदाहरण
#include<iostream>
using namespace std;
//return true if a number if lucky.
bool check_lucky(int size) {
bool arr[10];
for (int i=0; i<10; i++)
arr[i] = false;
while (size > 0) {
int digit = size % 10;
if (arr[digit])
return false;
arr[digit] = true;
size = size/10;
}
return true;
}
int main() {
int arr[] = {0,34,2345,1249,1232};
int size = sizeof(arr)/sizeof(arr[0]);
for (int i=0; i<size; i++)
check_lucky(arr[i])? cout << arr[i] << " is Lucky \n": cout << arr[i] << " is not Lucky \n";
return 0;
} आउटपुट
19 is Lucky 34 is Lucky 2345 is Lucky 1249 is Lucky 1232 is not Lucky