Click here to Skip to main content
13,089,246 members (65,397 online)
Rate this:
Please Sign up or sign in to 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 26-Mar-13 21:35pm
Updated 26-Mar-13 23:12pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

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:
if  [ x"$2" == x ]; then
    echo "Usage: PrintRange <start> <end>"
let i=$1
if ((i & 1)); then
    let i+=1
while ((i <= $2)); do
    echo "$i"
    let i+=2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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?
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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100