Sie sind auf Seite 1von 33

HDCLUB Programs

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++

2/*W.A.P.TO CALCULATE THE REMAINDER VALUE */


#include<conio.h> #include<iostream.h> void main() { clrscr(); int a,b; float r; cout<<"enter any no.\n"; cin>>a>>b; if(b<=a) { r=a%b; cout<<"result is ="<<r; } else { cout<<"not possible"; } getch(); } output enter any no. 15 2 result is =1

Page 2

PROGRAMMING IN C++

3/*W.A.P. to calculate total salary*/


#include<conio.h> #include<iostream.h> void main() { clrscr(); int bs; float da,hra,pf,as; cout<<"enter basic salary \n"; cin>>bs; da=bs*(.46); hra=bs*(.35); pf=bs*(.11); as=(bs+da+hra)-pf; cout<<"annual salary="<<as<<endl; if(as>=100000) cout<<"salary is taxable"; else cout<<"not taxable"; getch(); }

output enter basic salary 1000 annual salary=1700 not taxable

Page 3

PROGRAMMING IN C++

4/*w.a.p. to calculate taxable amount on total salary*/


#include<conio.h> #include<iostream.h> void main() { clrscr(); double bs,ts,tax; double da,hra,pf,as; cout<<"enter basic salary \n"; cin>>bs; da=bs*(.46); hra=bs*(.35); pf=bs*(.11); as=(bs+da+hra)-pf; cout<<"annual salary="<<as<<endl; if(as<=100000) { tax=0; cout<<"not taxable\n"; } else if(as>100000&&as<200000) { tax=as*.10; } else if(as>200000&&as<300000) { tax=as*.20; } else { tax=as*.30; } cout<<"amount of tax="<<tax; ts=as-tax; cout<<"\n total annual salary="<<ts; getch(); }

output enter basic salary 120000 annual salary=204000 amount of tax=40800 total annual salary=163200

Page 4

PROGRAMMING IN C++

5/*W.A.P. to check the character is capital, small, special symbol or a digit */


#include<conio.h> #include<iostream.h> void main() { clrscr(); char a; cout<<"enter any character\n"; cin>>a; if(a>=65&&a<=90) { cout<<"your character is capital latter\n"; } else if(a>=97&&a<=122) { cout<<"your character is small latter\n"; } else if(a>=48&&a<=57) { cout<<"your character is any digit\n"; cout <<"character="<<a; } else { cout<<"your character have a special symbole\n" ; } getch(); } output enter any character 4 your character is any digit character=4

Page 5

PROGRAMMING IN C++

6/*w.a.p. to perform arithmetic operation*/


#include<conio.h> #include<iostream.h> void main() { clrscr(); int a,b,ch; float r=0; cout<<"enter any no.\n"; cin>>a>>b; cout<<"available choices\n"; cout<<"1.addition\n2.subtraction\n3.multiplication\n4.division\n"; cout<<"enter choice"; cin>>ch; switch(ch) { case 1: r=a+b; break; case 2: r=a-b; break; case 3: r=a*b; break; case 4: r=a/b; break; default:cout<<"invalid choice\n"; } cout<<"r="<<r; getch(); } output enter any no. 15 10 available choices 1.addition 2.subtraction 3.multiplication 4.division enter choice3

Page 6

PROGRAMMING IN C++

r=150

7/*w.a.p. to print the Fibonacci series*/


#include<conio.h> #include<iostream.h> void main() { int a=0,b=1,c=0,num; cout<<"enter the range"; cin>>num; cout<<a<<"\t"<<b<<"\t"; while(c<=num) { c=a+b; cout<<c<<"\t"; a=b; b=c; } getch(); } output enter the range19 0 1 1 2

13

21

Page 7

PROGRAMMING IN C++

8/*w.a.p. to print the factorial of a number*/


#include<conio.h> #include<iostream.h> void main() { clrscr(); int a,b=1; cout<<"enter the number"; cin>>a; while(a>=1) { b=b*a; a--; } cout<<b; getch(); } output enter the number5 120

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++

11/*w.a.p. to print ABCDEFFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A */


#include<conio.h> #include<iostream.h> void main() { clrscr(); int i; char j; for(i=0;i<6;i++) { for(j=65;j<=(70-i);j++) { cout<<j; } for(j=1;j<=(i*2);j++) { cout<<" "; } for(j=(70-i);j>=65;j--) { cout<<j; } cout<<"\n"; } getch(); } output ABCDEFFEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A

Page 11

PROGRAMMING IN C++

12/*w.a.p. to print the length of a string*/


