Wednesday, June 25, 2025

💡Practicals Basics Programs - Imperative Logic 1

 

Write an executable program that performs the following calculation: 2 + 3 * 5

Solution:

REPORT z_abap101_041.

START-OF-SELECTION.

DATA v_result TYPE i.

v_result = 2 + 3 * 5. " 25 or 17?

WRITE v_result.


Write an executable program that get two integers inside variables and perform the addition,subtraction, multiplication, division and power between them.

Solution:

REPORT z_abap101_042.

DATA v_number_a TYPE i.

DATA v_number_b LIKE v_number_a VALUE 2.

DATA v_result TYPE f .

START-OF-SELECTION.

v_number_a = 5.

v_result = v_number_a + v_number_b.

WRITE: 'Addition:', v_result EXPONENT 0 .

NEW-LINE.

v_result = v_number_a - v_number_b.

WRITE: 'Subtraction:', v_result.

NEW-LINE.

v_result = v_number_a * v_number_b.

WRITE: 'Multiplication:', v_result.

NEW-LINE.


v_result = v_number_a / v_number_b.

WRITE: 'Division:', v_result.

NEW-LINE.


v_result = v_number_a ** v_number_b.

WRITE: 'Power:', v_result.


Write an executable program that get two integers inside parameters and perform the addition, subtraction, multiplication, division and power between them.

Solution:

REPORT z_abap101_043.

PARAMETERS p_num_a TYPE i.

PARAMETERS p_num_b LIKE p_num_a DEFAULT 2.

DATA v_result TYPE f.

START-OF-SELECTION.

p_num_a = 5.


v_result = p_num_a + p_num_b.

WRITE: 'Addition:', v_result EXPONENT 0 .

NEW-LINE.


v_result = p_num_a - p_num_b.

WRITE: 'Subtraction:', v_result.

NEW-LINE.


v_result = p_num_a * p_num_b.

WRITE: 'Multiplication:', v_result.

NEW-LINE.


v_result = p_num_a / p_num_b.

WRITE: 'Division:', v_result.

NEW-LINE.


v_result = p_num_a ** p_num_b.

WRITE: 'Power:', v_result.


Write an executable program that concatenates two words and write the result.

Solution:

REPORT z_abap101_044.

CONSTANTS c_abap TYPE c LENGTH 4 VALUE 'ABAP'.

DATA v_whole_text TYPE string.

START-OF-SELECTION.

CONCATENATE c_abap '101' INTO v_whole_text .

WRITE v_whole_text.


Write an executable program that concatenates two words and the current month, separating

each part by a "-" and write the result.

Solution:

REPORT z_abap101_045.

CONSTANTS c_abap TYPE c LENGTH 7 VALUE 'ABAP999'.

CONSTANTS c_separator TYPE c VALUE '-'.

DATA v_whole_text TYPE string.

START-OF-SELECTION.

CONCATENATE c_abap(4) '101' sy-datum+4(2) INTO v_whole_text SEPARATED BY

c_separator.

WRITE v_whole_text.


Write an executable program that reads the current system date and write it in your language in text format.

Ex: 20140727 should be written as July the Twenty-Seventh, 2014

Solution:

REPORT z_abap101_046.

DATA v_day TYPE string.

DATA v_month TYPE string.

DATA v_year TYPE string.


START-OF-SELECTION.


* Handle month

CASE sy-datum+4(2).

WHEN '01'.

v_month = 'January'.

WHEN '02'.

v_month = 'February'.

WHEN '03'.

v_month = 'March'.

WHEN '04'.

v_month = 'April'.

WHEN '05'.

v_month = 'May'.

WHEN '06'.

v_month = 'June'.

WHEN '07'.

v_month = 'July'.

WHEN '08'.

v_month = 'August'.

WHEN '09'.

v_month = 'September'.

WHEN '10'.

v_month = 'October'.

WHEN '11'.

v_month = 'November'.

WHEN '12'.

v_month = 'December'.

ENDCASE.


* Handle day

IF sy-datum+6(2) = '01'.

v_day = 'first'.

ELSEIF sy-datum+6(2) = '02'.

v_day = 'second'.

ELSEIF sy-datum+6(2) = '03'.

v_day = 'third'.

ELSE. " 04-31

IF sy-datum+6(2) BETWEEN 04 AND 19. " 04-19

CASE sy-datum+6(2).

WHEN '04'.

v_day = 'four'.

WHEN '05'.

v_day = 'fif'.

WHEN '06'.

v_day = 'six'.

WHEN '07'.

v_day = 'seven'.

WHEN '08'.

v_day = 'eigh'.

WHEN '09'.

v_day = 'nin'.

WHEN '10'.

v_day = 'ten'.

WHEN '11'.

v_day = 'eleven'.

WHEN '12'.

v_day = 'twelf'.

WHEN '13'.

v_day = 'thirteen'.

WHEN '14'.

v_day = 'fourteen'.

WHEN '15'.

v_day = 'fitteen'.

WHEN '16'.

v_day = 'sixteen'.

WHEN '17'.

