LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2005, 02:35 PM   #1
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Rep: Reputation: 15
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...
 
Old 10-19-2005, 02:56 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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).
 
Old 10-19-2005, 02:58 PM   #3
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
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?
 
Old 10-19-2005, 03:19 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 10-19-2005, 03:49 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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?
 
Old 10-19-2005, 03:54 PM   #6
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
Yes, that helped, i'll see now for the implementation.
 
Old 10-19-2005, 03:57 PM   #7
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
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.
 
Old 10-19-2005, 04:04 PM   #8
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
(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$

Last edited by perfect_circle; 10-19-2005 at 04:10 PM.
 
Old 10-19-2005, 04:09 PM   #9
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Basically:
3/2=1 but
3.0/2.0 or 3/2.0 or 3.0/2 equals 1.5
 
Old 10-19-2005, 04:15 PM   #10
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
Got that, I have to use floats, or I can implement that with the "%".
 
Old 10-19-2005, 04:21 PM   #11
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0

Etc.
 
Old 10-19-2005, 04:23 PM   #12
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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.

Last edited by perfect_circle; 10-19-2005 at 04:25 PM.
 
Old 10-19-2005, 04:27 PM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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);}
 
Old 10-19-2005, 04:30 PM   #14
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
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?

Last edited by Mercurius; 10-19-2005 at 04:33 PM.
 
Old 10-19-2005, 04:42 PM   #15
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Integer to Char gjagadish Programming 5 10-14-2005 10:09 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM
64 bit integer fahad153 Programming 9 08-26-2003 02:03 AM
length of integer variables daztheladd Programming 5 11-26-2002 07:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration