Convert to number with digits as 3 and 8 only Last Updated : 15 Dec, 2022 Comments Improve Suggest changes Like Article Like Report A charming number is a number which only consist 3 and 8 as its digits. We are given a n-digit number you have total of three operations: Adding 1 to given number.Subtracting 1 to given number.Selecting a digit of number and replacing it with any other desired digit. We need to find the total number of operations to change given number to charming number.Examples: Input : num = 343 Output : Minimum Operation = 1 Input : num = 88 Output : Minimum operation = 0 Before moving to proper solution let's have closer look on given operations. Adding 1 to given number, it will count 1 operation and what it can do is increment last digit by 1 unless it is 9 and if last digit is 9 then it will also change the digits preceding last one. So in the best case if last digit is 2 or 7 this operation will change them to 3 or 8 on the cost of 1 operation.Subtracting 1 to given number, will also count 1 as operation and only decrease the last digit unless it is 0. So in the best case if last digit is 4 or 9 this operation will change them to 3 or 8 on the cost of 1 operation.Selecting any digit and changing its value will count as a single operation but for sure it will change digit to charming digit i.e. 3 or 8. So, in the best as well as worst case this operation will change a single digit at once. So, for finding the minimum operation addition and subtraction is not going to be useful for us and only the number of digits which are not equal to 3 or 8 are going to be selected and changed. So, total number of digit not equal to 3 or 8 will be our answer. C++ // CPP to find min operations required to // convert into charming number #include <bits/stdc++.h> using namespace std; // function for minimum operation int minOp(long long int num) { // remainder and operations count int rem; int count = 0; // count digits not equal to 3 or 8 while (num) { rem = num % 10; if (!(rem == 3 || rem == 8)) count++; num /= 10; } return count; } // driver function int main() { long long int num = 234198; cout << "Minimum Operations =" << minOp(num); return 0; } Java // Java to find min operations required to // convert into charming number public class GFG { // function for minimum operation static int minOp(int num) { // remainder and operations count int rem; int count = 0; // count digits not equal to 3 or 8 while (num>0) { rem = num % 10; if (!(rem == 3 || rem == 8)) count++; num /= 10; } return count; } // Driver code public static void main (String[] args) { int num = 234198; System.out.print("Minimum Operations ="+minOp(num)); } } // This code is contributed by Anant Agarwal. Python3 # Python to find min # operations required to # convert into charming number # function for minimum operation def minOp(num): # remainder and operations count count = 0 #count digits not equal to 3 or 8 while (num): rem = num % 10 if (not(rem == 3 or rem == 8)): count=count + 1 num = num // 10 return count # Driver code num = 234198 print("Minimum Operations =" ,minOp(num)) # This code is contributed # by Anant Agarwal. C# // C# to find min operations required to // convert into charming number using System; class GFG { // function for minimum operation static int minOp(int num) { // remainder and operations count int rem; int count = 0; // count digits not equal to 3 or 8 while (num > 0) { rem = num % 10; if (! (rem == 3 || rem == 8)) count++; num /= 10; } return count; } // Driver code public static void Main () { int num = 234198; Console.WriteLine("Minimum Operations =" + minOp(num)); } } // This code is contributed by vt_m. PHP <?php // PHP to find min operations required to // convert into charming number // function for minimum operation function minOp($num) { // remainder and operations count $count = 0; // count digits not equal to 3 or 8 while ($num) { $rem = intval($num % 10); if (!($rem == 3 || $rem == 8)) $count++; $num = intval($num / 10); } return $count; } // Driver Code $num = 234198; echo "Minimum Operations = " . minOp($num); // This code is contributed by Sam007 ?> JavaScript <script> // Javascript to find min operations required to // convert into charming number // function for minimum operation function minOp(num) { // remainder and operations count var rem; var count = 0; // count digits not equal to 3 or 8 while (num) { rem = num % 10; if (!(rem == 3 || rem == 8)) count++; num = parseInt(num/10); } return count; } // driver function var num = 234198; document.write( "Minimum Operations = " + minOp(num)); </script> OutputMinimum Operations =4 Comment More infoAdvertise with us Next Article Number of substrings divisible by 8 but not by 3 S Shivam.Pradhan Follow Improve Article Tags : Mathematical DSA number-digits Practice Tags : Mathematical Similar Reads Program to convert a binary number to octal The problem is to convert the given binary number (represented as string) to its equivalent octal number. The input could be very large and may not fit even into unsigned long long int. Examples: Input : 110001110 Output : 616 Input : 1111001010010100001.010110110011011 Output : 1712241.26633 The id 9 min read Program to convert float decimal to Octal number Write a program that converts a floating-point decimal number into its octal format. The program should take a number and the precision for the octal fractional part as inputs from the user. It should calculate and provide its correct octal representation as output. Examples: Input: Number = 123.45, 8 min read Convert textual Phone Number to 10 digit numerical number Given a string S of size N containing lowercase English letters, representing a phone number(all phone numbers will be 10 digits) in words, the task is to convert the number into digits. Repeating digits can be shortened as follows: If any digit repeats two times then in words is written as "double" 9 min read Number of substrings divisible by 8 but not by 3 Given a string of digits "0-9". The task is to find the number of substrings which are divisible by 8 but not by 3. Examples : Input : str = "888" Output : 5 Substring indexes : (1, 1), (1, 2), (2, 2), (2, 3), (3, 3). Input : str = "6564525600" Output : 15Recommended: Please solve it on âPRACTICE â 11 min read JavaScript Number toString() Method The toString() method in JavaScript returns a number as a string. It allows converting numerical values to string representations, enabling operations like formatting output, displaying numbers in various formats, and facilitating string manipulation based on numeric values.Syntax:num.toString(base) 2 min read Convert the number from Indian system to International system Given an input string N consisting of numerals and separators (, ) in the Indian Numeric System, the task is to print the string after placing separators(, ) based on International Numeric System. Examples: Input: N = "12, 34, 56, 789" Output: 123, 456, 789 Input: N = "90, 05, 00, 00, 000" Output: 9 5 min read Like