- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Product of N with its largest odd digit in C
Given a number N with we have to product the number with its largest odd digit. If there is no odd digit then print -1.
Like we have initialized N with “153” and the largest odd digit in this number is 5 so the result would be the product of 153 with 5 i.e. 153 * 5 = 765 and if the number has no odd digit like 246 then the output must be -1.
Input − N = 198
Output − 1782
Explanation − 198 * 9 = 1782
Input − N = 15382
Output − 76910
Explanation − 15382 * 5 = 76910
Approach used below is as follows to solve the problem −
Take the input N.
Traverse every digit and look for odd digits
Find the largest odd element.
Product the largest off element with the original number N.
If there is no odd element update result with -1.
Return the result.
Algorithm
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
Example
#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
", findproduct(n)); return 0; }
Output
If run the above code it will generate the following output −
109459
- Related Articles
- Largest Even and Odd N-digit numbers in C++
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- Largest even digit number not greater than N in C++
- Largest N digit number divisible by given three numbers in C++
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Largest product of n contiguous digits of a number in JavaScript
- Maximum number with same digit factorial product in C++
- Largest Palindrome Product in C++
- Count number of ordered pairs with Even and Odd Product in C++
- Maximum GCD of N integers with given product in C++
- Largest set with bitwise OR equal to n in C++
- Check whether product of 'n' numbers is even or odd in Python
- Number of n digit stepping numbers in C++
- Find four factors of N with maximum product and sum equal to N in C++

Advertisements