- 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

# Convert a number m to n using minimum number of given operations in C++

In this tutorial, we will be discussing a program to convert a number m to n using minimum number of given operations.

For this we will be provided with two integers m and n. Our task is to convert the integer m to n using the given operations least times.

Allowed operations −

Multiply the given number by 2

Subtract one from the given number

## Example

#include <bits/stdc++.h> using namespace std; //finding minimum number of operations required int convert(int m, int n){ if (m == n) return 0; if (m > n) return m - n; //can't convert in this situation if (m <= 0 && n > 0) return -1; //when n is greater and n is odd if (n % 2 == 1) //performing '-1' on m return 1 + convert(m, n + 1); //when n is even else //performing '*2' on m return 1 + convert(m, n / 2); } int main(){ int m = 5, n = 11; cout << "Minimum number of operations : " << convert(m, n); return 0; }

## Output

Minimum number of operations : 5

- Related Articles
- Finding minimum number of required operations to reach n from m in JavaScript
- Minimum number of given operations required to make two strings equal using C++.
- C++ program to count minimum number of operations needed to make number n to 1
- Minimum number using set bits of a given number in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- Minimum number of squares whose sum equals to given number n
- Find the minimum number of steps to reach M from N in C++
- Minimum number of operations required to sum to binary string S using C++.
- Minimum number of operations required to delete all elements of the array using C++.
- Minimum and Maximum number of pairs in m teams of n people in C++
- Minimum number of palindromes required to express N as a sum using C++.
- Minimum number of operations on an array to make all elements 0 using C++.
- Count the number of operations required to reduce the given number in C++
- Minimum number of power terms with sum equal to n using C++.
- Reduce a number to 1 by performing given operations in C++

Advertisements