C++ Progrm
C++ Progrm
C++ Progrm
Suppose
we enter Mohandas Karamchand Gandhi, the output
will be M.K.Gandhi.
Program:-
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
class ab_name
{
private:
int i,k,ln;
char str[100];
public:
void fn();
};
void ab_name::fn()
{
printf("\nEnter a name: ");
gets(str);
ln=strlen(str);
for(i=ln-1;i>=0;i--)
{
if(str[i]==' ')
{
k=i;
break;
}
}
for(i=0;i<k;i++)
{
if(i==0)
cout<<str[i]<<".";
if(str[i]==' ')
cout<<str[i+1]<<".";
}
for(i=k+1;i<ln;i++)
{
cout<<str[i];
}
}
void main()
{
clrscr();
ab_name obj;
obj.fn();
getch();
}
Output:
2. Write a C++ to abbreviate a name. For example- if we
enter Mohandas Karamchand Gandhi , the output will
be M.K.G.
Program:-
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
class ab_name
{
private:
int i,k,ln;
char str[100];
public:
void fn();
};
void ab_name::fn()
{
printf("\nEnter a name: ");
gets(str);
ln=strlen(str);
for(i=0;i<ln;i++)
{
if(i==0)
cout<<str[i];
if(str[i]==' ')
cout<<"."<<str[i+1];
}
}
void main()
{
clrscr();
ab_name obj;
obj.fn();
getch();
}
Output:
3. Write a C++ program to print a number in words.
Program:-
#include<iostream.h>
#include<conio.h>
class alpha
{
private:
int arr[5],n,i,r;
public:
void fn();
};
void alpha::fn()
{
cout<<"\nEnter a Number:";
cin>>n;
if(n<=0&&n>999)
cout<<"\nInvalid Number..";
else
{
i=0;
while(n>0)
{
r=n%10;
arr[i]=r;
n=n/10;
i++;
}
for(i=i-1;i>=0;i--)
{
switch(arr[i])
{
case 0:
cout<<"Zero"<<" ";
break;
case 1:
cout<<"One"<<" ";
break;
case 2:
cout<<"Two"<<" ";
break;
case 3:
cout<<"Three"<<" ";
break;
case 4:
cout<<"Four"<<" ";
break;
case 5:
cout<<"Five"<<" ";
break;
case 6:
cout<<"Six"<<" ";
break;
case 7:
cout<<"Seven"<<" ";
break;
case 8:
cout<<"Eight"<<" ";
break;
case 9:
cout<<"Nine"<<" ";
break;
}
}
}
}
void main()
{
clrscr();
alpha obj;
obj.fn();
getch();
}
Output:
4. Write a C++ program to sort an array of numbers
using bubble sort.
Program:-
#include<iostream.h>
#include<conio.h>
class bubble_sort
{
private:
int arr[20],tmp,n,i,j;
public:
void fn();
};
void bubble_sort::fn()
{
{
cout<<"Enter The Number Of Elements:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Value For arr["<<i<<"]:";
cin>>arr[i];
}
}
for(i=1;i<n;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
}
}
cout<<"The Sorted Elements Are:";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
void main()
{
clrscr();
bubble_sort obj;
obj.fn();
getch();
}
Output:
void decimal_binary::display()
{
cout<<"The binary equivalent is ";
for(i=ln-1;i>=0;i--)
{
cout<<arr[i];
}
void main()
{
clrscr();
decimal_binary obj;
obj.input();
obj.calculate();
obj.display();
getch();
}
Output:
6. Write a C++ Program to find frequency of the
alphabets in a string.
Program:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class frec
{
private:
int arr[26],i,ln;
char str[50],ch;
public:
void initialize()
{
for(i=0;i<26;i++)
{
arr[i]=0;
}
}
void input()
{
cout<<"\nEnter a string:";
gets(str);
}
void calculate();
void display();
};
void frec::calculate()
{
ln=strlen(str);
for(i=0;i<ln;i++)
{
ch=str[i];
if(ch>=65 && ch<=90)
arr[ch-65]=arr[ch-65]+1;
if(ch>=97 && ch<=122)
arr[ch-97]=arr[ch-97]+1;
}
}
void frec::display()
{
cout<<"\nThe Frequencies of Characters:\n";
for(i=0;i<26;i++)
{
if(arr[i]!=0)
printf("\n\t%c: %d",i+65,arr[i]);
}
}
void main()
{
clrscr();
frec obj;
obj.initialize();
obj.input();
obj.calculate();
obj.display();
getch();
}
Output:
7. Write a C++ program to make the following pattern.
Program:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class graph
{
private:
public:
void fn();
};
void graph::fn()
{
int
p1[]={100,100,150,70,200,100,200,150,150,180,100,150,
100,100};
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(7,p1);
int
p2[]={300,100,350,70,400,100,400,150,350,180,300,150,
300,100};
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
fillpoly(7,p2);
int
p3[]={200,100,300,100,300,150,200,150,200,100};
setcolor(BLUE);
setfillstyle(SOLID_FILL,BLUE);
fillpoly(5,p3);
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
pieslice(400,125,270,360,25);
pieslice(400,125,0,90,25);
}
void main()
{
int gd,gm;
clrscr();
graph obj;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
obj.fn();
getch();
closegraph();
}
Output:
8. Write a C++ program to make the following pattern.
Program:-
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class graph
{
private:
public:
void fn();
};
void graph::fn()
{
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
rectangle(100,100,200,200);
floodfill(101,101,RED);
/* int p[]={100,100,100,200,200,200,200,100,100,100};
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillpoly(5,p); */
int p1[]={200,100,300,150,200,200,200,100};
setcolor(BLUE);
setfillstyle(SOLID_FILL,BLUE);
fillpoly(4,p1);
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
pieslice(230,150,0,360,30);
}
void main()
{
int gd,gm;
clrscr();
graph obj;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
obj.fn();
getch();
closegraph();
}
Output:
}
void main()
{
clrscr();
lcm_hcf obj;
obj.fn();
getch();
}
Output:
int i,j,n;
public:
void insert();
void display();
};
class node *start=NULL;
void node::insert()
{
int item;
node *tmp,*ptr;
cout<<"\nEnter Number of Nodes:";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\nEnter Item" <<i<<":";
cin>>item;
tmp=new node;
tmp->info=item;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
ptr=start;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=tmp;
}
cout<<"\nItem Inserted.";
}
}
void node::display()
{
int t;
node *ptr,*previous;
previous=ptr;
ptr=ptr->link;
if(previous->info>ptr->info)
{
t=previous->info;
previous->info=ptr->info;
ptr->info=t;
}
}
}
//code to Print values after sorting
cout<<"\nAfter Sorting the List is:\n";
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->info<<" ";
ptr=ptr->link;
}
}
void main()
{
int ch;
clrscr();
node obj;
obj.insert();
obj.display();
getch();
}
Output:
11. Write a C++ Program to find the sum of rows and the
columns of a matrix.
Program:-
#include<iostream.h>
#include<conio.h>
class mat_rw_clmn
{
private:
int mat[10][10],r,c,i,j,sc,sr;
public:
void fn();
};
void mat_rw_clmn::fn()
{
cout<<"Enter The Number of Rows:";
cin>>r;
cout<<"Enetr The Number of columns:";
cin>>c;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\nEnter Value For Position
["<<i<<"]["<<j<<"]:";
cin>>mat[i][j];
}
}
sr=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sr=sr+mat[i][j];
// cout<<"\nThe Sum Of Row
"<<i<<"is: "<<sr;
}
cout<<sr<<"\n";
}
sc=0;
for(j=0;j<c;j++)
{
for(i=0;i<r;i++)
{
sc=sc+mat[j][i];
//cout<<"\nThe Sum Of Column
"<<j<<"is: "<<sc;
}
cout<<sc<<" ";
}
}
void main()
{
clrscr();
mat_rw_clmn obj;
obj.fn();
getch();
}
Output:
12. Write a C++ Program to illustrate constructor
overloading.
Program:-
#include<iostream.h>
#include<conio.h>
class copy
{
private:
int a;
public:
copy()
{
a=0;
}
copy(int p)
{
a=p;
}
copy(copy &q)
{
a=q.a+100;
}
void display();
};
void copy::display()
{
cout<<"The Value Of A is:"<<a<<endl;
}
void main()
{
clrscr();
copy obj1;
copy obj2(100);
copy obj3(obj2);
obj1.display();
obj2.display();
obj3.display();
getch();
}
Output:
class pattern1
{
private:
int line,i,j;
public:
void fn();
};
void pattern1::fn()
{
cout<<"\nEnter No of Lines:";
cin>>line;
cout<<"\nThe Pattern is:\n";
for(i=1;i<=line;i++)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
cout<<"\n";
}
}
void main()
{
clrscr();
pattern1 obj;
obj.fn();
getch();
}
Output:
max=arr[0];min=arr[0];
for(i=1;i<10;i++)
{
if(arr[i]<min)
{
min=arr[i]; pos1=i;
}
if(arr[i]>max)
{
max=arr[i]; pos=i;
}
}
cout<<"\nMAX= "<<max<<"at
position"<<pos+1<<"\nMIN= "<<min<<"at position
"<<pos1+1;
getch();
}
Output:
15. Write a C++ program to illustrate single inheritance.
Program:-
#include<iostream.h>
#include<conio.h>
class base
{
/* protected:
int a,b;*/
public:
int a,b;
void fn(int p, int q)
{
a=p;
b=q;
}
};
class derived:public base
{
private:
int s;
public:
void display()
{
s=a+b;
cout<<"The Sum Of The Given Numbers Is:
"<<s;
}
};
void main()
{
int x,y;
clrscr();
cout<<"Enter Two numbers:\n";
cin>>x>>y;
clrscr();
derived obj;
obj.fn(x,y);
obj.display();
getch();
}
Output: