C++ Programs

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C++ Programs as PDF for free.

More details

  • Words: 3,722
  • Pages: 67
C++ LAB ASSIGNMENT

VRINDA KAUSHAL 2ND SEMESTER SECTION - A BBA (CAM) 1031211908

1. TO PRINT “HELLO” :-

#include #include void main() { clrscr(); cout<<" HELLO"; getch(); }

2. TO PRINT A=10 :#include #include void main() { clrscr(); int a=10; cout<
3. TO PRINT “A=10” :#include #include void main() { clrscr(); int a=10; cout<<"a="<
4. TO PRINT CHARACTER :#include #include void main() { clrscr(); char a='z'; cout<
5. TO PRINT FLOAT VALUE :#include #include void main() { clrscr(); float a=26935.635; cout<
6. TO PRINT THE VALUE OF TWO NUMBERS :#include

#include void main() { clrscr(); int a,b; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; getch(); }

7. TO PRINT THE SUM OF TWO NUMBERS :#include #include void main() { clrscr(); int a,b,c; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; c=a+b; cout<<"c="<
8. TO SWAP THE VALUES OF TWO NUMBERS USING THIRD VARIABLE :#include #include void main()

{ clrscr(); int a,b,c; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; c=a; a=b; b=c; cout<
9. TO SWAP THE VALUES OF TWO NUMBERS WITHOUT USING THIRD VARIABLE :#include #include void main() { clrscr(); int a,b; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; a=a+b; b=a-b; a=a-b; cout<
10.

TO ENTER BASIC SALARY OF AN EMPLOYEE :-

#include #include void main() { clrscr(); int basic,hra,pf,da,total; cout<<"Enter the value of basic"; cin>>basic; hra=basic*0.12; cout<<"hra="< #include void main() { clrscr(); int i=20,j=10,a,s,m,d; a=i+j; cout<<"a="< #include void main() { clrscr(); int basic; cout<<"Enter the employee basic"; cin>>basic; if(basic<5000) cout<<"B"; else cout<<"A"; getch(); }

14. TO PRINT THE BASIC SALARY WITH A, B OR C GRADES :#include #include void main() { clrscr(); int basic; cout<<"Enter the employee basic"; cin>>basic; if(basic<=5000) cout<<"C"; else if(basic>=5000 && basic<15000) cout<<"B"; else cout<<"A"; getch(); }

15. TO CHECK WHETHER THE GIVEN YEAR IS LEAP YEAR OR NOT:#include #include void main() { clrscr(); int year; cout<<"Enter the Year"; cin>>year; if(year%4==0) cout<<"Leap Year"; else cout<<"Not a Leap Year"; getch(); }

16. TO CHECK WHETHER THE GIVEN CHARACTER IS VOWEL OR CONSONANT :#include #include void main() { clrscr(); char z; cout<<"Enter the character of z"; cin>>z; if(z=='a'||z=='e'||z=='i'||z=='o'||z=='u') cout<<"Vowel"; else cout<<"Consonant"; getch(); }

17. TO PRINT THE TABLE :#include #include void main() { clrscr(); int x,y=1; cout<<"Enter the value of x"; cin>>x; while(y<=10) { cout<<x*y<<endl; y++; } getch(); }

18. TO PRINT THE NUMBERS FROM 1 TO 20 :#include #include void main() { clrscr(); int y=1; while(y<=20) { cout<
} getch(); }

19. TO SUM UP FROM 1 TO 20 AND PRINT IT :#include #include void main() { clrscr(); int y=1,sum=0; while(y<=20) { cout<>a; while(a!=0) { b=a%10; cout<
21. TO PRINT FROM 1 TO 50 WITH EVEN OR ODD :#include #include void main() {

clrscr(); int y=1; while(y<=50) { if(y%2==0) cout<<" Even"<<endl; else cout<<" Odd"<<endl; cout<
22.TO CHECK THE CONDITION OF WHILE :#include #include void main() { clrscr(); int a=12; do { cout<
23. TO PRINT FROM 1 TO 10 USING WHILE LOOP :#include #include void main() { clrscr(); int a=1; do { cout<
24. TO CHECK WHATHER THE NUMBER IS EVEN OR ODD :#include #include void main() { clrscr(); int n; cout<<"Enter the value of n"; cin>>n; if(n%2==0) cout<<"Even number"; else

cout<<"Odd number"; getch(); }

