एक संख्या N को देखते हुए हमें उस संख्या को उसके सबसे बड़े विषम अंक से गुणा करना होगा। अगर कोई विषम अंक नहीं है तो -1 प्रिंट करें।
जैसे हमने एन को “153” के साथ इनिशियलाइज़ किया है और इस संख्या में सबसे बड़ा विषम अंक 5 है, इसलिए परिणाम 153 का गुणनफल होगा 5 यानी 153 * 5 =765 और यदि संख्या में 246 की तरह कोई विषम अंक नहीं है, तो आउटपुट होना चाहिए हो -1.
इनपुट - एन =198
आउटपुट - 1782
स्पष्टीकरण − 198 * 9 =1782
इनपुट - एन =15382
आउटपुट - 76910
स्पष्टीकरण − 15382 * 5 =76910
समस्या को हल करने के लिए नीचे इस्तेमाल किया गया तरीका इस प्रकार है -
-
इनपुट एन लें।
-
प्रत्येक अंक को पार करें और विषम अंक खोजें
-
सबसे बड़ा विषम तत्व खोजें।
-
मूल संख्या N के साथ सबसे बड़ा बंद तत्व उत्पाद करें।
-
यदि -1 के साथ कोई विषम तत्व अद्यतन परिणाम नहीं है।
-
परिणाम लौटाएं।
एल्गोरिदम
Start In function int largestodd(int n) Step 1→ Declare and Initialize large as -1 Step 2→ Loop While n > 0 Set digit as n % 10 If digit % 2 == 1 && digit > large then, Set large as digit Set n as n / 10 Step 3→ Return large In function int findproduct(int n) Step 1→ Declare and Initialize large set largestodd(n) Step 2→ If large == -1 then, Return -1 Step 3→ Return (n * large) In function int main() Step 1→ Initialize n as 15637 Print the results from calling findproduct(n) Stop
उदाहरण
#include <stdio.h>
int largestodd(int n){
// If all digits are even then
// we wil return -1
int large = -1;
while (n > 0) {
// checking from the last digit
int digit = n % 10;
// If the current digit is odd and
// is greater than the large
if (digit % 2 == 1 && digit > large)
large = digit;
n = n / 10;
}
// To return the maximum
// odd digit of n
return large;
}
int findproduct(int n){
int large = largestodd(n);
// If there are no odd digits in n
if (large == -1)
return -1;
// Product of n with its largest odd digit
return (n * large);
}
int main(){
int n = 15637;
printf("%d\n", findproduct(n));
return 0;
} आउटपुट
यदि उपरोक्त कोड चलाया जाता है तो यह निम्न आउटपुट उत्पन्न करेगा -
109459