LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Integer Numbers in C (https://www.linuxquestions.org/questions/programming-9/integer-numbers-in-c-374771/)

Mercurius 10-19-2005 02:35 PM

Integer Numbers in C
 
Ok, this is kind of a weird question. But how can I tell the compiler something like this

a = 3/2

if a is not integer then it is a prime number and print it.

Better said, how can the compiler tell if "a" is integer or not? Let me guess...it can't...

Hko 10-19-2005 02:56 PM

Quote:

how can the compiler tell if "a" is integer or not
The compiler knows it's an integer for sure, because you told it so in your code. (by using int literals while not telling it to treat it as a floating point number).

Mercurius 10-19-2005 02:58 PM

Yes, but the problem is with the flow of the program. I want something similar like this

if(a is integer)
{
printf("%d is a prime number\n", a);
}

Is there a way to check if a is integer? I know it is integer, but how can I do it in programming?

jlliagre 10-19-2005 03:19 PM

C being a language where all variables must be declared and typed, your test doesn't make sense, a is either already, or never will be an integer (i.e. declared as an int).

Perhaps are you asking for some algorithm that will tell is some double precision number is also equal to an integer, in that case look at the ceil and floor functions, but beware that due to rounding errors, you cannot be totally sure.

Also you cannot print a double with %d, you must cast your number to an int, or use %ld as a format.

itsme86 10-19-2005 03:49 PM

The modulus operator (%) returns the remainder left after division. So if you have num1 % num2 and the result is 0, then num2 divides evenly into num1.

Does that help?

Mercurius 10-19-2005 03:54 PM

Yes, that helped, i'll see now for the implementation.

Mercurius 10-19-2005 03:57 PM

Well, basicly I want this. I want to take a number, divide it by 2, and if the result is not an integer, then for sure that number is prime and then I want to print it. Problem is I need to check if the result is an integer or not.

perfect_circle 10-19-2005 04:04 PM

(integer)/(integer) is always an integer in C.:
Code:

#include <stdio.h>

int main()
{
        int a;
        float b;
        a=3/2;
        b=3/2;
        printf("a=%i\nb=%f",a,b);
}

Code:

skalkoto@darkstar:~/src$ ./a.out
a=1
b=1.000000
skalkoto@darkstar:~/src$


perfect_circle 10-19-2005 04:09 PM

Basically:
3/2=1 but
3.0/2.0 or 3/2.0 or 3.0/2 equals 1.5

Mercurius 10-19-2005 04:15 PM

Got that, I have to use floats, or I can implement that with the "%".

itsme86 10-19-2005 04:21 PM

0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0

Etc.

perfect_circle 10-19-2005 04:23 PM

Quote:

Originally posted by Mercurius
Well, basicly I want this. I want to take a number, divide it by 2, and if the result is not an integer, then for sure that number is prime and then I want to print it. Problem is I need to check if the result is an integer or not.
Oh Really?
A number not divedable by 2 is an odd number. Not a prime.
15 is not a prime and not dividable by 2.

You cannot decide if a number is prime without knowing the prime numbers smaller than this one.
All you can do is start computing the prime numbers and check if this one is one of the list or something like that, but you cannot decide without knowing the smaller prime numbers.
It's already proven that this is an unsolvable problem back in the 40's or 50's.

PTrenholme 10-19-2005 04:27 PM

Quote:

Originally posted by Mercurius
Well, basically I want this. I want to take a number, divide it by 2, and if the result is not an integer, then for sure that number is prime and then I want to print it. Problem is I need to check if the result is an integer or not.
Prime? You test woul only show that the number (if an integer) was odd not prime. "Prime" means the the only divisors of the number are 1 and the number itself.

There are many ways to test a number for "primeness," but the number of operation required to do so increases exponentially with the size of the number being tested. (It's an "NP-complete" problem.)

Try "google" for details.

Oh, a quick test for the parity of an integer is to look at its last bit. If it's zero, the number is even, if one, it's odd. That is, for an integer, just use
Code:

inline logical test(int a) {return (a & 1);}

Mercurius 10-19-2005 04:30 PM

You are right, I was wrong :) 15 can be divided by 3. How do you suggest to compute if one number is prime or not?

Got it, thanks alot for the help. One last question about flowcontrol. Cant I do in C something like go to line 15?

perfect_circle 10-19-2005 04:42 PM

Quote:

Originally posted by Mercurius
Got it, thanks alot for the help. One last question about flowcontrol. Cant I do in C something like go to line 15?
It's they
Code:

goto
command, but you are not supposed to use it.
you put a <tag> in line 15, and you use "goto <tag>"
Code:

static int cpio_mkgeneric_line(const char *line, enum generic_types gt)
{
        char name[PATH_MAX + 1];
        unsigned int mode;
        int uid;
        int gid;
        int rc = -1;

        if (4 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d", name, &mode, &uid, &gid)) {
                fprintf(stderr, "Unrecognized %s format '%s'",
                        line, generic_type_table[gt].type);
                goto fail;
        }
        mode |= generic_type_table[gt].mode;
        rc = cpio_mkgeneric(name, mode, uid, gid);
 fail:
        return rc;
}



All times are GMT -5. The time now is 12:17 PM.