Tuesday, January 22, 2019

Integer division and remainder of an integer number by a natural number

Write a program that reads an integer number a and a natural number b, with b > 0, and prints the integer division d and the remainder r of a divided by b.

Remember that, by definition, d i r must be the only integer numbers such that 0 ≤ r < b and d · b + r = a.

Input : Input consists of a and b, with b > 0.

Output : Print a line with the integer division and the remainder of a divided by b, separated by a space.

#include <iostream>
using namespace std;

int main () {
    int a, b;
    cin >> a >> b;
    
    int d, r;
    if (a%b < 0) {
        r= (a%b) +b ;
        d= a/b -1 ;
        cout << d << " " << r << endl;
    }
    
    else {
        d=a/b;
        r=a-b*d;
        cout << d << " " << r << endl;
    }
    
}

No comments:

Post a Comment

Modelo OSI

Open System Interconnection , Interconexión de Sistemas Abiertos, es un modelo para estudiar las categorías en que se pueden dividir los pr...