100% found this document useful (2 votes)
236 views

Class 12 Practical File

This document contains 25 C++ programs with explanations and sample outputs. The programs cover basic concepts like input/output, arithmetic operations, conditional statements, loops and functions. Example programs calculate the sum of two numbers, area of shapes, temperature conversion, and more. Each program listing is followed by sample input values and the corresponding output.

Uploaded by

Karan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
236 views

Class 12 Practical File

This document contains 25 C++ programs with explanations and sample outputs. The programs cover basic concepts like input/output, arithmetic operations, conditional statements, loops and functions. Example programs calculate the sum of two numbers, area of shapes, temperature conversion, and more. Each program listing is followed by sample input values and the corresponding output.

Uploaded by

Karan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 83

COMPUTER

SCIENCE
PRACTICAL FILE
CLASS 11 & 12
Karan Raghuvanshi
Class XII-A
Roll no: 9181688
CLASS
11

2
INDEX
Q1. Program to print WELCOME IN C++.

Q2. Program to find sum of two numbers.

Q3. Program to find square of a number.

Q4. Program to check whether a number is


greater than or less than other number.

Q5. Program to calculate percentage marks for


three subjects.

Q6. Program that reads temperature in Celsius


and displays it in Fahrenheit.

Q7. Program that reads radius of a circle and


prints its area.

Q8. Program that enter value of x from user


and prints Y = 2x and Z = 2x -1.

Q9. Program to convert a given number of days


into years, weeks and days.

Q10. Program for swapping of two numbers


using third number.

Q11. Program for swapping of two numbers


without using third variable.

3
Q12. Program to input three numbers and print
the largest of three.
Q13. Program to check whether the entered
number is odd or even.

Q14. Program to find Simple Interest and


Compound Interest.

Q15. Program to find area of a triangle.

Q16. Program that seeds the name of user and


number of unit and displays the electricity
charge with name.
The electricity board charges according to
following data:

For first 100 units = 40p/unit


For next 200 units = 50p/unit
Beyond 300 units = 60p/unit
All the users are charged motor charge also
which is Rs 50.
Q17. Program to enter marks in five subjects
and calculate percentage. Display grade
according to following specifications.

Percentage Grade
>90 A
<= 90 and>81 B
<= 80 and>71 C
<= 70 and>61 D
<= 60 and>51 E
<= 50 and>41 F
<=40 FAIL

4
Q18. Program that accepts a character
between A and J and prints next 4 characters.

Q19. Program to input a student type (A or


B), and for A initialize the collage account
with Rs 200 otherwise initialize the hostel
account with Rs 200.

Q20. Program that print area for choice 1 and


perimeter for choice 2 of a circle

Q21. Program to find value of P if P= (w + x)/


(y-z). The value of w, x, y, z is entered by user.

Q22. Program which raise any number x to a


positive power n.

Q23. Program to calculate commission for the


salesmen the commission is calculated
according to following rates.

Sales Commission rate


30001 onwards 15%
22000 30000 10%
12001 22000 7%
5001 12000 3%
0 5000 0%

Q24. Program to print whether the entered


character is an uppercase or a lowercase
character or a digit or any other character. The
ASCII codes are given below.

Characters ASCII Range


5
0 9 48 57
A Z 65 90
a z 97 - 122

25. Program to print table of any number.

C++ PROGRAMS
Q1. Program to print WELCOME IN C++.

#include<iostream.h>
void main()
{
cout <<"Welcome in C++ Programming";
}

OUTPUT
Welcome in C++ Programming.

6
Q2. Program to find sum of two numbers.

#include<iostream.h>
void main()
{
int num1,num2,sum;
cout<<"Enter Number 1: ";
cin>>num1;
cout<<"Enter Number 2: ";
cin>>num2;
sum=num1+num2;
cout<<"Sum of numbers: "<<sum;
}

OUTPUT
Enter Number 1: 5
Enter Number 2: 6
Sum of numbers: 11

7
Q3. Program to find square of a number.

#include<iostream.h>
void main()
{
int num1,square;
cout<<"Enter number: ";
cin>>num1;
square=num1*num1;
cout<<"Square of number is: "<<square;
}

OUTPUT
Enter number: 5
Square of number is: 25

8
Q4. Program to check whether a number is
greater than or less than other number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2;
cout<<"Enter value for num 1: ;
cin>>num1;
cout<<"Enter value for num 2: ";
cin>>num2;
if(num1>num2)
cout<<"Num1 is greater than num 2";
else
cout<<"Num1 is smaller than num 2";
}
OUTPUT
Enter value for num 1: 5
Enter value for num 2: 10
Num1 is smaller than num 2

9
Q5. Program to calculate percentage marks for
three subjects.

#include<iostream.h>
void main()
{
float English, Maths, Science, Sum, Percentage;
cout<<"Enter marks of Subject ENGLISH, MATHS & SCIENCE: ";
cin>>English>>Maths>>Science;
Sum=English+Maths+Science;
Percentage=Sum*100/300;
cout<<"Percentage = "<<Percentage;
}

OUTPUT
Enter marks of Subject ENGLISH, MATHS & SCIENCE:
90
98
95
Percentage = 94.33333333

10
Q6. Program that reads temperature in Celsius
and displays it in Fahrenheit.

#include<iostream.h>
void main()
{
int Celsius, Fahrenheit;
cout<<"Enter temperature in degree CELCIUS: ";
cin>>Celsius;
Fahrenheit=9*Celsius/5+32;
cout<<"Temperature in degree FAHRENHEIT: "<<Fahrenheit;
}

OUTPUT
Enter temperature in degree CELCIUS: 40
Temperature in degree FAHRENHEIT: 104

11
Q7. Program that reads radius of a circle and
prints its area.

#include<iostream.h>
void main()
{
float radius,area;
cout<<"Enter Radius of circle: ";
cin>>radius;
area=radius*3.14*radius;
cout<<"Area of Circle:"<<area;
}

OUTPUT
Enter Radius of circle: 14
Area of Circle: 615.44

12
Q8. Program that enter value of x from user
and prints Y = 2x and Z = 2x -1.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z;
cout<<"Enter x: ";
cin>>x;
y=2*x;
z=2*x-1;
cout<<" Y = "<<y<<"\n"<<" Z = "<<z;
}

OUTPUT
Enter x: 5
Y = 10
Z= 9

Q9. Program to convert a given number of days


into years, weeks and days.

13
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int totaldays,years,weeks,days,rem1,rem2;
cout<<"Enter total number of days : ";
cin>>totaldays;
years=totaldays/365;
rem1=totaldays%365;
weeks=rem1/7;
rem2=rem1%7;
days=rem2;
cout<<"Years = "<<years<<"\tweeks = "<<weeks<<"\tdays = " <<days;
}

OUTPUT
Enter total number of days: 956
Years = 2 weeks = 32 days = 2

Q10. Program for swapping of two numbers


using third number.

#include<iostream.h>

14
#include<conio.h>
void main()
{
clrscr();
int a, b, c;
cout<<"Enter a : ";
cin>>a;
cout<<"Enter b : ";
cin>>b;
c=a;
a=b;
b=c;
cout<<"\na= "<<a<<"\n"<<"b= "<<b;
}

OUTPUT
Enter a: 5
Enter b: 7
a= 7
b= 5

Q11. Program for swapping of two numbers


without using third variable.
#include<iostream.h>
#include<conio.h>
void main()

15
{
clrscr();
int a,b;
cout<<"Enter a : ";
cin>>a;
cout<<"Enter b : ";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"Values after swapping: "<<"\na= "<<a<<"\nb= "<<b;
}
OUTPUT
Enter a: 4
Enter b: 9
Values after swapping:
a= 9
b= 4

Q12. Program to input three numbers and print


the largest of three.
#include<iostream.h>
void main()
{
int x,y,z;
cout<<"Enter three variables : "<<"\n";
cin>>x>>y>>z;

16
if(x>y&&x>z)
cout<<"\n"<<x<<" is greater";
if(y>x&&y>z)
cout<<"\n"<<y<<" is greater";
if(z>x&&z>y)
cout<<"\n"<<z<<" is greater";
}

OUTPUT
Enter three variables:
5
9
3
9 is greatest

Q13. Program to check whether the entered


number is odd or even.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter any number: ;
cin>>num;

17
if(num%2==0)
cout<<"Number is even";
else
cout<<"Number is odd";
}

OUTPUT
Enter any number: 5
Number is odd

Enter any number: 18


Number is even

Q14. Program to find Simple Interest and


Compound Interest.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int P,R,T;
float SI,CI;
cout<<"Enter values for P R T"<<"\n";
cin>>P>>R>>T;

18
SI=P*R*T/100;
CI=P*(1+R/100)^T;
cout<<"Simple interest= "<<SI<<"\n"<<"Compound Interest= "<<CI;
}
OUTPUT
Enter values for P R T
1000
5
3
Simple interest = 150
Compound Interest = 1003

Q15. Program to find area of a triangle.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int b, h, area;
cout<<"Enter values of Base and Height of triangle"<<"\n";
cin>>b>>h;
area=b*h/2;
cout<<"Area of triangle: "<<area;
}

19
OUTPUT
Enter values of Base and Height of triangle
5
4
Area of triangle: 10

Q16. Program that seeds the name of user and


number of unit and displays the electricity
charge with name.

The electricity board charges according to


following data:

For first 100 units = 40p/unit


For next 200 units = 50p/unit
Beyond 300 units = 60p/unit

20
All the users are charged motor charge also
which is Rs 50.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[25];
int unit, charge;
cout<<"Enter your name: ";
gets(name);
cout<<"Enter total units: "<<\n;
cin>>unit;
if(unit<=100)
charge=100*40/100+50;
if(unit>100&&unit<=300)
charge=100*40/100+(unit-100)*50/100+50;
if(unit>300)
charge=100*40/100+200*50/100+(unit-300)*60/100+50;
puts(name);
cout<<"Total Electricity Charge: "<<charge;
}
OUTPUT
Enter your name: Alok Kumar
Enter total units: 285

Alok Kumar
Total Electricity Charge: 182
Q17. Program to enter marks in five subjects
and calculate percentage. Display grade
according to following specifications.

Percentage Grade
>90 A
<= 90 and>81 B
<= 80 and>71 C

21
<= 70 and>61 D
<= 60 and>51 E
<= 50 and>41 F
<=40 FAIL

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[67], grade;
int eng, maths, cs, phy, chem, sum, percentage;
cout<<"Enter your name: ";
gets(name);
cout<<"Enter marks in 5 subjects"<<"\n";
cin>>eng>>maths>>cs>>phy>>chem;
sum=eng+maths+cs+phy+chem;
percentage=sum/5;
cout<<"\n"<<"\n";
puts(name);
cout<<"Percentage: "<< percentage <<'%';
if(percentage >90)
grade='A';
if(percentage <=90&& percentage >81)
grade='B';
if(percentage <=80&& percentage >71)
grade='C';
if(percentage <=70&& percentage >61)
grade='D';
if(percentage <=60&& percentage >51)
grade='E';
if(percentage <=50&& percentage >41)
grade='F';
if(percentage <=40)
grade='G';
cout<<"\n"<<"Grade: "<<grade;
}

OUTPUT
Enter your name: Alok Kumar
Enter marks in 5 subjects
22
93
97
95
90
99

Alok Kumar
Percentage: 94%
Grade: A

Q18. Program that accepts a character


between A and J and prints next 4 characters.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"Enter character between A to J: ";
cin>>ch;
int num =ch;
cout<<char(num+1);

23
cout<<" "<<char(num+2);
cout<<" "<<char(num+3);
cout<<" "<<char(num+4);
}

OUTPUT
Enter character between A to J: F
GHIJ

Q19. Program to input a student type (A or


B), and for A initialize the collage account
with Rs 200 otherwise initialize the hostel
account with Rs 200.

#include<iostream.h>
void main()
{
char stu_type;
int coll_acc,hostel_acc;
cout<<"Enter student type: ";
cin>>stu_type;
if(stu_type=='A')
coll_acc=200;
cout<<\nCollage account:<<coll_acc;
if(stu_type=='B')

24
hostel_acc=200;
cout<<\nHostel account:<<hostel_acc;
}

OUTPUT
Enter student type: A
Collage account: 200

Q20. Program that print area for choice 1 and


perimeter for choice 2 of a circle

#include<iostream.h>
void main()
{
char choice;
int radius, area, peri;
cout<<"Enter radius of circle: ";
cin>>radius;
cout<<"Enter 1 for area or 2 for perimeter: ";
cin>>choice;
area=22*radius*radius/7;
peri=2*22*radius/7;
if(choice=='1')
cout<<"Area: "<<area;
else
cout<<"Perimeter: "<<peri;

25
}