#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i; char str[10]; cout<<"enter the string\n"; cin>>str; for(i=0;str[i]!='\0';i++); cout<<"length="<<i; getch(); } output enter the string sandhya length=7

Page 12

PROGRAMMING IN C++

13/*w.a.p. to copy a string to another string*/


#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i; char str[10],str1[15]; cout<<"enter the string\n"; cin>>str; for(i=0;str[i]!='\0';i++) { str1[i]=str[i]; } cout<<str1; getch(); } output enter the string sandhya sandhya

Page 13

PROGRAMMING IN C++

14/*w.a.p. to comparision of two string*/


#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i,flag=0; char str[10],str1[15]; cout<<"enter the 2 string\n"; cin>>str>>str1; for(i=0;str[i]!='\0';i++) { if(str[i]!=str1[i]) { cout<<"string not equal"; flag=1; break; } } if(flag==0) { cout<<"str are equal"; } getch(); } output enter the 2 string sandhya kalra string not equal

Page 14

PROGRAMMING IN C++

15/*w.a.p. to concatenation of a two string*/


#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i,j; char str[10],str1[10],str2[20]; cout<<"enter the 2 string\n"; cin>>str>>str1; for(i=0;str[i]!='\0';i++) { str2[i]=str[i]; } for(j=0;str1[j]!='\0';j++) { str2[i]=str1[j]; i++; } str2[i]='\0'; cout<<str2; getch(); } output enter the 2 string sandhya kalra sandhyakalra

Page 15

PROGRAMMING IN C++

16/*w.a.p. to reverce a string*/


#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i,j,k; char str[10],str1[10]=" "; cout<<"enter the string\n"; cin>>str; for(i=0;str[i]!='\0';i++); i--; k=i; for(j=0;j<=k;j++,i--) { str1[i]=str[j]; } cout<<str1; getch(); } output enter the string sandhya ayhdnas

Page 16

PROGRAMMING IN C++

17/*w.a.p. to check either the string is palindrome or not*/


#include<iostream.h> #include<conio.h> #include<string.h> void main() { clrscr(); int i,j,k,flag=0; char str[10],str1[10]=" "; cout<<"enter the string\n"; cin>>str; for(i=0;str[i]!='\0';i++); i--; k=i; for(j=0;j<=k;j++,i--) { str1[j]=str[i]; } str1[j]='\0'; for(i=0;str1[i]!='\0';i++) { if(str1[i]!=str[i]) { cout<<"string not palindrome"; flag=1; break; } } if(flag==0) { cout<<"string are palindrome"; } getch(); } output enter the string naman string are palindrome

Page 17

PROGRAMMING IN C++

18/*w.a.p. to print an array in an ascending order*/


#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,arr[10],j,temp; for(i=0;i<10;i++) { cout<<"enter any number"; cin>>arr[i]; } for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(arr[i]>arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } for(i=0;i<10;i++) { cout<<arr[i]<<"\n"; } getch(); }

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

19/*w.a.p. to print the value & there location*/

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

20/*w.a.p. to print the value & there location*/


#include<iostream.h>

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

21/*w.a.p. simple program to create a function*/


#include<iostream.h> #include<conio.h>

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

22/*w.a.p. to create a function & display output from main fun*/


#include<iostream.h> #include<conio.h> int length()

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

23/*w.a.p. to create a function & passing the parameters*/


#include<iostream.h> #include<conio.h> int length(char arr[20])

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

24/*w.a.p. to passing the values as reference*/


#include<iostream.h> #include<conio.h> int length(char *arr)

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

25/*w.a.p. to swap the values using reference passing*/


#include<iostream.h> #include<conio.h> int swap(int *a,int *b) {

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

28/*w.a.p. using structure accept & display the employee information*/


#include<iostream.h> #include<conio.h> struct employee { int emp_id; char emp_n[15],emp_dep[15];

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 101 name &

id 102 name &

id 103 name &

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

30/* create a class of employee,accept& display the information*/


#include<iostream.h> #include<conio.h> class employee { private: int id; char name[20],dept[20]; public:

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

33/*declaration of constructor & destructor*/


#include<iostream.h> #include<conio.h> class demo { public: demo() { cout<<"constructor called\n"; }

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

34/* declaring a friend function*/


#include<iostream.h> #include<conio.h> class demo { void display() { cout<<"function from class\n"; } friend void show();

Page 32

PROGRAMMING IN C++

}; void show() { demo d; d.display(); } void main() { clrscr(); show(); getch(); } output function from class

Page 33

Das könnte Ihnen auch gefallen