0% found this document useful (0 votes)
2 views31 pages

C++ Practical File

The document contains a practical file for C++ programming with 19 different practical exercises. Each exercise includes a problem statement, a solution in C++ code, and an expected output. Topics covered include basic input/output, operators, control structures, functions, classes, inheritance, polymorphism, file handling, and structures.

Uploaded by

abomxd3832
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)
2 views31 pages

C++ Practical File

The document contains a practical file for C++ programming with 19 different practical exercises. Each exercise includes a problem statement, a solution in C++ code, and an expected output. Topics covered include basic input/output, operators, control structures, functions, classes, inheritance, polymorphism, file handling, and structures.

Uploaded by

abomxd3832
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

CPP Practical File

Practical 1:
Write a Program to print “Hello World” and “Hello Name” where
name is the input from the user.

Solution
#include <iostream>
#include <string>
using namespace std;

int main() {
string name;
cout << "Enter your name: ";
cin>>name;
cout << "Hello World" << endl;
cout << "Hello " << name << endl;
return 0;
}
OUTPUT
Practical 2:
WAP to demonstrate the working of operators and operator precedence in cpp

SOLUTION
#include<iostream>
#include <string>
using namespace std;

int main() {
int a = 10, b = 3;
bool x = true, y = false;
int add = a + b;
int sub = a - b;
int mult = a * b;
int div = a / b;
int modulu = a % b;
int inc = a++;
int dec = b--;

cout << "Use of Some Operators Present in cpp" << endl;


cout << "a + b = " << add << endl;
cout << "a - b = " << sub << endl;
cout << "a * b = " << mult << endl;
cout << "a / b = " << div << endl;
cout << "a % b = " << modulu << endl;
cout << "a++ = " << inc << endl;
cout << "b-- = " << dec << endl;
cout << endl;
int num1 = 10, num2 = 5, num3 = 2;
int result_precedence = num1 + num2 * num3;
int result_parentheses = (num1 + num2) * num3;
// Print the results
cout << "Original Expression (num1 + num2 * num3): " <<
result_precedence << endl;
cout << "Expression with Parentheses ((num1 + num2) * num3): " <<
result_parentheses << endl;

return 0;
}
OUTPUT
PRACTICAL 3:
WAP to create a Grading System with the help of nested and ladder
if/else statement.

SOLUTION:
#include <iostream>
using namespace std;

int main(){

int marks;
cout << "Enter the marks less than 100 or equal to 100";
cin >> marks;

if( marks <= 100){


if(marks <= 100 && marks > 90){
cout<<"Grade A";
}else if(marks <=90 && marks >80){
cout<<"Grade B";
}else if(marks <=80 && marks > 70){
cout<<"Grade C";
}else if(marks <=70 && marks >60){
cout << "Grade D";
}else{
cout<< "Marks are less than 60";
}
}else{
cout<<" Marks are greater than 100 no grade calculated";
}
return 0;
}
OUTPUT
PRACTICAL 4:
WAP to print the even numbers from 1 to 20 using while loop and if
statement.
SOLUTION:

#include <iostream>
using namespace std;
int main() {
int i = 2;
cout<<"Even Numbers from 1 to 20 are :"<<endl;
while (i <= 20) {
if(i % 2 == 0)
cout << i << " ";
i++;
}
cout << endl;
return 0;
}

OUTPUT:
PRACTICAL 5:
WAP to print right angle triangle Pattern using for loop:
SOLUTION:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
OUTPUT:
PRACTICAL 6:
WAP to print Fibonacci Series of given below
0 1 1 2 3 5 8 13 21 34
SOLUTION:
#include<iostream>
using namespace std;
int main(){
int num = 10;
int prv=0;
int crr=1;
int nxt = 0;
cout<<"Fibonacci Series is: "<<endl;
cout << prv << " " << crr << " ";
for(int i = 2;i<num;i++){
nxt = prv + crr;
cout << nxt << " ";
prv = crr;
crr = nxt;
}
}
OUTPUT
PRACTICAL 7:
WAP to implement basic array operations in cpp
SOLUTION:
#include<iostream>
using namespace std;
int main(){
//Declaring and Initializing an array
int arr[5] = {1, 2, 3, 4, 5};
// Traversing through the Array
cout<<"Traversing Array using for loop"<<endl;
for(int i=0;i<5;i++){
cout << arr[i] << " ";
}
cout<<endl;
// Changing Value of an element in array using index
arr[1] = 10;
cout<<"Traversing Array again after changing element of an array"<<endl;
for(int i=0;i<5;i++){
cout << arr[i] << " ";
}
cout<<endl;
}
OUTPUT:
PRACTICAL 8:
WAP to check whether a given string is Palindrome or not
SOLUTION
#include <iostream>
using namespace std;

