Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
#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
Advertisements
