By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

check if number is december

P: n/a
Hello,

What shoud I change in this code to check, if some number is december:

#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
int a,b;
int n=2;
int december=1;
cin>>a;
for(a/n;a<n;n++){
if (!(a%n)==0){
december=1;
}
else{
printf("Broj je prost");
}
}
scanf("\n");
return 0;
}
Nov 11 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"veki" <ve***********@v-programs.comwrote:
Hello,

What shoud I change in this code to check, if some number is december:
I'm not sure what it means for a number to be "december" could you
explain this concept better? Are you asking how to tell if some number
is prime?
#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
int a,b;
'b' is never used in the program, what is it for?
int n=2;
int december=1;
cin>>a;
What should your program do if the user types in "hello" instead of a
number?
for(a/n;a<n;n++){
The "a/n" part of the for loop does nothing. That is where you should be
initializing 'n'. If 'n' is already initialized, then leave that part of
the loop blank.
if (!(a%n)==0){
december=1;
}
else{
printf("Broj je prost");
}
}
scanf("\n");
return 0;
}
I suggest you start over with a function:

bool is_december( int value )
{
bool result = false;
// change result to true if the number is december
return result;
}

int main()
{
assert( is_december( 2 ) == true );
cout << "Good job!\n";
scanf("\n");
}

I'm assuming that '2' has the property december, if not use a number
that does.

Once you get the program to print out "Good Job!" show me your code and
I'll help you more.
Nov 11 '07 #2

P: n/a
veki wrote:
Hello,

What shoud I change in this code to check, if some number is december:
I have no idea what you mean by "some number is december".
>
#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
int a,b;
You never use b.
int n=2;
int december=1;
Looks like a better type for december would be bool.
cin>>a;
for(a/n;a<n;n++){
The initializer of your for loop makes no sense, since it has no side
effects. I also don't know what that loop is supposed to do. As it is now,
it starts with n=2, then increments it as long as it's greater than a. So
if the number the user entered is less than 2, the loop isn't executed at
all. In all other cases, it runs until your integer overflows, which then
results in undefined behavior.
if (!(a%n)==0){
december=1;
december was already initialized to 1. Here, you assign 1 to it again, so
the value isn't really changed. Your program never writes another value
into it, so that variable will always contain the value 1. You also don't
read that variable anywhere.
}
else{
printf("Broj je prost");
}
}
scanf("\n");
return 0;
}
You should explain in more detail what you want to do, because frankly, I
couldn't figure that out from your code.

Nov 11 '07 #3

P: n/a
On Nov 12, 2:51 am, "Daniel T." <danie...@earthlink.netwrote:
"veki" <vedrandeko...@v-programs.comwrote:
What shoud I change in this code to check, if some number is december:
printf("Broj je prost");

I'm not sure what it means for a number to be "december" could you
explain this concept better? Are you asking how to tell if some number
is prime?
Well, 'prost' means prime in slavic languages, so I
suppose you are on the right track. My best guess would
be that he means to say 'divisible' rather than
'december'.

Nov 12 '07 #4

P: n/a

"veki" <ve***********@v-programs.comwrote in message
news:fh*********@enews2.newsguy.com...
Hello,

What shoud I change in this code to check, if some number is december:

#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
int a,b;
int n=2;
int december=1;
cin>>a;
for(a/n;a<n;n++){
if (!(a%n)==0){
december=1;
}
else{
printf("Broj je prost");
}
}
scanf("\n");
return 0;
}
What you are trying to do is hard to ascertain. "December" in English is a
month of the year, the 12th month. But it seems you are trying to determine
if a number is prime or not (from others comments and your code) but it is
hard to determine what you are trying to do with the december variable.

Maybe you're trying to determine prime factors?
As for:
for(a/n;a<n;n++){
you probably meant
for(a=n;a<n;n++){

Yet, you are looking for the opposite, !(a%n)== 0 isntead of (a&n)==0. If a
number is evenly divisible, the remainder will be prime. So shouldn't this
be:

if ( a%n == 0 )
// Number is NOT prime

Now, it does no good to check just one number, you have to check them all.
Yet you are stating "Bjoj je prost" after every check. So I'm thinking that
december may be a flag you are trying to set. Mabe you want something like
this.

#include <iostream>

int main(){
int Number;
std::cout << "Enter number to check for prime:";
if ( std::cin >Number )
{
bool Prime = true; // Guilty until proven innocent
for( int i = 2; i < Number; i++){
if (Number%i ==0 ){
Prime = false;
break;
}
}
if ( Prime )
std::cout << "Number is prime\n";
else
std::cout << "Number is not prime\n";
}
return 0;
}


Nov 12 '07 #5

P: n/a
On 12 stu, 07:49, "Jim Langston" <tazmas...@rocketmail.comwrote:
"veki" <vedrandeko...@v-programs.comwrote in message

news:fh*********@enews2.newsguy.com...
Hello,
What shoud I change in this code to check, if some number isdecember:
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int a,b;
int n=2;
intdecember=1;
cin>>a;
for(a/n;a<n;n++){
if (!(a%n)==0){
december=1;
}
else{
printf("Broj je prost");
}
}
scanf("\n");
return 0;
}

What you are trying to do is hard to ascertain. "December" in English is a
month of the year, the 12th month. But it seems you are trying to determine
if a number is prime or not (from others comments and your code) but it is
hard to determine what you are trying to do with thedecembervariable.

Maybe you're trying to determine prime factors?
As for:
for(a/n;a<n;n++){
you probably meant
for(a=n;a<n;n++){

Yet, you are looking for the opposite, !(a%n)== 0 isntead of (a&n)==0. If a
number is evenly divisible, the remainder will be prime. So shouldn't this
be:

if ( a%n == 0 )
// Number is NOT prime

Now, it does no good to check just one number, you have to check them all.
Yet you are stating "Bjoj je prost" after every check. So I'm thinking thatdecembermay be a flag you are trying to set. Mabe you want something like
this.

#include <iostream>

int main(){
int Number;
std::cout << "Enter number to check for prime:";
if ( std::cin >Number )
{
bool Prime = true; // Guilty until proven innocent
for( int i = 2; i < Number; i++){
if (Number%i ==0 ){
Prime = false;
break;
}
}
if ( Prime )
std::cout << "Number is prime\n";
else
std::cout << "Number is not prime\n";
}
return 0;

}
Hello,
Thanks it was useful!

Regards,
Vedran
Nov 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.