LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2006, 09:57 AM   #1
urban1
LQ Newbie
 
Registered: Oct 2006
Posts: 2

Rep: Reputation: 0
bash script


Im trying to write a script that adds up even numbers. The script asks a user to input a number, then from 0 to whichever number is entered, it adds up all the even numbers.
 
Old 10-17-2006, 10:13 AM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
There are quite a few methods you could use for this: here's one for which I'll give you the literary algorithm, but you're going to have to write the code:

1. Prompt for the limit.
2. Iterate through the list.
3. For each iteration, test if the number is even (modulo division, perhaps).
4. Create a separate variable that represents the sum of the even numbers, and everytime the loop stumbles upon an even number add the number to the existing value of the variable (sum_of_evens += num).

Here's another one:
1. Prompt for the limit.
2. Fill an array/list with all values from 0 to the limit.
3. Since even numbers exist an even intervals (every other number is even), just iterate through the list. I'm not sure the BASH syntax, but C/C++/Java syntax would be:
Code:
for (int x = 0; x < listofnumbers.length; x += 2) { ...body...}
. The idea is instead of iterating by an interval of 1, iterate by an interval of 2).
4. Create a separate variable that represents the sum of the even numbers, and everytime the loop stumbles upon an even number add the number to the existing value of the variable (sum_of_evens += num).

 
Old 10-17-2006, 12:16 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Sounds like homework to me.
Did you read the LQ Rules about that?
 
Old 10-17-2006, 07:06 PM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
I'm sorry about the previous post unSpawn, I didn't mean any offense, and I apologize profusely for the offense that was taken. I realized yesterday that my words could have been - and were - taken in the wrong light.

Once again, sincere apologies.

Last edited by indienick; 10-20-2006 at 01:32 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
building a bash script from an install script paranoid times Programming 6 07-29-2006 03:24 AM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 05:05 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration