Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add each number in a variable together... 1

Status
Not open for further replies.

Elvis69

Technical User
Oct 28, 2001
3
US
Hi,
I need to add each number in a variable together. For example if the variable number was 12345 I would need to compute this, 1 + 2 + 3 + 4 + 5 = 15
I guess a loop would be in order, but after that I'm lost. Can someone PLEASE be of assistance?
Thanks!
 

This should work.

Ted

===================================

Working-Storage Section.

01 Work-Area.
05 Var-number pic 9(05) value 12345.
05 Var-Num redefines Var-number
pic 9(01) occurs 5 times indexed by
var-idx.
05 Var-total pic 9(05) value zero.

Procedure Division.

perform varying var-idx from 1 by 1 until var-idx > 5
add var-num(var-idx) to var-total
end-perform

stop run.
 
Hi Ted,

Very good. You get an A; I guess Elvis will get one too. The question is: Would you hire him?

Hi Elvis,

When you ask for help, show us the code you've written. Getting your homework done by somebody else is the worst thing that can happen to you. The pity is that you'll find out too late.

Regards, Jack.
 
Oops, kind of stumbled into something here. Reminds me of the time I hired a rather attractive young lady right out of a private business college. She was recommended because she had such high grades. Came to find out she had sweet talked all the guys in the class into letting her copy their programs. She lasted less than 1 day. Justice does prevail at times.

Ted
 
:eek:)
Agreed. I get 4.0 on the tests too though. Learning is no problem, but the book hadn't covered this. I'm sure I'll be the only one with compiled homework. I can write this in JavaScript or VB but this stuff looks alien next to that. Thanks for spoon feeding!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top