basic programs in c++

8
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(); }

Upload: arun-nair

Post on 02-Aug-2015

22 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: basic programs in C++

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

Page 2: basic programs in C++

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_

Page 3: basic programs in C++

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

Page 4: basic programs in C++

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(){

Page 5: basic programs in C++

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();}

Page 6: basic programs in C++

1. PUSH : 2. POP : 3. LIST : 4. EXIT : 1PUSHING....

Enter the element to be pushed:3

Do you wish to continue: y1. PUSH : 2. POP : 3. LIST : 4. EXIT : 1PUSHING....

Enter the element to be pushed:4

Do you wish to continue: y1. PUSH : 2. POP : 3. LIST : 4. EXIT : 3LISTING....The stack is ...43

Do you wish to continue: n