Convert to number with digits as 3 and 8 only in C++


In this tutorial, we will be discussing a program to convert a number to have digits as 3 and 8 only.

For this we will be provided with a random number. Our task is to convert its digits to be only 3 and 8 by either adding/subtracting 1 from the number or converting digits of the number to any desired digit.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//calculating minimum operations required
int cal_min(long long int num){
   //calculating remainder and operations
   int rem;
   int count = 0;
   while (num) {
      rem = num % 10;
      if (!(rem == 3 || rem == 8))
      count++;
      num /= 10;
   }
   return count;
}
int main(){
   long long int num = 2341974;
   cout << "Minimum Operations: " << cal_min(num);
   return 0;
}

Output

Minimum Operations: 6

Updated on: 29-Jan-2020

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements