C ++ Programs
C ++ Programs
C ++ Programs
➢ Basics
// C++ Program to print HELLO WORLD
#include<iostream>
using namespace std;
int main()
{
cout<<" HEllO WORLD !!!!!";
}
SURAJ MEHTA 1
cin>>a>>b;
sum=a+b;
cout<<"sum= "<<sum;
}
SURAJ MEHTA 2
// C++ program to convert upper case character to lower
case
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"ENTER the upper case character : ";
cin>>ch;
cout<<ch;
ch=ch+32;
cout<<"= "<<ch;
}
SURAJ MEHTA 3
// C++ program to swap two numbers
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"ENTER the two value a & b : ";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"a= "<<a<<endl<<"b= "<<b;
}
➢ Conditional statement
// C++ program to find the PROFIT and LOSS
#include<iostream>
using namespace std;
int main()
{
SURAJ MEHTA 4
int sp,cp,p,l;
cout<<"ENTER the cost price of the product : ";
cin>>cp;
cout<<"ENTER the selling price of the product : ";
cin>>sp;
if(sp>cp)
cout<<"PROFIT of $ "<<(sp-cp);
else
cout<<"LOSS of $ "<<(cp-sp);
}
SURAJ MEHTA 5
cout<<" largest no. is "<<c;
}
}
SURAJ MEHTA 6
for(i=1;i<=n;i++)
{
sum=sum+i;
}
cout<<"sum ="<<sum;
SURAJ MEHTA 7
cout<<n<<"x"<<i<<"="<<n*i<<endl;
}
SURAJ MEHTA 8
}
cout<<"sum of digits = "<<sum;
}
}
cout<<"reverse no. = "<<rev;
}
SURAJ MEHTA 9
int n,rev=0,m;
cout<<"enter the any number : ";
cin>>n;
m=n;
while(n!=0)
{
rev=(rev*10)+n%10;
n=n/10;
}
if(rev==m)
cout<<"given no. is palindrome number";
else
cout<<"given no. is NOT palindrome number";
}
r=n%10;
a=a+r*r*r;
n=n/10;
}
if(a==m)
cout<<"given no. is Armstrong number";
else
cout<<"given no. is NOT Armstrong number";
}
SURAJ MEHTA 10
// C++ program to find the factorial of a numbers
#include<iostream>
using namespace std;
int main()
{
int n,i,fact=1;
cout<<"enter the any number : ";
cin>>n;
if(n>=0)
{
for(i=n;i>=1;i--)
{
fact=fact*i;
}
cout<<"factorial="<<fact;
}
else
cout<<"factorial is 0";
}
SURAJ MEHTA 11
using namespace std;
int main()
{
int a,b,c,min,hcf,i;
cout<<"enter the 3 number : ";
cin>>a>>b>>c;
min=(a<b&&a<c?a:b<c?b:c);
for(i=1;i<=min;i++)
{
if(a%i==0&&b%i==0&&c%i==0)
{
hcf=i;
}
}
cout<<"HCF ="<<hcf;
}
SURAJ MEHTA 12
cout<<endl;
}
}
cout<<endl;
}
}
hollow triangle
*/
SURAJ MEHTA 13
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i==1||j==1||i==j||i==n)
cout<<"* ";
else
cout<<" ";
}
cout<<endl;
}
}
triangle */
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"* ";
SURAJ MEHTA 14
}
cout<<endl;
}
}
Inverted triangle */
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;
}
}
Inverted triangle */
SURAJ MEHTA 15
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i<=j)
cout<<"* ";
else
cout<<" ";
}
cout<<endl;
}
}
#include<iostream>
using namespace std;
int main()
{
int i,j,n,k;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(k=1;k<=2*n-2*i;k++)
cout<<" ";
for(j=1;j<=i;j++)
SURAJ MEHTA 16
{
cout<<"* ";
}
cout<<endl;
}
}
SURAJ MEHTA 17
#include<iostream>
using namespace std;
int main()
{
int i,j,n,k,c=1;
cout<<"enter the number of row : ";
cin>>n;
for(i=0;i<n;i++)
{
for(k=1;k<=i;k++)
cout<<" ";
for(j=1;j<=2*(n-i)-1;j++)
{
cout<<"* ";
}
c+=2;
cout<<endl;
}
}
/*C++ program to print pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
pyramid pattern
*/
#include<iostream>
using namespace std;
int main()
{
int i,j,n,k,l;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(k=1;k<=2*n-2*i;k++)
cout<<" ";
for(j=1;j<=i;j++)
SURAJ MEHTA 18
{
cout<<j<<" ";
}
for(l=i-1;l>=1;l--)
cout<<l<<" ";
cout<<endl;
}
}
pyramid pattern
*/
#include<iostream>
using namespace std;
int main()
{
int i,j,n,k,l;
cout<<"enter the number of row : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(k=1;k<=2*n-2*i;k++)
cout<<" ";
for(j=i;j>=1;j--)
{
cout<<j<<" ";
}
for(l=2;l<=i;l++)
cout<<l<<" ";
cout<<endl;
}
SURAJ MEHTA 19
}
➢Array
// c++ program to find the greatest and smallest between
multiple numbers
#include<iostream>
using namespace std;
int main()
{
int ar[100],i,n,g,s;
cout<<"enter the size of an array : ";
cin>>n;
cout<<"enter the data \n";
for(i=0;i<n;i++)
cin>>ar[i];
g=s=ar[0];
for(i=0;i<n;i++)
{
if(g<ar[i])
g=ar[i];
else
{
if(s>ar[i])
s=ar[i];
}
}
cout<<"greatest no. is : "<<g<<endl;
cout<<"smallest no. is :"<<s;
SURAJ MEHTA 20
cout<<"enter the data \n";
for(i=0;i<n;i++)
cin>>ar[i];
cout<<"Enter the which you want be search : ";
cin>>s;
for(i=0;i<n;i++)
{
if(s==ar[i])
{
cout<<"Searching successful";
break;
}
}
if(i==n)
cout<<s<<" is not found in array list ";
SURAJ MEHTA 21
cout<<" "<<ar[i];
➢ String
SURAJ MEHTA 22
cin.get(str,100);
l=strlen(str);
i=l;
while(i>=0)
{
cout<<str[i];
i--;
}
SURAJ MEHTA 23
{
if(str1[i]>='A'&&str1[i]<='Z')
str1[i]+=32;
i++;
}
cout<<"lower case string is "<<str1;
SURAJ MEHTA 24
str3[i]=str1[i];
i++;
}
j=0;
while(str2[j]!='\0')
{
str3[i]=str2[j];
i++;
j++;
}
str3[i]='\0';
cout<<"concatenated string is "<<str3;
SURAJ MEHTA 25
}
if(x==0)
cout<<"......fibonacci series is printed";
else
{
cout<<a<<" ";
c=a+b;
a=b;
b=c;
fibo(x-1);
}
}
int main()
{
int n,sum;
cout<<"enter the no. of terms : ";
cin>>n;
fibo(n);
}
➢Array
// implementation of Array in c++
#include<iostream>
using namespace std;
int x,ar[100],n,i;
SURAJ MEHTA 26
void insert()
{
int p;
cout<<"enter the data : ";
cin>>x;
cout<<"enter the positon no. where you want to be
insert ";
cin>>p;
n++;
for(i=n-1;i>=p-1;i--)
{
ar[i+1]=ar[i];
}
ar[p-1]=x;
}
void print()
{
for(i=0;i<n;i++)
{
cout<<" "<<ar[i];
}
}
void del()
{
int p;
cout<<"enter the positon no. which you want to be
delete ";
cin>>p;
for(i=p-1;i<n;i++)
{
ar[i]=ar[i+1];
}
n--;
}
int main()
{
int ch,c=1;
cout<<"enter the size of an array : ";
cin>>n;
cout<<"enter the data ";
SURAJ MEHTA 27
for(i=0;i<n;i++)
cin>>ar[i];
while(c)
{
cout<<"\n 1.insert\n 2.delete\n 3.print\n 4.exit\n
enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:print();
break;
case 4:c=0;
}
}
}
➢Stack
SURAJ MEHTA 28
}
void print()
{
int i;
if(top==-1)
cout<<"stack is empty ";
else
{
for(i=top;i>=0;i--)
{
cout<<"____"<<endl<<" "<<s[i]<<endl;
}
}
}
void pop()
{
int x;
if(top==-1)
cout<<"-----X--> stack is underflow <--X-----";
else
{
top--;
}
}
int main()
{
int ch,c=1;
while(c)
{
cout<<"\n 1.push\n 2.pop\n 3.print\n 4.exit\n
enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:print();
break;
SURAJ MEHTA 29
case 4:c=0;
}
}
}
➢Queue
// implementation of Queue in c++
#include<iostream>
using namespace std;
int f=0,r=-1,q[5];
void insert()
{
int x;
if(r==4)
cout<<"-----X Queue is overflow X-----";
else
{
r++;
cout<<"enter the data which you want to insert in
queue : ";
cin>>x;
q[r]=x;
}
}
void print()
{
int i;
if(r<f)
cout<<"queue is empty ";
else
{
for(i=f;i<=r;i++)
{
cout<<"|"<<" "<<q[i]<<" "<<"|";
}
}
}
void del()
{
SURAJ MEHTA 30
int x;
if(r<f)
{
r=-1;
f=0;
cout<<"-----X--> Queue is underflow <--X-----";
}
else
{
f++;
}
}
int main()
{
int ch,c=1;
while(c)
{
cout<<"\n 1.insert\n 2.delete\n 3.print\n 4.exit\n
enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:print();
break;
case 4:c=0;
}
}
}
SURAJ MEHTA 31
typedef struct node
{
int data;
struct node *next;
}Node;
Node *create(Node *p)
{
if(p==NULL)
{
p=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
p->next=NULL;
p->data=x;
}
else
p->next=create(p->next);
return p;
}
Node *insatbeg(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
q->next=p;
q->data=x;
return q;
}
void print(Node *p)
{
while(p!=NULL)
{
cout<<" "<<p->data;
p=p->next;
}
}
void insatany(Node *p)
{
int k;
SURAJ MEHTA 32
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
cout<<" enter the position where you want be insert
:";
cin>>k;
for(i=1;i<k-1;i++)
{
p=p->next;
}
q->next=p->next;
p->next=q;
q->data=x;
}
void insatend(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
while(p->next!=NULL)
{
p=p->next;
}
q->next=NULL;
p->next=q;
q->data=x;
}
Node *delnode(Node *p)
{
int k;
Node *q=NULL,*r;
r=p;
SURAJ MEHTA 33
q=r;
r=r->next;
}
if(q==NULL)
{
p=p->next;
free(r);
}
else
{
q->next=r->next;
free(r);
}
return(p);
int main()
{
Node *head=NULL;
int ch,c=1;
while(c)
{
cout<<"\n 1.create\n 2.insert at beginning\n
3.insert at any positon\n 4.insert at end\n 5.dlete a
node\n 6.print\n 7.exit\n enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:head=create(head);
break;
case 2:head=insatbeg(head);
break;
case 3:insatany(head);
break;
case 4:insatend(head);
break;
case 5: head=delnode(head);
break;
case 6:print(head);
SURAJ MEHTA 34
break;
case 7:c=0;
}
}
}
}Node;
Node *create(Node *p)
{
if(p==NULL)
{
p=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
p->next=p->prev=NULL;
p->data=x;
}
else
{
p->next=create(p->next);
p->next->prev=p;
}
return p;
}
Node *insatbeg(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));
SURAJ MEHTA 35
cout<<"enter the data : ";
cin>>x;
q->prev=NULL;
q->next=p;
p->prev=q;
q->data=x;
return q;
}
void print(Node *p)
{
while(p!=NULL)
{
cout<<" "<<p->data;
p=p->next;
}
}
void insatany(Node *p)
{
int k;
Node *q;
q=(Node*)malloc(sizeof(Node));
cout<<"enter the data : ";
cin>>x;
cout<<" enter the position where you want be insert
:";
cin>>k;
for(i=1;i<k-1;i++)
{
p=p->next;
}
p->next->prev=q;
q->next=p->next;
q->prev=p;
p->next=q;
q->data=x;
}
void insatend(Node *p)
{
Node *q;
q=(Node*)malloc(sizeof(Node));
SURAJ MEHTA 36
cout<<"enter the data : ";
cin>>x;
while(p->next!=NULL)
{
p=p->next;
}
q->next=NULL;
q->prev=p;
p->next=q;
q->data=x;
}
Node *delnode(Node *p)
{
int k;
Node *q=NULL,*r;
r=p;
SURAJ MEHTA 37
}
return(p);
int main()
{
Node *head=NULL;
int ch,c=1;
while(c)
{
cout<<"\n 1.create\n 2.insert at beginning\n
3.insert at any positon\n 4.insert at end\n 5.dlete a
node\n 6.print\n 7.exit\n enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:head=create(head);
break;
case 2:head=insatbeg(head);
break;
case 3:insatany(head);
break;
case 4:insatend(head);
break;
case 5: head=delnode(head);
break;
case 6:print(head);
break;
case 7:c=0;
}
}
}
SURAJ MEHTA 38