Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to display even and odd number ranges between two given inputs.

Former Member
0 Kudos

Hi Every one,

I am just started my career in sap abap and trying to find the solution for this.

I am trying to display number range using select options, but I am getting an error "Memory low leave the transaction before taking a break".

Example: when I give two number 2 and 10 , it should display set of all even and odd numbers in that range.

Below is the code logic that I am using:

data: a type i,

         b type i,

         c type i,

         d type i,

         num type i.

select-options: in for num.

a = inp-low.

b=inp-high.

c = inp-low mod 2.

d = inp-high mod 2.

while a <=b and c = 0.

write: "even numbers:', a.

b = a + 1.

endwhile.

while a <=b and c <> 0.

write: "odd numbers:', b.

b = a + 1.

endwhile.

Any help will be much appreciated.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

this is an infinite loop:

while a <=b and c = 0.

write: "even numbers:', a.

b = a + 1.

endwhile.

because a is alway less than b

Max

8 REPLIES 8

PeterJonker
Active Contributor
0 Kudos

Debug your code and see what is happening. It will definitely give you a clue. It has to do with statement C <> 0.

Former Member
0 Kudos

Hi

this is an infinite loop:

while a <=b and c = 0.

write: "even numbers:', a.

b = a + 1.

endwhile.

because a is alway less than b

Max

0 Kudos

Sorry, can you give me some clue on the logic to be used to resolve the infinite loop.

0 Kudos

This is your logic...changed:


data: a type i,

          b type i,

          c type i,

          d type i,

          num type i.

data: even type i,

       odd  type i.

select-options: in for num.

a = in-low.

b = in-high.

c = a mod 2.

if c is INITIAL. "It measn a is even

   even = a.

   odd  = a + 1.

else.

   odd  = a.

   even = a + 1.

endif.

* Even

a = even.

while a <= b.

   write: / 'even numbers:', a.

*  b = a + 1.

   a = a + 2. "The next even number

endwhile.

* Odd

a = odd.

while a <= b .

   write: / 'odd numbers:', a.

   a = a + 2. "The next odd number

endwhile.

0 Kudos

Thank you so much for your help, I am not familiar with debugger yet, will learn soon.

I hope to develop logical thinking soon.

All the replies are much appreciated.

0 Kudos

Hi

In this situation you don't need to use the debbuger, it's enough to know a little bit of mathematical logic, you r program was not right in certain step:

- Set the limit of your interval: this step is ok


a = in-low.

b = in-high.

- You should know if the range starts with even or odd number, so to use statament MOD is right:


c = inp-low mod 2.

Ther result will be 0 if lower limit is even, so this step should be use to know which kind of number is INP-LOW

If A is even, the next even number will be A + 2, or the next odd number will be A + 1: it's the same, it depends on you wan to calculate, so your code:


while a <= b and c = 0.

write: "even numbers:', a.

b = a + 1.

endwhile.

it's not ok, because:

- b will be a odd number (because it's the next one of even number)

- the loop will be infinitive, because you a (and so b) will be always the same and so a will be alway less than b, you should increase a instead of b, so your code should be:


while a <= b and c = 0.

   write: "even numbers:', a.

   a = a + 1.

endwhile.

but a = a + 1 will return all integer number between your range, if you want to know only the even one you need to increase a of 2:


while a <= b and c = 0.

   write: "even numbers:', a.

   a = a + 2.

endwhile.

the same considerations are valid for odd number calculation

Max

0 Kudos

Thank You so much for this analysis.

I greatly appreciate your help.

matt
Active Contributor
0 Kudos

Check the structure and usage of select options. If someone enters 1 to 10 in the select option, the select option (which is an internal table with a header line) will contain this:

I BT 1 10

and your program will work (once fixed!).

But what if they enter an exclusion?

E BT 1 10

This means "give me all numbers excluding the numbers 1 to 10".

Or start using GE or LT?

I LT 10.

If you want user input that is only to be used as range then you should use two parameters, rather than a select-option.