25. TO PRINT THE TOTAL OF A NUMBER :#include #include void main() { clrscr(); int a,b,sum=0; cout<<"Enter the value of a"; cin>>a; while(a!=0) { b=a%10; sum=sum+b; a=a/10; } cout<<sum; getch(); }

26.TO PRINT FROM A TO Z USING FOR :#include #include void main() { clrscr(); char a; for(a=65;a<=90;a++)

{ cout<
27. TO PRINT A NUMBER TRIANGLE :#include #include void main() { clrscr(); int a,b; for(a=1;a<=6;a++) { for(b=1;b<=a;b++) { cout<
28. TO PRINT TABLE USING FOR LOOP :#include #include void main() { clrscr(); int a,b; cout<<"Enter the value of a"; cin>>a; for(b=1;b<=10;b++) { cout<
29. TO CHECK WHETHER THE NUMBER IS EVEN OR ODD AND ASK TO CONTINU FURTHER OR NOT :#include #include void main() { clrscr(); int a; char z;

do { cout<<"Enter the value of a"; cin>>a; if(a%2==0) cout<<"Even no."<<endl; else cout<<"Odd no."<<endl; cout<<"Do you want to continue Y/N"; cin>>z; } while(z=='y'||z=='Y'); getch(); }

30. TO PRINT FROM Z TO A :#include #include void main() { clrscr(); char z; for(z=90;z>=65;z--) { cout<
31. TO PRINT A TRIANGLE OFCHARACTERS :#include #include void main() { clrscr(); char a; int i; for(i=0;i<=6;i++) { for(a=65;a<=70-i;a++) { cout<
32. TO PRINT CHARACTERS - DOWNWARD TRIANGLE :#include #include void main() { clrscr(); char a; int i,j; for(i=0;i<=6;i++) { for(a=65;a<=70-i;a++) { cout<=65;a--) { cout<
33. TO PRINT CHARACTERS – UPWARD TRIANGLE :#include #include void main() { clrscr(); char a; int i,j; for(i=6;i>=0;i--) { for(a=65;a<=70-i;a++) {

cout<0;j--) { cout<<" "; } for(a=70-i;a>=65;a--) { cout<
34. TO SUM UP THE NUMBERS AND PRINT THEM :#include #include void main() { clrscr(); int i,a=0,b=1,c; cout<
35. TO PRINT LEFTSIDE TRIANGLE OF NUMBERS :#include #include void main() { clrscr(); int i,j,k,l; for(i=1;i<=6;i++) { for(j=1;j<=i;j++) { cout<<j; } cout<<endl; } for(k=5;k>=1;k--) { for(l=1;l<=k;l++) { cout<
36. TO PRINT THE NUMBERS AS A TRIANGLE AND OF ONE KIND IN ONE ROW :#include #include void main() { clrscr(); int i,j; for(i=1;i<=6;i++) { for(j=1;j<=i;j++) { cout<
37. TO PRINT “HELLO” IN A CONTINUATION :#include #include void main() { clrscr(); int i,j;

for(i=1;i<=6;i++) { for(j=1;j<=i;j++) { cout<<"Hello"; } cout<<endl; } getch(); }

38. TO PRINT THE CHARACTERS AS A TRIANGLE AND OF ONE KIND IN ONE ROW :#include #include void main() { clrscr(); int i,j; char a=65; for(i=1;i<=6;i++) { for(j=1;j<=i;j++) { cout<
39. TO PRINT A STAR OF UPWARD TRIANGLE :#include #include void main() { clrscr(); int i,j,k; for(i=1;i<=6;i++) { for(j=i;j<=6;j++) { cout<<" "; } for(k=1;k<=i;k++) { cout<<" *"; } cout<<endl; } getch(); }

