Professional Documents
Culture Documents
Date :
Registration No. :
__________________ __________________
C++ Revision Tour
Q- Write a program to find the LCM and GCD of two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,gcd=1;
for(int i=1;i<1000;++i)
{
if((x%i==0)&&(y%i==0))
gcd=i;
}
cout<<"\n\n\nGCD :"<<gcd;
cout<<"\n\n\nLCM :"<<(x*y)/gcd;
getch();
}
OUTPUT:
ENTER 1st NO : 12
GCD :1
LCM :156
Q- Write a program to find the sum of sequence
1 + 1/1! + 1/2!+ .........
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i;
float sum=1.00;
float fact(int a);
clrscr();
cout<<"Enter n:";
cin>>n;
for(i=1;i<n;i++)
{
sum+=(1/fact(i));
}
cout<<"Sum of series ="<<sum;
getch();
}
OUTPUT:
Enter n : 5
Sum of series =2.708333
Q- Write a function that takes time as three integer
arguments(hours,minutes,seconds),
and returns the number of seconds since the clock last struck "12". Use this
function to write a program to calculate the amount of time in seconds between
two times, both of which are within one 12 hour cycle of clock.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int seconds(int hh,int mm,int ss)
{
int z,s;
z=abs(hh-12);
s=(z*60*60)+(mm*60)+ss;
return s;
}
int main()
{
clrscr();
int h1,h2,m1,m2,s1,s2,t1,t2;
cout<<"Enter the first time:"<<endl;
cout<<"\tHours:";
cin>>h1;
cout<<"\tMinutes:";
cin>>m1;
cout<<"\tSeconds:";
cin>>s1;
t1=seconds(h1,m1,s1);
cout<<"Enter the second time:"<<endl;
cout<<"\tHours:";
cin>>h2;
cout<<"\tMinutes:";
cin>>m2;
cout<<"\tSeconds:";
cin>>s2;
t2=seconds(h2,m2,s2);
cout<<"The difference in time is:"<<abs(t1-t2);
return 0;
}
OUTPUT:
Enter the first time:
Hours:5
Minutes:45
Seconds:59
#include<iostream.h>
#include<conio.h>
#include<math.h>
void sum(int x,int n)
{
double sum=0,s=0;
int k=1;
long fac=1;
for(int i=1;i<=n-1;i++)
{
for(int j=(2*k);j>=1;j--)
{
fac*=j;
}
sum+=(pow(x,i)/fac);
}
s=1+sum;
cout<<"\nThe sum is:\n";
cout<<s;
}
void main()
{
clrscr();
int x,n;
cout<<"\nEnter the limiting value\n";
cin>>n;
cout<<"\nEnter the value \n";
cin>>x;
sum(x,n);
getch();
}
OUTPUT:
Q- Computers are playing an increasing role in education. Write aprogram that will
help elementary school students leaern multiplication. Use rand function to
produce
two positive one digit integers.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
clrscr();
int x[2],ans;
time_t t;
char c;
do
{
srand(time(&t));
x[0]=rand()%10;
srand(x[0]);
x[1]=rand()%10;
if(ans==(x[0]*x[1]));
{
cout<<"correct!\n\n\nDO YOU WANT TO CONTINUE?";
cin>>c;
}
if(c=='N'||c=='n')
{
break;}
}while(1);
}
OUTPUT:
What is 5times 1 ?
ANS: 5
correct!
What is 6times 7 ?
ANS: 48
#include<iostream.h>
#include<conio.h>
void main()
{
int a[3];
clrscr();
cout<<"Enter three digits :"<<endl;
cin>>a[0]>>a[1]>>a[2];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
if (i!=j&&i!=k&&j!=k)
cout<<endl<<endl<<a[i]<<a[j]<<a[k];
}
}
}
getch();
}
OUTPUT:
Enter three digits :
123
123
132
213
231
312
321
Q - Use one dimensional array to solve the following proble. Read 20 integers from
data file, each of which is between 10 and 100, inclusive. As each number is read,
print it only if it is not a duplicate of a number already erad. Provide for the
worst case in which all 20 integers are different. Use the smallest possible array
to solve this problem.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[20];
for (int i=0;i<20;i++)
{
cout<<"enter the element no "<<i+1<<endl;
cin>>arr[i];
}
for (i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
if (i!=j)
{
if (arr[i]==arr[j])
arr[j]=0;
else
continue;
}
else
continue;
}
}
for (i=0;i<20;i++)
{
if (arr[i]<=100&&arr[i]>=10)
cout<<arr[i]<<endl;
else
continue;
}
getch();
}
OUTPUT:
34
11
12
14
15
16
17
18
19
10
13
23
34
Q Write a c++ program to sum the sequence
x -(x^2/2!) + (X^4/4!) -(x^6/6!) + .......
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,p,i,j;
double fact=1.0,ans=0;
cout<<"Enter the value of x:";
cin>>x;
cout<<"Enter till what power you want:";
cin>>p;
ans=x;
for(i=2,j=1;i<=p;i++,j++){
fact=fact*i;
if(i%2==0)
ans+=(pow(-1,j))*((pow(x,i))/(fact));
}
cout<<"The sum of the series is:"<<ans;
return 0;
}
OUTPUT:
Enter the value of x:3
Enter till what power you want:4
The sum of the series is:-4.875
Q Write a c++ function having two value parameters U and n with result type float
to find the sum of the series given below
1 - (U^2) + 1/2!(U^2) -1/3!(U^3) + .............
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,p,i,j;
double fact=1.0,ans=1;
cout<<"Enter the value of x:";
cin>>x;
cout<<"Enter till what power you want:";
cin>>p;
for(i=1;i<=p;i++){
fact=fact*i;
ans+=(pow(-1,i))*((pow(x,i))/(fact));
}
cout<<"The sum of the series is:"<<ans;
return 0;
}
OUTPUT:
Enter the value of x:3
Enter till what power you want:4
The sum of the series is:1.375
Structures
Q- Write a program to accept information about a person and then display it .
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class person
{
char name[30];
long phone;
public:
person()
{
cout<<"\nConstructor initialised\n";
}
void getname()
{
cout<<"\nEnter the name\n";
gets(name);
cout<<"\nEnter the phone number\n";
cin>>phone;
}
void display()
{
cout<<"\nName\t";puts(name);
cout<<"\nPhone number\t"<<phone;
}
~person()
{
cout<<"\n******************\n";
}
};
class spouse:public person
{
char spousename[40];
public:
void getsp();
void display();
};
void spouse::getsp()
{
person::getname();
cout<<"\nEnter the spouse name\n";
gets(spousename);
}
void spouse::display()
{
person::display();
cout<<"Spouse name\t";puts(spousename);
}
void main()
{
clrscr();
spouse obj;
obj.getsp();
obj.display();
getch();
}
OUTPUT:
Constructor initialised
Name ARUN
******************
Q- Write a program to record a score of a cricket match. One array stores
information of
batting team such as batman's name,runs scored,etc. The other array stores
information
about bowling team. The progdram reads in above informatin and depending on
user's choice,
it displays either batting team's information or bowling team's information.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct bat
{
char name[20],modeout[70], indica;
int runs, score, totruns, totove, xtras;
};
struct bowl
{
char name[20];
int ttvrs,rnsgvn,wktstkn;
};
void main()
{
clrscr();
int plno;
int plytyp;
bat pl1[3];
bowl pl2[3];
switch (plytyp)
{
case 1:
cout<<"Enter the batsman number to see his details "<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Batsman number :"<<plno+1<<endl;
cout<<"Batsman name :";
puts(pl1[plno].name);
cout<<"Runs scored by the batsman :"<<pl1[plno].runs<<endl;
cout<<"Total overs played by the batsman :"<<pl1[plno].totove<<endl;
cout<<"Player status out "<<pl1[plno].indica<<endl;
break;
case 2:
cout<<"Enter the bowlers number to see his details "<<endl<<endl<<endl;
cin>>plno;
plno--;
cout<<"Bowlers name :";
puts(pl2[plno].name);
cout<<"Runs given by the player is :"<<pl2[plno].rnsgvn<<endl;
cout<<"Total overs played by the player :"<<pl2[plno].ttvrs<<endl;
cout<<"Total wickets taken by the user :"<<pl2[plno].wktstkn<<endl;
break;
default:
cout<<"Idiot enter a decent value"<<endl;
goto abc;
}
output:
2
Bowlers name :JOHNSON
Runs given by the player is :25
Total overs played by the player :4
Total wickets taken by the user :0
Do you wish to continue? Y-1 N-2
Y
Select between batsmen(1) or bowlers(2) to see their details
Enter the bowlers number to see his details
#include<iostream.h>
#include<conio.h>
void main()
{
double x,ans;
int n;
clrscr();
cout<<"enter the number"<<endl;
cin>>x;
cout<<"enter the raising value"<<endl;
cin>>n;
ans=power(x,n);
cout<<"The answer is :"<<ans<<endl;
cout<<"The square of the numberis :"<<power(x,2);
getch();
}
OUTPUT:
enter the number
3
enter the raising value
4
The answer is :81
The square of the numberis :9
Q- Write overloaded prototypes of inquote(), a function that displays its arguments
enclosed in double quotation marks. Write three versions: one for an int
argument, one for double argument and one for char argument.
#include<iostream.h>
#include<conio.h>
void inquote(int x);
void inquote(double y);
void inquote(char s);
void main()
{
clrscr();
int a,c;
double b;
char s;
cout<<"Enter the
choice"<<endl<<"1.number"<<endl<<"2.double"<<endl<<"3.char"<<endl;
cin>>c;
switch (c)
{
case 1:
cout<<"Enter the number"<<endl;
cin>>a;
inquote(a);
break;
case 2:
cout<<"enter the double value"<<endl;
cin>>b;
inquote(b);
break;
case 3:
cout<<"enter the character"<<endl;
cin>>s;
inquote(s);
break;
}
getch();
}
void inquote(int x)
{
cout<<"\""<<x<<"\""<<endl;
}
void inquote(double y)
{
cout<<"\""<<y<<"\""<<endl;
}
void inquote(char s)
{
cout<<"\""<<s<<"\""<<endl;
}
OUTPUT:
3
enter the character
T
"T"
Q- Write overloaded prototypes of themax(), a function that returns volumes of
different structures. Write three versions : one for cube's volume, one for
cylinder's volume and one for rectangular box's volume.
#include<iostream.h>
#include<conio.h>
double vol;
void volume(int r,int hc)
{ vol=(3.14*r*r*hc);
cout<<"\nVolume of cylinder is "<<vol;
}
void volume(int l,int b,int h)
{ vol=(l*b*h);
cout<<"\nVolume of box is "<<vol;
}
void volume(int s)
{ vol=(s*s*s);
cout<<"\nVolume of cube is "<<vol;
}
void main()
{
clrscr();
int s,r,l,b,h,hc;
cout<<"\nEnter radius and height of cylinder ";
cin>>r>>hc;
cout<<"\nEnter l,b,h of box ";
cin>>l>>b>>h;
cout<<"\nEnter side of cube ";
cin>>s;
volume(r,hc);
volume(l,b,h);
volume(s);
getch();
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class counter
{
int count;
public:
counter()
{
count=0;
}
void incount()
{
count++;
}
int givecount()
{
return (count);
}
}c1,c2,c3;
void main()
{
clrscr();
char guest[10];
int i,a,b,c;
for(i=0;i<10;i++)
{
cout<<"enter gntlmn(g),ladies(l),chldrn(c) "<<endl;
cin>>guest[i];
if(guest[i]=='g'||guest[i]=='G')
{
c1.incount();
a=c1.givecount();
}
else if(guest[i]=='l'||guest[i]=='L')
{
c2.incount();
b=c2.givecount();
}
else
{
c3.incount();
c=c3.givecount();
}
}
cout<<"GENTLEMEN :"<<a<<endl;
cout<<"LADIES :"<<b<<endl;
cout<<"CHILDREN :"<<c<<endl;
getch();
}
OUTPUT:
enter gntlmn(g),ladies(l),chldrn(c)
c
enter gntlmn(g),ladies(l),chldrn(c)
g
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
g
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
l
enter gntlmn(g),ladies(l),chldrn(c)
c
enter gntlmn(g),ladies(l),chldrn(c)
c
GENTLEMEN :2
LADIES :5
CHILDREN :3
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class taxpayer{
int pan;
char name[20];
long float tableinc;
double tax;
public:
void inputdata();
void display();
double computetax();
};
void taxpayer::inputdata()
{
cout<<"enterpersonal acct num:";
cin>>pan;
cout<<"\nenter the name of the person:";
gets(name);
cout<<"\nenter total annual taxable income:";
cin>>tableinc;
}
double taxpayer::computetax()
{
if(tableinc<=60000)
tax=0;
else if((tableinc>60000)||(tableinc<=150000))
tax= tableinc *0.05;
else if(tableinc>150000||tableinc<=500000)
tax=tableinc*0.1;
else
tax=tableinc*0.15;
return (tax);
}
void taxpayer::display()
{
cout<<"\npan num:"<<pan<<"\tname:";
puts(name);cout<<"\ttotal annual income:"<<tableinc<<"\tTax payable:"<<tax;
}
void main()
{
clrscr();
taxpayer a;
a.inputdata();
clrscr();
a.computetax();
a.display();
getch();
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
class just
{
int x;
public:
void enter()
{ cout<<"Enter a value of x:";
cin>>x;
}
void out()
{
cout<<"The value is:"<<x<<endl;
}
};
int main()
{
just j;
char ch='y';
int i=0;
while(ch=='y')
{
j.enter();
i++;
cout<<"Do you eant to continue:";
cin>>ch;
}
cout<<"The number of objects created are:"<<i;
return 0;
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
char name[20];
char class_st[8];
int marks[5];
float percentage;
float calculate();
public:
void readmarks();
void displaymarks();
};
float student::calculate()
{
percentage=0;
for(int i=0;i<5;i++)
percentage+=marks[i];
percentage=(percentage/5);
return percentage;
}
void student::readmarks()
{
cout<<"Enter the roll no.:";
cin>>roll_no;
cout<<"Enter the name:";
cin>>name;
cout<<"Enter the class studing in:";
cin>>class_st;
cout<<"Enter the marks:"<<endl;
for(int j=0;j<5;j++){
cout<<"\tEnter mark "<<j+1<<":";
cin>>marks[j];
}
}
void student::displaymarks()
{
cout<<"Roll no:"<<roll_no<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Class:"<<class_st<<endl;
cout<<"Percentage:"<<calculate()<<endl;
}
int main()
{
student s1;
s1.readmarks();
s1.displaymarks();
return 0;
}
OUTPUT:
Enter the roll no.:12
Enter the name:KARTHIK
Enter the class studing in:12
Enter the marks:
Enter mark 1:99
Enter mark 2:95
Enter mark 3:90
Enter mark 4:80
Enter mark 5:99
Roll no:12
Name:KARTHIK
Class:12
Percentage:92.599998
Q- Write a c++ program using class serial to store and display serial's title,
duration, number of episodes,serial code.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class serial
{
int code;
char title[20];
float duration;
int noe;
public:
serial()
{
duration=30;
noe=103;
}
void newserial();
void otherentries(int dur,int no);
void displaydata();
};
void serial::newserial()
{
cout<<"\n\nEnter the serial code:";
cin>>code;
cout<<"Enter the title:";
gets(title);
}
void serial::displaydata()
{
cout<<"\tSerial code is:"<<code<<endl;
cout<<"\tTitle is:"<<title<<endl;
cout<<"\tDurations is:"<<duration<<endl;
cout<<"\tNo. of episodes are:"<<noe<<endl<<endl;
}
int main()
{
clrscr();
char ch='y';
int i=0,dur,no;
serial s1[10];
while(ch=='y')
{
s1[i].newserial();
cout<<"Enter the duration and the no. of episodes:";
cin>>dur>>no;
s1[i].otherentries(dur,no);
i++;
cout<<"\n\nDo you want to continue:";
cin>>ch;
}
cout<<"\n\nThe details you have entered are:"<<endl<<endl;
for(int j=0;j<i;j++){
cout<<"Data of serial "<<j+1<<" is:"<<endl;
s1[j].displaydata();
}
return 0;
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<process.h>
class calculator
{
float result;
int o1,o2;
public:
void enter();
void showresult();
void add();
void sub();
void mul();
void div();
void clear();
};
void calculator::enter()
{
cout<<"Enter a operant:";
cin>>o1;
cout<<"Enter the other operant:";
cin>>o2;
}
void calculator::showresult()
{
cout<<"The result of the operation is:"<<result<<endl;
}
void calculator::add()
{
result=o2+o1;
}
void calculator::sub()
{
result=o1-o1;
}
void calculator::mul()
{
result=o1*o2;
}
void calculator::div()
{
result=o1/o2;
}
void calculator::clear()
{
result=0;
}
int main()
{
char ch='y';
calculator c1;
while(ch=='y')
{
c1.enter();
cout<<"Which operation do you want to perform:";
cin>>ch;
switch(ch)
{
case '+':c1.add();
break;
case '-':c1.sub();
break;
case '*':c1.mul();
break;
case '/':c1.div();
break;
default :cout<<"Wrong choice:";
exit(0);
}
c1.showresult();
c1.clear();
cout<<"Do you want to continue:";
cin>>ch;
}
return 0;
}
OUTPUT:
Enter a operand:4
Enter the other operand:8
Which operation do you want to perform:*
The result of the operation is:32
Do you want to continue:y
Enter an operand:4
Enter the other operand:8
Which operation do you want to perform:+
The result of the operation is:12
Do you want to continue:n
Q- Imagine a ticket selling booth at a fair. People passing by are requested to
purchase a ticket. A ticket is priced at Rs. 2.50. The booth keeps track of the
number of people that have visited the fair and of the total amount of money
collected. Model this ticketselling booth with a class tick. Include a program
to rest this class.
#include<iostream.h>
#include<conio.h>
#include<process.h>
class tick
{
int nop;
float total;
public:
tick()
{
nop=0;
total=0;
}
void inc();
void display();
void displaynop();
};
void tick::inc()
{
nop++;
total+=2.50;
cout<<"\nThe no. of people and the total have beem incremented";
}
void tick::display()
{
cout<<"\nThe number of people who have entered the fair are:"<<nop<<endl;
cout<<"The total amount collected till now is:"<<total<<endl;
}
void tick::displaynop()
{
cout<<"The no. of people who have visited so far are:"<<nop<<endl;
}
int main()
{
char ch='y';
int choice;
tick t1;
}
return 0;
}
OUTPUT:
1.Increment person and total
2.Display no. of people and the amount collected till now
3.Display no. of people who entered
4.Exit
Enter your choice:1
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class stock
{
char author[50];
char title[50];
char pub[50];
double price;
int numcopies;
public:
stock();
int access_title(char a[]);
void input();
void getdata(int);
};
stock::stock()
{
char author[50]={"abc"};
char title[50]={"efg"};
char pub[50]={"hij"};
price=500;
numcopies=50;
}
int stock::access_title(char a[])
{
if(strcmp(title,a))
return 0;
else return 1;
}
void stock::getdata(int num)
{
if(numcopies>=num)
cout<<"\nCost of "<<num<<" books is Rs. "<<(price*num);
else
cout<<"\nSorry! These many copies are unavailable!";
}
void stock::input()
{
cout<<"\nTitle: ";
gets(title);
cout<<"\nAuthor:";
gets(author);
cout<<"\nPublisher:";
gets(pub);
cout<<"\nPrices:";
cin>>price;
cout<<"\ncopies available:";
cin>>numcopies;
}
void main()
{
clrscr();
stock obj[2];
int n;
char ttle[50];
cout<<endl;
cout<<"\n Enter title of required book\n";
gets(ttle);
for(i=0;i<2;i++)
{
if(obj[i].access_title(ttle))
{
cout<<"\nHow many copies? ";
cin>>n;
obj[i].getdata(n);
}
else
cout<<"\nBook unavailable";
}
getch();
}
OUTPUT:
Author:Dan Brown
Publisher:Sun
Prices:455
copies available:300
Author:J K Rowling
Publisher:Bloomsbury
Prices:800
copies available:100
Book available
How many copies? 20
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
int main()
{
char str[]="Time is a great teacher but unfortunately it kills all its
pupils.Berlioz",ch;
int i;
ofstream fout;
fout.open("OUT.TXT",ios::out);
int len=strlen(str);
for(i=0;i<len;i++){
ch=str[i];
fout.put(ch);
}
cout<<"The string has been successfully written into the file";
return 0;
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
#include<stdio.h>
void main()
{ clrscr();
char str[50];
fstream file;
file.open("Story.txt",ios::in|ios::out|ios::trunc);
cout<<"\nEnter string\n";
gets(str);
int i;
for(i=0;str[i]!='\0';i++)
file.put(str[i]);
file.seekg(0,ios::beg);
int ctr=0;
for(i=0;str[i]!='\0';i++)
{ if(isalpha(str[i]))
ctr++;
}
cout<<"\nNumber of alphabets is\t"<<ctr;
getch();
}
OUTPUT:
Enter string
KARTHIKRAO88.RAO
Number of alphabets is 13
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class joke{
int joke_id;
char type[5];
char joke_desc[200];
public:
void newjoke()
{
cout<<"\nEnter joke id:";
cin>>joke_id;
cout<<"\nEnter joke type:(max 4 letters)";
gets(type);
cout<<"\nEnter the joke:\n";
gets(joke_desc);
}
void showjoke()
{
cout<<"\nJoke id:"<<joke_id;
cout<<"\nJoke type:";
puts(type);
cout<<"\nJoke:\n";
puts(joke_desc);
cout<<"HAHAHA";
}
};
joke jo;
void add_joke()
{
fstream file;
file.open("joke",ios::binary|ios::app|ios::in|ios::out);
int n,i;
cout<<"\nHow many jokes?";
cin>>n;
for(i=0;i<n;i++)
{ jo.newjoke();
file.write((char*)&jo,sizeof(jo));
}
file.close();
}
void main()
{
clrscr();
cout<<"HAHAHA";
add_joke();
jo.showjoke();
getch();
}
OUTPUT:
HAHAHA
How many jokes?2
Joke id:2
Joke type:HUMR
Joke:
QWERTYUIO
HAHAHA
POINTERS
Q- Suppose 7 names are stored in an array of pointers names[] as shown below.
Write a program to reverse the order of these names.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *name[]={"anand","naureen","banjot","wahid","sheena"};
int i,j;
cout<<"\nOriginal string\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
char *t;
for(i=0,j=4;i<5/2;i++,j--)
{
t=name[i];
name[i]=name[j];
name[j]=t;
}
cout<<"\nReversed string:\n";
for(i=0;i<5;i++)
cout<<name[i]<<endl;
getch();
}
OUTPUT:
Original string
anand
naureen
banjot
wahid
sheena
Reversed string:
sheena
wahid
banjot
naureen
anand
Q- Write a function to encode a string that is passed to it. The string should
get converted into an unrecognizable form.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int main()
{
char str[30];
cout<<"Enter an string:";
gets(str);
int len=strlen(str);
encode(str,len);
return 0;
}
OUTPUT:
Enter an string:KARTHIK
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
int main()
{
char str[20],ch,*f;
cout<<"Enter a string:";
gets(str);
cout<<"Enter the character to be searched for:";
cin>>ch;
len=strlen(str);
f=search(str,ch);
if(f!=NULL)
cout<<"Character is found.";
else
cout<<"Character is not found.";
return 0;
}
char *search(char *x,char d)
{
int i;
for(i=0;i<len;i++){
if(*x==d)
return x;
x++;
}
return NULL;
}
OUTPUT:
Enter a string:KARTHIK
Enter the character to be searched for:R
Character is found.
Arrays
Q- Write a c++ program to sort an array of numbers, display the greatest and
smallest numbers of the array, delete the array and shift the rest of the elements.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int temp,ctr,i,j,a[20],size,bg,mn,sum=0,even=0,ch;
cout<<"Enter the size of the array:";
cin>>size;
cout<<"Enter the array:";
for(i=0;i<size;i++)
cin>>a[i];
for(i=0;i<size;i++){
for(j=0;j<size-1-i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"The sorted array is:"<<endl;
for(i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
bg=a[0];
for(j=0;j<size;j++){
if(bg<a[j])
bg=a[j];
}
mn=a[0];
for(j=0;j<size;j++){
if(a[j]%2==0)
even++;
sum+=a[j];
if(mn>a[j])
mn=a[j];
}
cout<<"The maximum value is:"<<bg<<endl;
cout<<"The minimum value is:"<<mn<<endl;
cout<<"The sum of the array is:"<<sum<<endl;
cout<<"The no. of even values is:"<<even<<endl;
for(i=0;i<size;i++){
//ch=a[i];
for(j=i+1;j<size;j++){
if(a[i]==a[j])
a[j]=0;
}
}
cout<<"The array now is(after removing the duplicate elements):";
for(i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
for(i=0;i<size;i++){
if(a[i]==0){
for(j=i+1;j<size;j++){
a[j-1]=a[j];
a[j]=0;
}
}
}
cout<<"The array now is(after shifting the elements):"<<endl;
for(i=0;i<size;i++){
if(a[i])
cout<<a[i]<<" ";
}
return 0;
}
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<process.h>
int main()
{
int a[20][20],b[20][20],i,c,r,ch,rsum[20],r1,c1,j,csum[20],x[20]
[20],ip,majsum=0,minsum=0;
char choice='y';
cout<<"Enter the no. of rows of the matrix:";
cin>>r;
cout<<"Enter the no. of columns of the matrix:";
cin>>c;
cout<<"Enter an array:"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
cin>>a[i][j];
}
}
while(choice=='y')
{
cout<<"1.Product"<<endl;
cout<<"2.Transpose"<<endl;
cout<<"3.Row sum and columns sum"<<endl;
cout<<"4.Exit"<<endl;
cout<<"5.Sum of diagonals:"<<endl;
cout<<"6.Addition of the matrices:"<<endl;
cout<<"7.Subtraction of the matrices:"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter the no. of rows of the other matrix:";
cin>>r1;
cout<<"Enter the no. of columns of the other matrix:";
cin>>c1;
cout<<"Enter the matrix:";
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
cin>>b[i][j];
}
}
if(c!=r1){
cout<<"The matrices cannot be multiplyed.";
break;
}
else
{
for(i=0;i<r;i++){
for(j=0;j<c;j++){
x[i][j]=0;
for(ip=0;ip<r1;ip++){
x[i][j]+=(a[i][ip]*b[ip][j]);
}
}
}
}
cout<<"The product is:"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c1;j++){
cout<<x[i][j]<<" ";
}
cout<<endl;
}
break;
case 2:for(i=0;i<r;i++){
for(j=0;j<c;j++){
cout<<a[j][i]<<" ";
}
cout<<endl;
}
break;
case 3:for(i=0;i<r;i++)
rsum[i]=0;
for(i=0;i<c;i++)
csum[i]=0;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
rsum[i]+=a[i][j];
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
csum[i]+=a[j][i];
}
}
for(i=0;i<r;i++)
cout<<"The sum of row "<<i+1<<" is:"<<rsum[i]<<endl;
for(i=0;i<c;i++)
cout<<"The sum of column "<<i+1<<" is:"<<csum[i]<<endl;
break;
case 4:exit(0);
break;
case 5:for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(i==j)
majsum+=a[i][j];
else if((i+j)==(c-1))
minsum+=a[i][j];
}
}
cout<<"Major sum is:"<<majsum<<endl;
cout<<"The sum of the minor diagonal is:"<<minsum<<endl;
break;
else
cout<<"Matrices cannot be subtracted:"<<endl;
break;
}
cout<<"want to continue:";
cin>>choice;
}
return 0;
}
OUTPUT:
Enter an array:
1
2
3
4
5
6
7
8
9
1.Product
2.Transpose
3.Row sum and columns sum
4.Exit
5.Sum of diagonals:
6.Addition of the matrices:
7.Subtraction of the matrices:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],i,j,r1,r2,c1,c2;
cout<<"enter rows and column\n";
cin>>r1>>c1;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"\n";
cout<<"this matrix a\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<"\t"<< a[i][j];
}
cout<<"\n";
}
cout<<"Tronspose\n";
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
{
b[i][j]=a[j][i];
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<"\n";
}
getch();
}
OUTPUT:
enter rows and column
3
4
1
2
3
4
5
6
7
8
9
10
12
13
this matrix a
1 2 3 4
5 6 7 8
9 10 12 13
Tronspose
1 5 9
2 6 10
3 7 12
4 8 13
Q- Write a function that will scan a string for the occurence of a given string for
substring.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
char *substr(char *string1,char *string2)
{
char *cp;
cp=NULL;
int n=0;
while(*string1!='\0')
{
if(*string1==*string2)
{
n=1;
cp=string1;
while(*string2!='\0')
{
if(*string1!=*string2)
{
n=0;
cp=NULL;
break;
}
string1++;
string2++;
}
}
string1++;
}
return cp;
}
void main()
{
clrscr();
char str[50],sub[50];
char *pos;
cout<<"\nEnter a string \n";
gets(str);
cout<<"\nEnter a string to searched\n";
gets(sub);
pos=substr(str,sub);
if(*pos)
{
cout<<"\nString found\n";
}
else
{
cout<<"\nString not found\n";
}
getch();
}
OUTPUT:
Enter a string
KARTHIK RAO 88
String found
WRITE A PROGRAM TO COUNT AND DISPLAY THE NUMBER OH LINES NOT
STARTING WITH ALPHABET 'A' PRESENT IN THE TEXT FILE "STORY .TXT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
int nooflines()
{
ifstream fin("STORY.TXT");
int number=-1;
char line[50];
while(!fin.eof())
{
fin.getline(line,50,'.');
for(int i=0;i<50;i++)
{
if((line[i]!='a'&&line[i]!='A')&&(line[i-1]=='\r'))
number++;
}
}
cout<<"\nNumber of lines="<<" "<<number;
}
void main()
{
clrscr();
ofstream fout;
char str[500];
fout.open("STORY.TXT");
cout<<"\nEnter a string\n";
gets(str);
fout<<str<<"\n";
fout.close();
nooflines();
getch();
}
output:
Enter a string
The rose is red.Agirl is playing there. There is a playground.An aeroplane is in
the sky.
Number of lines= 4
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
}*start,*newptr,*save,*ptr;
node *create_new_node(int );
void main()
{
start=NULL;
int inf,ch;
char c='y';
clrscr();
while (c=='y'||c=='Y')
{
cout<<"1.insert\n2.delete\n3.display"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter the info"<<endl;
cin>>inf;
newptr= create_new_node(inf);
insert_beg(newptr);
cout<<"Sucess"<<endl;
break;
case 2:
del();
break;
case 3:
display();
break;
}
cout<<"y-continue n-exit"<<endl;
cin>>c;
}
}
node* create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void del(void)
{
if (start==NULL)
cout<<"underflow"<<endl;
else
{
save=start;
start=start->next;
delete save;
}
}
void display(void)
{
ptr=start;
cout<<"Start";
while(ptr!=NULL)
{
cout<<"->"<<ptr->info;
ptr=ptr->next;
}
cout<<endl;
}
OUTPUT:
1.insert
2.delete
3.display
1
Enter the info
34
Sucess
y-continue n-exit
y
1.insert
2.delete
3.display
1
y-continue n-exit
y
1.insert
2.delete
3.display
1
Enter the info
678
Sucess
y-continue n-exit
y
1.insert
2.delete
3.display
3
Start->678->56->34
y-continue n-exit
y
1.insert
2.delete
3.display
2
y-continue n-exit
y
1.insert
2.delete
3.display
3
Start->56->34
y-continue n-exit
n
Q- Write a c++ program to convert infix expression to postfix form.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
const int size=50;
char infix[size],stack[size],postfix[size],oper,temp,p;
int top =-1;
void main()
{
clrscr();
int brace,i,j=0;
char pop();
while((p=pop())!='$')
{
postfix[j++]=p;
postfix[j]='\0';
cout<<"postfix expression is "<<postfix<<endl;
}
getch();
}
char pop()
{
char ch;
if (top==-1)
return'$';
else
{
ch=stack[top];
top--;
return (ch);
}
}
void push(char c)
{
if (top==size)
cout<<"stck overflow"<<endl;
else
{
top++;
stack [top]=c;
}
}
OUTPUT
Certificate
Acknowledgement
System Requirements
Introduction
Data Dictionary
Source Code
End User Application
Screenshots
Bibliography