Page 1 of 2

How to handle number series logics in cobol pgm

PostPosted: Thu Sep 09, 2010 11:38 pm
by joseseb
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

Re: How to handle number series logics in cobol pgm

PostPosted: Thu Sep 09, 2010 11:56 pm
by dick scherrer
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.

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 10, 2010 12:03 am
by Robert Sample
What did your search of the Language Reference manual tell you? You did search the manual before posting, right?

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 10, 2010 2:00 am
by joseseb
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..

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 10, 2010 2:36 am
by Robert Sample
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).

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 10, 2010 1:59 pm
by GuyC
Table 17. Binary operators
Binary operator M
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 10, 2010 5:15 pm
by joseseb
@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

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 10, 2010 5:32 pm
by GuyC
Pseudo code :

if i =1 then 1 else (10 ** i + 1)

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 10, 2010 5:33 pm
by Robert Sample
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.

Re: How to handle number series logics in cobol pgm

PostPosted: Fri Sep 17, 2010 9:37 am
by joseseb
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