Divide a number into two parts in C++ Program


In this tutorial, we are going to write a program that divides the given number into two parts

It's a straightforward problem to solve. We can get a number by diving the given number. And we can get the second number by subtracting the result from the total.

If the given number is n, then the two numbers are

a = n / 2
b = n - a

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void divideTheNumber(int n) {
   int a = n / 2;
   int b = n - a;
   cout << a << " " << b << endl;
}
int main() {
   int n = 13;
   divideTheNumber(n);
}

Output

If you execute the above program, then you will get the following result.

6 7

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 27-Jan-2021

549 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements