ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

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).

Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL

Posts: 9,767

Rep:

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.

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.

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.

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 05:25 PM.

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

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.