bool isPalindrome(const std::string& str) {


int left = 0;
int right = [Link]() - 1;

while (left < right) {


if (str[left] != str[right]) {
return false; // Mismatch found, not a palindrome
}
left++;
right--;
}

return true; // No mismatches found


}

int main() {
string s1 = "level";
string s2 = "world";
if (isPalindrome(s1)) {
cout << "\"" << s1 << "\" is a palindrome." << endl;
} else {
cout << "\"" << s1 << "\" is not a palindrome." << endl;
}

if (isPalindrome(s2)) {
cout << "\"" << s2 << "\" is a palindrome." << endl;
} else {
cout << "\"" << s2 << "\" is not a palindrome." << endl;
}

return 0;
}

OUTPUT
PRACTICAL 9:
WAP to implement multiple ADD functions with the help of function
overloading.

SOLUTION
#include <iostream>
using namespace std;

// Function to add two integers


int add(int a, int b) {
return a + b;
}

// Function to add two doubles


double add(double a, double b) {
return a + b;
}

// Function to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

int main() {
cout<<"Calling a function to add two numbers" << add(5, 3) <<
endl; // Calls add(int, int)
cout<<"Calling a function to add two pointing numbers" <<
add(5.5, 3.3) << endl; // Calls add(double, double)
cout<<"Calling a function to add three numbers" << add(1, 2, 3)
<< endl; // Calls add(int, int, int)
return 0;
}
OUTPUT
PRACTICAL 10:
WAP to read and print the student details using class and object

SOLUTION
#include<iostream>
using namespace std;

class StudentClass {
private:
string name;
int regNo, sub1, sub2, sub3;
float total, avg;
public:
void read(string pname,int pregno, int psub1, int psub2, int
psub3) {
this -> name = pname;
this -> regNo = pregno;
this -> sub1 = psub1;
this -> sub2 = psub2;
this -> sub3 = psub3;
}
void sum() {
total =sub1 +sub2 +sub3;
avg =total / 3;
}
void display(){
cout<<endl<<"Student Details are as follow :- "<<endl;
cout <<"Name :" <<name <<endl;
cout <<"Registration Number :" <<regNo <<endl;
cout <<"Marks :" <<sub1 <<" , " <<sub2 <<" , " <<sub3 <<endl;
cout <<"Total :" <<total <<endl;
cout <<"Average :" <<avg <<endl;
}
};

int main() {
StudentClass stu1;
cout <<"Read and Print Student Information Class Example
Program In C++"<<math_errhandling;
[Link]("vaibhav", 1234, 85, 80, 90);
[Link]();
[Link]();
}
OUTPUT:
PRACTICAL 11:
WAP to implement the working of Constructor and Destructor.

SOLUTION
#include <iostream>
using namespace std;

class A {
private:
int val;

public:
// Parameterized constructor
A(int x) {
val = x;
}

// Copy constructor
A(A& a) {
val = [Link];
}
void display(){
cout<<val;
}
};
int main() {
A a1(20);
cout<<"Value stored in a1 object is ";
[Link]();
cout<<endl;

// Creating another object from a1


A a2(a1);
cout<<"Value stored in a2 object is ";
[Link]();
return 0;
}
OUTPUT
PRACTICAL 12:
WAP to demonstrate the working of operator overloading:
SOLUTION:
#include <iostream>
using namespace std;
class Count{
private:
int value = 5;
public:
void operator++(){
++value;
}
void display(){
cout<<"Count : " << value <<endl;
}
};
int main(){
Count cnt1;
++cnt1;
[Link]();
return 0;
}
OUTPUT:
PRACTICAL 13:
WAP to implement the concept of Single Inheritance
SOLUTION:
#include <iostream>
using namespace std;
class Parent {
public:
void Method1() {
cout << "This is the parent method." << endl;
}
};
class Child : public Parent {
public:
void Method2() {
cout << "This is the child method." << endl;
}
};
int main() {
Child Obj;
Obj.Method1();
Obj.Method2();
}
OUTPUT
PRACTICAL 14:
WAP to implement the concept of multi-level Inheritance
Solution:
#include <iostream>
using namespace std;
class Grandfather {
public:
void displayGrandfather() {
cout << "Grandfather" << endl;
}
};
class Father : public Grandfather {
public:
void displayFather() {
cout << "Father" << endl;
}
};
class Child : public Father {
public:
void displayChild() {
cout << "Child" << endl;
}
};
int main() {
Child child;
[Link]();
[Link]();
[Link]();
}

