## How to handle number series logics in cobol pgm

### How to handle number series logics in cobol pgm

Suppose I have a number series like 1^1,2^2,3^3,4^4...i.e,1,4,27,256..
and pattern like
1
101
1001
10001
is there any functions to solve powers?
This was asked for me in IBm technical interview
joseseb

Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

### Re: How to handle number series logics in cobol pgm

Hello and welcome to the forum,

You need to write the code and post here when you get stuck.

People here will help you work thru the code but will not write it for you.

We are here to help people learn, not do the work for them.
Hope this helps,
d.sch.

dick scherrer
Global moderator

Posts: 6269
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 92 times

### Re: How to handle number series logics in cobol pgm

What did your search of the Language Reference manual tell you? You did search the manual before posting, right?
Robert Sample
Global moderator

Posts: 3535
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 252 times

### Re: How to handle number series logics in cobol pgm

Yeah robert i found tat the logics works out with perform n compute statement..
Plzz check my logic

1, number series 1,4,27,
data division
01 i pic 9(5)
procedure division
move 0 to i.
compute i=i+1
perform until i times
compute i=i*i
end-perform
display i
2,number pattern
data division
01 i pic 9(3).
01 j pic 9(3).
procedure division
move 1 to i,j.
perform para-1 varying i from 1 by 1 until i<4 after varying j from 1 by 1 until j<=i
para-1
if j=1 and i=j then
display 1
else
display 0

plzz correct me if the logics are wrong thanx..
joseseb

Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

### Re: How to handle number series logics in cobol pgm

Your solution number 1 will fail miserably. You cannot use I as the index for your PERFORM loop AND your result -- it can be one or the other, but not both. I haven't checked the manual to see if there's a syntax problem with PERFORM UNTIL I TIMES but I suspect even if the statement passes syntax checking, it won't do what you think it will. I would run a loop from 1 to whatever. In the loop, set X to 1, then PERFORM loop TIMES COMPUTE X = X * loop END-PERFORM

Your solution number 2 won't produce a trailing 1 -- nor will it produce a horizontal pattern. I would solve this one by noting that after the first term, the nth term becomes a 1 followed by (n-1) zeroes followed by a 1 -- and I would set up a loop using reference modification to generate a variable with the pattern. What you don't want to do is use repeated DISPLAY statements since each DISPLAY statement starts on a new line (unless you use the language construct to not do so).
Robert Sample
Global moderator

Posts: 3535
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 252 times

### Re: How to handle number series logics in cobol pgm

Table 17. Binary operators
Binary operator M
- Subtraction
* Multiplication
/ Division
** Exponentiation
I can explain it to you, but i can not understand it for you.
GuyC

Posts: 315
Joined: Tue Aug 11, 2009 3:23 pm
Has thanked: 1 time
Been thanked: 4 times

### Re: How to handle number series logics in cobol pgm

@all thank u 4 the reply...
yes guyc ..tats it..** operator works fine to get that series...and robert is there any other way other than reference modification
joseseb

Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

### Re: How to handle number series logics in cobol pgm

Pseudo code :

if i =1 then 1 else (10 ** i + 1)
I can explain it to you, but i can not understand it for you.
GuyC

Posts: 315
Joined: Tue Aug 11, 2009 3:23 pm
Has thanked: 1 time
Been thanked: 4 times

### Re: How to handle number series logics in cobol pgm

You could use an array, of course, or you could experiment with DISPLAY ... WITH NO ADVANCING.

What's the reluctance to use reference modification? It is a very good technique for dealing with certain problems.
Robert Sample
Global moderator

Posts: 3535
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 252 times

### Re: How to handle number series logics in cobol pgm

Thanks robert..i have another doubt regarding this . Can we do arthematic operations on the identifier that we use along with perform statements like if i want to display pattern like
1
2 4
3 6 9
is the follw.. method correct
data
01 number1 pic 9(2) value zero
01 number1 pic 9(2) value zero
01 number1 pic 9(2)
procedure
perform varying number1 1 by 1 until number1<=3
perform varying number2 1 by 1 until number2<=number1
compute number3=number1*number2
display 'bb' number3 with no advancing. //bb - two blank spaces
end-perform
end-perform
stop-run
Do chk thhe code , and correct me if am wrng.. thank u
joseseb

Posts: 7
Joined: Thu Sep 09, 2010 9:23 pm
Has thanked: 0 time
Been thanked: 0 time

Next