OUTPUT
Enter radius of circle: 7
Enter 1 for area or 2 for perimeter: 1
Area: 154

Q21. Program to find value of P if P= (w + x)/


(y-z). The value of w, x, y, z are entered by
user.

#include<iostream.h>
void main()
{
float w, x, y, z, P;
cout<<"Enter numbers w, x, y, z";
cin>>w>>x>>y>>z;
P= (w + x)/(y-z);
cout<<"\nP = (w + x)/(y-z) = "<<P;
}

OUTPUT
Enter numbers w, x, y, z
5
26
7
6
3

P = (w + x)/(y-z) = 4

Q22. Program which raise any number x to a


positive power n.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
long double x,n,p;
cout<<"Enter any number x: ";
cin>>x;
cout<<"Enter value of n: ";
cin>>n;
p=pow(x,n);
cout<<"x raised to the power n is: "<<p;
}

OUTPUT

27
Enter any number x: 5
Enter value of n: 4
x raised to the power n is: 625

Q23. Program to calculate commission for the


salesmen the commission is calculated
according to following rates.

Sales Commission rate


30001 onwards 15%
22000 30000 10%
12001 22000 7%
5001 12000 3%
0 5000 0%

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float sales;
cout<<"Enter sales made by salesman: ";
cin>>sales;

28
if(sales>5000)
if(sales<12000)
if(sales<22000)
if(sales<30000)
cout<<"Commission = "<<sales*0.15;
else
cout<<"Commission = "<<sales*0.10;
else
cout<<"Commission = "<<sales*0.07;
else
cout<<"Commission = "<<sales*0.03;
else
cout<<"Commission = "<<sales*0;
}

OUTPUT
Enter sales made by salesman: 36548
Commission = 5482.2
Q24. Program to print whether the entered
character is an uppercase or a lowercase
character or a digit or any other character. The
ASCII codes are given below.

Characters ASCII Range


0 9 48 57
A Z 65 90
a z 97 - 122

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int num;
cout<<"Enter any Character: ";
cin>>ch;
num=ch;
if(num>=48)
if(num>=65)

29
if(num>=97&&num<123)
cout<<"\nThe entered character is a lower case alphabet";
else
cout<<"\nThe entered character is a upper case alphabet";
else
cout<<"\nThe entered character is a digit";
else
cout<<"\nThe entered character is any other character";
}

OUTPUT
Enter any Character: j
The entered character is a lower case alphabet

Q25. Program to print table of any number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<"\nEnter any number :";
cin>>n;
for(i=1;i<=10;i++)
{
cout<<"\t";
cout<<n*i;
}
}

OUTPUT
Enter any number: 2
2 4 6 8 10 12 14 16 18 20

30
CLASS
12

31
INDEX
Q1. C++ PROGRAMS.
Q1.1. Enter name, age and salary and
display it by using outside the class
member.

Q1.2. Program which gives the squares of


all the odd numbers stored in an array.

Q1.3. Program to store numbers in an array


and allow user to enter any number which
he/she wants to find in array. (Binary
Search)

Q1.4. Program for copy constructor.

Q1.5. Enter numbers in an array in any


order and it will give the output of
numbers in ascending order.

Q1.6. Enter any number from 2 digits to 5


digits and the output will be the sum of all
the distinguish digits of the numbers.

Q1.7. Enter rows and columns and the


output will be the sum of the diagonals.

Q1.8. Enter item no., price and quantity in


a class.

Q1.9. Enter any line or character in a file


and press * to exit the program.
32
Q1.10. Program will read the words which
starts from vowels stored in a file.

Q1.11. Enter employee no, name, age,


salary and store it in a file.

Q1.12. Deleting the information of


employee by entering the employee
number.

Q1.13. Enter any number and its power and


the output will the power of the number.

Q1.14. Enter 3 strings and the output will


show the longest string and the shortest
string.

Q1.15 Enter any number and the output


will be all the prime numbers up to that
number.

Q1.16. Selection sort

Q1.17. Using loop concept, the program


will give the output of numbers making a
right triangle.

Q1.18. Program for Stacks and Queue.

Q1.19Enter two arrays and the output will


be the merge of two arrays.

Q1.20. Sequence sort.

33
Q2.SQL QUERIES AND OUTPUT.

