check if number is december

V

veki

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;
}
 
D

Daniel T.

veki said:
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.
 
R

Rolf Magnus

veki said:
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.
 
O

Old Wolf

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'.
 
J

Jim Langston

veki said:
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;
}
 
V

vedrandekovic

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top