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

C++Server Side ProgrammingProgramming

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
raja
Published on 29-Jan-2020 12:14:12
Advertisements