C++ PROGRAMS
Q1.1. Enter name, age and salary and display it
by using outside the class member.
#include<iostream.h>
#include<conio.h>
class abc
{
//private:
char n[20];
int a;
float sal;
public:
void getdata(void)
{
cout<<"\nEnter name ";
cin>>n;
cout<<"\nEnter age ";
cin>>a;
cout<<"\nEnter salary ";
cin>>sal;
}
void putdata(void)
{
cout<<"\n\nNAME IS "<<n;
cout<<"\nAGE IS "<<a;
cout<<"\nSALARY IS "<<sal;
}
};
main()
{
abc p;
clrscr();
p.getdata();
p.putdata();
getch();
34
}

OUTPUT

35
Q1.2. Program which gives the squares of all
the odd numbers stored in an array.

#include<iostream.h>
#include<conio.h>
main()
{
int a[20],i,x;
clrscr();
cout<<"\nEnter no of elements ";
cin>>x;
for(i=0;i<x;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\nArr1: ";
for(i=0;i<x;i++)
cout<<" "<<a[i];

cout<<"\n\nArr1: ";
for(i=0;i<x;i++)
{
if(a[i]%2==1)
a[i]=a[i]*a[i];
cout<<" "<<a[i];
}

getch();
}

OUTPUT
36
37
Q1.3. Program to store numbers in an array
and allow user to enter any number which
he/she wants to find in array. (Binary Search)

#include<iostream.h>
#include<conio.h>
main()
{
int a[20],n,i,no,low=0,high,mid,f=0;
clrscr();
cout<<"\nEnter no of elements ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

cout<<"\nEnter no to be search ";


cin>>no;

high=n-1;
while(low<=high && f==0)
{
mid=(low+high)/2;

if(a[mid]==no)
{
cout<<"\n\n"<<no<<" store at "<<mid+1;
f=1;
break;
}
else if(no>a[mid])
low=mid+1;
else
high=mid-1;
}
if(f==0)
cout<<"\n\n"<<no<<" does not exist";
getch();
}
OUTPUT
38
Q1.4. Program for copy constructor.

39
#include<iostream.h>
#include<conio.h>
#include<string.h>
class data
{
char n[20];
int age;
float sal;
public:
data(){}

data(char x[],int a,float k)


{
strcpy(n,x);
age=a;
sal=k;
}

data(data &p)
{
strcpy(n,p.n);
age=p.age;//+20;
sal=p.sal;//+1000;
}

display()
{
cout<<"\n\nNAME : "<<n;
cout<<"\n\nAGE : "<<age;
cout<<"\n\nSalary: "<<sal;

}
};
main()
{
clrscr();
data d("ajay",44,5000); //implicit caling
data d1(d);
data d2=d;
data d3;
d3=d2;
d.display();
d1.display();
d2.display();
d3.display();
getch();
}
40
OUTPUT

Q1.5. Enter numbers in an array in any order


and it will give the output of numbers in
ascending order. (Bubble Sort)

#include<iostream.h>

41
#include<conio.h>
void main()
{
int a[20],n,i,j,t;
clrscr();
cout<<"\nEnter no of elements in A ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}

cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
getch();
}

OUTPUT

42
Q1.6. Enter any number from 2 digits to 5
digits and the output will be the sum of all the
distinguish digits of the numbers.
#include<iostream.h>
#include<conio.h>
main()
{

43
int a=0,b=0,c;
clrscr();
cout<<"\nEnter value for A ";
cin>>a;
while(a>0) // for(;a>0;) or for(i=a;i>0;i=i/10)
{
c=a%10;
b=b+c;
a=a/10;
}
cout<<"\nSum of digits "<<b;

getch();
}
OUTPUT

Q1.7. Enter rows and columns and the output


will be the sum of the diagonals.

#include<iostream.h>
#include<conio.h>
main()
{
int a[10][10],i,j,r,c;
clrscr();

44
cout<<"\nEnter no of rows ";
cin>>r;
cout<<"\nEnter no of Col ";
cin>>c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
cout<<"\nEnter no ";
cin>>a[i][j];
}

int sum=0,sum1=0;
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<" "<<a[i][j];
/// if(i==j) //&& a[i][j]>0)
// cout<<a[i][j];
// else
// cout<<" ";
//sum=sum+a[i][j];
/* if(i+j==r-1)// && a[i][j]>0)
cout<<a[i][j];
else
cout<<" "; */
if(i==j)
sum1=sum1+a[i][j];
}
}

