c programming lab - kar...6 program to find the largest of five number. 7 program to find smallest...
TRANSCRIPT
Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department
Of Computer Science
B.Sc. 1st Semester Lab Manual
C PROGRAMMING LAB
INDEX C Programming
Sl.No Name of the program
1 Program to find area of circle.
2 Program to find Simple Interest.
3 Program to perform Arithmetic operations.
4 Program to find area of a triangle using three sides
5 Program to find the largest of two numbers.
6 Program to find the largest of five number.
7 Program to find smallest of two numbers using conditional operator.
8 Program to perform the Sum of N natural numbers.
9 Program to perform factorial of a given number.
10 Program to find the given number is palindrome or not.
11 Program to find the given number is Armstrong or not.
12 Program to reverse the given number.
13 Program to find Sum of digits.
14 Program to find day of the week.
15 Program to find the given character is vowel or not.
/* PROGRAM TO FIND THE AREA OF CIRCLE */
#include<stdio.h> main() { float r,area; clrscr(); printf(“ Enter the radius of the circle \n”); scanf(“%f”,&r); area=3.142*r*r; printf(“ The area of a circle =%f”,area); }
OUTPUT: Enter the radius of the circle 5 The area of a circle= 78.549995
/* PROGRAM TO FIND THE SIMPLE INTEREST*/
#include<stdio.h> main () { float p,r,si; int t; clrscr(); printf(“ Enter the values of p,r, and t \n”); scanf(“%f%f%d”,&p,&r,&t); si=(p*r*t)/100.0; printf(“Amount=Rs. %5.2f \n”,p); printf(“Rate=Rs. %5.2f% \n”,r); printf(“Time=%d years \n”,t); printf(“Simple Interest=%5.2f \n”,si); }
OUTPUT:
Enter the values of p,r, and t 2000 8 3 Amount=Rs. 2000.00 Rate=Rs. 8.00% Time=3 years Simple Interest= 480.00
/* PROGRAM TO PERFORM ARITHMATIC OPERATOR’S */ #include<stdio.h> #include<math.h> main() { int a,b,sum,sub,mult,div,rem; printf(“Enter the values of a & b \n”); scanf(“%d%d”,&a,&b);
sum=a+b; sub=a-b; mult=a*b; div=a/b; rem=a%b; printf(“The addition of two number’s is %d \n”,sum); printf(“The subtraction of two number’s is %d \n”,sub); printf(“The multiplication of two number’s is %d \n”,mult); printf(“The division of two number’s is %d \n”,div); printf(“The remainder of two number’s is %d \n”,rem); getch();
}
OUTPUT: Enter the values of a & b 50 5 The addition of two number’s is 55 The subtraction of two number’s is 45 The multiplication of two number’s is 250 The division of two number’s is 10 The remainder of two number’s is 0
/* PROGRAM TO FIND AREA OF A TRIANGLE USING 3 SIDES */ #include<stdio.h> #include<math.h> main() { int a,b,c,s; float area; printf(“ Enter the values \n”); scanf(“%d%d%d”,&a,&b,&c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“ The area of triangle is= %f”,area); }
OUTPUT: Enter the values 2 2 2 The area of triangle is= 1.7320
/* PROGRAM TO FIND THE LARGEST OF TWO NUMBERS */ #include<stdio.h> main() { int a,b,large; clrscr(); printf(“ Enter the two numbers \n”); scanf(“%d%d”,&a,&b); large=a; if (b>large) { large=b; } printf(“Largest of two numbers =%d \n”,large); getch(); }
OUTPUT:
Enter the two numbers 9 3 Largest of two numbers=9
/* PROGRAM TO FIND THE LARGEST OF FIVE NUMBERS */ #include<stdio.h> main() { int a,b,c,d,e,l; clrscr(); printf(“ Enter 5 numbers \n”); scanf(“%d%d%d%d%d”,&a,&b,&c,&d,&e); l=a; if (l<b) { l=b; } if (l<c) { l=c; } if (l<d) { l=d; } if (l<e) { l=e; } printf(“ The largest among these is : %d \n”,l); getch(); } OUTPUT: Enter 5 numbers 6 3 9 5 1 The largest among these is: 9
/* PROGRAM TO FIND THE SMALLEST OF TWO NUMBERS USING CONDITIONAL OPERATOR*/ #include<stdio.h> main() {
int a,b,s; clrscr(); printf(“ Enter the two numbers \n”); scanf(“%d%d”,&a,&b); s=a>b?a:b; printf(“ The smallest of 2 number’s : %d \n”,s);
}
OUTPUT: Enter the two numbers 9 5 The smallest of 2 number’s : 5
/* PROGRAM TO PERFORM THE SUM OF N NATURAL NUMBER */ #include<stdio.h> main() { int n,sum,i; clrscr(); printf(“ Enter the value of n \n”); scanf(“%d”,&n); sum=0; i=1; do { sum=sum+i; i=i+1; } while (i<=n); printf(“Sum of first %d number =%d”,n,sum); } OUTPUT: Enter the value of n 4 Sum of first 4 number =10
/* PROGRAM TO PERFORM FACTORIAL OF A GIVEN NUMBER */ #include<stdio.h> main() { int n,fact,i; clrscr(); printf(“ Enter the value of n \n”); scanf(“%d”,&n); fact=1; if (n==0) { printf(“Factorial of %d is %d”,n,fact); } else { for (i=1;i<=n;i++) { fact=fact*i; } printf(“Factorial of %d is %d”,n,fact); } getch(); } OUTPUT: Enter the value of n 4 Factorial of 4 is 24
/* PROGRAM TO FIND THE GIVEN NUMBER IS PALINDROM OR NOT */ #include<stdio.h> main() { int num,rem,sum,temp; clrscr(); printf(“ Enter any number \n”); scanf(“%d”,&num); sum=0; temp=num; while(num>0) { rem = num % 10; sum = sum * 10 + rem; num = num /10; } if(tem = = sum) { printf(“The given number is palindrome %d”,sum); } else { printf(“The given number is not palindrome %d”,sum); } getch( ); } OUTPUT: Enter any number 121 The given number is palindrome 121 Enter any number 234 The given number is not palindrome 432
/* PROGRAM TO FIND THE GIVEN NUMBER IS ARMSTRONG OR NOT */ #include<stdio.h> main() { int n,r,s,t; clrscr(); printf(“ Enter any number \n”); scanf(“%d”,&n); s=0; t=n; while(n>0) { r = n % 10; s = s + (r * r * r); n = n /10; } if(t = = s) { printf(“The given number %d is Armstrong ”,s); } else { printf(“The given number %d is not Armstrong “ ,s); } getch( ); } OUTPUT: Enter any number 153 The given number 153 is Armstrong Enter any number 123 The given number 123 is not Armstrong
/* PROGRAM TO REVERSE THE GIVEN NUMBER */ #include<stdio.h> main() { long int r,rev,n; clrscr(); printf(“ Enter any 4 digit number \n”); scanf(“%ld”,&n); rev=0; while(n ! = 0) { r = n % 10; rev = rev * 10 +r; n = n/10; } printf(“The reverse of a given number is %ld”,rev); getch( ); } OUTPUT: Enter any 4 digit number 1234 The reverse of given number is 4321
/* PROGRAM TO FIND SUM OF DIGITS */ #include<stdio.h> main() { int number, n, sum=0; clrscr(); printf(“ Enter a positive number \n”); scanf(“%d”,&number); n=number; do { sum = sum+ n % 10; n = n/10; } while(n>0); printf(“The sum of digits of %d is %d”, number, sum); getch( ); } OUTPUT: Enter a positive number 12345 The sum of digits of 12345 is 15
/* PROGRAM TO FIND DAY OF THE WEEK */
#include<stdio.h> main() { int dayno; clrscr(); printf(“Input day number of the week \n”); scanf(“%d”,&dayno); switch(dayno) { case 1: printf(“Sunday \n”); break; case 2: printf(“Monday \n”); break; case 3: printf(“Tuesday \n”); break; case 4: printf(“Wednesday \n”); break; case 5: printf(“Thursday \n”); break; case 6: printf(“Friday \n”); break; case 7: printf(“Saturday \n”); break; default: printf(“Invalid Input \n”); break; } getch(); } OUTPUT: Input day number of the week 5 Thursday
/* PROGRAM TO FIND THE GIVEN CHARACTER IS VOWEL OR NOT */
# include<stdio.h> main() { char ch; clrscr(); printf(“Input a character \n”); ch=getchar(); if (ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’ || ch==’u’ || ) { printf(“The given character is vowel \n”); } else { printf(“The given character is not vowel \n”); } getch(); } OUTPUT: Input a character A The given character is vowel Input a character B The given character is not vowel
Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department
Of Computer Science
B.Sc. 2nd
Semester Lab Manual
ADVANCE C PROGRAMMING LAB
INDEX
Advance C Programming
Sl.No Program Name
1. Program Compute Their Sum Using Function Addnums ()
2. Program to Determine The Largest Among Them.
3. Program to Illustrate Passing Array As An Argument To A Called Function.
4. Program Compute The Area Of A Triangle
5. Program to Illustrate A Call By Value Parameter Passing Mechanism
6. Program to Accept Two Positive Integers And Compute Their GCD Using Recursive
Function.
7. Program to Compute Its Factorial By Recursive Function.
8. Program to Accept The Information Of A C_Book Such As Number,Author,Publisher And
Price And Also Display It.
9. Program to Accept The Roll Number, Name, Marks Obtained In Three Tests Of 2
Students Of A Class And Display The Rollnum, Name Marks And Their Average.
10. Program to Find The Sum Of Statically Declared 5 Elements.This Program Makes Use Of
The Concept Of Pointers (Pass_By Reference) And Function.
S11. Program to Illustrate The Call_By_Value Method To Interchange The Contents Of Two
Integer Variable
12. Program to Create A File Called Emp.Rec And Store Information About Person, Interms
Of His Name, Age And Salary.
13 Program to Illustrate The Function fputc () And fputs () To Write A Single Character
And String To A Data File.
14. Program to Draw a Line Using graphics.
15. Program to Draw a Circle Using graphics.
//1. Program to compute their sum using function addnums()
#include<stdio.h>
Void main()
{
int n1,n2,result;
clrscr();
printf("Enter the two numbers");
scanf("%d%d",&n1,&n2);
result=addnums(n1,n2);
printf("The sum of %d and %d %d\n",n1,n2,result);
getch(); `
}
intaddnums(val1,val2)
int val1,val2;
{
int sum;
sum=val1+val2;
return sum;
}
OUTPUT
Enter the two numbers
12
23
The sum of 12 and 23 is=35
//2. Program to accept the two numbers and determine the largest among them.
#include<stdio.h>
void main()
{
floatx,y,max;
float large(float m,float n);
clrscr();
printf("Enter the two numbers");
scanf("%f%f",&x,&y);
printf("x=%f and y=%f\n",x,y);
max=large(x,y);
printf("The largest=%f\n",max);
getch();
}
float large(float m,float n)
{
if(m>n)
return m;
else
return n;
}
OUTPUT
Enter the two numbers
10.0
20.0
x=10.000000 and y=20.000000
The largest=20.000000
//3.Program to illustrate passing array as an argumunt to a called function
#include<stdio.h>
void main()
{
int a[6];
int i;
voidarrpass(int a[]);
clrscr();
printf("Enter array elemnts\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
arrpass(a);
getch();
}
voidarrpass(int a[])
{
int i;
printf("Array elements are\n");
for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
}
OUTPUT
Enter array elemnts
1
2
3
4
5
Array elements are
1
2
3
4
5
//4. Program to accept three sides of a triangle and pass them to a function to compute
the area of a triangle
#include<stdio.h>
#include<math.h>
void main()
{
float side1,side2,side3,area;
floattri_area(); /* Function Declaration */
clrscr();
printf("Enter the three sides of a triangle\n");
scanf("%f%f%f",&side1,&side2,&side3);
area=tri_area(side1,side2,side3);
printf("The area of the triangle=%f\n",area);
getch();
}
/* Function to compute the area of triangle */
floattri_area(a,b,c)
floata,b,c;
{
floats,area;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
OUTPUT
Enter the three sides of a triangle
3.0
4.0
5.0
The area of the triangle=6.000000
//5. Program to illustrate a call by value parameter passing mechanism.
#include<stdio.h>
void main()
{
int n1,n2,x;
intcal_by_val(int p1,int p2);
clrscr();
n1=6;
n2=9;
printf("n1=%d and n2=%d\n",n1,n2);
x=cal_by_val(n1,n2);
printf("n1=%d and n2=%d\n",n1,n2);
printf("x=%d\n",x);
getch();
}
intcal_by_val(int p1,int p2)
{
int sum;
sum=p1+p2;
p1+=2;
p2*=p1;
printf("p1=%d and p2=%d\n",p1,p2);
return sum;
}
OUTPUT
n1=6 and n2=9
p1=8 and p2=72
n1=6 and n2=9
x=15
//6. Program to accept two positive integers and compute their GCD using recursive
function.
#include<stdio.h>
void main()
{
inta,b,gcd;
int GCD(intx,int y);
printf("Enter the value of A and B");
scanf("%d%d",&a,&b);
gcd=GCD(a,b);
printf("The GCD of %d and %d =%d\n",a,b,gcd);
getch();
}
int GCD(intx,int y)
{
int rem;
if(y==0)
return x;
else
{
rem=x%y;
return(GCD(y,rem));
}
}
OUTPUT
Enter the value of A and B
12
8
The GCD of 12 and 8 =4
//7. Program to accept a number from the keyboard and compute its factorial by
recursive function.
#include<stdio.h>
#include<conio.h>
void main()
{
intnum,fact;
intrec_funct(int N);
clrscr();
printf("Enter a number\n");
scanf("%d",&num);
fact=rec_funct(num);
printf("factorial of %d=%d\n",num,fact);
getch();
}
intrec_funct(int N)
{
intfct;
if(N==0)
return 1;
else
fct=N*rec_funct(N-1);
returnfct;
}
OUTPUT
Enter a number
5
factorial of 5=120
//8. Program to accept the information of a C_book such as number, author, publisher
and price and also display it.
#include<stdio.h>
void main()
{
struct C_BOOK
{
intbook_num;
char author[20];
char publish[20];
float price;
};
struct C_BOOK bkinfo;
clrscr();
printf("Enter the book number\n");
scanf("%d",&bkinfo.book_num);
printf("Enter the Name of the author\n");
scanf("%s",bkinfo.author);
printf("Enter the name of the publisher\n");
scanf("%s",bkinfo.publish);
printf("Enter the price of the book\n");
scanf("%f",&bkinfo.price);
printf("______________________________\n");
printf(" C_BOOK \n");
printf("_______________________________\n");
printf("Book Number : %d\n",bkinfo.book_num);
printf("Author : %s\n",bkinfo.author);
printf("Publisher : %s\n",bkinfo.publish);
printf("Price : Rs.%6.2f\n",bkinfo.price);
printf("___________________________________\n");
getch();
}
OUTPUT
Enter the book number
12345
Enter the Name of the author
kottur
Enter the name of the publisher
sapna
Enter the price of the book
200.00
_____________________________
C_BOOK
_____________________________
Book Number : 12345
Author : kottur
Publisher : sapna
Price : Rs.200.00
______________________________
//9. Program to accept the roll number, name, marks obtained in three tests of
two students of a class and display the rollnum, name marks and their
average.
#include<stdio.h>
void main()
{
structstud_rec
{
intRollno;
char Name[20];
int M1;
int M2;
int M3;
floatavg;
};
inti,total;
structstud_rec student[2];
printf("Type in information of 2 students\n");
for(i=0;i<2;i++)
{
printf("Enter the rollno of student%d=\n",i+1);
scanf("%d",&student[i].Rollno);
printf("Enter the nameof the student %d=\n",i+1);
scanf("%s",student[i].Name);
printf("Enter the marks1\n");
scanf("%d",&student[i].M1);
printf("Enter the marks2\n");
scanf("%d",&student[i].M2);
printf("Enter the marks3\n");
scanf("%d",&student[i].M3);
}
printf("___________________________________\n");
printf("Rollno Name Mark1 Mark2 Mark3 Average \n"):
printf("___________________________________\n");
for(i=0;i<2;i++)
{
total=student[i].M1+student[i].M2+student[i].M3;student[i].avg=total/3.0;
printf("%d%s%d%d%d%5.2f\n",student[i].Rollno,student[i].Name,student[i].M1,student[i].
M2,student[i].M3,student[i].avg);
}
printf("___________________________________\n");
getch();
}
OUTPUT
Type in information of 2 students
Enter the rollno of student 1=
1
Enter the name of the student 1=
abhi
Enter the marks1
80
Enter the marks2
90
Enter the marks3
80
Enter the rollno of student 2=
2
Enter the name of the stydent 2=
akash
Enter the marks1
70
Enter the marks2
80
Enter the marks3
60
___________________________________________
Rno Name Mark1 Mark2 Mark3 Average
___________________________________________
1 abhi 80 90 80 83.33
2 akash 70 80 60 70.00
_________________________________________
//10. Program to find the sum of statically declared 5 elements.Thisprogram makes use
of the concept of pointers (pass_by reference) and function.
#include<stdio.h>
void main()
{
staticint array[5]={200,400,600,800,1000};
intaddnum(int *ptr);
int sum;
sum=addnum(array);
printf("Sum of all array elements=%d\n",sum);
getch();
}
intaddnum(int *ptr)
{
inti,total=0;
for(i=0;i<5;i++)
total=total+*(ptr+i);
Return(total);
}
OUTPUT
Sum of all array elements=3000
//11. Program to illustrate the call_by_value method to interchange the contents of two
integer variable
#include<stdio.h>
main()
{
inta,b;
a=10;b=20;
clrscr();
printf("\n The main before exchanging");
printf("\n a=%d and b=%d\n",a,b);
exchang(a,b);
printf("\n The main after exchanging");
printf("\n a=%d and b=%d\n",a,b);
getch();
}
voidexchang(intm,int n)
{
int temp;
printf("\n Function before exchanging \n");
printf("\n m=%d and n=%d\n",m,n);
temp=m;
m=n;
n=temp;
printf("\n The function after exchanging\n");
printf("\n m=%d and n=%d\n", m,n);
}
OUTPUT
The main before exchanging
a=10 and b=20
Function before exchanging
m=10 and n=20
The function after exchanging
m=20 and n=10
The main after exchanging
a=10 and b=20
//12. Program to create a file called emp.rec and store information about person, interms
of his name, age and salary.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
float salary;
clrscr();
fptr=fopen("emp.rec","w");
if(fptr==NULL)
{
printf("File doesnot exist \n");
return;
}
printf("Enter the name \n");
scanf("%s",name);
fprintf(fptr,"Name=%s\n",name);
printf("Enter the age\n");
scanf("%d",&age);
fprintf(fptr,"Age =%d\n",age);
printf("Enter the salary \n");
scanf("%f",&salary);
fprintf(fptr,"Salary = %2.2f\n",salary);
fclose(fptr);
getch();
}
OUTPUT
Enter the name
madhu
Enter the age
30
Enter the salary
40000
Emp.rec
Name=madhu
Age =30
Salary = 40000.00
//13. Program to illustrate the function fputc() and fputs() to write a single charatcter
and string to a data file.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fptr;
charmychar;
char string[20];
clrscr();
fptr=fopen("out.txt","w");
if(fptr==NULL)
{
printf("File doesnot Exist \n");
return;
}
printf("Enter a character \n");
scanf("%c",&mychar);
fputc(mychar,fptr);
fflush(stdin);
printf("Enter a string \n");
gets(string);
fputs(string,fptr);
fclose(fptr);
getch();
}
OUTPUT
Enter a character
h
Enter a string
hello
out.txt
h hello
//14. Program To Draw a Line Using graphics.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
inta,b;
detectgraph(&a,&b);
initgraph(&a,&b,"");
line(75,50,150,50);
getch();
closegraph();
}
OUTPUT:
_________________
//15. Program To Draw a Circle Using graphics.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
inta,b;
detectgraph(&a,&b);
initgraph(&a,&b,"");
circle(100,120,75);
getch();
closegraph();
}
OUTPUT
Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department
Of Computer Science
B.Sc. 1st Semester Lab Manual
COBOL PROGRAMMING LAB
INDEX
COBOL
Sl. No
Name of the Program
1 Program to find sum of individual numbers.
2 Program to find Simple interest using on size error.
3 Program to find largest of three numbers.
4 Program to find the sum of even numbers.
5 Program to perform a reverse of a given number.
6 Program to find the given number prime or not
7 Program to find the factorial of a given number
8 Program to check whether Armstrong or not.
9 Program to generate Fibonacci numbers.
10 Program to find the given number is palindrome or not
11 Program to find the largest of n-Numbers
12 Program to find the Trace of a Matrix.
13 Program to find marks occurrences.
14 Program for Sorting of files.
15 Program to perform Merging of files.
* PROGRAM TO PERFORM SUM OF INDIVIDUAL NUMBERS
IDENTIFICATION DIVISION.
PROGRAM-ID. INDIVIDUALDIGITS.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 GN PIC 9(10).
77 ND PIC 9(2).
77 C PIC 9(2) VALUE 0.
77 D PIC 9.
77 WS PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
S-PARA.
DISPLAY "THE GIVEN NUMBER".
ACCEPT GN.
DISPLAY "THE NUMBER OF DIGITS".
ACCEPT ND.
CALL-PARA.
DIVIDE GN BY 10 GIVING GN REMAINDER D.
ADD D TO WS.
ADD 1 TO C.
IF C LESS THAN ND
GO TO CALL-PARA.
DISPLAY "SUM OF DIGITS=" , WS.
STOP RUN.
OUTPUT
THE GIVEN NUMBER
123
THE NUMBER OF DIGITS
3
SUM OF DIGITS = 06
* PROGRAM TO FIND SIMPLE INTEREST USING ON SIZE ERROR
IDENTIFICATION DIVISION.
PROGRAM-ID. SIMPLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 P PIC 9(6).
77 T PIC 9(3).
77 R PIC 9(3).
77 SI PIC 9(5)V9(2).
77 SI1 PIC 9(5).9(2).
PROCEDURE DIVISION.
START-PARA.
CAL-PARA.
DISPLAY " ENTER P AMOUNT".
ACCEPT P.
IF P = 0 GO TO DIS-PARA.
DISPLAY "ENTER TIME DURATION".
ACCEPT T.
DISPLAY " ENTER RATE OF INTEREST ".
ACCEPT R.
COMPUTE SI = ( P * T * R ) / 100 ON SIZE ERROR
GO TO E-PARA.
MOVE SI TO SI1.
DISPLAY " SIMPLE INTEREST=" , SI1.
STOP RUN.
E-PARA.
DISPLAY "PICTURE SPECIFICATION IS NOT SUFFICIENT".
GO TO CAL-PARA.
DIS-PARA.
DISPLAY " ENTER PROPER PRINCIPALE VALUE".
GO TO CAL-PARA.
OUT PUT
ENTER P AMOUNT
1000
ENTER TIME DURATION
3
ENTER RATE OF INTEREST
2
SIMPLE INTREST = 0060.00
* PROGRAM TO FIND LARGEST OF THREE NUMBERS
IDENTIFICATION DIVISION.
PROGRAM-ID. LARG.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 A PIC 9(3).
77 B PIC 9(3).
77 C PIC 9(3).
77 L PIC 9(3).
PROCEDURE DIVISION.
P1.
DISPLAY " A , B , C ".
ACCEPT A.
ACCEPT B.
ACCEPT C.
COMPUTE L = A.
IF L < B
COMPUTE L = B.
IF L < C
COMPUTE L = C.
DISPLAY “LARGEST IS " , L.
STOP RUN.
OUTPUT
A, B , C
25
35
15
LARGEST IS 35
* PROGRAM TO PERFORM SUM OF EVEN NUMBERS
IDENTIFICATION DIVISION.
PROGRAM-ID. EVE.
ENVIRONMENT DIVISION,
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(3).
77 I PIC 9(3).
77 S PIC 9(3).
PROCEDURE DIVISION.
P1.
DISPLAY " ENTER VALUE OF N ".
ACCEPT N.
COMPUTE S = 0.
COMPUTE I = 1.
PERFORM P2 UNTIL I > N.
DISPLAY " SUM OF EVEN NUMBERS IS " , S.
STOP RUN.
P2.
COMPUTE S = S + 2 * I.
COMPUTE I = I + 1.
OUT PUT
ENTER THE VALUE OF N.
5
THE SUM OF EVEN NUMBER IS 030
*PROGRAM TO FIND REVERSE OF A GIVEN NUMBER
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(5).
77 M PIC 9(4) VALUE 0.
77 D PIC 9.
PROCEDURE DIVISION.
P1.
DISPLAY " ENTER THE VALUE OF N ".
ACCEPT N.
PERFORM P2 UNTIL N = 0.
DISPLAY " THE REVERSE OF GIVEN NUMBER IS " , M.
STOP RUN.
P2.
DIVIDE N BY 10 GIVING N REMAINDER D.
COMPUTE M = M * 10 + D.
OUT PUT
ENTER THE VALUE OF N.
1234
THE REVERSE OF GIVEN NUMBER IS 4321
*PROGRAM TO FIND THE GIVEN NUMBER PRIME OR NOT
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(4).
77 S PIC 9(3) VALUE 0.
77 N1 PIC 9(4).
77 I PIC 9(2).
77 R PIC 9(2).
PROCEDURE DIVISION.
P1.
DISPLAY "ENTER THE VALUE OF N".
ACCEPT N.
PERFORM P2 VARYING I FROM 1 BY 1 UNTIL I > N .
IF ( S = 2 )
DISPLAY " THE GIVEN NUMBER IS PRIME "
ELSE
DISPLAY " THE GIVEN NUMBER IS NOT PRIME ".
STOP RUN.
P2.
DIVIDE N BY I GIVING N1 REMAINDER R.
IF R = 0
COMPUTE S = S + 1.
OUT PUT
ENTER THE VALUE OF N
7
THE GIVEN NUMBER IS PRIME
* PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER
IDENTIFICATION DIVISION.
PROGRAM-ID. FAC.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(3).
77 I PIC 9(4) VALUE 1.
77 FACT PIC 9(3) VALUE 1.
77 FACT1 PIC 999.
PROCEDURE DIVISION.
START-PARA.
DISPLAY " ENTER N ".
ACCEPT N.
PERFORM PARA1 UNTIL I > N.
MOVE FACT TO FACT1.
DISPLAY " FACTORIAL IS " , FACT1.
STOP RUN.
PARA1.
COMPUTE FACT = FACT * I.
COMPUTE I = I + 1.
OUT PUT
ENTER N
5
FACTORIAL IS 120
*PROGRAM TO CHECK WHETHER ARMSTRONG OR NOT
IDENTIFICATION DIVISION.
PROGRAM-ID. ARMSTRONG.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(3).
77 X PIC 9(3).
77 M PIC 9(3).
77 S PIC 9(3).
77 D PIC 9(3).
PROCEDURE DIVISION.
P1.
DISPLAY " INPUT N ".
ACCEPT N.
DISPLAY " ENTER THE NUMBER OF DIGITS ".
ACCEPT D.
COMPUTE S = 0.
COMPUTE M = N.
PERFORM P2 D TIMES.
IF M = 0 GO TO P3.
IF M = S
DISPLAY “IS ARMSTRONG "
ELSE
DISPLAY " IS NOT ARMSTRONG ".
STOP RUN.
P3.
DISPLAY "ENTER THE CORRECT VALUE".
GO TO P1.
P2.
DIVIDE N BY 10 GIVING N REMAINDER X.
COMPUTE S = S + X ** 3.
OUTPUT
INPUT N
153
ENTER THE NUMBER OF DIGITS
3
IS ARMSTRONG
*PROGRAM TO GENERATE FIBONACCI NUMBERS
IDENTIFICATION DIVISION.
PROGRAM-ID. FIB.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(3).
77 F1 PIC 9(3).
77 F2 PIC 9(3).
77 F3 PIC 9(3).
77 I PIC 9(3).
PROCEDURE DIVISION.
P1.
DISPLAY " INPUT NUMBER ".
ACCEPT N.
COMPUTE F1 = 0.
COMPUTE F2 = 1.
COMPUTE I = 1.
DISPLAY F1.
DISPLAY F2.
PERFORM P2 UNTIL I > N - 2.
STOP RUN.
P2.
COMPUTE F3 = F1 + F2.
DISPLAY F3.
COMPUTE F1 = F2.
COMPUTE F2 = F3.
COMPUTE I = I + 1.
OUTPUT
INPUT NUMBER
5
0
1
1
2
3
*PROGRAM TO TO FIND GIVEN NUMBER IS PALINDROM OR NOT.
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 N PIC 9(4).
77 M PIC 9(4) VALUE 0.
77 K PIC 9(4) VALUE 1.
77 D PIC 9(4) VALUE 1.
PROCEDURE DIVISION.
P1.
DISPLAY " ENTER THE VALUE OF N".
ACCEPT N.
COMPUTE K = N.
PERFORM P2 UNTIL N = 0.
IF M = K
DISPLAY " GIVEN NUMBER IS PALINDROM "
ELSE
DISPLAY " GIVEN NUMBER IS NOT PALINDROM ".
STOP RUN.
P2.
DIVIDE N BY 10 GIVING N REMAINDER D.
COMPUTE M = M * 10 + D.
OUT PUT
ENTER THE VALUE OF N
121
GIVEN NUMBER IS PALINDROM
*PROGRAM TO FIND THE LARGEST OF N-NUMBERS
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 H1.
02 A PIC 9(4) OCCURS 10 TIMES.
77 I PIC 9(3).
77 N PIC 9(3).
77 L PIC 9(3).
PROCEDURE DIVISION.
S-PARA.
DISPLAY " INPUT N ".
ACCEPT N.
DISPLAY " INPUT N NUMBERS ".
PERFORM P2 VARYING I FROM 1 BY 1 UNTIL I > N.
COMPUTE L = A(1).
PERFORM P3 VARYING I FROM 2 BY 1 UNTIL I > N.
DISPLAY " LARGEST IS : " , L.
STOP RUN.
P2.
ACCEPT A( I ).
P3.
IF (L < A (I))
COMPUTE L = A (I).
OUT PUT
INPUT N
4
INPUT N NUMBERS
4
3
6
2
LARGEST IS : 006
* PROGRAM TO FIND TRACE OF A MATRIX
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 H1.
02 R OCCURS 5 TIMES.
03 C OCCURS 5 TIMES.
04 A PIC 999.
77 T PIC 999.
77 I PIC 999.
77 J PIC 999.
77 M PIC 999.
PROCEDURE DIVISION.
P1.
DISPLAY " I/P M ".
ACCEPT M.
DISPLAY "I/P N NO'S.".
PERFORM P2 VARYING I FROM 1 BY 1 UNTIL I > M.
COMPUTE T = 0.
PERFORM P4 VARYING I FROM 1 BY 1 UNTIL I > M.
DISPLAY "TRACE = " , T.
STOP RUN.
P2.
PERFORM P3 VARYING J FROM 1 BY 1 UNTIL J > M.
P3.
ACCEPT A(I , J).
P4.
COMPUTE T = T + A (I , I).
OUT PUT
I/P M
2
I/P N NO’S
1
2
3
4
TRACE = 005
* PROGRAM TO FIND MARKS OCCURRENCES.
IDENTIFICATION DIVISION.
PROGRAM-ID. BB1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 H1.
02 K PIC 999 OCCURS 2 TIMES.
02 E PIC 999 OCCURS 2 TIMES.
02 H PIC 999 OCCURS 2 TIMES.
02 TOT PIC 999 OCCURS 2 TIMES.
02 AVG PIC 999.99 OCCURS 2 TIMES.
02 I PIC 9 VALUE 1.
PROCEDURE DIVISION.
P1.
PERFORM P2 2 TIMES.
STOP RUN.
P2.
DISPLAY " ENTER THE KANNADA ".
ACCEPT K ( I ).
DISPLAY " ENGLISH ".
ACCEPT E ( I ).
DISPLAY " ENTER THE HINDI ".
ACCEPT H ( I ).
COMPUTE TOT ( I ) = K ( I ) + E ( I ) + H ( I ).
COMPUTE AVG ( I ) = TOT ( I ) / 3.
DISPLAY " K ANNADA = " , K ( I ).
DISPLAY " ENGLISH = " , E ( I ).
DISPLAY " HINDI = " , H ( I ).
DISPLAY " TOTAL = " , TOT ( I ).
DISPLAY " AVERAGE = " , AVG ( I ).
COMPUTE I = I + 1.
OUT PUT
ENTER KANNADA MARKS ENTER KANNADA MARKS
67 35
ENTER ENGLISH MARKS ENTER ENGLISH MARKS
57 55
ENTER HINDI MARKS ENTER HINDI MARKS
89 45
KANNADA : 67 KANNADA : 35
HINDI : 89 HINDI : 55
ENGLISH : 57 ENGLISH : 45
TOTAL : 213 TOTAL : 135
AVERAGE : 071.00 AVERAGE : 045.00
*PROGRAM TO SORTING OF FILES
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT WORK-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT SORTED-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD IN-FILE
VALUE OF FILE-ID IS "SORT.IN"
LABEL RECORDS ARE STANDARD.
01 IN-REC.
02 NAME PIC X(20).
SD WORK-FILE
DATA RECORDS ARE WORK-REC.
01 WORK-REC.
02 NAME PIC X(20).
FD SORTED-FILE
VALUE OF FILE-ID IS "SORT.OUT"
LABEL RECORDS ARE STANDARD.
01 SORTED-REC.
02 NAME PIC X(20).
PROCEDURE DIVISION.
S-PARA.
DISPLAY “SORTED LISTS”.
SORT WORK-FILE ASCENDING KEY
NAME OF WORK-REC USING IN-FILE
GIVING SORTED-FILE.
STOP RUN.
OUTPUT
SORT.IN
BASAVARAJ
ANAND
VISHNU
MAHESH
SORT.OUT
ANAND
BASAVARAJ
MAHESH
VISHNU
*PROGRAM TO MERGING OF TWO FILES
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT IN1-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT WORK-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
SELECT MERGE-FILE ASSIGN TO DISK
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD IN-FILE
VALUE OF FILE-ID IS "IN.DAT"
LABEL RECORDS ARE STANDARD.
01 IN-REC.
02 NAME PIC X(15).
02 CITY PIC X(15).
02 STATE PIC X(15).
FD IN1-FILE
VALUE OF FILE-ID IS "IN1.DAT"
LABEL RECORDS ARE STANDARD.
01 IN1-REC.
02 NAME PIC X(25).
02 CITY PIC X(15).
02 STATE PIC X(15).
SD WORK-FILE
DATA RECORDS ARE WORK-REC.
01 WORK-REC.
02 NAME PIC X(25).
02 CITY PIC X(15).
02 STATE PIC X(15).
FD MERGE-FILE
VALUE OF FILE-ID IS "OUT.DAT"
LABEL RECORDS ARE STANDARD.
01 MERGE-REC.
02 NAME PIC X(25).
02 CITY PIC X(15).
02 STATE PIC X(15).
PROCEDURE DIVISION.
S-PARA.
DISPLAY “MERGED LISTS “ .
MERGE WORK-FILE ASCENDING KEY
NAME OF WORK-REC USING
IN-FILE IN1-FILE
GIVING MERGE-FILE.
STOP RUN.
OUTPUT
IN.DAT
RAJESH RAICHUR KARNATAKA
MAHESH KURNOOL ANDRAPRADESH
KRISHNAN CHENNAI TAMILNADU
IN1.DAT
ANAND KALYAN MAHARASTRA
VISHAL TIRUCHI KERALA
OUT.DAT
ANAND KALYAN MAHARASTRA
KRISHNAN CHENNAI TAMILNADU
MAHESH KURNOOL ANDRAPRADESH
RAJESH RAICHUR KARNATAKA
VISHAL TIRUCHI KERALA
Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department Of
Computer Science
B.Sc. 4th
Semester Lab Manual
DATABASE MANAGEMENT SYSTEM LAB
Index Data Base Management System
Sl.No Name of the Queries
1. Retrieve the details of student whose rollno is 100.
2. Retrieve the sname and class from the table student whose class is BSC
3. Update the table student change the contents of the field sname to ‘Ravi’ and the
contents of the field class to ‘BSC ’ for the record identified by the field rno
containing the value 100.
4. Delete rows from the table student where the value in the fees field is equal to
2500.
5. Add the field sfathername to the student table, which is a field that can hold a
char upto 15.
6. Retrieve the names of all the clients and salesman in the city of “mumbai’ from the
tables client_ master and salesman_master.
7. Retrieve the salesman name in ‘mumbai’ whose efforts have resulted into at least
one sales transaction.
8. Retrieve all employee whose address is Houston.
9. Retrieve all employees in the department no 1 whose salary is between 30000 and
40000.
10. Count the number of distinct salary of the employee’s.
11. Create a table project with a primary key constraint on the column pno and insert
the records.
12. Retrieve all orders placed by a client named ‘Rahul Desai’ from the order details
table.
13. PL/SQL for finding largest of three numbers.
14. PL/SQL for finding factorial of a given number.
15. PL/SQL program for display of pattern.
16. PL/SQL program to check the given string is palindrome or not.
17. PL/SQL program to find the sum of natural numbers.
18. PL/SQL program to find the reverse of a given number.
19. PL/SQL for finding the given number is prime or not.
CREATE TABLE FOR STUDENT:
SQL> create table student (sname char (10), rno number (3), class char (10),
fees number (5), combination char(15));
Table created.
SQL> insert into student values ('Smith', 100,'BSC ',5000,'PMCS');
1 row created.
SQL> insert into student values ('Raj',101,'M.Sc',2500,'CS');
1 row created.
SQL> select * from student;
SNAME RNO CLASS FEES COMBINATION
---------- --------- ---------- --------- --------------- ----------------------------
Smith 100 BSc 5000 PMCS
Raj 101 M.Sc 2500 CS
1. Retrieve the details of student whose rollno is 100.
SQL> select * from student where rno=100;
SNAME RNO CLASS FEES COMBINATION
---------- --------- ---------- --------- ----------------------- --------------------------
Smith 100 BSC 5000 PMCS
2. Retrieve the sname and class from the table student whose class is BSC .
SQL> select sname, class from student where class='BSC ';
SNAME CLASS
---------- --------------
Smith BSC
3. Update the table student change the contents of the field sname to
‘Ravi’ and the contents of the field class to ‘BSC ’ for the record
identified by the field rno containing the value 100.
SQL> update student set sname='Ravi', class='BSC ' where rno=100;
1 row updated.
SQL> select * from student;
SNAME RNO CLASS FEES COMBINATION
----------------------------- --------- ----------------------------------------
Ravi 100 BSC 5000 PMCS
Raj 101 M.Sc 2500 CS
4. Delete rows from the table student where the value in the fees field is equal to
2500.
SQL> delete from student where fees=2500;
1 row deleted.
SQL> select * from student;
SNAME RNO CLASS FEES COMBINATION
----------- ------------------------------------------ ---------------
Ravi 100 BSC 5000 PMCS
5. Add the field sfathername to the student table, which is a field that can hold a
char upto 15.
SQL> alter table student add(sfathername char(15));
Table altered.
SQL> select * from student;
SNAME RNO CLASS FEES COMBINATION SFATHERNAME
--------------------------------------------------------------------------------------------
Raj 101 M.Sc 2500 CS
CREATE TABLE FOR CLIENT-DETAILS:
SQL> create table client_details (client_no varchar(10), name varchar(20), city
varchar(10)); Table Created.
SQL> insert into client_details values ('c0001', 'latha', 'mumbai');
1 row created.
SQL> insert into client_details values ('c0002', 'jaya', 'delhi');
1 row created.
SQL> insert into client_details values ('c0003', 'ajay', 'mumbai');
1 row created.
SQL> insert into client_details values ('c0004', 'rohit', 'calcutta');
1 row created.
SQL> insert into client_details values ('c0005', 'nalini', 'mumbai');
1 row created.
SQL> select * from client_details;
CLIENT_NO NAME CITY
----------------------------------------------------
c0001 latha mumbai
c0002 jaya delhi
c0003 ajay mumbai
c0004 rohit calcutta
c0005 nalini mumbai
5 rows selected.
CREATE TABLE FOR SALESMAN-MASTER:
SQL> create table salesman_master (salesman_no varchar(10), name char(15),
city char(10));
Table created.
SQL> insert into salesman_master values ('s0001', 'anitha', 'mumbai');
1 row created.
SQL> insert into salesman_master values ('s0002', 'latha', 'calcutta');
1 row created.
SQL> insert into salesman_master values ('s0003', 'sunitha', 'delhi');
1 row created.
SQL> insert into salesman_master values ('s0004', 'vishal', 'mumbai');
1 row created.
SQL> select * from salesman_master;
SALESMAN_NO NAME CITY
----------------------------------------------------
s0001 anitha mumbai
s0002 latha calcutta
s0003 sunitha delhi
s0004 vishal mumbai
6. Retrieve the names of all the clients and salesman in the city of “mumbai’ from
the tables client_master and salesman_master.
SQL> select client_no "ID", name from client_details where city='mumbai'
UNION select salesman_no "ID", name from salesman_master where
city='mumbai';
ID NAME
--------------------------
c0001 latha
c0003 ajay
c0005 nalini
s0001 anitha
s0004 vishal
CREATE TABLE FOR SALES-ORDER:
SQL> create table sales_order (order_no varchar(10), order_date date,
salesman_no varchar(8));
Table created.
SQL> insert into sales_order values ('o19001', '12-apr-97', 's0001');
1 row created.
SQL> insert into sales_order values ('o19002', '14-mar-99', 's0002');
1 row created.
SQL> insert into sales_order values ('o19003', '18-may-07', 's0003');
1 row created.
SQL> insert into sales_order values ('o19004', '20-jun-08', 's0004');
1 row created.
SQL> insert into sales_order values ('o19005', '25-aug-08', 's0005');
1 row created.
SQL> insert into sales_order values ('o19006', '24-jan-09', 's0006');
1 row created.
SQL> select * from sales_order;
ORDER_NO ORDER_DAT SALESMAN
-----------------------------------------------------------
o19001 12-APR-97 s0001
o19002 14-MAR-99 s0002
o19003 18-MAY-07 s0003
o19004 20-JUN-08 s0004
o19005 25-AUG-08 s0005
o19006 24-JAN-09 s0006
6 rows selected.
7. Retrieve the salesman name in ‘mumbai’ whose efforts have resulted into atleast
one sales transaction.
SQL> select salesman_no, name from salesman_master where city='mumbai'
INTERSECT select salesman_master.salesman_no, name from
salesman_master, sales_order where
salesman_master.salesman_no=sales_order.salesman_no;
SALESMAN NAME
----------------------------------
s0001 anitha
s0004 vishal
CREATE TABLE FOR EMPLOYEE:
SQL> create table Employee(Fname varchar(10), Mname varchar(10), Lname
varchar(10),SSN number(3), Bdate date, Addr varchar(20), Sex
varchar(6), salary number(10), Dno number(5));
Table Created.
SQL> insert into Employee values('John', 'B' , 'Smith' , 101 , '12-jan-81',
'Houston','Male',30000,1);
1 row created
SQL> insert into Employee values('Sriram','Srikant','Guddad',102,
'23-jun-83','Bangalore','Male',45000,2);
1 row created.
SQL> insert into Employee values('Thimmappa','K','M',103,'14-dec-83' ,
'Raichur','Male',38000,3);
1 row created.
SQL> insert into Employee values('Raj Shekar','K','M',105,'14-jan-83',
'Houston','Male',38000,4);
1 row created.
SQL> select * from employee;
FNAME MNAME LNAME SSN BDATE ADDR SEX SALARY DNO
---------------------------------------------------------------------------------------------------------
John B Smith 101 12-JAN-81 Houston Male 30000 1
Sriram Srikant Guddad 102 23-JUN-83 Bangalore Male 45000 2
Thimmappa K M 103 14-DEC-83 Raichur Male 38000 3
Raj Shekar K M 105 14-JAN-83 Houstan Male 38000 4
8. Retrieve all employee whose address is Houston.
SQL> select fname,lname from employee where addr like '%Houston%';
FNAME LNAME
-------------------------------------
John Smith
Raj Shekar M
9. Retrive all employees in the department no 1 whose salary is between 30000 and
40000.
SQL> select * from employee where (salary between 30000 and 40000) and dno=1;
FNAME MNAME LNAME SSN BDATE ADDR SEX SALARY DNO
---------------------------------------------------------------------------------------------------------
John B Smith 101 12-JAN-81 Houston Male 30000 1
10. Count the number of distinct salary of the employee’s.
SQL> select count(distinct salary) from employee;
COUNT (DISTINCTSALARY)
----------------------------------------
3
11. Create a table project with a primary key constraint on the column pno and insert
the records.
SQL> create table project(pno number(5), pname varchar(15),
ploc varchar(15),hours number(3), primary key(pno));
Table created.
SQL> insert into project values(11,'Hospital','Stafford',3);
1 row created.
SQL> insert into project values(12,'Animation','Bangalore',4);
1 row created.
SQL> insert into project values(13,'Bank','Mandya',6);
1 row created.
SQL> insert into project values(14,'Networking','Mysore',3);
1 row created.
SQL> insert into project values(15,'Loan','Stafford',4);
1 row created.
SQL> select * from project;
PNO PNAME PLOC HOURS
------------------------------------------------------------------------
11 Hospital Stafford 3
12 Animation Bangalore 4
13 Bank Mandya 6
14 Networking Mysore 3
15 Loan Stafford 4
12. Retrieve all orders placed by a client named ‘Rahul Desai’ from the order_details
table
SQL> create table order_details(order_no varchar(10), client_no varchar(10),
order_date date);
Table created.
SQL> insert into order_details values('019002','c00002','25-dec-97');
1 row created
SQL> insert into order_details values('019003','c00007','03-oct-97');
1 row created
SQL> insert into order_details values('019004','c00005','18-jun-97');
1 row created
SQL> insert into order_details values('019005','c00002','20-aug-97');
1 row created
SQL> insert into order_details values('019006','c00007','12-jan-97');
1 row created
SQL> select * from order_details;
ORDER_NO CLIENT_NO ORDER_DAT
--------------------------------------------------------------------
019001 c00006 12-APR-91
019002 c00002 25-DEC-97
019003 c00007 03-OCT-97
019004 c00005 18-JUN-97
019005 c00002 20-AUG-97
019006 c00007 12-JAN-97
6 rows selected.
SQL> create table client_master(client_no varchar(10),name char(15),bal_due
number(5));
Table created.
SQL> insert into client_master values('c00001','Ashok Mehra',500);
1 row created.
SQL> insert into client_master values('c00002','Vishal Parikh',1000);
1 row created.
SQL> insert into client_master values('c00003','Ajay Mehra',0);
1 row created.
SQL> insert into client_master values('c00004','Rohit Roy',0);
1 row created.
SQL> insert into client_master values('c00005','Naline Deewan',0);
1 row created.
SQL> insert into client_master values('c00006','Prem Iyer',0);
1 row created.
SQL> insert into client_master values('c00007','Rahul desai',0);
1 row created.
SQL> select * from client_master;
CLIENT_NO NAME BAL_DUE
--------------------------------------------------------------------------
c00001 Ashok Mehra 500
c00002 Vishal Parikh 1000
c00003 Ajay Mehra 0
c00004 Rohit Roy 0
c00005 Naline Deewan 0
c00006 Prem Iyer 0
c00007 Rahul desai 0
7 rows selected.
SQL> select * from order_details where client_no=(select client_no from
client_master where name='Rahul desai');
ORDER_NO CLIENT_NO ORDER_DAT
--------------------------------------------------------
019003 c00007 03-OCT-97
019006 c00007 12-JAN-97
-:13. PL/SQL for finding largest of three numbers:- Declare a number(4); b number(4); c number(4); l number(4); Begin a:=10; b:=20; c:=30;
l:=a; if (l<b) then l:=b; end if; if(l<c) then l:=c; end if; dbms_output.put_line('Largest of three number'||l); end; / -:Output:-
Largest of three number 30 PL/SQL procedure successfully completed. -:14. PL/SQL for finding factorial of a given number:- Declare i number(3); n number(3); fact number(3); Begin n:=4; fact:=1; if (n=0) then fact:=1; else for i in 1..n loop fact:=fact*i; end loop; end if; dbms_output.put_line('The factorial is='||fact); end; / -:Output:-
The factorial is=24 PL/SQL procedure successfully completed. -:15. PL/SQL program for display of pattern:- Declare n number(3); i number(3); j number(3); Begin n:=4; dbms_output.put_line('Pattern Display'); for i in 1..n loop for j in 1..i loop dbms_output.put(j); end loop; dbms_output.put_line(' '); end loop; end; / -:Output:- Pattern Display 1 12 123 1234 PL/SQL procedure successfully completed.
-:16. PL/SQL program to check the given string is palindrome or not. :- Declare g varchar(15); r varchar(15); i number(3); Begin g:=’madam’; for i in reverse 1..length(g) loop r:=r||substr(g,i,1); end loop; if(g=r) then dbms_output.put_line('The given string ' ||g||' is palindrome'); else dbms_output.put_line('The given string ' ||g||' is not palindrome'); end if; end; / -:Output:- The given string madam is palindrome PL/SQL procedure successfully completed.
-:17. PL/SQL program to find the sum of natural numbers. :- Declare n number(4); i number(4); s number(4); Begin n:=5; s:=0; i:=1; while(i<=n) loop s:=s+i; i:=i+1; end loop; dbms_output.put_line('Sum of natural number is '||s); end; / -:Output:- Sum of natural number is 15
-:18. PL/SQL program to find the reverse of a given number. :- Declare n number(4); r number(4); d number(4); Begin n:=234; r:=0; while(n>0) loop d:= n mod 10; r:=r*10+d; n:=trunc(n/10); end loop; dbms_output.put_line('The reverse of a given number is '||r); end; / -:Output:- The reverse of a given number is 432
-:19. PL/SQL for finding the given number is prime or not :- Declare i number(3); n number(3); s number(3); Begin n:=3; s:=0; i:=1; while (i<=n) loop if (n mod i =0) then s:=s+1; end if; i:=i+1; end loop; if (s=2) then dbms_output.put_line('Prime'); else dbms_output.put_line('Not Prime'); end if; end; / -:Output:- Prime PL/SQL procedure successfully completed.
Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department Of
Computer Science
B.Sc. 5th
Semester Lab Manual
OOPS WITH C++ Lab (5.2)
INDEX
C++ Programming
Sl. No Name of the Program
1 Program For Class Implementation.
2 Program For Static Class Member.
3 Program To Static Member Function.
4 Program To A Friendly Function To Two Class.
5 Program for Class With Constructor.
6 Program for Implementation Of Destructors.
7 Program for Overloading Unary Minus.
8 Program for Overloading + Operator.
9 Program for Single Inheritance: Public
10 Program for Multilevel Inheritance.
11 Program for Multiple Inheritance.
12 Program for Virtual Functions.
13 Program for Runtime Polymorphism.
14 Program to find Prime or not.
15 Program to find Armstrong or not.
// 1.CLASS IMPLEMENTATION:
#include<iostream.h>
class item
{
int number;
float cost;
public:
void getdata( int a, float b);
void putdata(void)
{
cout << “number : “ << number << “\n”;
cout << “cost: “ << cost << “\n”;
}
};
void item :: getdata (int a, float b)
{
number = a;
cost = b;
}
void main()
{
item x;
cout << “\n object x” << “\n”;
x.getdata(100, 299.95);
x.putdata();
item y;
cout << “ \nobject y “ << “\n”;
y.getdata(200, 175.50);
y.putdata();
}
OUTPUT:
Object x
number : 100
cost : 299.95
object y
number : 200
cost :175.5
// 2.STATIC CLASS MEMBER:
#include<iostream.h>
class item
{
static int count;
int number;
public:
void getdata( int a)
{
number = a;
count++;
}
void getcount(void)
{
cout << “ count: “;
cout << count <<”\n”;
}
};
Int item::count;
void main()
{
item a, b, c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout << “After reading data :“ <<”\n”;
a.getcount();
b.getcount();
c.getcount();
}
OUTPUT:
count:0
count:0
count:0
After reading data :
count:3
count:3
count:3
// 3.STATIC MEMBER FUNCTION:
#include<iostream.h>
class test
{
int code;
static int count;
public:
void setcode(void)
{
code = ++count;
}
void showcode(void)
{
cout << “ object number : “ << code << “\n”;
}
static void showcount(void)
{
cout<< “count: “<< count << “\n”;
}
};
int test :: count;
void main()
{
test t1, t2;
t1.setcode();
t2.setcode();
test :: showcount();
test t3;
t3.setcode();
test :: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
}
OUTPUT:
count : 2
count : 3
object number : 1
object number : 2
object number : 3
// 4. A FRIENDLY FUNCTION TO TWO CLASS :
#include<iostream.h>
class ABC;
class XYZ
{
int x;
public:
void setvalue(int i)
{
x = i;
}
friend void max(XYZ, ABC);
};
class ABC
{
int a;
public:
void setvalue(int i)
{
a = i;
}
friend void max(XYZ, ABC);
};
void max(XYZ m, ABC n)
{
if(m.x>=n.a)
{
cout << m.x;
}
else
{
cout <<n.a;
}
}
void main()
{
ABC abc;
abc.setvalue(100)
XYZ xyz;
xyz.setvalue(20)
max(xyz, abc);
}
OUTPUT:
20
// 5. CLASS WITH CONSTRUCTOR:
#include<iostream.h>
class integer
{
int m, n;
public:
integer (int , int);
void display(void)
{
cout << “ m = “ << m <<”\n”;
cout << “ n = “ << n <<”\n”;
}
};
integer :: integer(int x, int y)
{
m = x; n = y;
}
void main()
{
integer int1(0, 100);
integer int2 = integer(25, 75);
cout<<”\n object1 “ <<”\n”;
int1.display();
cout<<”\n object2 “ <<”\n”;
int2.display();
}
OUTPUT:
object1
m = 0
n = 100
object2
m = 25
n = 75
// 6.IMPLEMENTATION OF DESTRUCTORS:
#include<iostream.h>
int count = 0;
class alpha
{
public:
alpha()
{
count++;
cout<<”\n No. of object created ” << count;
}
~alpha()
{
cout<<”\n No. of object destroyed ” << count;
count- -;
}
};
void main()
{
cout << “\n \n ENTER MAIN\n”;
alpha A1 , A2 , A3, A4;
{
cout << “\n\n ENTER BLOCK1\n”;
alpha A5;
}
{
cout << “\n\n ENTER BLOCK2\n”;
alpha A6;
}
cout <<”\n\n RE-ENTER MAIN\n”;
}
OUTPUT:
ENTER MAIN
No. of object created 1
No. of object created 2
No. of object created 3
No. of object created 4
ENTER BLOCK1
No. of object created 5
No. of object destroyed 5
ENTER BLOCK2
No. of object created 5
No. of object destroyed 5
RE-ENTER MAIN
No. of object destroyed 4
No. of object destroyed 3
No. of object destroyed 2
No. of object destroyed 1
//7. OVERLOADING UNARY MINUS:
#include<iostream.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-();
};
void space :: getdata( int a, int b, int c)
{
x = a;
y = b;
z = c;
}
void space :: display(void)
{
cout << x << ” “;
cout << y << ” “;
cout << z << ” \n “;
}
void space :: operator-()
{
x = -x;
y = -y;
z = -z;
}
void main()
{
space S;
S.getdata(10, -20, 30);
cout << “S : “;
S.display();
-S;
cout <<”S : “;
S.display();
}
OUTPUT:
S : 10 -20 30
S : -10 20 -30
//8. OVERLOADING + OPERATOR:
#include<iostream.h>
class complex
{
float x;
float y;
public:
complex(){ }
complex(float real , float img)
{ x = real; y = img; }
complex operator+( complex);
void display(void);
};
complex complex :: operator+( complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return(temp);
}
void complex :: display(void)
{
cout << x << “ + j” << y << “\n”;
}
void main()
{
complex C1, C2, C3;
C1 = complex(2.5, 3.5);
C2 = complex(1.6, 2.7);
C3 = C1 + C2;
cout << “ C1 = “; C1.display();
cout << “ C2 = “; C2.display();
cout << “ C3 = “; C3.display();
}
OUTPUT:
C1 = 2.5 + j3.5
C2 = 1.6 + j2.7
C3 = 4.1 + j6.2
//9. SINGLE INHERITANCE: PUBLIC
#include<iostream.h>
class B
{
int a;
public:
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class D : public B
{
int c;
public:
void mul(void);
void display(void);
};
void B :: get_ab(void)
{
a = 5; b = 10;
}
int B :: get_a
{
return a;
}
void B :: show_a()
{
cout << “a = “ << a << ”\n”;
}
void D :: mul()
{
c = b * get_a();
}
void D :: display()
{
cout << “a = “ << get_a() << ”\n”;
cout << “b = “<< b << “\n”;
cout << “c= “<< c << “\n\n”;
}
void main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b = 20;
d.mul();
d.display();
}
OUTPUT:
a = 5
a = 5
b = 10
c = 50
a = 5
b = 20
c = 100
//10. MULTILEVEL INHERITANCE:
#include<iostream.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student :: get_number(int a)
{
roll_number = a;
}
void student::put_number()
{
cout << “Roll Number: “ << roll_number << “\n”;
}
class test : public student
{
protected:
float sub1;
float sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x;
sub2 = y;
}
void test :: put_marks()
{
cout << “Marks in sub1 = “ << sub1 << “\n”;
cout << “Marks in sub2 = “ << sub2 << “\n”;
}
class result :public test
{
float total;
public:
void display(void);
};
void result :: display(void)
{
total = sub1 + sub2;
put_number();
put_marks();
cout << “Total = “ << total <<”\n”;
}
void main()
{
result student1;
student1.get_number(111);
student2.get_marks(75.0, 59.5);
student1.display();
}
OUTPUT:
Roll Number: 111
Marks in sub1 = 75
Marks in sub2 = 59.5
Total = 134.5
// 11.MULTIPLE INHERITANCE:
#include<iostream.h>
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P : public M, public N
{
public:
void display(void);
};
void M :: get_m(int x)
{
m = x;
}
void N :: get_n(int y)
{
n = y;
}
void P :: display(void)
{
cout << “m = “ << m << “\n”;
cout << “n = “ << n << “\n”;
cout << “m*n = “ << m*n << “\n”;
}
void main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
}
OUTPUT:
m = 10
n = 20
m*n = 200
//12. VIRTUAL FUNCTIONS:
#include<iostream.h>
class Base
{
public:
void display()
{ cout << “\n Display base”; }
virtual void show()
{ cout << “\n show base”;}
};
class Derived : public Base
{
public:
void display()
{ cout << “\n Display derived”; }
void show()
{ cout << “\n Show derived”; }
};
void main()
{
Base B;
Derived D;
Base *bptr;
cout << ”\n bptr points to Base \n”;
bptr = &B;
bptr -> display();
bptr -> show();
cout << “\n\n bptr points to Derived\n”;
bptr = &D;
bptr -> display();
bptr -> show();
}
OUTPUT:
bptr points to Base
Display base
Show base
bptr points to Derived
Display base
Show derived
//13. RUNTIME POLYMORPHISM:
#include<iostream.h>
#include<cstring>
class media
{
protected:
char title[50]
float p rice;
public:
media(char *s, float a)
{
strcpy(title, s);
price = a;
}
virtual void display() { }
};
class book : public media
{
int pages;
public:
book(char *s, float a, int p):media(s, a)
{
pages = p;
}
void display();
};
class tape : public media
{
float time;
pubic:
tape(char * s, float a, float t):media(s, a)
{
time = t;
}
void display()’ };
void book :: display()
{
cout << “\n Title: “ << title;
cout << “\n Pages: “ << pages;
cout << “\n Price: “ << price;
}
void tape :: display()
{
cout << “\n Title: “ << title;
cout << “\n Play time: “ << time << “mins”;
cout << “\n Price: “ << price;
}
void main()
{
char * title = new char[30];
float price, time;
int pages;
cout << “\n ENTER BOOK DETAILS\n”;
cout << “ Title: “; cin >> title;
cout << “ Price: “; cin >> price;
cout << “ Pages: “; cin >> pages;
book book1(title, price, pages);
cout << “ ENTER TAPE DETAILS\n”;
cout << “ Title: “; cin >> title;
cout << “ Price: “; cin >> price;
cout << “Play time (mins): “; cin >> time;
tape tape1(title, price, time);
media* list[2];
list[0] = &book1;
list[1] = &tape1;
cout << “\n MEDIA DETAILS”;
cout << “\n ......BOOK……”;
list[0] -> display();
cout <<”\n ……tape……”;
list[1] -> display();
}
OUTPUT:
ENTER BOOK DETAILS
Title: Programming_in_ANSI_C
Price: 88
Pages: 400
ENTER TAPE DETAILS
Title: Computing_Concepts
Price: 90
Play time(mins) : 55
MEDIA DETAILS
……BOOK……
Title: Programming_in_ANSI_C
Pages: 400
Price: 88
……TAPE……
Title: Computing_Concepts
Play time(mins) : 55
Price: 90
//14.FIND PRIME OR NOT
#include<iostream.h>
#include<conio.h>
class prime
{
int n,s,i;
public:
void getdata(void);
void calculate(void);
void display (void);
};
void prime::getdata(void)
{
cout<<"enter the number";
cin>>n;
}
void prime::calculate(void)
{
s=0;
i=2;
while(i<n)
{
if(n%i==0)
s=s+1;
i=i+1;
}
}
void prime::display(void)
{
if(s==0)
{
cout<<"the given number is prime";
}
else
{
cout<<"the given number is not prime";
}
getch();
}
int main()
{
clrscr();
prime p;
p.getdata();
p.calculate();
p.display();
return 0;
OUTPUT:
Enter the number
7
The given number is prime
// 15.FIND ARMSTRONG OR NOT
#include<iostream.h>
#include<math.h>
#include<conio.h>
class armstrong
{
int n,r,s,t;
public:
void getdata(void);
void calculate(void);
void display(void);
};
void armstrong::getdata(void)
{
cout<<"enter n:";
cin>>n;
}
void armstrong::calculate(void)
{
s=0;
t=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
}
void armstrong::display(void)
{
if(t==s)
{
cout<<"given number is armstrong:"<<t;
}
else
{
cout<<"given number is not armstrong:"<<t;
}
getch();
}
int main()
{
clrscr();
armstrong a;
a.getdata();
a.calculate();
a.display();
return (0);
}
OUTPUT:
Enter n:153
Given number is armstrong:153
Smt. Padmavatibai Raghavendraroa Deshpande Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department
Of
Computer Science
B.Sc. 5th
Semester (5.1) Lab Manual
VISUAL BASICS Lab
INDEX
VB Programming
Sl. No Name of the Program
1 Write a Visual Basic Program to find Arithmetic Operations.
2 Write a Visual Basic Program to find Area of a triangle.
3 Write a Visual Basic Program to find Simple Interest.
4 Write a Visual Basic Program to find Whether the given number is Even or
Odd.
5 Write a Visual Basic Program to Find Prime number or not.
6 Write a Viual Basic Program to find Largest of three numbers.
7 Write a Visual Basic Program to Display different Shapes.
8 Write a Visual Basic Program for Changing the colours.
9 Write a Visual Basic Program for Changing Text Styles.
10 Write a Visual Basic Program for Displaying message on the form.
11 Write a Visual Basic Program to Change the string Function.
12 Write a Visual Basic Program to Create MDI form.
13 Write a VB Program to create an application to view the pictures
14 Write a Visual Basic program to create animation files.
15 Create a VB Program to display a menu on the form.
1. Write a Visual Basic Program to find Arithmetic Operations.
Procedure for finding Arithmetic Operations.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 3 Labels, 3 Text boxes, 5 command buttons.
Step3: Changing the properties of controls CONTROL PROPERTIES VALUE
Form1 Caption Arithmetic Operations
Label1 Caption Input First Number
Label2 Caption Input Second Number
Label3 Caption The Sum Of Two Number is
Command1 Caption Sum
Command2 Caption Sub
Command3 Caption Mul
Command4 Caption Div
Command5 Caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING FOR ARITHMETIC OPERATIONS Private Sub command1_Click ()
Text3.text = Val (text1.text) + Val (text2.text)
End Sub
Private Sub command2_Click ()
Text3.text = Val (text1.text) - Val (text2.text)
End Sub
Private Sub command3_Click ()
Text3.text = Val (text1.text) * Val (text2.text)
End Sub
Private Sub command4_Click ()
Text3.text = Val (text1.text) / Val (text2.text)
End Sub
Private Sub command5_Click ()
End
End Sub
DESIGN FOR ARITHMETIC OPERATION
2. Write a Visual Basic Program to find Area of a triangle.
Procedure for finding Area of a triangle.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 3 Labels, 3 Text boxes, 2 command buttons.
Step3: Changing the properties of controls
CONTROL PROPERTIES VALUE
Form1 caption Area Of a Triangle
Label1 caption Input Base
Label2 caption Input Height
Label3 caption The Area Of a Triangle is
Command1 caption Result
Command2 caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING TO FIND AREA OF A TRIANGLE
Private Sub Command1_Click()
Text3.Text = 1 / 2 * Val(Text1.Text) * Val(Text2.Text)
End Sub
Private Sub Command2_Click()
End
End Sub
DESIGNING TO FIND AREA OF A TRIANGLE
3. Write a Visual Basic Program to find Simple Interest.
Procedure for finding Simple interest.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 4 Labels, 4 Text boxes, 2 command buttons.
Step3: Changing the properties of controls
CONTROL PROPERTIES VALUE
Form1 caption SIMPLE INTEREST
Label1 caption Input Principal
Label2 caption Input Time
Label3 caption Input Rate of Interest
Label4 caption The S.I is
Command1 caption Result
Command2 caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING TO FIND SIMPLE INTEREST
Private Sub Command1_Click()
Text4.Text = Val(Text1.Text) * Val(Text2.Text) * Val(Text3.Text) / 100
End Sub
Private Sub Command2_Click()
End
End Sub
DESIGNING TO FIND SIMPLE INTEREST
4. Write a Visual Basic Program to find Whether the given number is Even or Odd.
Procedure to find the given number is even or odd.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 2 Labels, 2 Text boxes, 2 command buttons.
Step3: Changing the properties of controls
CONTROL PROPERTIES VALUE
Form1 Caption Even or Odd
Label1 Caption Input Number
Label2 Caption The Result is
Command1 Caption Result
Command2 Caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING FOR FINDING EVEN OR ODD NUMBER.
Private Sub Command1_Click()
If Val(Text1.Text) Mod 2 = 0 Then
Text2.Text = "NUMBER IS EVEN"
Else
Text2.Text = "NUMBER IS ODD"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
DESIGNING TO FIND EVEN OR ODD NUMBER
5. Write a Visual Basic Program to Find Prime number or not.
Procedure for finding prime number or not.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 2 Labels ,2 text boxes, 3 command buttons.
Step3: Changing the properties of controls
CONTROL PROPERTIES VALUE
Form1 caption Project to find prime number or not
Label1 caption Input number
Label2 caption The input number is
Command1 caption Display
Command2 caption Clear
Command3 caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING FOR DISPLAYING PRIME NUMBER OR NOT
Private Sub Command1_Click()
Dim i, n As Integer
Dim strmsg As String * 30
n = Val(Text1.Text)
For i = 2 To n / 2
If (n Mod i) = 0 Then
strmsg = "Not a Prime Number"
Text2.Text = strmsg
Exit Sub
End If
Next i
strmsg = "Prime Number"
Text2.Text = strmsg
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
Private Sub Command3_Click()
End
End Sub
DESIGNING TO FIND PRIME NUMBER OR NOT
6. Write a Viual Basic Program to find Largest of three numbers.
Procedure for finding Largest of three numbers.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 4 Labels, 4 Text boxes, 3 command buttons.
Step3: Changing the properties of controls
CONTROL PROPERTIES VALUE
Form1 caption Largest of three numbers
Label1 caption Input First number
Label2 caption Input Second number
Label3 caption Input Third number
Label4 caption The Largest Number is
Command1 caption Largest
Command2 caption Clear
Command3 caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING FOR LARGEST OF THREE NUMBERS
Private Sub Command1_Click()
Dim a, b, c As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
if ((a >b) And (a>c)) then
Text4.Text=a
else
if(b>c) then
Text4.Text=b
else
Text4.Text=c
endif
endif
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
Private Sub Command3_Click()
End
End Sub
DESIGNING FOR DISPLAYING LARGEST OF THREE NUMBERS
7. Write a Visual Basic Program to Display different Shapes.
Procedure for displaying different shapes.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 1 Shape button, 4 Option buttons, 1 Frame and 1 command button.
Step3: Changing the properties of controls.
CONTROL PROPERTIES VALUE
Form1 Caption To Display Different Shapes
Frame1 Caption Changing the Shapes
Option1 Caption
Name
Square
Optsquare
Option2 Caption
Name
Rectangle
Optrec
Option3 Caption
Name
Oval
Optoval
Option4 Caption
name
Circle
optcir
Command1 Caption Exit
Shape1 Name Shape1
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application.
CODING FOR DISPLAYING DIFFERENT SHAPES Private Sub Optcir_Click()
Shape1.Shape = 3
End Sub
Private Sub Optoval_Click()
Shape1.Shape = 2
End Sub
Private Sub optrec_Click()
Shape1.Shape = 0
End Sub
Private Sub optsquare_Click()
Shape1.Shape = 1
End Sub
Private Sub cmdexit_Click()
Unload Me
End Sub
DESIGNING FOR DISPLAYING DIFFERENT SHAPES
8. Write a Visual Basic Program for Changing the colours.
Procedure for Changing the colours.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 1 Text box, 1 command button,3 option buttons.
Step3: Changing the properties of controls
CONTROL PROPERTIES VALUE
Form1 Caption Changing the Colours
Text1 Name
Text
Text1
Welcome to L.V.D College
Option1 Caption Blue
Option2 Caption Green
Option3 Caption Red
Command1 Caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING FOR CHANGING THE COLOURS
Private Sub Option1_Click()
Text1.ForeColor = vbBlue
End Sub
Private Sub Option2_Click()
Text1.ForeColor = vbGreen
End Sub
Private Sub Option3_Click()
Text1.ForeColor = vbRed
End Sub
Private Sub Command1_Click()
End
End Sub
DESIGNING FOR CHANGING THE COLOURS
9.Write a Visual Basic Program for Changing Text Styles.
Procedure for Changing Text Styles.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 1 Text box, 1 command button,3 Check Boxes.
Step3: Changing the properties of controls
CONTROL PROPERTIES VALUE
Form1 Caption Changing the text styles
Text1 Name
Text
Text1
Welcome to L.V.D College
Check1 Caption Bold
Check2 Caption Underline
Check3 Caption Italic
Command1 Caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING FOR CHANGING TEXT STYLES
Private Sub Check1_Click()
Text1.FontBold = True
End Sub
Private Sub Check2_Click()
Text1.FontUnderline = True
End Sub
Private Sub Check3_Click()
Text1.FontItalic = True
End Sub
Private Sub Command1_Click()
End
End Sub
DESIGNING FOR CHANGING THE TEXT STYLES
10. Write a Visual Basic Program for Displaying message on the form.
Procedure for displaying message on the form.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 1 command button.
Step3: Changing the properties of controls CONTROL PROPERTIES VALUE
Form1 caption To Display Message
Command1 Caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
CODING FOR DISPLAYING MESSAGE
Private Sub Command1_Click()
Print “Welcome to Visual Basic"
End Sub
DESIGNING FOR DISPLAYING MESSAGE
11. Write a Visual Basic Program to Change the string Function.
Procedure to Change the string function.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select 1 Text box, 3 command buttons.
Step3: Changing the properties of controls CONTROL PROPERTIES VALUE
Form1 Caption To Change the String Function
Text1 Name Text1
Command1 Caption Ucase
Command2 Caption Exit
Command3 Caption Clear
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
CODING FOR CHANGING THE STRING FUNCTION.
Private Sub Command1_Click()
Text1.Text = UCase(Text1.Text)
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Command3_Click()
Text1.Text = ""
End Sub
DESIGNING TO CHANGE THE STRING FUNCTION
12. Write a Visual Basic Program to Create MDI form.
Procedure to Create MDI form.
Step1: A new standard EXE project item is selected from file menu.
Step2: Click Project button and select MDI form.
Step3: Adding form1 and form2.
Step4: Changing the properties of forms.
CONTROL PROPERTIES VALUE
MDI FORM Name
Caption
MDIFORM
MDIFORM
FORM1 Name
Caption
sunset
sunset
FORM2 Name
Caption
Waterlily
waterlily
Step5:To get picture on the form1 .first click picture property from the properties
window.select the picture. After selecting click open.
Step6:In the similar manner select picture for form2.
Step7: Make the MDIFORM as the Startup form.
Step8:Writing and connecting the code.
To write the code for form1.Select forms Option from the menu of the MDI form
& Clicking on the Form1 will open a code window.
Type the following code
Private Sub sunset_Click()
Sunset.Show
End Sub
Step9: In the similar manner write the following code for form2
Private Sub waterlily_Click()
waterlily.Show
End Sub
Step10: Write the following code for the Exit
Private Sub Exit_Click()
End
End Sub
Step11: Run the application
DESIGNING TO CREATE MDI FORM
13. Write a VB Program to create an application to view the pictures
Procedure for running picture views.
Step1: A new standard EXE project item is selected from file menu.
Step2: Select a PictureBox option from the Tool Box
Step3: select two command box.
CONTROL PROPERTIES VALUE
PictureBox
Command1 Caption Picture1
Command2 Caption Exit
Step 4: After doing the above settings the form would look as follows.
Step 5: Write the following code and run the application.
Coding for Picture View
Private Sub Command1_Click()
Picture1.Picture = LoadPicture("c:\sample2.jpg")
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Designing for Picture View
14. Write a Visual Basic program to create animation files.
Procedure for running Animation
Step1: A new standard EXE project item is selected from file menu.
Step2: To select animation, go to component, project : under project select following controls
a) Microsoft common dialog control 6.0.
b) Microsoft common dialog controls 2 5.0 (SP2).
c) Microsoft Animation or Windows Media Player.
Step3: Select the control on the form and in the properties window set the following: CONTROL PROPERTIES VALUE
Common dialog control 6.0.
Common dialog controls 2 5.0 (SP2).
Animation
Command1 Caption Open
Command2 Caption Play
Command3 Caption Stop
Command4 Caption Exit
Step4: After doing the above settings the form would look as follows.
Step5: Write the following code.
Step6: Run the application
Coding for Animation
Private Sub Command1_Click()
CommonDialog1.ShowOpen
Animation1.Open (CommonDialog1.FileName)
End Sub
Private Sub Command2_Click()
Animation1.Play
End Sub
Private Sub Command3_Click()
Animation1.Stop
End Sub
Private Sub Command4_Click()
Unload Me
End Sub
Designing for Animation
15. Create a VB Program to display a menu on the form.
Procedure to Display a menu.
Step 1: A new standard EXE project item is selected from file menu.
Step 2: To add a menu into form1. Click Tools menu and then Click menu Editor
to display a menu editor window.
Step 3: Type the Caption Breakfast & Name Breakfast in the Menu Editor. Step 4: Click the Next button.The caption and name textboxes will clear .
Step 5: Click the Right arrow button for the next item.
Step 6: Type the caption Idli &Name Idli.
Step 7: Repeat step 4 to 6 for dosa & vada.
Step 8: Click the Left arrow button to return to the previous level.
Step 9: Click the next button.Type caption exit &Name Exit.
Step10: click ok to save and back to form.
Step 11: Run the project.
DESIGNING TO DISPLAY A MENU BAR ON THE FORM
Smt. Padmavatibai Raghavendraroa Deshpande
Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department Of
Computer Science
B.Sc. 6th
Semester Lab Manual
DATA STRUCTURE USING C++ Lab
(6.1)
INDEX
Data Structure using C++
Sl.
No Name of the Program
1 Program to insert and delete an element in an array
2 Program to perform linear search using array;
3 Program to perform the Binary Search operations in array;
4 Program to perform STACK OPERATIONS
5 Program to perform operations on QUEUE.
6 Program to demonstrate a concept of Linked list.
7 Program to perform TRAVERSE a tree in PREORDER
8 Program to TRAVERSE a tree in INORDER
9 Program to TRAVERSE a tree in POSTORDER
10 Program to calculate POSTFIX expression
11 Program to perform SELECTION sort
12 Program to perform BUBBLE sort
13 Program to perform INSERTION sort
14 Program to perform RADIX sort
//1.Program to insert and delete an element in an array
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class arrays
{
private: int a[10],n,i,k,item,ch,j;
public: void opers();
};
void arrays:: opers()
{
cout<<"\n\n OUTPUT FOR ARRAY OPERATIONS \n\n";
cout<<"enter the number of elements you want:\n";
cin>>n;
cout<<"enter the "<<n<<" elements:\n";
for(i=1;i<=n;i++)
cin>>a[i];
do
{
cout<<"\n enter the choice: \n 1.insert \n 2.delete \n 3.exit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n enter the item to be inserted \n";
cin>>item;
cout<<"\n enter the position to insert the item\n";
cin>>k;
j=n;
while(j>=k)
{
a[j+1]=a[j];
j--;
}
a[k]=item;
n++;
cout<<"After insertion the array\n";
for(i=1;i<=n;i++)
cout<<"\t"<<a[i];
break;
case 2:
if(n==0)
cout<<"The array is empty,deletion is not possible\n";
else
{
cout<<"enter the position of the item to be deleted\n";
cin>>k;
item=a[k];
j=k;
while(j<=n-1)
{
a[j]=a[j+1];
j++;
}
n--;
if(n==0)
cout<<"\n now the array is empty\n";
else
cout<<"\n After deletion the array is:\n";
for(i=1;i<=n;i++)
cout<<"\t"<<a[i];
}
break;
default:
cout<<"the code is mismatch try again!\n";
break;
}
}
while(ch!=3);
}
void main()
{
clrscr();
arrays ar;
ar.opers();
getch();
}
OUTPUT FOR ARRAY OPERATIONS
enter the number of elements you want:
4
enter the 4 elements:
12 34 56 67
enter the choice:
1.insert
2.delete
3.exit
1
enter the item to be inserted
23
enter the position to insert the item
3
After insertion the array
12 34 23 56 78
enter the choice:
1.insert
2.delete
3.exit
2
enter the position of the item to be deleted
3
After deletion the array is:
12 34 56 78
enter the choice:
1.insert
2.delete
3.exit
3
the code is mismatch try again!
//2.Program to perform linear search using array;
#include<iostream.h>
#include<conio.h>
class linear
{
private: int i,n,item,a[10],loc;
public: void search();
};
void linear:: search()
{ cout<<"OUT PUT FOR LINEAR SEARCH"<<endl;
cout<<"Enter no of elements"<<endl;
cin>>n;
cout<<"Enter the elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the item to search"<<endl;
cin>>item;
for(i=0;i<n;i++)
if(a[i]==item)
{
loc=i;
cout<<"Search is sucessful at location "<<loc;
}
if(loc!=item)
cout<<"\n Search is unsucessful\n";
}
void main()
{
clrscr();
linear ln;
ln.search();
getch();
}
OUT PUT FOR LINEAR SEARCH
Enter no of elements
4
Enter the elements
12 34 45 67
Enter the item to search
67
Search is successful at location 3
//3.program to perform the binary search operations in array;
#include<iostream.h>
#include<conio.h>
class binsear
{
private: int a[20],st,i,j,temp,end,n,mid,loc,item;
public: void search();
};
void binsear:: search( )
{
cout<<"\n OutPut of binary search\n";
cout<<"enter number of elements:\n";
cin>>n;
cout<<"enter the elemens of array:\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter the elements to search:\n";
cin>>item;
st=1;
end=n;
mid = ((st+end)/2);
while((st<=end)&&(a[mid]!=item))
{
if(item<a[mid])
end=mid-1;
else
st=mid+1;
mid=((st+end)/2);
}
if(a[mid]==item)
cout<<"search is successful at location:"<<mid;
else
cout<<"search is unsucessful \n";
}
void main( )
{
binsear br;
clrscr();
br.search();
getch();
}
OUTPUT OF BINARY SEARCH
enter number of elements:
5
enter the elemens of array:
12 34 56 67 23
enter the elements to search:
67
search is successful at location:3
//4. Program to perform STACK OPERATIONS
#include<iostream.h>
#include<conio.h>
#include<process.h>
class stacks
{
private: int top,st[10],item,i,ch;
public: void operations();
};
void stacks::operations()
{
top=0;
cout<<"output for stack operations:\n";
cout<<"\n enter the choice:\n 1.push \n 2.pop \n 3.exit\n";
cin>>ch;
while(ch!=3)
{
switch(ch)
{
case 1: cout<<"enter the item to insert in stack:\n";
cin>>item;
st[++top] = item;
break;
case 2: --top;
if(top<=0)
{
cout<<"empty stack";
top=0;
}
break;
case 3:exit(1);
default: cout<<"illegal entry\n";
}
cout<<"\n After stack operation\n";
for(i=1;i<=top;i++)
cout<<"\n"<<st[i];
cout<<"\n enter the choice 1.push 2.pop 3.exit\n";
cin>>ch;
}
}
void main()
{
stacks st;
clrscr();
st.operations();
getch();
}
OUTPUT FOR STACK OPERATIONS:
enter the choice:
1.push
2.pop
3.exit
1
enter the item to insert in stack:
23
After stack operation
23
enter the choice 1.push 2.pop 3.exit
1
enter the item to insert in stack:
24
After stack operation
23
24
enter the choice 1.push 2.pop 3.exit
1
enter the item to insert in stack:
25
After stack operation
23
24
25
enter the choice 1.push 2.pop 3.exit
2
After stack operation
23
24
enter the choice 1.push 2.pop 3.exit
3
//5.Program to peform operations on QUEUE.
#include<iostream.h>
#include<conio.h>
#define maxq 10
class queue
{
private: int rear,front,q[10],ch,i,item;
public:
void calc();
};
void queue::calc()
{
front=1;
rear=0;
cout<<"Select your choice"<<endl;
cout<<"1.Insert\n 2.Delete\n 3.Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
do
{
switch(ch)
{
case 1:{
if(rear==maxq)
cout<<"queue is full"<<endl;
else
cout<<"Enter item to be inserted"<<endl;
cin>>item;
q[++rear]=item;
}
break;
case 2:{
if(front>=rear)
cout<<"queue is empty"<<endl;
else
item=q[front++];
}
break;
default:{
cout<<"illegal entry"<<endl;
}
break;
}
cout<<"After queue operation"<<endl;
for(i=front;i<=rear;i++)
cout<<q[i]<<endl;
cout<<"enter choice"<<endl;
cout<<"1.Insert 2.Delete 3.Exit"<<endl;
cin>>ch;
}
while(ch!=3);
}
void main()
{
clrscr();
queue q;
q.calc();
getch();
}
OUT PUT FOR QUEUES:
enter choice:
1.insert 2.delete 3.exit
1
Enter item to be inserted
12
enter choice:
1.insert 2.delete 3.exit
1
Enter item to be inserted
After queue operation
12
13
enter choice
1.Insert 2.Delete 3.Exit
1
Enter item to be inserted
14
After queue operation
12
13
14
enter choice
1.Insert 2.Delete 3.Exit
2
After queue operation
13
14
enter choice
1.Insert 2.Delete 3.Exit
3
//6. Program to demonstrate a concept of Linked list.
#include<iostream.h>
#include<malloc.h>
#include<conio.h>
struct list
{
int info;
struct list*next;
};
typedef struct list node;
node *first,*ptr,*start;
void main()
{
clrscr();
first=(node*)malloc(sizeof(node));
cout<<"Enter the elements at end enter 0"<<endl;
cin>>first->info;
ptr=start=first;
while(first->info!=0)
{
first=(node*)malloc(sizeof(node));
cin>>first->info;
ptr->next=first;
ptr=first;
}
ptr->next=0;
cout<<"List after creation"<<endl;
while(start->next!=0)
{
cout<<start->info<<"->";
start=start->next;
}
cout<<"Null";
getch();
}
OUTPUT:-
Enter the elements at end enter 0
12
14
35
67
99
0
List after creation
12->14->35->67->99->Null
//7. Program to perform TRAVERSE a tree in PREORDER
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
#define null 0
struct tree
{
int info;
struct tree *left,*right;
};
typedef struct tree node;
node * root;
void preorder(node *p)
{
if(p!=null)
{
cout<<p->info;
preorder(p->left);
preorder(p->right);
}
}
void read1(node **p2)
{
node *p1;
int item;
p1=(node *)malloc(sizeof(node));
cin>>item;
if(item!=0)
{
p1->info=item;
p1->left=null;
p1->right=null;
*p2=p1;
}
if(item!=0)
{
cout<<"the left child of is:\n"<<p1->info;
read1(&p1->left);
cout<<"the right child of is:\n"<<p1->info;
read1(&p1->right);
}
}
void main()
{
root=null;
clrscr();
cout<<"\n\nOUTPUT FOR PREORDER TRAVERSAL\n\n";
cout<<"enter the root or 0 to quit\n";
read1(&root);
cout<<"\nThe preorder traversal of the tree is:\n";
preorder(root);
getch();
}
OUTPUT FOR PREORDER TRAVERSAL
enter the root or 0 to quit
24
the left child of is:
24 6
the left child of is:
6 0
the right child of is:
6 0
the right child of is:
24 5
the left child of is:
5 8
the left child of is:
8 0
the right child of is:
8 0
the right child of is:
5 0
The preorder traversal of the tree is:
24658
//8. Program to TRAVERSE a tree in INORDER
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
#define null 0
struct tree
{
int info;
struct tree *left,*right;
};
typedef struct tree node;
node *root;
void inorder(node *p)
{
if(p!=null)
{
inorder(p->left);
cout<<p->info;
inorder(p->right);
}
}
void read1(node **p2)
{
node *p1;
int item;
p1=(node *)malloc(sizeof(node));
cin>>item;
if(item!=0)
{
p1->info=item;
p1->left=null;
p1->right=null;
*p2=p1;
}
if(item!=0)
{
cout<<"the left child of is:\n"<<p1->info;
read1(&p1->left);
cout<<"the right child of is:\n"<<p1->info;
read1(&p1->right);
}
}
void main()
{
root=null;
clrscr();
cout<<"\n\nOUTPUT FOR INORDER TRAVERSAL\n\n";
cout<<"enter the root or 0 to quit\n";
read1(&root);
cout<<"\n The inorder traversal of the tree is:\n";
inorder(root);
getch();
}
OUTPUT FOR INORDER TRAVERSAL
enter the root or 0 to quit
24
the left child of is:
24 12
the left child of is:
12 0
the right child of is:
12 43
the left child of is:
43 67
the left child of is:
67 0
the right child of is:
67 0
the right child of is:
43 0
the right child of is:
24 0
The inorder traversal of the tree is:
12674324
//9. Program to TRAVERSE a tree in POSTORDER
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
#define null 0
struct tree
{
int info;
struct tree *left,*right;
};
typedef struct tree node;
node *root;
void postorder(node *p)
{
if(p!=null)
{
postorder(p->left);
postorder(p->right);
cout<<p->info;
}
}
void read1(node **p2)
{
node *p1;
int item;
p1=(node *)malloc(sizeof(node));
cin>>item;
if(item!=0)
{
p1->info=item;
p1->left=null;
p1->right=null;
*p2=p1;
}
if(item!=0)
{
cout<<"the left child of is:\n"<<p1->info;
read1(&p1->left);
cout<<"the right child of is:\n"<<p1->info;
read1(&p1->right);
}
}
void main()
{
root=null;
clrscr();
cout<<"\n\nOUTPUT FOR POSTORDER TRAVERSAL\n\n";
cout<<"enter the root or 0 to quit\n";
read1(&root);
cout<<"\nThe inorder traversal of the tree is:\n";
postorder(root);
getch();
}
OUT PUT FOR POST ORDER TRAVERSAL
enter the root or 0 to quit
24
the left child of is:
24 3
the left child of is:
3 0
the right child of is:
3 6
the left child of is:
6 0
the right child of is:
6 0
the right child of is:
24 5
the left child of is:
5 9
the left child of is:
9 0
the right child of is:
9 0
the right child of is:
5 0
The inorder traversal of the tree is:
639524
//10.Program to calculate POSTFIX expression
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class postfix
{
private: char item;
float top,op1,op2,value,st[20];
public: void pstfxopers();
};
void postfix::pstfxopers()
{
cout<<"\n\n OUTPUT FOR POSTFIX EXPRESSION \n\n";
cout<<"\n enter the posfix expression at end enter ) \n";
cin>>item;
top=0;
while(item!=')')
{
if((item>='0') && (item<='9'))
st[++top]=item-'0';
else
{
op1=st[top--];
op2=st[top--];
switch(item)
{
case '+': value=op2+op1;
break;
case '-': value=op2-op1;
break;
case '*': value=op2*op1;
break;
case '/': value=op2/op1;
break;
default:
{
cout<<"invalid entry\n";
}
}
st[++top] = value;
}
fflush(stdin);
cin>>item;
}
cout<<"\n value of the entered postfix exp:\n"<<value;
}
void main()
{
clrscr();
postfix p;
p.pstfxopers();
getch();
}
OUTPUT FOR POSTFIX EXPRESSION
enter the posfix expression at end enter )
23*7+45*9
)
value of the entered postfix exp:
20
//11. Program to perform SELECTION sort
#include<iostream.h>
#include<conio.h>
class selsort
{
private: int a[20],k,n,i,j,loc,min,temp;
public: void sort();
};
void selsort::sort()
{
cout<<"\n OUT PUT FOR SELECTION SORT\n";
cout<<"enter the number of elements:\n";
cin>>n;
cout<<"enter the " <<n<< " elements\n";
for(i=1;i<=n;i++)
cin>>a[i];
for(j=1;j<=n;j++)
{
min=a[j];
loc=j;
for(k=j+1;k<=n;k++)
if(min>a[k])
{
min = a[k];
loc=k;
temp=a[j];
a[j]=a[loc];
a[loc]=temp;
}
}
cout<<"sorted elements are: \n";
for(i=1;i<=n;i++)
cout<<"\n"<<a[i];
}
void main()
{
selsort srt;
clrscr();
srt.sort();
getch();
}
OUT PUT FOR SELECTION SORT
enter the number of elements:
5
enter the 5 elements
12 24 16 89 87
sorted elements are:
12
16
24
87
89
//12. Program to perform BUBBLE sort
#include<iostream.h>
#include<conio.h>
class bublesrt
{
private: int i,j,n,a[20],temp;
public: void sort();
};
void bublesrt:: sort()
{
cout<<" OUTPUT FOR BUBBLE SORT\n";
cout<<"enter the number of elements to sort:\n";
cin>>n;
cout<<"enter "<< n <<"elements in an array:\n";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n-1;i++)
for(j=1;j<=n-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
cout<<"sorted elements are:\n";
for(i=1;i<=n;i++)
cout<<"\n"<<a[i];
}
void main()
{
bublesrt br;
clrscr();
br.sort();
getch();
}
OUTPUT FOR BUBBLE SORT
enter the number of elements to sort:
5
enter 5elements in an array:
12 45 34 67 40
sorted elements are:
12
34
40
45
67
// 13.Program to perform INSERTION sort
#include<iostream.h>
#include<conio.h>
class insersort
{
private: int a[10],ptr,i,n,k,temp;
public: void sort();
};
void insersort::sort()
{
cout<<"\n OUT PUT FOR INSERTION SORT\n";
cout<<"enter the number of elements:\n";
cin>>n;
cout<<"enter the "<<n<<" elements:\n";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=2;i<=n;i++)
{
temp=a[i];
ptr=i-1;
while(temp<a[ptr])
{
a[ptr+1]=a[ptr];
ptr--;
}
a[ptr+1]=temp;
}
cout<<"sorted elements are:\n";
for(i=1;i<=n;i++)
cout<<"\n"<<a[i];
}
void main()
{
insersort isrt;
clrscr();
isrt.sort();
getch();
}
OUT PUT FOR INSERTION SORT
enter the number of elements:
5
enter the 5 elements:
12 45 24 78 99
sorted elements are:
12
24
45
78
99
// 14.Program to perform RADIX sort
#include<iostream.h>
#include<conio.h>
class radixsrt
{
private: int c[20],a[20],d,i,j,k,l,m,n,rem,temp,temp1;
public: void radixsort();
};
void radixsrt:: radixsort()
{
cout<<"\n OUT PUT FOR RADIX SORT\n";
cout<<"enter the value of n:\n";
cin>>n;
cout<<"enter the no.of digits:\n";
cin>>d;
cout<<"enter the "<<d<<" numbers:\n";
for(i=1;i<=n;i++)
cin>>a[i];
temp1=1;
for(l=1;l<=d;l++)
{
i=1;
for(j=0;j<=9;j++)
{
for(k=1;k<=n;k++)
{
temp=a[k]/temp1;
rem=temp%10;
if(rem==j)
{
c[i]=a[k];
i++;
}
}
}
}
temp1=temp1*10;
cout<<" at pass "<<l<<endl;
for(m=1;m<=n;m++)
{
a[m]=c[m];
cout<<"\n"<<a[m];
}
}
void main()
{
radixsrt rsrt;
clrscr();
rsrt.radixsort();
getch();
}
OUT PUT FOR RADIX SORT
enter the value of n:
4
enter the no.of digits:
2
enter the 2 numbers:
-25
50
-10
60
at pass 3
50
-10
60
40
Smt. Padmavatibai Raghavendraroa Deshpande
Pikalihal
Government First Grade College, Mudgal -584125
Affiliated under
Gulbarga University
Department
Of Computer Science
B.Sc. 6th
Semester Lab Manual
JAVA AND INTERNET PROGRAMMING
LAB(6.2)
INDEX
Java programming
Sl.
No Name of the Program
1 Program to demonstrate Simple Arithmetic operations using
BufferedReader
2 Program to generate the Fibonacci numbers using readLine( )
method.
3 Program to demonstrate Strings concept and display whether
String is Palindrome or not.
4
Program to demonstrate 2D arrays using Transpose of matrix.
5
Program to demonstrate how to write methods ( ) in java.
6 Program to demonstrate concept of Method overloading &
Polymorphism.
7 Program to demonstrate Interface concept.
8 Program to demonstrate Exception Handling concept.
9 Program to demonstrate different methods in Threads concept.
10 Program to create a text file using Files & copying chars from
one File into.
11 Program to display a Simple Applet.
//1)Program to demonstrate Simple Arithmetic operations using BufferedReader
import java.io.*;
importjava.util.*;
classArithematic
{
public static void main(String args[]) throws IOException
{
BufferedReaderbr =new BufferedReader(new
InputStreamReader(System.in));
String str=br.readLine( );
StringTokenizerst = new StringTokenizer(str," ,");
String s1=st.nextToken( );
String s2=st.nextToken( );
s1=s1.trim( );
s2=s2.trim( );
double n1 = Double.parseDouble(s1);
double n2 = Double.parseDouble(s2);
System.out.println("Addition:"+(n1+n2));
System.out.println("Substraction:"+(n1-n2));
System.out.println("Multiplication:"+(n1*n2));
System.out.println("Division:"+(n1/n2));
}
}
OUT PUT:
D:\javapg>javac Arithematic.java
D:\javapg>java Arithematic
20, 10
Addition:30.0
Subtraction:10.0
Multiplication:200.0
Division:2.0
//2)Program to generate the Fibonacci numbers using readLine( ) method
import java.io.*;
importjava.util.*;
classFibo
{
public static void main(String args[]) throws IOException
{
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("how many fibnoacci U need:");
int n= Integer.parseInt(br.readLine( ));
long f1=0,f2=1;
System.out.println(f1);
System.out.println(f2);
long f = f1+f2;
System.out.println(f);
int count=3;
while(count<n)
{
f1=f2;
f2=f;
f=f1+f2;
System.out.println(f);
count++;
}
}
}
OUT PUT:-
D:\javapg>javac Fibo.java
D:\javapg>java Fibo
how many fibnoacci U need:
5
0
1
1
2
3
//3)Program to demonstrate Strings concept and display whether String is
Palindrome or not!
import java.io.*;
importjava.lang.String;
class Palindrome
{
public static void main(String args[]) throws IOException
{
BufferedReaderbr = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter a string:");
String str = br.readLine( );
String temp= str;
StringBuffersb = new StringBuffer(str);
sb.reverse( );
str= sb.toString( );
if(temp.equalsIgnoreCase(str))
System.out.println(temp+" is a palindrome");
else
System.out.println(temp+" is not a palindrome");
}
}
OUT PUT:-
D:\javapg>javac Palindrome.java
D:\javapg>java Palindrome
enter a string:
madam
madam is a palindrome
D:\javapg>java Palindrome
enter a string:
computer
computer is not a palindrome
//4)Program to demonstrate 2D arrays using Transpose of matrix
import java.io.*;
importjava.util.*;
importjava.util.Scanner;
class Transpose {
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("enter rows,columns:");
int r = sc.nextInt( );
int c= sc.nextInt( );
intarr[ ] [ ] = new int [r][c];
System.out.println("enter elements of matrix:");
for(inti=0;i<r;i++)
for(int j=0;j<c;j++)
arr [i] [j]= sc.nextInt( );
System.out.print("the transpose of matrix:");
for(inti= 0;i<c;i++)
{
for(int j= 0;j<r;j++)
{
System.out.print(arr[j][i]+" ");
}
System.out.println("\n");
}
}
}
OUT PUT:-
D:\javapg>javac Transpose.java
D:\javapg>java Transpose
enterrows,columns:
2 2
enter elements of matrix:
1 2
3 4
the transpose of matrix:
1 3
2 4
//5) Program to demonstrate how to write methods ( ) in java.
import java.io.*;
importjava.lang.String;
class person
{
private String name;
privateint age;
public void accept( ) throws IOException
{
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a name:");
name= br.readLine( );
System.out.println("Enter age:");
age=Integer.parseInt(br.readLine( ));
}
public void check( )
{
if (age<=30)
System.out.println(name+"is young");
else if (age<=50)
System.out.println(name+" is middle age");
else
System.out.println(name+ " is old");
}
}
class Demo
{
public static void main(String args[]) throws IOException
{
person p = new person( );
p.accept( );
p.check( );
}
}
OUT PUT:-
D:\javapg>javac Demo.java
D:\javapg>java Demo
enter a name:
sample
Enter age:
22
Sampleis young
//6) Program to demonstrate concept of Method overloading &Polymorphism
import java.io.*;
class Sample
{
void add (int a, int b)
{
System.out.println("Sum of two ="+(a+b));
}
void add(int a, int b, int c)
{
System.out.println("Sum of three="+(a+b+c));
}
}
class Poly
{
public static void main(String args[ ])
{
Sample s = new Sample( );
s.add(10,15);
s.add(10,15,20);
}
}
OUT PUT:-
D:\javapg>javac Poly.java
D:\javapg>java Poly
Sum of two =25
Sum of three=45
//7) Program to demonstrate Interface concept.
Class c = Class.forName(args[0]);
import java.io.*;
importjava.lang.Exception;
interfaceMyInter
{
void connect( );
void disconnect( );
}
classOracleDb implements MyInter
{
public void connect( )
{
System.out.println("connecting to OracleDB---");
}
public void disconnect( )
{
System.out.println("Disconnected from Oracle!");
}
}
classSybaseDb implements MyInter
{
public void connect( )
{
System.out.println("connecting to SybaseDb---");
}
public void disconnect( )
{
System.out.println("disconnected from Sysbase!");
}
}
classInterfaceDemo
{
public static void main(String args[ ]) throws Exception
{
MyInter mi = (MyInter)c.newInstance( );
mi.connect();
mi.disconnect();
}
}
OUT PUT:-
D:\javapg>javac InterfaceDemo.java
D:\javapg>java InterfaceDemoOracleDb
connecting to OracleDB---
Disconnected from Oracle!
// 8) Program to demonstrate Exception Handling concept
import java.io.*;
importjava.lang.Exception;
importjava.lang.Throwable;
class Exception
{
public static void main(String args[])
{
try
{
int a=2,b=4;
int c=2,x=7,z;
int p[]={2};
p[3]=33;
try {
z=x/((b*b)-(4*a*c));
System.out.println("The value of z is="+z);
}
catch(ArithmeticException e)
{
System.out.println("Division by zero in Arithematic expression");
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index is out of bound!!");
}
}
}
OUT PUT:-
D:\javapg>javac Exception.java
D:\javapg>java Exception
Array index is out of bound!!
//9).Program to demonstrate different methods in Threads concept
importjava.lang.Exception;
importjava.lang.Runnable;
importjava.lang.Throwable;
class A extends Thread
{
public void run( )
{
for(inti=1;i<=10;i++)
{
if(i==1)
yield( );
System.out.println("\tFrom Thread A:i="+i);
}
System.out.println("exit from A");
}
}
class B extends Thread
{
public void run( )
{
for(int j=1;j<=10;j++)
{
System.out.println("\t From thread B:j="+j);
if(j==3)
stop( );
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run( )
{
for(int k=1;k<=10;k++)
{
System.out.println("\t From Thread C:k="+k);
if(k==1)
try
{
sleep(1000);
}
catch(Exception e){ }
}
System.out.println("Exit from C");
}
}
classThreadMethods
{
public static void main(String args[ ])
{
A threadA =new A();
B threadB =new B();
C threadC= new C();
System.out.println("Start Thread A");
threadA.start( );
System.out.println("Start Thread B");
threadB.start( );
System.out.println("Start Thread C");
threadC.start( );
System.out.println("End of Main method");
}
}
OUTPUT:-
D:\javapg>javac ThreadMethods.java
D:\javapg>java ThreadMethods
Start Thread A
Start Thread B
Start Thread C
End of Main method
From thread B:j=1
From thread B:j=2
From thread B:j=3
From Thread C:k=1
From Thread A:i=1
From Thread A:i=2
From Thread A:i=3
From Thread A:i=4
From Thread A:i=5
From Thread A:i=6
From Thread A:i=7
From Thread A:i=8
From Thread A:i=9
From Thread A:i=10
exit from A
From Thread C:k=2
From Thread C:k=3
From Thread C:k=4
From Thread C:k=5
From Thread C:k=6
From Thread C:k=7
From Thread C:k=8
From Thread C:k=9
From Thread C:k=10
Exit from C
// 10).Program to create a text file using Files©ing chars from one File into
another file.
import java.io.*;
importjava.lang.Exception;
importjava.io.DataInputStream;
class Files
{
public static void main(String args[])
{
File inFile = new File("input.dat");
File outFile = new File ("output.dat");
FileReaderins = null;
FileWriter outs= null;
try
{
ins = new FileReader(inFile);
outs=new FileWriter(outFile);
intch;
while((ch=ins.read( ))!=-1)
{
outs.write(ch);
}
}
catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}
finally
{
try
{
ins.close();
outs.close();
}
catch(IOException e){ }
}
}
}//class
// 11).Program to display a Simple Applet.
importjava.awt.*;
importjava.applet.*;.
public class Myapp extends Applet
{
public void init( )
{
setBackground(Color.yellow);
}
public void paint(Graphics g)
{
g.drawString("Hello Applets!!",50,100);
}
}
Myapp.html
<html>
<applet code ="Myapp.class" height=300 width=400>
</applet>
</html>
OUT PUT:-
D:\javapg>javac Myapp.java
D:\javapg>appletviewer Myapp.html