| Author |
Message |
beginner Guest
|
Posted: Sat Jun 25, 2005 10:08 pm Post subject: please help me fell a bit of satisfacion |
|
|
First of all, my english is extremely bad, so maby you will have some difficulties uderstanding my problem. I am beginner in c++ programming, and in programming at all. after few days learning sytax, I wanted to create a program that tests whether a inserted N number can be divided by 2 ( I think you call this number "even numbers", nbt as I said, my english is a weakspot ). So far, I know about the sequence, loops, and conditional statements. Is this a good code:
#include <iostream>
using namespace std;
int main()
{
double Broj, BrojPola, BrojMinus;
char Odg;
do {
cout<< "Unesi jedan prirodan broj: "; //prompt for N number to test
cin>> Broj;
cin.ignore();
BrojPola = Broj / 2; //half of the original number
BrojMinus = BrojPola;
while ( BrojMinus > 0.5 ) {
BrojMinus = BrojMinus - 1;
}
if ( BrojMinus == 0.5 ) {
cout<< "Broj "<< Broj<< " nije paran.\n"; //condition met> !even
}
else {
cout<< "Broj "<< Broj<< " jeste paran.\n"; //reverse ^ comment
}
cout<< "Zelite li da proverite novi broj"
"(d za DA, bilo sta drugo za NE): ";
cin>> Odg;
cin.ignore();
}
while ( Odg == 'd' );
} |
|
| Back to top |
|
 |
intrest86 Samurai++
Joined: 31 Dec 1969 Posts: 64 Location: Johns Hopkins
|
Posted: Sat Jun 25, 2005 11:22 pm Post subject: |
|
|
Well, to make life easier, read about the "%" (mod or modulus) operator. It returns the remainder from dividing the left integer by the right integer. For example:
5%2=1
6%2=0
125%11=4
Using that you can use integers to do the test instead of floating point numbers, and not use a loop. |
|
| Back to top |
|
 |
beginner Guest
|
Posted: Sun Jun 26, 2005 5:42 am Post subject: |
|
|
Something like this:
//==============================================
#include <iostream>
using namespace std;
int main()
{
long broj; //The number
short ostatak; //The Remainder
cout<< "Unesi N broj: ";
cin>> broj;
cin.ignore();
ostatak = broj % 2;
if ( ostatak == 0 ) {
cout<< "Broj "<< broj<< " je paran.\n";
}
else {
cout<< "Broje "<< broj<< " je neparan.\n";
}
cin.get();
} |
|
| Back to top |
|
 |
WbHack Bushi
Joined: 27 Jan 2005 Posts: 335 Location: Seattle
|
Posted: Sun Jun 26, 2005 11:04 am Post subject: |
|
|
if "je paran" means even - (or no remainder) - then yes.
by the way, you did use the right word for 2, 4, 6... EVEN.
If you didn't know, the word for numbers like 1, 3, 5... is ODD. |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2002 phpBB Group
|