Visit The New, Modern Unix Linux Community

Sum of even numbers from 0 to 100 script

Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Sum of even numbers from 0 to 100 script
# 1  
Sum of even numbers from 0 to 100 script

1. The problem statement, all variables and given/known data:

Write a shell script that finds and display the sum of even positive integers from 0 to 100. Use the while control structure. Show your script and a sample run.

2. Relevant commands, code, scripts, algorithms:

Must us a while loop

3. The attempts at a solution (include all code and scripts):

This is one attempt:
for i in {0..100}
        (( b = $i % 2 ))
        if [ $b -ne 0 ]
        echo $i

Another attempt, just trying to get somewhere and have it print something, but with this one I was getting a syntax error..

while (( i=0; i<=100; i++ ));do
        if (( $i % 2 )); then
        echo $i is odd
        echo %i is even

Portland Community College, Portland, OR, United States, Shawli Sengupta, CS-140U-0-14858 - Intro to UNIX/Linux
I cannot add the URL to the course because I do not have enough post on this forum.
# 2  
Hi Nastybutler,
What does this line mean?
echo %i is even

--- Post updated at 11:11 ---

Missing space in this line after ;
while (( i=0; i<=100; i++ ));do

correct string
for ((i=0; i<=100; i++)); do

--- Post updated at 11:21 ---

You just make out your mistakes, do not copy mindlessly please
for i in {0..100}; do
        b=$(( $i % 2 ))
        if [ $b -ne 0 ]; then
                echo $i

Last edited by nezabudka; 03-03-2019 at 04:26 AM..
# 3  
Did you read the answers to your other thread attentively? They mentioned the "step" parameter of the for command - you might try to adapt it to your problem?
And, you might want to read the man page of your shell to learn about the correct syntax of the (mandatory to your problem) while statement - as you used it, the shell will issue a "syntax error".
# 4  
What shell are you using for your class assignment?

What do your class notes, your text book, or the manual page for your shell say is the proper format for a while loop? Since your assignment says you have to use a while loop, that would seem to be a good place to start (especially since neither of your coding attempts contains a while loop that isn't getting a syntax error).

Note that the syntax for a while loop is not the same as the syntax for a for loop. And, the script that you are writing does not need two loops. If you need to use a while loop, you do not need a for loop to get the job you are trying to do done.

As a hint, I would say that if you're using a while loop to solve this problem, anything using {1..100} is probably not going to help. Using that construct does not fit with the syntax of a while loop. And, after reading your other thread, I would comment that when you said you didn't know how to add numbers that weren't supplied as input from a user, you were wrong. The statement in your previous thread:
        sum=$((sum + $n))

did exactly that. Neither sum nor n were provided by a user, but you have a statement that is adding two numbers together when neither of those numbers were typed in by a user. This statement, or one very similar to it, is likely to be needed in your script. It doesn't contain any syntax errors and it does add two numbers together and store the updated results correctly.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Hi Nastybutler...

Your OP suggests you 'must us[e] a while loop', therefore a for loop is not required at this point.
This is pseudocode, see if you can adapt it.

shebang needed to call your shell required


while test[ing] against the 100 limit
    do your arithmetic here which uses both SUM and COUNT
    optional, print each line if you want to here to see how it is doing
    increase your COUNTer by your needed amount
print your final value here and should equal the last line of you optional listing

And thoroughly read Don's post...

Last edited by wisecracker; 03-03-2019 at 08:10 AM..
# 6  
Instead of incrementing the counter by 1 in the loop and then testing if it even, you could also increment the counter by 2 at every iteration. That way you would not need to test if it is even if the initial value of the counter is even.
# 7  
We are using this in the bash shell. Sorry I'm very new with this so I don't have the syntax down 100%.

--- Post updated at 07:08 AM ---

After everyones feedback this is what I have so far. Please bare in mind again, I'm still learning and I know it's not perfect.



while [[ $counter -le 100 ]]
        echo -n "$counter"
if ! [ $((counter % 2)) -eq 0 ]
        echo "$sum"
        sum=((sum + $counter))


I am getting an error message that reads:

./ line 14: syntax error near unexpected token `fi'
./ line 14: `fi'

Moderator's Comments:
Mod Comment Please remember to use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 03-05-2019 at 03:43 AM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #644
Difficulty: Easy
The first web browser was called WorldWideWeb.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sum even numbers from 1 to 100

I need help with this assignment. I'm very new to using UNIX/LINUX, and my only previous experience with programing anything is using python. We are writing scripts using vim, and this one I'm stumped on. "Write a shell script that finds and display the sum of even positive integers from 0 to... (5 Replies)
Discussion started by: Nastybutler
5 Replies

2. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

3. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies

4. Shell Programming and Scripting

Sum Numbers from different files

Hi All, I need to print the sum of numbers from different files. Input files: file1.out 10 20 30 file2.out 10 20 30 (5 Replies)
Discussion started by: saint2006
5 Replies

5. Shell Programming and Scripting

getting the sum of numbers

I basically have a file where I had to do a bunch of greps to get a list of numbers example: a file called numbers.txt 10000 10000 superman 10000 batman 10000 10000 grep '100' * | 10000 10000 10000 10000 10000 (2 Replies)
Discussion started by: zerofire123
2 Replies

6. Shell Programming and Scripting

Finding the sum of two numbers

cat *.out |grep "<some text>" | awk '{print $6}' For ex,This will reutrn me 11111 22222 is it possible to add these two numbers in the above given command itself?I can write this to a file and find the sum. But I prefer to this calculation in the above given line itself. Any... (3 Replies)
Discussion started by: prasperl
3 Replies

7. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

8. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

9. Shell Programming and Scripting

sum numbers from stdout

hello im looking for short way to sum numbers from stdout the way i found to do it is to long for me i wander if there is shorter way to do it ok it 2 stage action this will make the list of number in to file sum.txt grep -c include *.c | awk '{l=split($0,a,":");print a;}' > sum.txt this... (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

how to sum numbers in column

Hi, i want to sum all nubers in one column. Example: 12.23 11 23.01 3544.01 I'm trying to do this in awk, but it doesn't work properly. Seems like awk is summing only integers, for example: 12 11 23 3544 It cuts off numbers after dot. I used this command: akw /text/ file.txt |nawk... (1 Reply)
Discussion started by: iahveh
1 Replies

Featured Tech Videos