Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need help with this program. I've racked my brain for a week on it.

I need a program in that a user enters two numbers, and the program prints all the even numbers from the first number to the last.
Posted
Updated 26-Mar-13 23:12pm
v2

This can be done using a C program calling standard C library functions only. Then this source would be OS independant.

To do this with Unix shells passing the range on the command line, read about shell programming (especially passing command line parameters, conditions, and arithmetric expressions). For the Linux bash shell see the Bourne-Again SHell Manual[^].

An implementation might look like this:
Bash
#!/bin/bash
#

if  [ x"$2" == x ]; then
    echo "Usage: PrintRange <start> <end>"
    exit
fi

let i=$1
if ((i & 1)); then
    let i+=1
fi
while ((i <= $2)); do
    echo "$i"
    let i+=2
done

Your question seems to be homework. I would not post code in this case. But I guess you must explain what you have done and why. So you must understand the above shell script before using it.
 
Share this answer
 
This has little to do with UNIX. You need just a little knowledge of the C programming language and a bit of logic. How would you solve the problem with pencil and paper?
 
Share this answer
 
Comments
CPallini 27-Mar-13 4:19am    
[Carlo on behalf of the OP]
On paper I would write something like, 2 4 6 8 10 12 etc.

But that doesn't help in unix. I have to teach unix what a even number is, tell it where to begin, and when to stop.
CPallini 27-Mar-13 4:20am    
You have to write a C program to do the job. Do you know the C programming language?
If you have tried it for a week... what did you try? what is giving you problems? Have you tried debugging?

I am not so familiar with Unix, so I can not say about the syntax (it may have some differences)... but the function needed is quite simple.

You need:

- Specify the range: From X to Y
- Make a loop starting in X and ending in Y
- With each value of the counter (X, X+1, X+2, X+3...) use a division by 2
- If you get an exact division is even, if you get decimals or a rest is not even
- You can save the values in an collection or print them on the fly as you find them
 
Share this answer
 
v2
Comments
CPallini 27-Mar-13 4:18am    
Well, division is not really needed.
Nelek 28-Mar-13 3:36am    
I know, but if already having problems with trivial things... I think the easiest way to do it is like that
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

But, I'll give you a hint or two:

printf will print a string to the user, prompting him.
scanf will read a number from the user into a variable.
There are two ways to check if a number is odd or even:
1) A binary AND with 1 will show it as odd if the result is non-zero.
2) A MODULUS operation with 2 will show it as odd if the result is non-zero.
A loop is probably easiest to do with a for loop, in this case.

Your notes should cover all of this in sufficient detail, or there is always Google!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900