OUTPUT
PRACTICAL 15:
WAP to Implement the concept of Multiple Inheritance
Solution:
#include <iostream>
using namespace std;
class A {
public:
void methodA() {
cout << "Method from class A" << endl;
}
};
class B {
public:
void methodB() {
cout << "Method from class B" << endl;
}
};
class C : public A, public B {
public:
void methodC() {
cout << "Method from class C" << endl;
}
};
int main() {
C obj;
[Link]();
[Link]();
[Link]();
}

OUTPUT:
PRACTICAL 16:
WAP to Implement the Concept of Polymorphism
Solution:
#include <iostream>
using namespace std;
class Calculate{
public:
void display(int x, int y) {
cout<<"Method of Base class is called and value is "<<x +
y<<endl;
}
};
class Subtract : public Calculate{
public:
void display(int x, int y) {
cout<<"Method of Derived class is called with two variables
"<<x - y<<endl;
;
}
void display(int x, int y, int z) {
cout<<"Method of Derived class is called with three variables
"<<x - y - z<<endl;
;
}
};
int main() {
Subtract s;
[Link](50, 20);
[Link](50,20);
[Link](50,20, 10);
}
OUTPUT:
PRACTICAL 17:
WAP to implement the concept of pointer to function
SOLUTION:
#include <iostream>
using namespace std;

// A simple function to add two numbers


int add(int a, int b) {
return a + b;
}

int main() {
// Declare a function pointer 'funcPtr' that points to a function
// which takes two ints and returns an int
int (*funcPtr)(int, int);

// Assign the address of the 'add' function to the pointer


funcPtr = add; // The function name itself decays to its address

// Call the function using the pointer


int result1 = funcPtr(5, 3);
cout << "Result using funcPtr: " << result1 << endl; // Output: 8

// Alternatively, you can call it like this:


int result2 = (*funcPtr)(5, 3);
cout << "Result using (*funcPtr): " << result2 << endl; // Output: 8

return 0;
}
OUTPUT:
PRACTICAL 18:
WAP to Demonstrate the working of Structures in CPP
SOLUTION:
#include <iostream>
#include <string>

using namespace std;

// Define a structure named 'Student'


struct Student {
string name;
int roll_no;
float marks;
}; // Semicolon is important at the end of the structure definition

int main() {
// Declare a structure variable 's1' of type 'Student'
Student s1;

// Access and assign values to structure members using the dot (.)
operator
[Link] = "Alice";
s1.roll_no = 101;
[Link] = 92.5;
// Display the information
cout << "Student Information:" << endl;
cout << "Name: " << [Link] << endl;
cout << "Roll Number: " << s1.roll_no << endl;
cout << "Marks: " << [Link] << endl;

return 0;
}

OUTPUT
PRACTICAL 19:
WAP to Create and perform I/O operations on a file.
SOLUTION:
#include <iostream>
#include <fstream> // Required for file handling
#include <string>
using namespace std;
int main() {
// 1. Create and open a file for writing (and creating if it doesn't
exist)
// Using ofstream for output operations
ofstream outFile("[Link]"); // Opens the file in write mode,
overwriting content if it exists

// 2. Write data to the file using the insertion operator (<<)


outFile << "Hello, world!" << endl;
outFile << "This is a line written in C++ file handling." << endl;
cout << "Data successfully written to [Link]" << endl;

// 3. Close the output file


[Link]();

// 4. Open the file for reading


// Using ifstream for input operations
ifstream inFile("[Link]");
// 5. Read data from the file using getline or extraction operator
(>>)
string line;
cout << "\nReading from [Link]:" << endl;
while (getline(inFile, line)) { // Read the file line by line
cout << line << endl;
}
// 6. Close the input file
[Link]();
return 0;
}
OUTPUT:

Content written in new file created -> [Link]

You might also like