0% found this document useful (0 votes)
5 views43 pages

CPP Ppractical CPP

The document provides a comprehensive overview of basic C++ programming concepts, including printing to the console, using operators, control structures like if-else and switch-case, loops, arrays, string operations, and functions. It also covers advanced topics such as recursion, constructors, destructors, and friend functions. Each section includes code examples and expected output to illustrate the concepts effectively.

Uploaded by

g.k.gamerz2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views43 pages

CPP Ppractical CPP

The document provides a comprehensive overview of basic C++ programming concepts, including printing to the console, using operators, control structures like if-else and switch-case, loops, arrays, string operations, and functions. It also covers advanced topics such as recursion, constructors, destructors, and friend functions. Each section includes code examples and expected output to illustrate the concepts effectively.

Uploaded by

g.k.gamerz2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1|Page

[Link] print “hello world”

#include <iostream>
using namespace std;

int main()
{
cout << "Hello, World!";
return 0;
}
OUTPUT :

Hello, World!

2|Page
[Link] arithmetic, relational and logical
operators.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;

// Arithmetic Operators
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Modulus: " << a % b << endl;

3|Page
// Relational Operators
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a > b: " << (a > b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a >= b: " << (a >= b) << endl;
cout << "a <= b: " << (a <= b) << endl;
// Logical Operators
cout << "Logical AND (a>5 && b>2): " << (a >
5 && b > 2) << endl;
cout << "Logical OR (a>5 || b<2): " << (a > 5
|| b < 2) << endl;
cout << "Logical NOT !(a>b): " << !(a > b) <<
endl;
return 0;}

4|Page
OUTPUT :
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= b: 1
a <= b: 0
Logical AND (a>5 && b>2): 1
Logical OR (a>5 || b<2): 1
Logical NOT !(a>b): 0

5|Page
[Link] show the use of ‘IF ELSE’
Statement
#include <iostream>
using namespace std;
int main() {
int num;
cout<<"Enter any number :";
cin >> num;
if (num % 2 == 0)
cout << "Number is Even";
else
cout << "Number is Odd";
return 0;}

6|Page
OUTPUT :

Enter any number :7


Number is Odd

7|Page
4. To show the use of nested if
statement :
#include <iostream>
using namespace std;

int main() {
int num;
cout<<"Enter any number :";
cin >> num;

if (num > 0) {
if (num % 2 == 0)
cout << "Positive Even Number";
else
cout << "Positive Odd Number";

8|Page
} else {
cout << "Negative Number";
}
return 0;
}

OUTPUT :

Enter any number :7


Positive Odd Number

9|Page
5. Program to show the “switch case”
statement :

#include<iostream>
using namespace std;
int main() {
int day;

cout << "Enter number (1-7): ";


cin >> day;

switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;

10 | P a g e
case 4: cout << "Thursday"; break;
case 5: cout << "Friday"; break;
case 6: cout << "Saturday"; break;
case 7: cout << "Sunday"; break;
default: cout << "Invalid input";
}

return 0;
}
Output :

Enter number (1-7): 6


Saturday

=== Code Execution Successful ===

11 | P a g e
6. To show thee use of “while loop” ,
“do while” and “for loop” :

#include<iostream>
using namespace std;
int main() {
// FOR LOOP
cout << "For loop:\n";
for(int i = 1; i <= 10; i++) {
cout << i << " ";}

// WHILE LOOP
cout << "\nWhile loop:\n";
int i = 1;
while(i <= 10) {

12 | P a g e
cout << i << " ";
i++; }

// DO-WHILE LOOP
cout << "\nDo-while loop:\n";
i = 1;
do {cout << i << " ";
i++;
} while(i <= 10);
return 0; }
Output :
For loop:
1 2 3 4 5 6 7 8 9 10
While loop:
1 2 3 4 5 6 7 8 9 10

13 | P a g e
Do-while loop:
1 2 3 4 5 6 7 8 9 10

=== Code Execution Successful ===

7. program for 2-D array :


#include<iostream>
using namespace std;

int main() {
int a[2][2], b[2][2], sum[2][2];

cout << "Enter first 2x2 matrix:\n";


for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {

14 | P a g e
cin >> a[i][j];
}
}

cout << "Enter second 2x2 matrix:\n";


for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cin >> b[i][j];
}
}

// Addition
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];

15 | P a g e
}
}

cout << "Sum of matrices:\n";


for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cout << sum[i][j] << " ";
}
cout << endl;
}

return 0;
}

16 | P a g e
Output :

Enter first 2x2 matrix:


12
34
Enter second 2x2 matrix:
34
45
Sum of matrices:
46
79

=== Code Execution Successful ===

17 | P a g e
8. Program to show “string” operation :

#include<iostream>
using namespace std;

int main() {
string str;

cout << "Enter a string: ";


cin >> str;

cout << "You entered: " << str;

return 0;
}
18 | P a g e
Output :

Enter a string: hello


You entered: hello

=== Code Execution Successful ===

19 | P a g e
9. Program to swapping to two
Numbers( call by value) :

