Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int enter(int max, int min) {
- int temp = 0;
- bool iscorrect;
- do {
- cin >> temp;
- if ((temp > min) && (temp < max)) {
- iscorrect = false;
- } else {
- cout << "This is a mistake. Please enter again!\n";
- iscorrect = true;
- }
- } while (iscorrect);
- return temp;
- }
- int transfer(int j, int n, int num) {
- int res = 0;
- int temp = 1;
- for (int i = j; i < n - 1; i++) {
- temp = temp * 10;
- }
- res = res + (num * temp);
- return res;
- }
- int subtraction(int j, int n, int num) {
- int res = 0;
- int temp = 1;
- temp = temp * 10;
- res = res + (num % temp);
- return res;
- }
- int main() {
- int n;
- int num1 = 0;
- int num2 = 0;
- int numb1[10];
- int numb2[10];
- int res[10];
- cout << "Enter the number of numb (n > 0 and n < 10)\n";
- n = enter(10, 0);
- for (int i = 0; i < n; i++) {
- cout << "Enter the " << i + 1 << " digit of the first number(digit > 0 and digit < 10)\n";
- numb1[i] = enter(10, -1);
- }
- for (int i = 0; i < n; i++) {
- cout << "Enter the " << i + 1 << " digit of the second number(digit > 0 and digit < 10)\n";
- numb2[i] = enter(10, -1);
- }
- for (int i = n - 1; i > -1; i--) {
- num1 = num1 + transfer(i, n, numb1[i]);
- num2 = num2 + transfer(i, n, numb2[i]);
- }
- num1 = num1 - num2;
- if (num1 < 0) {
- num1 = abs(num1);
- for (int i = n - 1; i > -1; i--) {
- res[i] = subtraction(i, n, num1);
- num1 = num1 / 10;
- }
- res[0] = res[0] * (-1);
- } else {
- for (int i = n - 1; i > -1; i--) {
- res[i] = subtraction(i, n, num1);
- num1 = num1 / 10;
- }
- }
- for (int i = 0; i < n; i++) {
- cout << res[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement