Sample COBOL Programs
Sample COBOL Programs
1
PROCEDURE
CD cobol
1) To Edit
NE Filename.cob
5) To Compile
Cobol> Cobol filename.cob;
No errors or warnings
6) To Run
Cobol> Runcob Filename.cob
Enter data
2
Program No.1
Program in COBOL to check whether a person Eligible for Vote or
not.
Algorithm :
1. Execute program
2. Enter Name and Age of a person
3. Check the age of a person
4. If age is equal or greater than 18, then display “Eligible to Vote”
Else Display “Not Eligible to Vote”
5. Stop
3
Flowchart for the program to check whether a person Eligible for
Vote or not.
Start
Read
Name, Age
Is Yes
Age ≥ 18
Write
No Eligible to Vote
Write
Not Eligible to Vote
Stop
4
CODE :
ACCEPT AGE.
DISPLAY " ".
P2.
IF AGE = 18 OR > 18
DISPLAY "******************************* "
DISPLAY NAME " IS ELIGIBLE TO VOTE "
ELSE
DISPLAY "******************************* "
DISPLAY NAME " IS NOT ELIGIBLE TO VOTE".
DISPLAY "******************************* "
STOP RUN.
5
Program No.2
Program in COBOL to find the given number is Even or Odd.
Algorithm :
1. Execute program
2. Enter the number
3. Compute : Number % 2
4. If Reminder = 0, then display “Given Number is Even” Else
Display “Given Number is Odd”
5. Stop
6
Flowchart for the program to find the given number is Even or Odd.
Start
Read
Num
Num % 2
Is Yes
Rem = 0
No Write
Given Num is Even
Write
Given Num is Odd
Stop
7
CODE :
8
Program No.3
Program in COBOL to find the given year is Leap year or Not.
Algorithm :
1. Execute program
2. Enter the year
3. Compute , Year % 4
4. If Reminder = 0, then display “Given Year is Leap year” Else
Display “Given Year is not Leap year”
5. Stop
9
Flowchart for the program to find the given year is Leap year or
Not.
Start
Read
Year
Num % 4
Is Yes
Rem = 0
Write
No Given year is Leap year
Write
Given year is not Leap year
Stop
10
CODE :
11
Program No.4
Program in COBOL to find area of rectangle on size of option.
Algorithm :
1. Execute program
2. Enter the Length and Breadth
3. Compute : A = L * B
4. Check size of A
5. If no size error, then display “The area of rectangle is : ”
Else display “Insufficient space, want to continue ? ”
6. Enter Ch = Y or N
7. If Ch = Y, then repeat step 2
8. Stop
12
Flowchart for the program to find area of rectangle on size of
option.
Start
Read
Length & Breadth
Compute
A=L*B
Is Yes Write
Error Insufficient space.
Want to continue ?
No
Read
CH
Write
The Area of Rectangle is
If Yes
CH = Y
No
Stop
13
CODE :
14
Program No.5
Program in COBOL to swapping numbers.
Algorithm :
1. Execute program
2. Enter the numbers A and B
3. Display numbers A and B before swapping
4. Swap : C = A, A = B, B = C
5. Display numbers A and B after swapping
6. Stop
15
Flowchart for the program to swapping numbers.
Start
Read
A, B
Write
Before Swapping A, B
C=A
A=B
B=C
Write
After Swapping A, B
Stop
16
CODE :
17
Program No.6
Program in COBOL to find temperature conversion.
Algorithm :
1. Execute program
2. Enter the temperature in Celsius
3. Convert temperature from Celsius to Fahrenheit, F = C * 1.8 + 32
4. Display temperature in Fahrenheit = F
5. Enter the temperature in Fahrenheit
6. Convert temperature from Fahrenheit to Celsius, C = (F – 32) * 1.8
7. Display temperature in Celsius
8. Stop
18
Flowchart for the program to find temperature conversion.
Start
Read
Temp in Celsius
F = C * 1.8 + 32
Write
Temp in Fahrenheit = F
Read
Temp in Fahrenheit
C = (F – 32) * 1.8
Write
Temp in Celsius
Stop
19
CODE :
20
Program No.7
Program in COBOL to find Area and Circumference of a circle.
Algorithm :
1. Execute program
2. Enter the radius of a circle
3. Compute , (i) CIR = 2 * PI * R, (ii) A = PI * R ** 2
4. Display Circumference of a circle
5. Display Area of a circle
6. Stop
21
Flowchart for the program to find Area and Circumference of a
circle.
Start
Read
Radius = R
CIR = 2 * PI * R
A = PI * R ** 2
Write
Circumference = CIR
Area = A
A
Stop
22
CODE :
23
Program No.8
Program in COBOL to find Largest of Three numbers.
Algorithm :
1. Execute program
2. Enter three numbers A,B,C
3. Check conditions :
a) If (A > B) and (A > C), Then Display A is Largest
b) If (A < B) and (B > C), Then Display B is Largest
c) If (A < B) and (B < C), Then Display C is Largest
4. Stop
24
Flowchart for program to find Largest of Three numbers.
Start
Read
A, B, C
If Yes
A>B
If Yes
No
A>C
No
If Yes
B>C Write
A is Largest
Write
No B is Largest
Write
C is Largest
Stop
25
CODE :
*PROGRAN TO FIND LARGEST OF THREE NUMBERS
IDENTIFICATION DIVISION.
PROGRAM-ID. BIG.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(2).
77 B PIC 9(2).
77 C PIC 9(2).
PROCEDURE DIVISION.
P1.
DISPLAY "ENTER THE THREE NUMBERS".
DISPLAY " ".
ACCEPT A.
ACCEPT B.
ACCEPT C.
IF ( A > B )
IF ( A > C )
DISPLAY "========================="
DISPLAY A " IS LARGEST NUMBER "
DISPLAY "========================="
ELSE
DISPLAY "========================="
DISPLAY C " IS LARGEST NUMBER "
DISPLAY "========================="
ELSE
IF ( B > C )
DISPLAY "========================="
DISPLAY B " IS LARGEST NUMBER "
DISPLAY "========================="
ELSE
DISPLAY "========================="
DISPLAY C " IS LARGEST NUMBER "
DISPLAY "========================="
STOP RUN.
26
Program No.9
Program in COBOL to find Smallest of Three numbers.
Algorithm :
1. Execute program
2. Enter three numbers A,B,C
3. Check conditions :
a) If (A < B) and (A < C), Then Display A is Smallest
b) If (A > B) and (B < C), Then Display B is Smallest
c) If (A > B) and (B > C), Then Display C is Smallest
4. Stop
27
Flowchart for program to find Smallest of Three numbers.
Start
Read
A, B, C
If Yes
A<B
If Yes
No
A<C
No
If Yes
B<C Write
A is Smallest
Write
No B is Smallest
Write
C is Smallest
Stop
28
CODE :
*PROGRAN TO FIND LARGEST OF THREE NUMBERS
IDENTIFICATION DIVISION.
PROGRAM-ID. BIG.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(2).
77 B PIC 9(2).
77 C PIC 9(2).
PROCEDURE DIVISION.
P1.
DISPLAY "ENTER THE THREE NUMBERS".
DISPLAY " ".
ACCEPT A.
ACCEPT B.
ACCEPT C.
IF ( A < B )
IF ( A < C )
DISPLAY "========================="
DISPLAY A " IS SMALLEST NUMBER "
DISPLAY "========================="
ELSE
DISPLAY "========================="
DISPLAY C " IS SMALLEST NUMBER "
DISPLAY "========================="
ELSE
IF ( B < C )
DISPLAY "========================="
DISPLAY B " IS SMALLEST NUMBER "
DISPLAY "========================="
ELSE
DISPLAY "========================="
DISPLAY C " IS SMALLEST NUMBER "
DISPLAY "========================="
STOP RUN.
29
Program No.10
Program in COBOL to calculate simple and compound interest.
Algorithm :
1. Execute program
2. Enter :
a) Principle amount = A
b) Period = T
c) Rate of interest = R
3. Compute :
a) SI = ( P * T * R ) / 100
b) A = P * ( 1 + R / 100 ) ** T
c) CI = A - P
4. Display “Simple Interest = “, SI
5. Display “Compound Interest = “, CI
6. Display “Amount = “, A
7. Stop
30
Flowchart for program to calculate simple and compound interest.
Start
Read
P, T, R
SI = ( P * T * R ) / 100
A = P * ( 1 + R / 100 ) ** T
CI = A - P
Write
SI, CI, A
Stop
31
CODE :
32
Program No.11
Program in COBOL to find reverse of number and check for
palindrome.
Algorithm :
1. Execute program
2. Enter the number to find reverse, N
3. Compute, M = N
4. Divide N by 10 giving N reminder R
5. Compute Rev = Rev * 10 + R
6. If N = 0, then Move Rev to Rev1
7. Display “Reverse of given number = “, Rev1
8. Else go to step 4
9. If Rev = M
10. Display “ Given number is Palindrome”
11. Else Display “Given number is not Palindrome”
12. Stop
33
Flowchart for program to find reverse of number and check for
palindrome.
Start
Read
N
Compute
M=N
N % 10
Rev = Rev * 10 + R
Is Is Yes
N=0 Rev = M
Yes
No Write
No
Given No. is Palindrome
Write
Reverse of Given Number
Write
Given No. is Not Palindrome
Stop
34
CODE :
35