Beruflich Dokumente
Kultur Dokumente
PROGRAMMING IN C++
1/*W.A.P. TO CHECK WETHER A NO. IS +VE OR -VE*/
#include<conio.h> #include<iostream.h> void main() { clrscr(); int a; cout<<"enter any no.\n"; cin>>a; if(a>=0) cout<<"no. is +ve no.\n"; else cout<<"no. is -ve no.\n"; getch(); } output enter any no. 3 no. is +ve no. enter any no. -4 no. is -ve no.
PROGRAMMING IN C++
Page 2
PROGRAMMING IN C++
Page 3
PROGRAMMING IN C++
output enter basic salary 120000 annual salary=204000 amount of tax=40800 total annual salary=163200
Page 4
PROGRAMMING IN C++
Page 5
PROGRAMMING IN C++
Page 6
PROGRAMMING IN C++
r=150
13
21
Page 7
PROGRAMMING IN C++
Page 8
PROGRAMMING IN C++
9/*w.a.p. to print
1 121 12321 1234321*/ #include<conio.h> #include<iostream.h> void main() { clrscr(); int i,j; for(i=1;i<=4;i++) { for(j=(4-i);j>=1;j--) { cout<<" "; } for(j=1;j<=i;j++) { cout<<j; }j--; for(j=j-1;j>=1;j--) { cout<<j; } cout<<"\n"; } getch(); } output 1 121 12321 1234321
Page 9
PROGRAMMING IN C++
10/*w.a.p. to print
A AB ABC ABCD*/ #include<conio.h> #include<iostream.h> void main() { clrscr(); int i; char j; for(i=1;i<=4;i++) { for(j=(4-i);j>=1;j--) { cout<<" "; } for(j=65;j<(65+i);j++) { cout<<j; } cout<<"\n"; } getch(); } output A AB ABC ABCD
Page 10
PROGRAMMING IN C++
Page 11
PROGRAMMING IN C++
Page 12
PROGRAMMING IN C++
Page 13
PROGRAMMING IN C++
Page 14
PROGRAMMING IN C++
Page 15
PROGRAMMING IN C++
Page 16
PROGRAMMING IN C++
Page 17
PROGRAMMING IN C++
output enter any number32 enter any number45 enter any number67 enter any number45 enter any number87 enter any number23 enter any number12 enter any number99 enter any number01 enter any number17 1 12 17 23 32 45 45 67 87 99
Page 18
PROGRAMMING IN C++
#include<iostream.h> #include<conio.h> void main { int num=20; char ch='a'; int=*ptr; ptr=num; cout<<*ptr<<endl; cout<<ptr<<endl; getch(); } output 20 0x8feafff4
Page 19
PROGRAMMING IN C++
#include<conio.h> void main { int num=20; char ch='a'; int=*ptr; ptr=num; cout<<*ptr<<endl; cout<<ptr<<endl; getch(); } output 20 0x8feafff4
Page 20
PROGRAMMING IN C++
void length() { char str[10]; int i; cout<<"enter a string"; cin>>str; for(i=0;str[i]!='\0';i++) { } cout<<"length="<<i; } void main() { clrscr(); length(); getch(); } output enter a string sandhya length=7
Page 21
PROGRAMMING IN C++
{ clrscr(); char str[10]; int i; cout<<"enter a string\n"; cin>>str; for(i=0;str[i]!='\0';i++) {} return i; } void main() { int len; clrscr(); length(); len=length(); cout<<"length="<<len<<endl; getch(); } output enter a string sandhya length=7
Page 22
PROGRAMMING IN C++
{ int i; for(i=0;arr[i]!='\0';i++) {} return i; } void main() { clrscr(); char str[20]; int len; cout<<"enter a string"; cin>>str; len=length(str); cout<<"length="<<len<<endl; getch(); } output enter a string sandhya length=7
Page 23
PROGRAMMING IN C++
{ int i; for(i=0;*arr!='\0';i++) { arr++; } return i; } void main() { clrscr(); char str[20]; int len; cout<<"enter a string"; cin>>str; len=length(str); cout<<"length="<<len<<endl; getch(); } output enter a string sandhya length=7
Page 24
PROGRAMMING IN C++
int temp; temp=*a; *a=*b; *b=temp; } void main() { clrscr(); int a,b,i; cout<<"enter any two number\n"; cin>>a>>b; swap(&a,&b); cout<<"values="<<a<<" "<<b; getch(); } output enter any two number 34 76 values=76 34
27/*w.a.p. using call by value & call by reference print the value of a*/
#include<iostream.h> #include<conio.h> void increment (int *a) { (*a)++;
Page 25
PROGRAMMING IN C++
} void increment1(int a ) { a++; } void main() { int x=15; clrscr(); cout<<x<<endl; increment(&x); cout<<x<<endl; increment1(x); cout<<x<<endl; getch(); } output 15 16 16
Page 26
PROGRAMMING IN C++
}; struct employee emp; void accept() { cout<<"enter employee id "; cin>>emp.emp_id; cout<<"enter employee name & department"; cin>>emp.emp_n>>emp.emp_dep; } void display() { cout<<"\n id of an employee="<<emp.emp_id; cout<<"\n name of an employee"<<emp.emp_n; cout<<"\n department name="<<emp.emp_dep; } void main() { clrscr(); accept(); display(); getch(); } output enter employee id 101 enter employee name & department sandhya purchase id of an employee=101 name of an employeesandhya department name=purchase
29/*w.a.p. using structure accept & display the three employee information*/
#include<iostream.h> #include<conio.h> struct employee { int emp_id; char emp_n[15],emp_dep[15];
Page 27
PROGRAMMING IN C++
}; struct employee emp[3]; void accept() { for(int i=0;i<3;i++) { cout<<"enter employee id "; cin>>emp[i].emp_id; cout<<"enter employee name & department"; cin>>emp[i].emp_n>>emp[i].emp_dep; } } void display() { for(int i=0;i<3;i++) { cout<<"\n id of an employee="<<emp[i].emp_id; cout<<"\n name of an employee"<<emp[i].emp_n; cout<<"\n department name="<<emp[i].emp_dep; } } void main() { clrscr(); accept(); display(); getch(); }
output enter employee enter employee department sandhya marketing enter employee enter employee department abc HR enter employee enter employee department xyz purchase
id of an employee=101 name of an employeesandhya department name=marketing id of an employee=102 name of an employeeabc department name=HR id of an employee=103 name of an employeexyz department name=purchase
Page 28
PROGRAMMING IN C++
void accept() { cout<<"enter employee id "; cin>>id; cout<<"enter employee name & department"; cin>>name>>dept; } void display() { cout<<"\n id of an employee="<<id; cout<<"\n name of an employee"<<name; cout<<"\n department name="<<dept; } }; void main() { clrscr(); employee emp; emp.accept(); emp.display(); getch(); } output enter employee id 12 enter employee name & department sandhya purchase id of an employee=12 name of an employeesandhya department name=purchase
31/* using scope resolution operator to define the function out side the class*/
#include<iostream.h> #include<conio.h> class employee { private: int id; char name[20],dept[20]; public:
Page 29
PROGRAMMING IN C++
void accept(); void display(); }; void employee ::accept() { cout<<"enter employee id "; cin>>id; cout<<"enter employee name & department"; cin>>name>>dept; } void employee :: display() { cout<<"\n id of an employee="<<id; cout<<"\n name of an employee"<<name; cout<<"\n department name="<<dept; } void main() { clrscr(); employee emp; emp.accept(); emp.display(); getch(); } output enter employee id 101 enter employee name & department sandhya HR id of an employee=101 name of an employeesandhya department name=HR
32/*declaring constructor*/
#include<iostream.h> #include<conio.h> class employee { private: int id; char name[20],dept[20]; public: void accept();
Page 30
PROGRAMMING IN C++
void display(); employee() { id=0; name[0]=dept[0]=' '; } }; void employee :: accept() { cout<<"\n enter employee id "; cin>>id; cout<<"\n enter employee name & department"; cin>>name>>dept; } void employee :: display() { cout<<"\n id of an employee="<<id; cout<<"\n name of an employee"<<name; cout<<"\n department name="<<dept; } void main() { clrscr(); employee emp; emp.display(); emp.accept(); emp.display(); getch(); }
output id of an employee=0 name of an employee department name= enter employee id 13 enter employee name & department sandhya purchase id of an employee=13 name of an employeesandhya department name=purchase
Page 31
PROGRAMMING IN C++
~demo() { cout<<"destructor called\n" ; } }; void main() { clrscr(); demo d1; { demo d2; } getch(); } output constructor called constructor called destructor called destructor called
Page 32
PROGRAMMING IN C++
}; void show() { demo d; d.display(); } void main() { clrscr(); show(); getch(); } output function from class
Page 33