Programmer's Corner Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programmer's Corner - Forums


please help me fell a bit of satisfacion

 
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Analysis and Design
Author Message
beginner
Guest





PostPosted: Sat Jun 25, 2005 10:08 pm    Post subject: please help me fell a bit of satisfacion Reply with quote

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

PostPosted: Sat Jun 25, 2005 11:22 pm    Post subject: Reply with quote

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





PostPosted: Sun Jun 26, 2005 5:42 am    Post subject: Reply with quote

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

PostPosted: Sun Jun 26, 2005 11:04 am    Post subject: Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    Programmer's Corner Forum Index -> Analysis and Design All times are GMT - 5 Hours
Page 1 of 1

 


Powered by phpBB © 2001, 2002 phpBB Group