for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
// cout<<" "<<a[i][j];
/* if(i==j) //&& a[i][j]>0)
cout<<a[i][j];
else
cout<<" ";*/
//sum=sum+a[i][j];
if(i+j==r-1)// && a[i][j]>0)
// cout<<a[i][j];
// else
// cout<<" ";
45
sum=sum+a[i][j];
}
}

cout<<"\n\nFirst diagonal sum "<<sum1;


cout<<"\n\nSecond diagonal sum "<<sum;

getch();
}

OUTPUT

Q1.8. Enter item no., price and quantity in a


class.
#include<iostream.h>
#include<conio.h>
class dd
{
char item[20];
int pr,qty;
public:

46
void getdata();
void putdata();
};
void dd::getdata()
{
cout<<"\nEnter item name ";
cin>>item;
cout<<"\nEnter price ";
cin>>pr;
cout<<"\nEnter quantity ";
cin>>qty;
}
void dd::putdata()
{
cout<<"\n\nItem name:"<<item;
cout<<"\n\nPrice: "<<pr;
cout<<"\n\nQty:"<<qty;
}

main()
{
dd a1,o1,k1;
clrscr();
a1.getdata();
o1.getdata();
k1.getdata();

a1.putdata();
o1.putdata();
k1.putdata();
getch();
}

OUTPUT

47
Q1.9. Enter any line or character in a file and
press * to exit the program.

#include<iostream.h>
#include<fstream.h>

48
#include<conio.h>
main()
{
char ch;
clrscr();
ofstream f1("emp"); //implicit
//ofstream f1;
//f1.open("emp",ios::out); //explicit
cout<<"\nEnter char ";
while(1) //infinity
{
ch=getche(); // to input a single charactor by keybord.
if(ch=='*')
break;
if(ch==13)
{
cout<<"\n";
f1<<'\n';
}
f1<<ch;
}
f1.close();
//getch();
}

OUTPUT

49
Q1.10. Program will read the words which
starts from vowels stored in a file.

#include<iostream.h>

50
#include<fstream.h>
#include<conio.h>
main()
{
char ch[80];
int cnt=0;
clrscr();
ifstream f1("emp");
ofstream f2("temp");
while(!f1.eof())
{
f1>>ch;
cout<<ch<<"\n";
if(ch[0]=='a' || ch[0]=='e' || ch[0]=='o' || ch[0]=='i' || ch[0]=='u')
f2<<"\n"<<ch;
}
f1.close();
f2.close();

cout<<"\n\nName start with vowels \n\n";


f1.open("temp",ios::in);
while(f1) //while(!f1.eof())
{
f1>>ch;
cout<<"\n"<<ch;
if(f1.eof())
break;

}
f1.close();

getch();
}

OUTPUT

51
Q1.11. Enter employee no, name, age, salary
and store it in a file.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
int row=5,col=2;
52
class abc
{
private:
int empno;
char n[20];
int age;
float sal;
public:
void getdata()
{
char ch;
cin.get(ch); // To empty buffer
cout<<"\nEnter employee no ";
cin>>empno;
cout<<"\nEnter name ";
cin>>n;
cout<<"\nEnter age ";
cin>>age;
cout<<"\nEnter salary ";
cin>>sal;
}
void putdata()
{
// gotoxy(col,row);
cout<<"\n"<<empno;
// gotoxy(col+10,row);
cout<<"\t"<<n;
// gotoxy(col+25,row);
cout<<"\t"<<age;
// gotoxy(col+35,row);
cout<<"\t"<<sal;
// row++;
}
};
main()
{
clrscr();
abc p,p1;
fstream f1("emp5.txt",ios::in|ios::out|ios::binary);
int i;
for(i=0;i<3;i++)
{

p.getdata();
f1.write((char *)&p,sizeof(p));
}
cout<<"\n\nEMPNO\tNAME\tAGE\tSALARY\n";
53
f1.seekg(0);
//f1.clear();
//clrscr();
for(i=0;i<3;i++)
{

f1.read((char*)&p1,sizeof(p1));
p1.putdata();
}
f1.close();
getch();
}

OUTPUT

54
Q1.12. Deleting the information of employee by
entering the employee number.

#include<iostream.h>
#include<conio.h>

