Create a C + + program to determine whether a number is the user input is odd or even number.
Analysis:
To create C + + program that determines the odd or even number, we must first know how to distinguish between odd and even numbers. Examples of odd numbers are 1, 3, 5, 7, 9 and so on. Examples of even number is 2, 4, 6, 8 and so on. Before you can determine the odd and even numbers, you should know the rest of the division operator (MOD) / modulus.
Division remainder operator yields the remainder quotient, for more details see the following example:
10 MOD 3 = 1
12 MOD 10 = 2
4 MOD 2 = 0
5 MOD 10 = 5
Back to the matter, the logic of the answer, even numbers are numbers which can be divided in two, in the sense of an even number is the number that can be shared (with MOD) two (2) then the rest of her results is zero. While the odd number is opposite, ie numbers which are divided (MOD) two (2) remaining after him equal to 1 (not 0). Example:
4 MOD 2 = 0 means 4 is an even number.
13 MOD 2 = 1 means 13 is odd number.
8 MOD 2 = 0 means 8 is an even number.
25 MOD 2 = 1 means 25 are odd numbers.
PROGRAM :
#include (*iostream.h)
#include (*conio.h)
main(){
clrscr();
int bil,sisa;
cout<<"Masukan sebuah bilangan : ";
cin>>bil;
sisa = bil % 2;
if(sisa == 0)
cout<<bil<< "adalah bilangan genap"<<endl;
else
cout<<bil<< "adalah bilangan ganjil"<<endl;
getch();
}
Tidak ada komentar:
Posting Komentar