40. TO PRINT A STAR OF DOWNWARD TRIANGLE :#include #include void main() { clrscr(); int i,j,k; for(i=6;i>=1;i--)

{ for(j=i;j<=6;j++) { cout<<" "; } for(k=1;k<=i;k++) { cout<<" *"; } cout<<endl; } getch(); }

41. TO PRINT STARS AS A SHAPE OF DIAMOND :#include #include void main() { clrscr(); int a,b,c,d,e,f; for(a=1;a<=6;a++) { for(b=a;b<=6;b++) { cout<<" "; } for(c=1;c<=a;c++) { cout<<" *"; } cout<<endl; } for(d=5;d>=1;d--) { for(e=d;e<=6;e++) { cout<<" "; } for(f=1;f<=d;f++)

{ cout<<" *"; } cout<<endl; } getch(); }

42. TO PRINT NUMBER TRIANGLE :#include #include void main() { clrscr(); int a,b,c,d; for(a=1;a<=7;a++) { for(b=a;b<=7;b++) { cout<<" "; } for(c=1;c<=a;c++) { cout<=1;d--) { cout<
43. TO PRINT THE SHAPE OF A HUT :#include #include void main() { clrscr(); int a,b,c,d; for(a=1;a<=6;a++) { for(b=a;b<=6;b++) { cout<<" "; } for(c=1;c<=a;c++) { cout<<" *"; } cout<<endl; } for(d=1;d<=6;d++) { cout<<" * * * *"; cout<<endl; } getch(); }

44. TO PRINT THE SHAPE OF DOWNWARD TRIANGLE WITH NUMBERS :#include #include void main() { clrscr(); int a,b,c,d; for(a=0;a<=6;a++) { for(b=1;b<=6-a;b++) { cout<=1;d--) { cout<
45. TO PRINT TWO NUMBERS AND USE THE SWITCH CASE :#include #include void main() { clrscr(); int a,b,c; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b";

cin>>b; cout<<"Enter the case"; cin>>c; switch(c) { case 1: cout<
46. TO USE SWITCH CASE WITH DO – WHILE :#include #include void main() { clrscr(); char a,z; do { cout<<"Enter the case"; cin>>a; switch(a) { case 'A': cout<<"Sunday"; break; case'B': cout<<"Monday";

break; case 'C': cout<<"Tuesday"; break; case 'D': cout<<"Wednesday"; break; case 'E': cout<<"Thrusday"; break; case 'F': cout<<"Friday"; break; case 'G': cout<<"Saturday"; break; default: cout<<"No match case"; }

47. TO SHOW THE USE OF GOTO :#include #include void main() { clrscr(); int a,b; char c; x: cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; a=a+b; cout<<"\n\Do you want to continue Y/N"; cin>>c; if(c=='y'||c=='Y') goto x; getch(); }

48. TO DECLARE & DEFINE THE FUNCTIONS WITH NO ARGUMENTS AND NO RETURN VALUES. #include #include void add() { int a,b; cout<<"Enter the value of a & b"; cin>>a>>b; cout<>a; for(i=1;i<=10;i++) { cout<>a; if(a%2==0) { cout<<"Even no."<<endl; } else { cout<<"Odd n."<<endl; } } void main() { put(); getdata();

add(); getch(); }

49. USE OF SWICTH CASE WITH FUNCTIONS HAVING NO ARGUMENTS AND RETURN VALUE. #include #include #include<stdlib.h> void add() { int a,b; cout<<"Enter the value of a & b"; cin>>a>>b; cout<>a; for(i=1;i<=10;i++) { cout<>a; if(a%2==0) { cout<<"Even no."<<endl; }

else { cout<<"Odd n."<<endl; } } void main() { int z=0; clrscr(); while (z<5) { cout<<"1 add \n 2 getdata \n 3 put \n 4 exit \n"; cout<<"Enter the choice "; cin>>z; switch(z) { case 1: add(); break; case 2: getdata(); break; case 3: put(); break; case 4: exit(0); } getch(); } }

50. DECLARE & DEFINE FUNCTION WITH ARGUMENTS AND NO RETURN VALUE. #include #include sum (int x,int y); void main () { int a,b; cout<<"Enter the value of a & b"; cin>>a>>b; sum (a,b); getch(); } sum (int x, int y) { int p = x+y; cout<
51. DECLARE & DEFINE FUNCTION WITH ARGUMENTS AND RETURN VALUE. #include #include sum(int x,int y); void main () { int a,b; cout<<"Enter the value of a & b"; cin>>a>>b; sum(a,b); cout<<sum(a,b); } sum(int x,int y) { int p = x+y; return(p); }

52. CALLING OF A FUNCTION WITHIN ANOTHER FUNCTION. #include #include sum(int x,int y); sub(int x,int y); mul(int x,int y); div(int x,int y); void main() { clrscr(); int a,b,c; cout<<"Enter the value of a & b"; cin>>a>>b; c=sum(a,b); cout<<"Sum="< #include void main() { int a[5],i; for (i=0;i<=4;i++) { cout<<"Enter the elements"; cin>>a[i]; } for (i=4;i>=0;i--) {

cout<
55. TO FIND THE SUM OF ARRAYS. #include #include void main() { int a[5],i,sum=0; for (i=0;i<=4;i++) { cout<<"Enter the elements"; cin>>a[i]; } for (i=0;i<=4;i++) { sum=sum+a[i]; } cout<<sum; getch(); }

56. TO FIND EVEN AND ODD WITH THE HELP OF ARRAYS. #include #include void main() { int a[5],i;

for (i=0;i<=4;i++) { cout<<"Enter the elements"; cin>>a[i]; } for (i=0;i<=4;i++) { if(a[i]%2==0) { cout<<"Even no."<
57. SORT THE ORDER WITH THE HELP OF ARRAYS. #include #include void main() { int a[5],i,j,k; for (i=0;i<=4;i++) { cout<<"Enter the elements"; cin>>a[i]; } for (i=0;i<=4;i++) { for (j=i;j<=4;j++) { if(a[i]>a[j]) { k=a[i]; a[i]=a[j]; a[j]=k

} } } for (i=0;i<=4;i++) { cout<
58. SHOW THE USE OF TWO DIMENSIONAL ARRAYS. #include #include void main() { int a[3][3],i,j; for (i=0;i<3;i++) { for (j=0;j<3;j++) { cin>>a[i][j]; } } for (i=0;i<3;i++) { for (j=0;j<3;j++) { cout<
59. SHOW THE USE OF STRING IN ONE DIMENSIONAL ARRAY. #include #include void main() { char name[10]; cout<<"Enter the name"; cin>>name; cout<
60. SHOW THE USE OF STRINGS IN TWO DIMENSIONAL ARRAYS. #include #include void main() { char name[5][10]; int i; for(i=0;i<5;i++) { cout<<"Enter the name"; cin>>name[i]; } for(i=0;i<5;i++) { cout<
61. SHOW THE USE OF STRING LENGTH FUNCTION. #include #include #include<string.h> void main() { char name[10]; int l; cout<<"Enter the name"; cin>>name; l = str len(name); cout<
62. SHOW THE USE OF STRING COPY FUNCTION. #include #include #include<string.h> void main() { char z[10],y[10]; cout<<"Enter the string of z"; cin>>z; str cpy(y,z); cout<
63. COMPARE TO FUNCTIONS WITH THE HELP OF STRING COMPARE FUNCTION. #include #include #include<string.h> void main() { char z[10],y[10]; cout<<"Enter the string of z"; cin>>z; cout<<"Enter the string of y"; cin>>y; if(str cmp(z,y)==0) cout<<"String is equal"; else cout<<"String is not equal"; getch(); }

64. SHOW THE USE OF STRING REVERSE FUNCTION. #include #include #include<string.h> void main() { char z[10]; int *p; cout<<"Enter the name"; cin>>z; p =str rev(z) cout<
65. SHOW THE USE OF STRING CONCATENATION FUNCTION. #include #include #include<string.h> void main() { char z[10],y[10],p[20]; int *p; cout<<"Enter the string of z"; cin>>z; cout<<"Enter the string of y"; cin>>y; str cat(p,z) str cat(p,y) cout<
66. USE OF POINTER. #include #include #include<string.h> void main() { int a=10,*p,z; p = &a; z = *p; *p = 40; cout<<"a="< #include #include<string.h> void main() { int a=20,*p,z,**p1; p = &a; z = *p; p1 = &p; **p1 = 50; cout<<"a="< #include swap(int x,int y); void main() { int a,b; cout<<"Enter the value of a"; cin>>a; cout<<"Enter the value of b"; cin>>b; swap(&a , &b) getch(); } swap(int *x,int *y)

{ int p = *x; *x = *y; *y = p; cout<<"*x="<<*x<<endl; cout<<"*y="<<*y<<endl; }

69. TO DEFINE A STRUCTURE. #include #include void main() { struct emp { int code; char name[10]; }e; cout<<"Enter the emp code"; cin>>e.code; cout<<"Enter the emp name"; cin>>e.name; cout<<"Emp code="<>e.code; cout<<"Enter the emp name"; cin>>e.name; } for(i=0;i<5;i++) { cout<<"Emp code="<<e[i].code<<endl; cout<<"Emp name="<<e[i].name<<endl; } getch(); }

71. SHOW ARRAY OF POINTERS. #include #include void main() { int a=10, b=20, c=30, d=40, e=50; int *ar[5],i; ar[0] = &a; ar[1] = &b; ar[2] = &c; ar[3] = &d;

ar[4] = &e; for(i=0;i<=4;i++) { cout<<"Value="<<*ar[i]<<"Add="<<ar[i]<<endl; } getch(); }

72. TO DEFINE A STRUCTURE WITHIN ANOTHER STRUCTURE. #include #include void main() { struct emp { int code; char name[10]; }; struct student int roll no., char name[10]; struct emp e; }s; cout<<"Enter the student roll no."; cin>>s.roll no.; cout<<"Enter the student name"; cin>>s.name; cout<<"Enter the emp code"; cin<<s.e.code; cout<<"Enter the emp name"; cin>>s.e.name; cout<<"St. Rollno.="<<s.roll no.<<endl; cout<<"St. Name="<<s.name<<endl; cout<<"Emp Code="<<s.e.code<<endl; cout<<"Emp Name="<<s.e.name<<endl; getch(); }

73. TO DEFINE A STRUCTURE WITH THE USE OF ARRAYS. #include #include void main() { struct emp { int code; char name[10]; }; struct student int roll no., char name[10]; struct emp e; }s[5]; int i; for(i=0;i<5;i++) { cout<<"Enter the student roll no."; cin>>s.roll no.; cout<<"Enter the student name"; cin>>s.name; cout<<"Enter the emp code"; cin<<s.e.code; cout<<"Enter the emp name"; cin>>s.e.name; } for(i=0;i<5;i++) { cout<<"St. Rollno.="<<s.roll no.<<endl; cout<<"St. Name="<<s.name<<endl; cout<<"Emp Code="<<s.e.code<<endl; cout<<"Emp Name="<<s.e.name<<endl; } getch(); }

74.TO DECLARE A CLASS. #include #include class emp { int code; char name[10]; public: void getdata() { cout<<"Enter the emp code"; cin<>name; } void put() { cout<<"Emp Code="<>name; } void put() { cout<<"Emp Code="<
77. TO CREATE A CONSTRUCTOR. #include #include class A { int a; public: A() { a=10; } void show() { cout<
} }; void main() { A a2; a2.show(); getch(); }

78. TO CREATE A CONSTRUCTOR WITH THE ARGUMENT. #include #include class A { int a; public: A(int x) { a= x; } void show() { cout<
79. TO CREATE A DESTRUCTOR. #include

#include class A { int a; public: A(int x) { a= x; } void show() { cout<
80. TO CREATE A FRIEND FUNCTION. #include #include class B; class A { int a; public: A() { a=10; } void friend show(A,B); }; class B { int z; public: B()

{ z=20; } void friend show(A,B); }; void show(A a2, B b2) { cout<
81. TO CREATE A FUNCTION MEMBER OVERLOADING. #include #include class A { public: void add() { cout<<"Rahul"<<endl; } void add(int x) { cout<<x<<endl; } void add(int x, int y) { cout<<x+y; } }; void main() { A a2; a2.add(); a2.add(10); a2.add(10,20); getch();

}

82. TO CREATE A CONSTRUCTOR OVERLOADING WITH ARGUMENTS. #include #include class A { public: A (char *p) { cout<
83. TO CREATE A CONSTRUCTOR OVERLOADING WITHOUT ARGUMENTS.

#include #include class A { public: A (char *p) { cout<
84. TO CREATE A OPERATOR OVERLOADING. #include #include class A { public: int a; A() { a = 10; } void operator ++() { a++; } void show()

{ cout<
85. TO CREATE A STATIC DATA MEMBERS. #include #include class A { static int a; public: void add() { a++; } void show() { cout<
86. TO CREATE A SINGLE-LEVEL INHERITANCE. #include #include class A { public: void show() { cout<<"Class A"<<endl; } }; class B: class A { public: void put() { cout<<”Class B”; } }; void main() { B b2; b2.show(); b2.put(); getch(); }

87. TO CREATE A MULTI-LEVEL INHERITANCE. #include #include class A { public: void show() { cout<<"Class A"<<endl;

} }; class B: class A { public: void show1() { cout<<”Class B”<<endl; } }; class C : public B { public: void show2() { cout<<”Class C”; } }; void main() { C e; e.show(); e.show1(); e.show2(); getch(); }

88. TO CREATE A MULTIPLE-LEVEL INHERITANCE. #include #include class A { public: void show() { cout<<"Class A"<<endl; } }; class B { public: void show1()

{ cout<<”Class B”<<endl; } }; class C : public A, public B { public: void show2() { cout<<”Class C”; } }; void main() { C e; e.show(); e.show1(); e.show2(); getch(); }

89. TO CREATE A HYBRID INHERITANCE. #include #include class A { public: void show() { cout<<"Class A"<<endl; } }; class B: class A { public: void get() { cout<<”Class B”<<endl; } }; class C : public A {

public: void put() { cout<<”Class C”<<endl; } }; void main() { B b2; C c1; b2.show(); b2.get(); c2.put(); getch(); }

90. TO CREATE A HIERARCHIAL INHERITANCE. #include #include class A { int a; public: void add() { cout<<"Enter the value of a”; cin>>a; } }; void sum() { cout<
cin rel="nofollow">>a>>b; } }; class C : public virtual A { protected: float p; public: void getdata() { cout <<"Enter the value of p”; cin>>p; } }; class D: public B , public C { int total; public: void display() { total=a+b+p; cout<
91. TO SHOW POLYMORPHISM. #include #include class A { public: void virtual show()

{ cout<<"class A”<<endl; } }; class B : public A { public: void show() { cout <<"class B”<<endl; } }; class C : public A { public: void show () { cout <<"class C ”<<endl; } }; void main() { A *p; B b1; C c1; p = &b1 p → show(); p = &c1; p → show(); getch(); }

92. TO CREATE A PURE VIRTUAL FUNCTION. #include #include class A { public: void virtual show() = 0; }; class B : public A {

public: void show() { cout <<"class B”<<endl; } }; class C : public A { public: void show () { cout <<"class C ”<<endl; } }; void main() { A *p; B b1; C c1; p = &b1 p → show(); p = &c1; p → show(); getch(); }

93. TO SHOW THE USE OF GET(). #include #include void main() { char a; cout<<”Enter the char of a”; cin.get(a); cout<
94. TO SHOW THE USE OF PUT(). #include #include void main() { char a = ‘z’; cin.get(a); cout<
95. TO SHOW THE USE OF GETLINE(). #include #include void main() { char name[10]; cout<<”Enter the name”; cin.getline(name,10); cout<
96. TO SHOW THE USE OF WRITE(). #include #include void main()

{ char name[10] = “RAHUL”; cout.write(name,10); getch(); }

Related Documents

C Programs
April 2020 23
C++ Programs
November 2019 25
C++ Programs
June 2020 21
C Programs
November 2019 40