55
#include<fstream.h>
#include<stdio.h>
class abc
{
private:
int empno;
char n[20];
int age;
float sal;
public:
void getdata()
{
cout<<"\nEnter employee no ";
cin>>empno;
cout<<"\nEnter name ";
cin>>n;
cout<<"\nEnter age ";
cin>>age;
cout<<"\nEnter salary ";
cin>>sal;
}
void putdata()
{
cout<<"\n\n"<<empno<<"\t"<<n<<"\t"<<age<<"\t"<<sal;
}
int get_empno()
{
return empno;
}
};
main()
{
abc p,p1;
clrscr();
ifstream f1("emp5.txt",ios::in|ios::binary);
ofstream f2("temp",ios::out|ios::binary);

int i,emp;
cout<<"\n\nNAME\tAGE\tSALARY";

f1.clear();
f1.seekg(0);
//for(i=0;i<4;i++)
while(!f1.eof())
{
f1.read((char*)&p1,sizeof(p1));
if(f1.eof())
56
break;
p1.putdata();
}

cout<<"\nEnter employee no ";


cin>>emp;
f1.clear();
f1.seekg(0);

//while(f1)
while(!f1.eof())
{
f1.read((char*)&p1,sizeof(p1));
if(f1.eof())
break;
if(emp!=p1.get_empno())
f2.write((char*)&p1,sizeof(p1));
}
f1.close();
f2.close();
remove("emp5.txt");
rename("temp","emp5.txt");

f1.open("emp5.txt",ios::in|ios::binary);
cout<<"\n\nNAME\tAGE\tSALARY";
f1.seekg(0);
//for(i=0;i<3;i++)
while(!f1.eof())
{
f1.read((char*)&p1,sizeof(p1));
if(f1.eof())
break;
p1.putdata();
}
f1.close();
getch();
}

OUTPUT

57
Q1.13. Enter any number and its power and the
output will the power of the number.

#include<iostream.h>

58
#include<conio.h>
#include<math.h>
#define CUBE(a,b) pow(a,b)
//a>b?a:b
main()
{
int x,y,z;
clrscr();
cout<<"\nEnter base value ";
cin>>x;
cout<<"\nEnter power ";
cin>>y;

z=CUBE(x,y);
cout<<"\n"<<z;
getch();
}

OUTPUT

Q1.14. Enter 3 strings and the output will show


the longest string and the shortest string.

#include<iostream.h>
#include<conio.h>

59
#include<string.h>
main()
{
char n[20],n1[20],n2[20];
int l1,l2,l3;
clrscr();
cout<<"\nEnter String1 ";
cin>>n;
cout<<"\nEnter String2 ";
cin>>n1;
cout<<"\nEnter String3 ";
cin>>n2;
l1=strlen(n);
l2=strlen(n1);
l3=strlen(n2);
(l1>l2 && l1>l3) ?cout<<"\nLong: "<<n:(l2>l1 && l2>l3) ? cout<<"\nLong: "<<n1
:cout<<"\nLong: "<<n2;
(l1<l2 && l1<l3)?cout<<"\nshort:"<<n:(l2<l1 && l2<l3)?
cout<<"\nshort:"<<n1:cout<<"\nshort:"<<n2;

getch();
}

OUTPUT

60
Q1.15 Enter any number and the output will be
all the prime numbers up to that number.

#include<iostream.h>
#include<conio.h>
main()
{

61
int n,i,j,p;
clrscr();
cout<<"\nEnter no of N ";
cin>>n;
for(i=1;i<=n;i++)
{
p=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
p=1;

if(p==0)
cout<<" "<<i;
}
getch();
}

OUTPUT

Q1.16. Selection sort.


#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,t,min,p;
clrscr();

62
cout<<"\nEnter no of elements in A ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

for(i=0;i<n;i++)
{
min=a[i];
p=i;
for(j=i;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
p=j;
}
}
t=a[i];
a[i]=a[p];
a[p]=t;
}

cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
getch();
}

OUTPUT

63
Q1.17. Using loop concept, the program will
give the output of numbers making a right
triangle.

#include<iostream.h>
#include<conio.h>

64
main()
{
int i,j;
clrscr();
for(i=1;i<=4;i++)
{
cout<<"\n\n";
for(j=1;j<=i;j++)
cout<<" "<<i;
}
getch();
}

OUTPUT

Q1.18. Program for Stacks and Queue.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct queue

65
{
int x;
queue *next;
}*front=NULL,*rear;

