- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this tutorial, we are going to write a program that divides a number into two parts with a difference of k.

Let's see an example.

**Input**

n = 100 k = 30

**Output**

65 35

Here, we need to understand a little bit of math before diving into the problem. Let's see it.

We have a + b = n and a - b = k. By adding the two equations, we get

a = (n + k)/2 b = n - a

That's it. We have n and k. And there is nothing more in it. Let's see the code

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

If you run the above code, then you will get the following result.

33 21

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

- Related Questions & Answers
- Divide a number into two parts in C++ Program
- Divide number into two parts divisible by given numbers in C++ Program
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Divide a string into n equal parts - JavaScript
- Count number of ways to divide a number in parts in C++
- Divide a string in N equal parts in C++ Program
- Check if a line at 45 degree can divide the plane into two equal weight parts in C++
- Program to count substrings that differ by one character in Python
- How to divide an unknown integer into a given number of even parts using JavaScript?
- Possible cuts of a number such that maximum parts are divisible by 3 in C++
- Java Program to divide a number into smaller random ints
- All ways to divide array of strings into parts in JavaScript
- Prime points (Points that split a number into two primes) in C++
- Find nth number that contains the digit k or divisible by k in C++
- Find count of digits in a number that divide the number in C++

Advertisements