nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

script to add all numbers from 1 to 100

This is a discussion on script to add all numbers from 1 to 100 within the Shell scripting forums, part of the Development/Scripting category; Hi all, I want to write a script that adds up all the numbers from 1 to 100. I really ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-16-2005, 03:31 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default script to add all numbers from 1 to 100

Hi all,
I want to write a script that adds up all the numbers from 1 to 100. I really don't know how, but I know that this is not the right one and we have to use until loop only.


Code:
x=1
until [ $x = 100 ]
do
echo x=$x
y=`expr $x + 1`
echo y=$y
done
I just hope you all know what the script is supposed to do. Add up all the numbers from 1 to 100. or for that matter may be 1 to 200 or more

Thanks,

rc
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-16-2005, 11:58 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

ricc,

Code:
#!/bin/bash
total=0
for i in $(seq 1 10)
do
        total=`expr $total + $i`
done
echo $total
seq print a sequence of numbers so seq 1 10 will create a sequence of numbers 1 2 3 4 5 6 7 8 9 10. Then in total I'm adding the value of $i (which is from 1 to 10) when loop done you should see total via echo command.

Home this helps
Reply With Quote
  #3 (permalink)  
Old 08-17-2005, 12:36 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default

Great, it works.

Thank you. I have so much to learn. I guess I can do it with help from you guys.

Thanks again.

rc
Reply With Quote
  #4 (permalink)  
Old 08-17-2005, 03:27 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
My distro: Suse, RHEL, Vista
Posts: 154
Rep Power: 4
sweta will become famous soon enough
Default

Just wanna point out that you can use following code too

Code:
#!/bin/bash
x=1
t=0
while [ $x -le 100 ]
do
t=`expr $t + $x`
x=`expr $x + 1`

done
echo $t
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
How to check CPU Numbers in Linux raj Linux hardware 1 06-17-2008 03:29 PM
pattern search with numbers mercuryshipz Coding in General 0 02-15-2008 12:16 AM
Use float real numbers in a shell script hao Shell scripting 1 03-16-2007 02:35 AM
Help with numbers ricc Shell scripting 3 12-16-2006 07:48 PM


All times are GMT +5.5. The time now is 02:36 AM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36