void insert(int);
void delnode();
void display();

void main()
{
int a,ch;
clrscr();
do
{
cout<<"\nEnter 1 for Insert";
cout<<"\nEnter 2 for Delete";
cout<<"\nEnter 3 for display";
cout<<"\nEnter 4 for exit";
cout<<"\n\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter no for insert ";
cin>>a;
insert(a);
break;
case 2: delnode();
break;
case 3: display();
break;
case 4: exit(0);
}
}
while(1);
getch();
}
void insert(int no) // char str[]
{
queue *ptr;
ptr=new queue; // strcpy(ptr->n,str)
// ptr=(struct queue*)malloc(sizeof(struct queue));
ptr->x=no;
ptr->next=NULL;
if(front==NULL)
{
front=ptr;
66
rear=ptr;
}
else
{
rear->next=ptr;
rear=ptr;
}
}
void delnode()
{
int p;
queue *ptr;
if(front==NULL)
{
cout<<"\n\nQueue is Empty";
return;
}
p=front->x; //strcpy(p,front->n)
ptr=front;
front=front->next;
delete ptr;
cout<<"\n\ndeleted element "<<p<<"\n";
}

void display()
{
queue *ptr;
cout<<"\nQueue now:- \n";
for(ptr=front;ptr!=NULL;ptr=ptr->next)
cout<<" "<<ptr->x;
}

OUTPUT

67
68
Q1.19Enter two arrays and the output will be
the merge of two arrays.
69
# include <iostream.h>
# include <conio.h>
main()
{
int arr1[100],arr2[100],arr3[100],c,i=0,j=0,k=0,size1,size2;
clrscr();
cout<<"\n enter no of element of arr1 ";
cin>>size1;
for (i=0;i<size1;i++)
{
cout<<"\n enter no. ";
cin>>arr1[i];
}
cout<<"\n enter no of element of arr2 ";
cin>>size2;
for (i=0;i<size2;i++)
{
cout<<"\n enter no. ";
cin>>arr2[i];
}
cout<<"\n arr1 ";
for (i=0;i<size1;i++)
cout<<" "<<arr1[i];

cout<<"\n arr2 ";


for (i=0;i<size2;i++)
cout<<" "<<arr2[i];

i=0;j=0;k=0;
while (i<size1 && j<size2)
{
if (arr1[i]<arr2[j])
arr3[k++]=arr1[i++];

else if (arr2[j]<arr1[i])
arr3[k++]=arr2[j++];

else
i++;
}
while (i<size1)
arr3[k++]=arr1[i++];

while (j<size2)
arr3[k++]=arr2[j++];

70
cout<<"\n arr3 ";

for (i=0;i<k;i++)
cout<<" "<<arr3[i];

getch();
}

OUTPUT

Q1.20. Sequence Sort (Insertion Sort)

#include<iostream.h>
#include<conio.h>

71
void main()
{
int a[20],n,i,j,t;
clrscr();
cout<<"\nEnter no of elements in A ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter no ";
cin>>a[i];
}
cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];

for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}

cout<<"\n\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
getch();
}

OUTPUT

72
My SQL
School homework

73
QUERIES
Q2.1. mysql> select ename, sal, dept_no from
empl where commission is null;

74
Q2.2. mysql> select ename, sal, dept_no from
empl where commission is not null;

75
Q2.3. mysql> select * from empl where sal
between 1000 and 2500;

Q2.4. mysql> select ename from empl where


ename like a%;

76
Q2.5. mysql> select concat (ename, dept_no)
as name dept. from empl;

Q2.6. mysql> select lower(ename) empl


name from empl;

77
Q2.7. mysql> select upper(ename) empl
name from empl;

78
Q2.8. mysql> select substr(job,1,5) from empl
where empno=8888 or empno=8900;

Q2.9. mysql> select job, instr (job,le) as le in


ename from empl;

79
Q2.10. mysql> select ename, length (ename)as
name length from empl where empno
<=8888;

OUTPUT QUESTIONS
80
Q2.11. mysql> select left (Informatics
practices,5);

Q2.12. mysql> select right (Informatics


practices,5);

81
Q2.13. mysql> select mid (Informatics
practices,6,9);

Q2.14. mysql> select ltrim ( akshit);

82
Q2.15. mysql> select rtrim (akshit );

83

You might also like