v_day = 'seventeen'.

WHEN '18'.

v_day = 'eighteen'.

WHEN '19'.

v_day = 'nineteen'.

ENDCASE.


CONCATENATE v_day 'th' INTO v_day.

ELSE.

CASE sy-datum+6(2). " 20-31

WHEN '20'.

v_day = 'twentieth'.

WHEN '21'.

v_day = 'twenty-first'.

WHEN '22'.

v_day = 'twenty-second'.

WHEN '23'.

v_day = 'twenty-third'.

WHEN '24'.

v_day = 'twenty-fourth'.

WHEN '25'.

v_day = 'twenty-fifth'.

WHEN '26'.

v_day = 'twenty-six'.

WHEN '27'.

v_day = 'twenty-seventh'.

WHEN '28'.

v_day = 'twenty-eighth'.

WHEN '29'.

v_day = 'twenty-first'.

WHEN '30'.

v_day = 'thirtieth'.

WHEN '31'.

v_day = 'thirty-first'.

ENDCASE.

ENDIF.

ENDIF.

* Handle Year

v_year = sy-datum(4).

* print result

WRITE: v_month, ' the ', v_day, ', ', v_year. NEW-LINE.

Write an executable program that reads the current system time and write the time in 6 different zones (3 of them should be compulsorily Greenwich, Delhi and Brasilia).

Solution:

REPORT z_abap101_047.

DATA v_timezone1 TYPE tznzone VALUE 'GMTUK'. " Greenwich

DATA v_timezone2 TYPE tznzone VALUE 'INDIA'. " Delhi

DATA v_timezone3 TYPE tznzone VALUE 'BRAZIL'. " Brasilia

DATA v_timezone4 TYPE tznzone VALUE 'CST'.

DATA v_timezone5 TYPE tznzone VALUE 'ISRAEL'.

DATA v_timezone6 TYPE tznzone VALUE 'RUS06'.

DATA v_timestamp TYPE tzonref-tstamps.

DATA v_timestamp_string TYPE string.

START-OF-SELECTION.

CONCATENATE sy-datum sy-uzeit INTO v_timestamp_string.

v_timestamp = v_timestamp_string.

WRITE v_timestamp TIME ZONE v_timezone1. NEW-LINE.

WRITE v_timestamp TIME ZONE v_timezone2. NEW-LINE.

WRITE v_timestamp TIME ZONE v_timezone3. NEW-LINE.

WRITE v_timestamp TIME ZONE v_timezone4. NEW-LINE.

WRITE v_timestamp TIME ZONE v_timezone5. NEW-LINE.

WRITE v_timestamp TIME ZONE v_timezone6. NEW-LINE.


Write an executable program that counts how many vowels are in the name of the user running the program and print the result

Solution:

REPORT z_abap101_048.

DATA v_vowels_count TYPE i.

DATA v_vowels_total TYPE i.

DATA v_user LIKE sy-uname.


START-OF-SELECTION.


v_user = sy-uname.

TRANSLATE v_user TO UPPER CASE.


* One option

FIND ALL OCCURRENCES OF 'A' IN v_user MATCH COUNT v_vowels_count.

v_vowels_total = v_vowels_total + v_vowels_count.

FIND ALL OCCURRENCES OF 'E' IN v_user MATCH COUNT v_vowels_count.

v_vowels_total = v_vowels_total + v_vowels_count.

FIND ALL OCCURRENCES OF 'I' IN v_user MATCH COUNT v_vowels_count.

v_vowels_total = v_vowels_total + v_vowels_count.

FIND ALL OCCURRENCES OF 'O' IN v_user MATCH COUNT v_vowels_count.

v_vowels_total = v_vowels_total + v_vowels_count.

FIND ALL OCCURRENCES OF 'U' IN v_user MATCH COUNT v_vowels_count.

v_vowels_total = v_vowels_total + v_vowels_count.


* Another option

FIND ALL OCCURRENCES OF REGEX 'A|E|I|O|U' IN v_user MATCH COUNT

v_vowels_count.

WRITE v_vowels_total.


Write an executable program that counts a string length and if it's bigger than 2o characters, write 'Too big'. If not, write the string length.

Solution:

REPORT z_abap101_049.

DATA v_string TYPE string VALUE '1234567890ABCDEFGHIJ'.

DATA v_string_length TYPE i.


START-OF-SELECTION.


v_string_length = strlen( v_string ).


IF v_string_length > 20 .

WRITE 'Too big'.

ELSE.

WRITE v_string_length.

ENDIF.


Write an executable program that counts from 1 to 100 and for each multiple of 8, write the message: "The number [number] is a multiple of 8 ".

Solution:

REPORT z_abap101_050.

DATA v_current_number TYPE i VALUE 1.


START-OF-SELECTION.


WHILE v_current_number <= 100.

IF ( v_current_number MOD 8 ) = 0.

WRITE: 'The number', v_current_number, ' is a multiple of 8'.

NEW-LINE.

ENDIF.

ADD 1 TO v_current_number.

ENDWHILE.

No comments:

Post a Comment