#include<iostream>
using namespace std;

void swap(int a, int b) {


int temp;
temp = a;
a = b;
b = temp;

cout << "Before swapping : " << a << " " << b
<< endl;
}
20 | P a g e
int main() {
int x = 5, y = 10;
swap(x, y);

cout << "After swapping: " << x << " " << y <<
endl;
return 0;}

Output :
Before swapping : 10 5
After swapping: 5 10

=== Code Execution Successful ===

21 | P a g e
10. Program to swapping to two
Numbers using third variable (call by
reference) :

#include<iostream>
using namespace std;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;

22 | P a g e
cout << "Before swapping: " << x << " " << y
<< endl;

swap(x, y);
cout << "After swapping: " << x << " " << y <<
endl;
return 0;
}

Output :
Before swapping: 5 10
After swapping: 10 5

=== Code Execution Successful ===

23 | P a g e
11. Program to swapping to two
Numbers without using third variable
( call by value) :

#include<iostream>
using namespace std;
void swapValue(int a, int b) {
// Swap without third variable
a = a + b;
b = a - b;
a = a - b;

cout << "Before swapping: " << a << " " << b
<< endl;}

24 | P a g e
int main() {
int x = 5, y = 10;
swapValue(x, y);
cout << "After swapping: " << x << " " << y <<
endl;
return 0;
}
Output :

Before swapping: 10 5
After swapping: 5 10

=== Code Execution Successful ===

25 | P a g e
12. Write a program to find factorial of a
number (recursion) :

#include<iostream>
using namespace std;

int fact(int n) {
if(n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}

int main() {
int n;

26 | P a g e
cout << "Enter a number: ";
cin >> n;
cout << "Factorial = " << fact(n);

return 0;
}

Output :
Enter a number: 5
Factorial = 120

=== Code Execution Successful ===

27 | P a g e
13. Write a program to print Fibonacci
Series :

#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int terms;
cout << "Enter the number of terms: ";
28 | P a g e
cin >> terms;
cout << "Fibonacci Series: ";
for (int i = 0; i < terms; i++) {
cout << fibonacci(i) << " ";
}
cout << endl;
return 0;
}
Output :

Enter the number of terms: 6


Fibonacci Series: 0 1 1 2 3 5

=== Code Execution Successful ===

29 | P a g e
14. Program for structure :

#include<iostream>
using namespace std;
struct Student {
int Rollno;
string name;
};
int main() {
Student s;
[Link] = 12;
[Link] = "Gulshan";
cout << "ID: " << [Link] << endl;
cout << "Name: " << [Link];
return 0;}
30 | P a g e
Output :

ID: 12
Name: Gulshan

=== Code Execution Successful ===

31 | P a g e
15. Program for constructor and deconstructor
(defalt) :

#include<iostream>
using namespace std;
class Test {
public:
Test() {
cout << "Default Constructor";
}
};
int main() {
Test t;
return 0;
}

32 | P a g e
Output :

Default Constructor

=== Code Execution Successful ===

33 | P a g e
16. Program for constructor and deconstructor
(Parameterized constructor ) :

#include<iostream>
using namespace std;

class Test {
public:
int a;

Test(int x) {
a = x;
}
};

34 | P a g e
int main() {
Test t(10);
cout << t.a;
return 0;
}

Output :

10

=== Code Execution Successful ===

35 | P a g e
17. Program for constructor and deconstructor
( copy constructor ) :

#include<iostream>
using namespace std;

class Test {
public:
int a;
Test(int x) {
a = x;
}
// Copy constructor
Test(const Test &t) {
a = t.a;

36 | P a g e
}
};

int main() {
Test t1(10);
Test t2 = t1;
cout << t2.a;
return 0;
}

Output :

10

=== Code Execution Successful ===

37 | P a g e
18. Program to show Global and Local variable :

#include<iostream>
using namespace std;

int x = 10; // Global variable

int main() {
int x = 5; // Local variable

cout << "Local x = " << x << endl;


cout << "Global x = " << ::x;

return 0;
}

38 | P a g e
Output :

Local x = 5
Global x = 10

=== Code Execution Successful ===

39 | P a g e
19. To show different datatype used in c++ :

#include<iostream>
using namespace std;
int main() {
int a = 10; // integer
float b = 5.5; // float
char c = 'A'; // character
bool d = true; // boolean

cout << "Integer: " << a << endl;


cout << "Float: " << b << endl;
cout << "Character: " << c << endl;
cout << "Boolean: " << d << endl;

40 | P a g e
return 0;
}

Output :

Integer: 10
Float: 5.5
Character: A
Boolean: 1

=== Code Execution Successful ===

41 | P a g e
20. Program for Friend Function :

#include<iostream>
using namespace std;
class A {
int x;
public:
A() {
x = 10;
}
// Declare friend function
friend void show(A obj);
};

// Define friend function

42 | P a g e
void show(A obj) {
cout << "Value of x = " << obj.x;
}
int main() {
A a;
show(a);
return 0;
}

Output :

Value of x = 10

=== Code Execution Successful ===

43 | P a g e

You might also like