Register to reply 
Basic C Program  Help with logic! 
Share this thread: 
#1
Jan3013, 06:27 PM

P: 52

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
Jan3013, 06:44 PM

P: 194

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
Jan3013, 07:08 PM

P: 52

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



#4
Jan3013, 08:02 PM

Engineering
Sci Advisor
HW Helper
Thanks
P: 6,957

Basic C Program  Help with logic!
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
Jan3013, 08:25 PM

P: 188

To check whether a number is even or odd, as NemoReally mentioned, use the modulus (%) operator which returns the remainder after division, like this:
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:
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
Jan3113, 03:46 AM

P: 90

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,
For the comparison, ask yourself: if you had only one number, what would be the maximum? If you already have the maximum of the n1 previously entered number, what will be the maximum when you enter the nth one? 


Register to reply 
Related Discussions  
Help with Logic of Program  Programming & Computer Science  3  
Need Help with the Logic of my Program  Programming & Computer Science  4  
Searching for Interdisciplinary PhD Program on Logic  Academic Guidance  1  
Logic circuits drawing program  Engineering, Comp Sci, & Technology Homework  3 