Show List
COBOL Coding Questions
- Write a program to add two numbers and print their sum.
scssCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3).
01 NUM2 PIC 9(3).
01 SUM PIC 9(4).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter the first number: ".
ACCEPT NUM1.
DISPLAY "Enter the second number: ".
ACCEPT NUM2.
COMPUTE SUM = NUM1 + NUM2.
DISPLAY "The sum is ", SUM.
STOP RUN.
- Write a program to find the largest number among three numbers.
scssCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. LARGEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3).
01 NUM2 PIC 9(3).
01 NUM3 PIC 9(3).
01 LARGEST PIC 9(3).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter the first number: ".
ACCEPT NUM1.
DISPLAY "Enter the second number: ".
ACCEPT NUM2.
DISPLAY "Enter the third number: ".
ACCEPT NUM3.
IF NUM1 > NUM2 AND NUM1 > NUM3
COMPUTE LARGEST = NUM1
ELSE IF NUM2 > NUM3
COMPUTE LARGEST = NUM2
ELSE
COMPUTE LARGEST = NUM3
END-IF
DISPLAY "The largest number is ", LARGEST.
STOP RUN.
- Write a program to print the first n Fibonacci numbers.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. FIBONACCI.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3) VALUE 0.
01 NUM2 PIC 9(3) VALUE 1.
01 TEMP PIC 9(3).
01 COUNT PIC 99 VALUE 10.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "The Fibonacci series is: ".
DISPLAY NUM1.
DISPLAY NUM2.
PERFORM VARYING I FROM 3 BY 1 UNTIL I > COUNT
COMPUTE TEMP = NUM1 + NUM2
DISPLAY TEMP
MOVE NUM2 TO NUM1
MOVE TEMP TO NUM2
END-PERFORM
STOP RUN.
- Write a program to convert Celsius to Fahrenheit.
cssCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. TEMP-CONV.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CELSIUS PIC 9(3).
01 FAHRENHEIT PIC 9(3).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter the temperature in Celsius: ".
ACCEPT CELSIUS.
COMPUTE FAHRENHEIT = (CELSIUS * 9 / 5) + 32.
DISPLAY "The temperature in Fahrenheit is ", FAHRENHEIT.
STOP RUN.
- Write a program to check whether a given number is even or odd.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. ODD-EVEN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a number: ".
ACCEPT NUM.
IF NUM MOD 2 = 0
DISPLAY "The number is even."
ELSE
DISPLAY "The number is odd."
END-IF
STOP RUN.
- Write a program to print the multiplication table of a given number.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. MULT-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3).
01 COUNT PIC 99 VALUE 10.
01 PRODUCT PIC 9(5).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a number: ".
ACCEPT NUM.
DISPLAY "Multiplication table of ", NUM.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > COUNT
COMPUTE PRODUCT = NUM * I
DISPLAY NUM, " x ", I, " = ", PRODUCT
END-PERFORM
STOP RUN.
- Write a program to find the sum of digits of a given number.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. DIGIT-SUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(9).
01 DIGIT-SUM PIC 9(9) VALUE 0.
01 REMAINDER PIC 9(1).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a number: ".
ACCEPT NUM.
PERFORM UNTIL NUM = 0
COMPUTE REMAINDER = NUM MOD 10
COMPUTE DIGIT-SUM = DIGIT-SUM + REMAINDER
COMPUTE NUM = NUM / 10
END-PERFORM
DISPLAY "The sum of digits is ", DIGIT-SUM.
STOP RUN.
- Write a program to reverse a given number.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. NUM-REVERSE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(9).
01 REVERSED-NUM PIC 9(9) VALUE 0.
01 REMAINDER PIC 9(1).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a number: ".
ACCEPT NUM.
PERFORM UNTIL NUM = 0
COMPUTE REMAINDER = NUM MOD 10
COMPUTE REVERSED-NUM = (REVERSED-NUM * 10) + REMAINDER
COMPUTE NUM = NUM / 10
END-PERFORM
DISPLAY "The reversed number is ", REVERSED-NUM.
STOP RUN.
- Write a program to find the largest number among three given numbers.
scssCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. LARGEST-OF-THREE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3).
01 NUM2 PIC 9(3).
01 NUM3 PIC 9(3).
01 LARGEST PIC 9(3).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter three numbers: ".
ACCEPT NUM1.
ACCEPT NUM2.
ACCEPT NUM3.
IF NUM1 > NUM2 AND NUM1 > NUM3
COMPUTE LARGEST = NUM1
ELSE IF NUM2 > NUM1 AND NUM2 > NUM3
COMPUTE LARGEST = NUM2
ELSE
COMPUTE LARGEST = NUM3
END-IF
DISPLAY "The largest number is ", LARGEST.
STOP RUN.
- Write a program to find the factorial of a given number.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. FACTORIAL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(3).
01 FACTORIAL PIC 9(9) VALUE 1.
01 I PIC 99.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a number: ".
ACCEPT NUM.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > NUM
COMPUTE FACTORIAL = FACTORIAL * I
END-PERFORM
DISPLAY "The factorial of ", NUM, " is ", FACTORIAL.
STOP RUN.
- Write a program to find the prime factors of a given number.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. PRIME-FACTORS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(9).
01 I PIC 99 VALUE 2.
01 PRIME-FACTORS PIC 9(9) OCCURS 30 TIMES.
01 COUNT PIC 99 VALUE 0.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a number: ".
ACCEPT NUM.
PERFORM UNTIL NUM = 1
IF NUM MOD I = 0
COMPUTE COUNT = COUNT + 1
COMPUTE NUM = NUM / I
MOVE I TO PRIME-FACTORS(COUNT)
ELSE
COMPUTE I = I + 1
END-IF
END-PERFORM
DISPLAY "The prime factors are:"
PERFORM VARYING I FROM 1 BY 1 UNTIL I > COUNT
DISPLAY PRIME-FACTORS(I)
END-PERFORM
STOP RUN.
- Write a program to sort an array of integers in ascending order.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. SORT-ARRAY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMS PIC 9(3) OCCURS 10 TIMES.
01 TEMP PIC 9(3).
01 I PIC 99.
01 J PIC 99.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter 10 numbers: ".
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
ACCEPT NUMS(I)
END-PERFORM
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 9
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 9 - I + 1
IF NUMS(J) > NUMS(J + 1)
MOVE NUMS(J) TO TEMP
MOVE NUMS(J + 1) TO NUMS(J)
MOVE TEMP TO NUMS(J + 1)
END-IF
END-PERFORM
END-PERFORM
DISPLAY "The sorted array is: "
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
DISPLAY NUMS(I)
END-PERFORM
STOP RUN.
- Write a program to reverse a given string.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. REVERSE-STRING.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 STR PIC X(20).
01 REV-STR PIC X(20).
01 LENGTH PIC 99.
01 I PIC 99.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a string: ".
ACCEPT STR.
COMPUTE LENGTH = FUNCTION LENGTH(STR)
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LENGTH
COMPUTE REV-STR(I) = STR(LENGTH - I + 1)
END-PERFORM
DISPLAY "The reversed string is: ", REV-STR.
STOP RUN.
- Write a program to calculate the sum of digits of a given number.
javaCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. SUM-OF-DIGITS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM PIC 9(9).
01 SUM PIC 9(9) VALUE 0.
01 REMAINDER PIC 9(3).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a number: ".
ACCEPT NUM.
PERFORM UNTIL NUM = 0
COMPUTE REMAINDER = NUM MOD 10
COMPUTE SUM = SUM + REMAINDER
COMPUTE NUM = NUM / 10
END-PERFORM
DISPLAY "The sum of digits is: ", SUM.
STOP RUN.
- Write a program to find the highest common factor (HCF) of two given numbers.
vbnetCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. HCF.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(9).
01 NUM2 PIC 9(9).
01 REMAINDER PIC 9(3).
01 HCF PIC 9(9).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter first number: ".
ACCEPT NUM1.
DISPLAY "Enter second number: ".
ACCEPT NUM2.
PERFORM UNTIL NUM2 = 0
COMPUTE REMAINDER = NUM1 MOD NUM2
MOVE NUM2 TO NUM1
MOVE REMAINDER TO NUM2
END-PERFORM
MOVE NUM1 TO HCF
DISPLAY "The HCF of the two numbers is: ", HCF.
STOP RUN.
- Write a program to convert a given binary number to decimal.
sqlCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. BIN-TO-DEC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BIN-NUM PIC 9(9).
01 DEC-NUM PIC 9(9) VALUE 0.
01 REMAINDER PIC 9(3).
01 BASE PIC 9(3) VALUE 1.
01 TEMP PIC 9(9).
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a binary number: ".
ACCEPT BIN-NUM.
PERFORM UNTIL BIN-NUM = 0
COMPUTE REMAINDER = BIN-NUM MOD 10
COMPUTE TEMP = REMAINDER * BASE
COMPUTE DEC-NUM = DEC-NUM + TEMP
COMPUTE BASE = BASE * 2
COMPUTE BIN-NUM = BIN-NUM / 10
END-PERFORM
DISPLAY "The decimal equivalent is: ", DEC-NUM.
STOP RUN.
- Write a program to convert a given decimal number to binary.
sqlCopy code
IDENTIFICATION DIVISION.
PROGRAM-ID. DEC-TO-BIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DEC-NUM PIC 9(9).
01 BIN-NUM PIC X(20).
01 REMAINDER PIC 9(3).
01 I PIC 99.
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY "Enter a decimal number: ".
ACCEPT DEC-NUM.
PERFORM UNTIL DEC-NUM = 0
COMPUTE REMAINDER = DEC-NUM MOD 2
STRING REMAINDER DELIMITED BY SPACE
INTO BIN-NUM(I)
COMPUTE DEC-NUM = DEC-NUM / 2
COMPUTE I = I - 1
END-PERFORM
DISPLAY "The binary equivalent is: ", FUNCTION TRIM(BIN-NUM).
STOP RUN.
Leave a Comment