# Basic C Program - Help with logic!

1. Jan 30, 2013

### Saterial

So I'm working on two questions while learning C.

The first question is, I need to create a program that prompts a user "Please enter an integer" and display whether the answer is odd or even. I'm using the logic that you can divide the input by 2, if the answer is a whole number it is even, if it's a decimal it is odd. How can you check whether the input is a decimal or not? Would this solution work for a question that asked for an input of 2 numbers, say "15 5"? Let 15 be a, 5 be b and divide a by b; if answer is whole number then it is a multiple, else it is an odd number?

Second question is, I need to create a program that prompts a user "Please enter a five integers:" for example "5 12 -10 0 1". From there it will display "The max is: 12" and "The min is -10". How can I do this without using an array?

I get the idea behind it, I just can't figure out how to make it check whether the output of the calculation is a float. Additionally, this is an intro to learning C class; my idea for a solution to question 1 was using something like "scanf("%d", a b c d e); and then a long line of code checking if each variable is greater than eachother. This obviously didn't seem very efficient to me is there another basic way I can try?

Thanks!

2. Jan 30, 2013

### NemoReally

Look up the modulus operator.

I've never done a C programming course, but I imagine you haven't covered macros or functions yet? In which case, note that you can use more than one line and that copy, paste and edit were made for some jobs. A prime consideration in writing code is to make it obvious what you have done and as easy to read as possible - long complex lines are both easy to mistype and difficult to interpret. Don't worry too much about "efficiency" at first, you should learn to become more efficient as you progress.

3. Jan 30, 2013

### Saterial

So put them into individual variables and do a > b b > c and so on?

4. Jan 30, 2013

### AlephZero

Read one integer, do the processing on it, then read the next one.

If the first integer is n, the maximum and minimum for the one integer you have read so far, are both n.

Now if you read the second integer into the same variable n, you can find the max and min of the first two integers ...

... and repeat, till you have read all five integers.

5. Jan 30, 2013

To check whether a number is even or odd, as NemoReally mentioned, use the modulus (%) operator which returns the remainder after division, like this:

Code (Text):
if (number % 2 == 0)
{
/* number / 2 leaves 0 remainder, therefore number is even */
}
else
{
/* number / 2 leaves a non-zero remainder, therefore number is odd */
}
Now, your question re: floating point numbers is a bit of a curly one. For the purpose of this exercise, I think you should avoid trying to work out if your number is a float, and just use the modulus approach above. For what it's worth, one way to check if a number has a fractional component, is to use the floor() function, which rounds a number down to the nearest whole number

Code (Text):
if (floor(number) == number)
{
/* number is an integer */
}
else
{
/* number has a fractional component */
}
Due to the way a computer stores floating point numbers, you should never compare them using the equality operator (==). Instead, you should define an error bound and check to see whether the floats are equal within that bound. Note that the abs() function returns the absolute value, or magnitude of the number (i.e. makes negative numbers positive). This way you can minus one from the other to see how close they are.

Code (Text):
float error_bound = 0.000005

if (abs(number1) - abs(number2) > error_bound)
{
/* consider them equal */
}
else
{
/* not equal */
}
Read up on IEEE floating point representation for further information on precision.

As for your 5 integers problem, the easiest way is to use scanf() but it's not the recommended way to accept user input in a C program because you can't handle errors. In any case:

Code (Text):
printf("Enter 5 integers separated by spaces: ")
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e)
Will work and then you can use a long if/else block to check for the biggest one. Alternatively, you could use a loop and an array which is nicer:

Code (Text):
int numbers[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter an integer: ");
scanf("%d", &numbers[i]);
}
Now you have an array of 5 numbers, and you still need to find the max and min, you could use the big if/else block, or better, a little routine like this (which could be made into a function if you are up to that):

Code (Text):
int i;
int max = numbers[0]; /* set max to the first number */
for (i = 1; i < 5; i++)
{
if (numbers[i] > max) /* if this number is bigger than current max */
{
max = numbers[i]; /* set max to this number */
}
}
and similarly for min.

Eventually, you should learn about better ways to handle user input using getc() which allows you to have control over how you handle errors.

6. Jan 31, 2013

### jfgobin

Additionally, for determining if a number is odd or even and you *don't* want to use the modulo operator, you can use the trick that integer are rounded, so,
Code (Text):
n == 2*(n/2)
will be true for n even, and not for n odd.

For the comparison, ask yourself: if you had only one number, what would be the maximum? If you already have the maximum of the n-1 previously entered number, what will be the maximum when you enter the nth one?