KEMBAR78
basic programs in C++ | DOCX
SOURCE CODE
#include <iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int n,i;
cout<<"n Enter the number to display the multiplication table:";
cin>>n;
cout<<endl;
for(int i=0;i>=10;i++)
{
cout<<setw(5)<<n<<setw(5)<<"*"<<setw(5)<<i<<setw(5)<<n*i<<endl;
}
getch();
}
Enter the number to display the multiplication table :2
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
SOURCE CODE
#include <iostream.h>
#include<conio.h>
class add
{
int a,b,c;
public:
void getdata();
friend void calc(add);
};
void calc(add x)
{
x.c=x.a+x.b;
cout<<"n The sum is ="<<x.c;
}
void add::getdata()
{
cout<<"n Enter a number:"<<endl;
cin>>a;
cout<<"n Enter another number :"<<endl;
cin>>b;
}
void main()
{
add y;
clrscr();
y.getdata();
calc(y);
getch();
}
Enter a number: 5
Enter another number :2
The sum is = 7_
SOURCE CODE
#include <iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<iomanip.h>
void main()
{
char a[20],b[20],ch;
clrscr();
do
{
cout<<"n Enter a word to check if its a palindrome or not :"<<endl;
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
{
cout<<"n The entered string is palindrome !!!!";
}
else
{
cout<<"n Not a palindrome !!!!!!!!!!!";
}
cout<<"n Do you wish to enter more(y/n)!!";
cin>>ch;
}while(ch=='y')
getch();
}
(3)
Enter a word to check if its a palindrome or not :
MALAYALAM
The entered string is palindrome !!!!
you wish to enter more(y/n)!! n
SOURCE CODE
#include <iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int top,a[10],i,temp,n;
public:
stack()
{
top=0;
}
~stack()
{
cout<<"n DestructinG ............!!!!!!!!!!!!!!!! ";
}
void push();
void pop();
void list();
};
void stack::push()
{
if(top==10)
{
cout<<"n STACK OVERFLOW!!";
}
cout<<"n Enter the element to be pushed :n";
cin>>n;
top++;
a[top]=n;
}
void stack::pop()
{
if(top==0)
{
cout<<"n Stack overflow!!!";
}
temp=a[top];
cout<<"n The popped element is....."<<temp;
top--;
}
void stack::list()
{
cout<<"n The stack is...n";
for(i=top;i>0;i--)
{
cout<<a[i];
cout<<"n";
}
}
void main()
{
stack s;
int c;
char ch;
clrscr();
do
{
cout<<"1. PUSH :"<<endl;
cout<<"2. POP :"<<endl;
cout<<"3. LIST :"<<endl;
cout<<"4. EXIT :"<<endl;
cin>>c;
switch(c)
{
case 1:cout<<"n PUSHING ...";
s.push();
break;
case 1:cout<<"n POPING ...";
s.pop();
break;
case 1:cout<<"n LISTING ...";
s.list();
break;
case 1:cout<<"n EXIT ...";
exit(0);
break;
}
cout<<"n Do you wish to continue :";
cin>>ch;
}while(ch=='y');
getch();
}
1. PUSH :
2. POP :
3. LIST :
4. EXIT :
1
PUSHING....
Enter the element to bepushed:
3
Do you wish to continue: y
1. PUSH :
2. POP :
3. LIST :
4. EXIT :
1
PUSHING....
Enter the element to bepushed:
4
Do you wish to continue: y
1. PUSH :
2. POP :
3. LIST :
4. EXIT :
3
LISTING....
The stack is ...
4
3
Do you wish to continue: n

basic programs in C++

  • 1.
    SOURCE CODE #include <iostream.h> #include<conio.h> #include<iomanip.h> voidmain() { int n,i; cout<<"n Enter the number to display the multiplication table:"; cin>>n; cout<<endl; for(int i=0;i>=10;i++) { cout<<setw(5)<<n<<setw(5)<<"*"<<setw(5)<<i<<setw(5)<<n*i<<endl; } getch(); } Enter the number to display the multiplication table :2 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20
  • 2.
    SOURCE CODE #include <iostream.h> #include<conio.h> classadd { int a,b,c; public: void getdata(); friend void calc(add); }; void calc(add x) { x.c=x.a+x.b; cout<<"n The sum is ="<<x.c; } void add::getdata() { cout<<"n Enter a number:"<<endl; cin>>a; cout<<"n Enter another number :"<<endl; cin>>b; } void main() { add y; clrscr(); y.getdata(); calc(y); getch(); } Enter a number: 5 Enter another number :2 The sum is = 7_
  • 3.
    SOURCE CODE #include <iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> #include<iomanip.h> voidmain() { char a[20],b[20],ch; clrscr(); do { cout<<"n Enter a word to check if its a palindrome or not :"<<endl; gets(a); strcpy(b,a); strrev(b); if(strcmp(a,b)==0) { cout<<"n The entered string is palindrome !!!!"; } else { cout<<"n Not a palindrome !!!!!!!!!!!"; } cout<<"n Do you wish to enter more(y/n)!!"; cin>>ch; }while(ch=='y') getch(); } (3) Enter a word to check if its a palindrome or not : MALAYALAM The entered string is palindrome !!!! you wish to enter more(y/n)!! n
  • 4.
    SOURCE CODE #include <iostream.h> #include<conio.h> #include<stdlib.h> classstack { int top,a[10],i,temp,n; public: stack() { top=0; } ~stack() { cout<<"n DestructinG ............!!!!!!!!!!!!!!!! "; } void push(); void pop(); void list(); }; void stack::push() { if(top==10) { cout<<"n STACK OVERFLOW!!"; } cout<<"n Enter the element to be pushed :n"; cin>>n; top++; a[top]=n; } void stack::pop() { if(top==0) { cout<<"n Stack overflow!!!"; } temp=a[top]; cout<<"n The popped element is....."<<temp; top--; } void stack::list() {
  • 5.
    cout<<"n The stackis...n"; for(i=top;i>0;i--) { cout<<a[i]; cout<<"n"; } } void main() { stack s; int c; char ch; clrscr(); do { cout<<"1. PUSH :"<<endl; cout<<"2. POP :"<<endl; cout<<"3. LIST :"<<endl; cout<<"4. EXIT :"<<endl; cin>>c; switch(c) { case 1:cout<<"n PUSHING ..."; s.push(); break; case 1:cout<<"n POPING ..."; s.pop(); break; case 1:cout<<"n LISTING ..."; s.list(); break; case 1:cout<<"n EXIT ..."; exit(0); break; } cout<<"n Do you wish to continue :"; cin>>ch; }while(ch=='y'); getch(); }
  • 6.
    1. PUSH : 2.POP : 3. LIST : 4. EXIT : 1 PUSHING.... Enter the element to bepushed: 3 Do you wish to continue: y 1. PUSH : 2. POP : 3. LIST : 4. EXIT : 1 PUSHING.... Enter the element to bepushed: 4 Do you wish to continue: y 1. PUSH : 2. POP : 3. LIST : 4. EXIT : 3 LISTING.... The stack is ... 4 3 Do you wish to continue: n