2007 Jul 11 7:02 AM
Hi Techies,
I am attending interviews in ABAP and i m facing problem in getting logic for Prime number , Palindrome and Amstrong.
If anybody can provide me logic for the following.
Thanking u all,
karan
2007 Jul 11 7:11 AM
<b>PALINDROME</b>
Selection Screen Elements............................................
PARAMETERS:
P_string(20) TYPE C. " Enter the String
Data Declarations....................................................
DATA:
W_string(20) TYPE C, " String
W_length TYPE I, " Length
W_count TYPE I, " Counter
W_count1 TYPE I. " Counter
W_length = STRLEN( P_string ).
W_count = W_length - 1.
DO W_length TIMES.
MOVE P_stringW_count(1) TO W_stringW_count1(1).
SUBTRACT 1 FROM W_count.
ADD 1 TO W_count1.
ENDDO.
IF W_string EQ P_string.
WRITE 'PALINDROME'.
ELSE.
WRITE 'NOT A PALINDROME'.
ENDIF.
<b>ARMSTRONG</b>
*34) Write a program to find the sum & product of the first N
*Armstrong numbers. The user should enter a value for N. (Word of
*caution: See that the user should not enter a value for N which is
*more than 5).
PARAMETERS:
P_INPUT TYPE I.
DATA:
W_COUNT TYPE I,
W_INTVALUE TYPE I,
W_TEMP TYPE I,
W_QUO TYPE I,
W_RESULT(10) TYPE N VALUE 0,
W_PRODUCT(10) TYPE N VALUE 1,
W_SUM(10) TYPE N VALUE 0.
IF P_INPUT GT 5.
WRITE:
'Donot Enter Number > 5'(003).
ELSE.
WHILE W_COUNT LE P_INPUT.
ADD 1 TO W_INTVALUE.
W_TEMP = W_INTVALUE.
W_RESULT = 0.
WHILE W_TEMP > 0.
W_QUO = W_TEMP MOD 10.
W_RESULT = W_RESULT + W_QUO * W_QUO * W_QUO.
W_TEMP = W_TEMP DIV 10.
ENDWHILE.
IF W_RESULT = W_INTVALUE.
W_COUNT = W_COUNT + 1.
W_SUM = W_SUM + W_RESULT.
W_PRODUCT = W_PRODUCT * W_RESULT.
ENDIF.
W_INTVALUE = W_INTVALUE + 1.
ENDWHILE.
ENDIF.
WRITE:
/ 'Sum of the Armstrong Numbers '(001),W_SUM,
/ 'Product of the Armstrong Numbers '(002),W_PRODUCT.
<b>PRIME</b>
*28) Write a program to find and display the greatest prime number
*less than the number entered by the user. Optimize the program so that
*the run time is less.
PARAMETERS:
P_input TYPE I OBLIGATORY. " Enter the Number
DATA:
W_flag TYPE I,
W_sqrt TYPE I,
W_mod TYPE I.
IF P_input LE 0.
WRITE: 'There are no negative prime numbers'(002).
ELSEIF P_input eq 1 or P_input eq 2.
WRITE: 'There are no prime numbers'(003).
ELSEIF P_input eq 3.
write: '2'.
ELSEIF P_input eq 4.
write: '3'.
ELSEIF P_input eq 5.
write: '3'.
ELSEIF P_input eq 6.
write: '5'.
ELSE.
WHILE P_input GT 2.
W_sqrt = ( P_input ** ( 1 / 2 ) ) + 1.
W_flag = 0.
P_input = P_input - 1.
WHILE W_sqrt GT 1.
W_mod = P_input MOD W_sqrt.
IF W_mod = 0.
W_flag = 1.
EXIT.
ENDIF.
W_sqrt = W_sqrt - 1.
ENDWHILE.
IF W_flag EQ 0.
WRITE :
/ ' Greatest Prime number is'(001), P_input.
EXIT.
ENDIF.
ENDWHILE.
ENDIF.
Regards,
Pavan.