Hàm div lấy thương và số dư của phép chia

div_t div(int numer, int denom);
ldiv_t ldiv(long numer, long denom);

Parameter:

numer: tử số

denom:  mẫu số

Remark:

Hàm div( ) (ldiv( )) tính toán phép chia numer/denom. Hàm trả về struct div_t hoặc ldiv_t. struct div_t (ldiv_t) gồm có 2 thành phần thương và số dư.

Nếu denom = 0 thì chương trình sẽ kết thúc (exception) với message lỗi.

typedef struct _div_t 
{
    int quot;
    int rem;
} div_t;

typedef struct _ldiv_t 
{
    long quot;
    long rem;
} ldiv_t;

Ví dụ:

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

void main( void )
{
    long x = 5149627, y = 234879;
    ldiv_t div_result;

    div_result = ldiv( x, y );
    printf( "For %ld / %ld, the quotient is ", x, y );
    printf( "%ld, and the remainder is %ld\n", 
    div_result.quot, div_result.rem );
    getch();
}

Kết quả:

Hàm div lấy thương và số dư của phép chia

Be the first to comment

Leave a Reply