Unix Shell - simple adding numbers logic help needed

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > simple adding numbers logic help needed





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author simple adding numbers logic help needed
Eli

2006-10-23, 1:18 am

Hello! im a noob nix student who is trying to complete such an easy but
tricky task: Script that will give me the total of all even numbers.
For example, if an user enters 5 then the program should give you 6,
because 1, 2, 3, 4, 5 =E0 The addition of all even numbers is 2 + 4 =3D 6.
Simple, hugh?
Well, what i did so far is:

echo "Enter the number : "
read n

for(( i=3D0; i<$n; i++))
do
# see if it is odd or even number
test=3D$(( $i % 2 ))
if [ $test -ne 1 ];
then
sum=3D$(($sum+$i))

echo "You entered $n Sum of all odd digit is $sum"

fi
done

the output of this mess seems to be right:
Enter the number :
5
You entered 5 Sum of all odd digit is 0
You entered 5 Sum of all odd digit is 2
You entered 5 Sum of all odd digit is 6

However, it's a bit awkward. I need a clear output the following:
You entered 5
The total sum of even numbers is 6

But I broke my head and had bunch of beers, when I realized that Im
completely dumb, therefore, im here , begging for your help!!!

Chris F.A. Johnson

2006-10-23, 1:18 am

On 2006-10-23, Eli wrote:
> Hello! im a noob nix student who is trying to complete such an easy but
> tricky task: Script that will give me the total of all even numbers.
> For example, if an user enters 5 then the program should give you 6,
> because 1, 2, 3, 4, 5 à The addition of all even numbers is 2 + 4 = 6.
> Simple, hugh?
> Well, what i did so far is:
>
> echo "Enter the number : "
> read n
>
> for(( i=0; i<$n; i++))


That is non-standard syntax that only a couple of shells support.
It is better to use POSIX-compliant code:

i=0
while [ $i -le $n ]

and increment i within the loop.

> do
> # see if it is odd or even number
> test=$(( $i % 2 ))
> if [ $test -ne 1 ];
> then
> sum=$(($sum+$i))
>
> echo "You entered $n Sum of all odd digit is $sum"
>
> fi
> done
>
> the output of this mess seems to be right:
> Enter the number :
> 5
> You entered 5 Sum of all odd digit is 0
> You entered 5 Sum of all odd digit is 2
> You entered 5 Sum of all odd digit is 6
>
> However, it's a bit awkward. I need a clear output the following:
> You entered 5
> The total sum of even numbers is 6
>
> But I broke my head and had bunch of beers, when I realized that Im
> completely dumb, therefore, im here , begging for your help!!!


What problem are you having? If you want to add the even numbers,
reverse the test (-eq instead of -ne).


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Ed Morton

2006-10-23, 1:18 am

Eli wrote:

> Hello! im a noob nix student who is trying to complete such an easy but
> tricky task: Script that will give me the total of all even numbers.
> For example, if an user enters 5 then the program should give you 6,
> because 1, 2, 3, 4, 5 à The addition of all even numbers is 2 + 4 = 6.
> Simple, hugh?


Yup:

$ awk -v nr=5 'BEGIN{for (i=2;i<=nr;i+=2); print i}'
6

Regards,

Ed.
Eli

2006-10-23, 1:18 am

"What problem are having?"
I need a clear output like the following:
> You entered 5
> The total sum of even numbers is 6

However, what i get as the result of the code is:
> You entered 5 Sum of all odd digit is 0
> You entered 5 Sum of all odd digit is 2
> You entered 5 Sum of all odd digit is 6

Question is should i change the logic or simply add a case if in order
to get the needed?
Please note that I can use some fancy features as awk.... . gotta look
noob


Im sorry supposed
Chris F.A. Johnson wrote:
> On 2006-10-23, Eli wrote:
6.[vbcol=seagreen]
>
> That is non-standard syntax that only a couple of shells support.
> It is better to use POSIX-compliant code:
>
> i=3D0
> while [ $i -le $n ]
>
> and increment i within the loop.
>
>
> What problem are you having? If you want to add the even numbers,
> reverse the test (-eq instead of -ne).
>
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> =3D=3D=3D=3D=3D My code in this post, if any, assumes the POSIX locale
> =3D=3D=3D=3D=3D and is released under the GNU General Public Licence


Barry Margolin

2006-10-23, 1:18 am

In article <1161573887.099932.285200@m7g2000cwm.googlegroups.com>,
"Eli" <elushkin@gmail.com> wrote:

> Hello! im a noob nix student who is trying to complete such an easy but
> tricky task: Script that will give me the total of all even numbers.
> For example, if an user enters 5 then the program should give you 6,
> because 1, 2, 3, 4, 5 à The addition of all even numbers is 2 + 4 = 6.
> Simple, hugh?
> Well, what i did so far is:
>
> echo "Enter the number : "
> read n
>
> for(( i=0; i<$n; i++))
> do
> # see if it is odd or even number
> test=$(( $i % 2 ))
> if [ $test -ne 1 ];
> then
> sum=$(($sum+$i))
>
> echo "You entered $n Sum of all odd digit is $sum"
>
> fi
> done
>
> the output of this mess seems to be right:
> Enter the number :
> 5
> You entered 5 Sum of all odd digit is 0
> You entered 5 Sum of all odd digit is 2
> You entered 5 Sum of all odd digit is 6
>
> However, it's a bit awkward. I need a clear output the following:
> You entered 5
> The total sum of even numbers is 6
>
> But I broke my head and had bunch of beers, when I realized that Im
> completely dumb, therefore, im here , begging for your help!!!


Move the echo statements outside of the loop. Put the "You entered"
line before the loop, and the "Total sum" line after the loop.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Bill Marcum

2006-10-23, 1:18 am

On 22 Oct 2006 21:12:06 -0700, Eli
<elushkin@gmail.com> wrote:
> "What problem are having?"
> I need a clear output like the following:
> However, what i get as the result of the code is:
> Question is should i change the logic or simply add a case if in order
> to get the needed?

If you only want one answer, move the echo command after the end of the
loop, and change it to "Sum of all even digits".



--
Drop in any mailbox.
Eli

2006-10-23, 1:16 pm

thank you all!
Bill Marcum wrote:
> On 22 Oct 2006 21:12:06 -0700, Eli
> <elushkin@gmail.com> wrote:
> If you only want one answer, move the echo command after the end of the
> loop, and change it to "Sum of all even digits".
>
>
>
> --
> Drop in any mailbox.


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com