How Can I Implement Basic C Programs for Odd/Even and Max/Min Detection?

  • Thread starter Thread starter Saterial
  • Start date Start date
  • Tags Tags
    Logic Program
Click For Summary

Discussion Overview

The discussion revolves around implementing basic C programs for detecting odd/even numbers and finding the maximum/minimum of a set of integers. It includes theoretical considerations, practical coding strategies, and user input handling.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks guidance on determining if an integer is odd or even, suggesting division by 2 as a method but questioning how to check for decimal results.
  • Another participant suggests using the modulus operator (%) to determine odd/even status, explaining that if the remainder is 0, the number is even.
  • A different approach is proposed, using the equality of n and 2*(n/2) to check for evenness without the modulus operator.
  • Participants discuss how to read multiple integers without using arrays, with one suggesting to read each integer sequentially and update max/min values accordingly.
  • One participant mentions the use of the floor() function to check if a number is an integer, while cautioning against using equality comparisons for floating-point numbers.
  • Another participant emphasizes the importance of writing clear and readable code, suggesting that efficiency can be developed over time.
  • There is a suggestion to use scanf() for reading integers, but concerns are raised about error handling with this method.

Areas of Agreement / Disagreement

Participants generally agree on the use of the modulus operator for odd/even detection, but there are multiple approaches discussed for handling user input and determining max/min values, indicating that no single method is universally accepted.

Contextual Notes

Some participants express concerns about the efficiency and readability of code, while others highlight the importance of handling user input errors, suggesting that the discussion may be limited by the participants' current understanding of C programming concepts.

Saterial
Messages
54
Reaction score
0
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 each other. This obviously didn't seem very efficient to me is there another basic way I can try?

Thanks!
 
Technology news on Phys.org
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.
 
So put them into individual variables and do a > b b > c and so on?
 
Saterial said:
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?

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.
 
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:
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:
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:
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:
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:
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:
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.
 
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:
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?
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
1
Views
2K
Replies
47
Views
5K
Replies
18
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
8K