Operator Sisa Pembagian c++

Selasa, 20 Maret 2012

Operator Sisa Pembagian
Operator sisa pembagian (operator modulus) yang berupa %. Operator ini diterapkan pada operand bertipe integer. Untuk lebih jelasnya perhatikan contoh berikut :

7 % 2 ® 1
Sisa pembagian bilangan 7 dengan 2 adalah 1
6 % 2 ® 0
Sisa pembagian bilangan 6 dengan 2 adalah 0
 8 % 3 ® 2
Sisa pembagian bilangan 8 dengan 3 adalah 2

Contoh program :
//*----------------------------------------------------------*
//* Contoh 2.3 : Melihat sisi pembagian dengan      *
//*                      menggunakan operator %.              *
//*----------------------------------------------------------*
#include <iostream.h>
#include <conio.h>

void main()
{
    clrscr();
    cout << 5 % 7 << ‘\n’;    //sisa 5
    cout << 6 % 7 << ‘\n’;    //sisa 6
    cout << 7 % 7 << ‘\n’;    //sisa 0
    cout << 8 % 7 << ‘\n’;    //sisa 1
    cout << 9 % 7 << ‘\n’;    //sisa 2
}
Hasil eksekusi:

 5
 6
 0
 1
 2

Kegunaan operator % diantaranya bisa dipakai untuk menentukan suatu bilangan bulat termasuk ganjil atau genap.


Share this article :

0 komentar:

Speak up your mind

Tell us what you're thinking... !

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kunci Nemu - All Rights Reserved
Template Created by Creating Website Inspired by Sportapolis Shape5.com
Proudly powered by Blogger