C++ PRACTICAL
1. Check Whether Number is Even or
Odd
2. Swap Two Numbers
3. Check Leap Year
4. Sort words in Dictionary order
5. Find Factorial of a given number
6. Fibonnacci Series of N numbers
7. Transpose of Matrix
8. Constructor And Destructor
9. Multiple Inheritance
10.Static members and functions
11.Exception Handling
12.File handling
INDEX
1. Check Whether Number is Even or
Odd
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
OUTPUT:
Enter an integer: 23
23 is odd.
2. Swap Two Numbers
#include <iostream>
using namespace std;
int main(){
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b
<< endl;
temp = a;
a = b;
b = temp;
cout << "n After swapping." <<
endl;
cout << "a = " << a << ", b = " << b
<< endl;
return 0;
}
OUTPUT:
Before swapping a = 5, b = 10
After swapping. a = 10, b = 5
3. Check Leap Year
#include <iostream>
using namespace std;
int main(){
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 4 == 0) {
if (year % 100 == 0){
if (year % 400 == 0)
cout << year << " is a
leap year.";
else
cout << year << " is not a
leap year.";
} else
cout << year << " is a leap
year.";
} else
cout << year << " is not a leap
year.";
return 0;
}
OUTPUT:
Enter a year: 2014
2014 is not a leap year.
4. Sort Words in Dictionary Order
#include <iostream>
using namespace std;
int main(){
string str[10], temp;
cout << "Enter 10 words: " << endl;
for(int i = 0; i < 10; ++i){
getline(cin, str[i]);
}
for(int i = 0; i < 9; ++i)
for( int j = i+1; j < 10; ++j){
if(str[i] > str[j]){
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
cout << "Sorted order is: " << endl;
for(int i = 0; i < 10; ++i){
cout << str[i] << endl;
}
return 0;
}
OUTPUT:
Enter 10 words:
C
C++
Java
Python
Perl
R
Matlab
Ruby
JavaScript
PHP
Sorted order is:
C
C++
Java
JavaScript
Matlab
PHP
Perl
Python
R
Ruby
5.Find Factorial of a given number
#include <iostream>
using namespace std;
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " <<
factorial;
return 0;
}
OUTPUT:
Enter a positive integer: 12
Factorial of 12 = 479001600
6. Fibonnacci Series of N numbers
#include <iostream>
using namespace std;
int main(){
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i){
// Prints the first two terms.
if(i == 1) {
cout << " " << t1;
continue;
}
if(i == 2){
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
return 0;
}
OUTPUT:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,
21, 34
7. Transpose of Matrix
#include <iostream>
using namespace std;
int main(){
int a[10][10], trans[10][10], r, c, i,
j;
cout << "Enter rows and columns of
matrix: ";
cin >> r >> c;
// Storing element of matrix entered by user in
array a[][].
cout << endl << "Enter elements of
matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter elements a" << i + 1
<< j + 1 << ": ";
cin >> a[i][j];
}
// Displaying the matrix a[][]
cout << endl << "Entered Matrix: " <<
endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j){
cout << " " << a[i][j];
if(j == c - 1)
cout << endl << endl;
}
// Finding transpose of matrix a[][] and storing it
in array trans[][].
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j){
trans[j][i]=a[i][j];
}
// Displaying the transpose,i.e, Displaying array
trans[][].
cout << endl << "Transpose of Matrix: "
<< endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{
cout << " " << trans[i][j];
if(j == r - 1)
cout << endl << endl;
}
return 0;
}
OUTPUT:
Enter rows and column of matrix: 2 3
Enter elements of matrix:
Enter elements a11: 1
Enter elements a12: 2
Enter elements a13: 9
Enter elements a21: 0
Enter elements a22: 4
Enter elements a23: 7
Entered Matrix:
1 2 9
0 4 7
Transpose of Matrix:
1 0
2 4
9 7
8. Constructor And Destructor
#include <iostream>
using namespace std;
class Arithmetic {
public:
Circle(){
cout << "Constructor called";
}
int sum(int a, int b){
return a + b;
}
~Circle(){
cout << "Destructor called";
}
};
int main() {
Arithmetic arth;
cout << “Sum is: ” << arth.sum(50 + 17)
<<endl;
return 0;
}
OUTPUT:
Constructor called
Sum is: 67
Destructor called
9. Multiple Inheritance
#include<iostream>
using namespace std;
class A{
public:
A() {
cout << "A's constructor
called" << endl;
}
};
class B {
CLASS A CLASS B
CLASS C
public:
B() {
cout << "B's constructor
called" << endl;
}
};
class C: public B, public A {
public:
C() {
cout << "C's constructor
called" << endl;
}
};
int main(){
C c;
return 0;
}
OUTPUT:
B's constructor called
A's constructor called
C's constructor called
10. Static members and functions
#include <iostream>
using namespace std;
class Box {
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0,
double h = 2.0) {
cout <<"Constructor called." <<
endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
int main(void) {
// Print total number of objects before creating
object.
cout << "Inital Stage Count: " <<
Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // Declare
box1
Box Box2(8.5, 6.0, 2.0); // Declare
box2
// Print total number of objects after creating
object.
cout << "Final Stage Count: " <<
Box::getCount() << endl;
return 0;
}
OUTPUT:
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2
11.Exception Handling
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
OUTPUT:
Division by zero condition!
12. File Handling
#include <fstream>
#include <iostream>
using namespace std;
int main () {
char data[100];
// open a file in write mode.
ofstream outfile;
outfile.open("afile.dat");
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile << data << endl;
cout << "Enter your age: ";
cin >> data;
cin.ignore();
// again write inputted data into the file.
outfile << data << endl;
// close the opened file.
outfile.close();
// open a file in read mode.
ifstream infile;
infile.open("afile.dat");
cout << "Reading from the file" << endl;
infile >> data;
// write the data at the screen.
cout << data << endl;
// again read the data from the file and display it.
infile >> data;
cout << data << endl;
// close the opened file.
infile.close();
return 0;
}
OUTPUT:
$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

C++ practical

  • 1.
  • 2.
    1. Check WhetherNumber is Even or Odd 2. Swap Two Numbers 3. Check Leap Year 4. Sort words in Dictionary order 5. Find Factorial of a given number 6. Fibonnacci Series of N numbers 7. Transpose of Matrix 8. Constructor And Destructor 9. Multiple Inheritance 10.Static members and functions 11.Exception Handling 12.File handling INDEX
  • 3.
    1. Check WhetherNumber is Even or Odd #include <iostream> using namespace std; int main(){ int n; cout << "Enter an integer: "; cin >> n; if ( n % 2 == 0) cout << n << " is even."; else cout << n << " is odd."; return 0; } OUTPUT: Enter an integer: 23 23 is odd.
  • 4.
    2. Swap TwoNumbers #include <iostream> using namespace std; int main(){ int a = 5, b = 10, temp; cout << "Before swapping." << endl; cout << "a = " << a << ", b = " << b << endl; temp = a; a = b; b = temp; cout << "n After swapping." << endl; cout << "a = " << a << ", b = " << b << endl; return 0; } OUTPUT: Before swapping a = 5, b = 10 After swapping. a = 10, b = 5
  • 5.
    3. Check LeapYear #include <iostream> using namespace std; int main(){ int year; cout << "Enter a year: "; cin >> year; if (year % 4 == 0) { if (year % 100 == 0){ if (year % 400 == 0) cout << year << " is a leap year."; else cout << year << " is not a leap year."; } else cout << year << " is a leap year."; } else cout << year << " is not a leap year."; return 0; } OUTPUT: Enter a year: 2014 2014 is not a leap year.
  • 6.
    4. Sort Wordsin Dictionary Order #include <iostream> using namespace std; int main(){ string str[10], temp; cout << "Enter 10 words: " << endl; for(int i = 0; i < 10; ++i){ getline(cin, str[i]); } for(int i = 0; i < 9; ++i) for( int j = i+1; j < 10; ++j){ if(str[i] > str[j]){ temp = str[i]; str[i] = str[j]; str[j] = temp; } } cout << "Sorted order is: " << endl; for(int i = 0; i < 10; ++i){ cout << str[i] << endl; } return 0; }
  • 7.
    OUTPUT: Enter 10 words: C C++ Java Python Perl R Matlab Ruby JavaScript PHP Sortedorder is: C C++ Java JavaScript Matlab PHP Perl Python R Ruby
  • 8.
    5.Find Factorial ofa given number #include <iostream> using namespace std; int main() { unsigned int n; unsigned long long factorial = 1; cout << "Enter a positive integer: "; cin >> n; for(int i = 1; i <=n; ++i) { factorial *= i; } cout << "Factorial of " << n << " = " << factorial; return 0; } OUTPUT: Enter a positive integer: 12 Factorial of 12 = 479001600
  • 9.
    6. Fibonnacci Seriesof N numbers #include <iostream> using namespace std; int main(){ int n, t1 = 0, t2 = 1, nextTerm = 0; cout << "Enter the number of terms: "; cin >> n; cout << "Fibonacci Series: "; for (int i = 1; i <= n; ++i){ // Prints the first two terms. if(i == 1) { cout << " " << t1; continue; } if(i == 2){ cout << t2 << " "; continue; } nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; cout << nextTerm << " "; } return 0; } OUTPUT: Enter the number of terms: 10 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
  • 10.
    7. Transpose ofMatrix #include <iostream> using namespace std; int main(){ int a[10][10], trans[10][10], r, c, i, j; cout << "Enter rows and columns of matrix: "; cin >> r >> c; // Storing element of matrix entered by user in array a[][]. cout << endl << "Enter elements of matrix: " << endl; for(i = 0; i < r; ++i) for(j = 0; j < c; ++j) { cout << "Enter elements a" << i + 1 << j + 1 << ": "; cin >> a[i][j]; } // Displaying the matrix a[][] cout << endl << "Entered Matrix: " << endl; for(i = 0; i < r; ++i) for(j = 0; j < c; ++j){ cout << " " << a[i][j]; if(j == c - 1) cout << endl << endl;
  • 11.
    } // Finding transposeof matrix a[][] and storing it in array trans[][]. for(i = 0; i < r; ++i) for(j = 0; j < c; ++j){ trans[j][i]=a[i][j]; } // Displaying the transpose,i.e, Displaying array trans[][]. cout << endl << "Transpose of Matrix: " << endl; for(i = 0; i < c; ++i) for(j = 0; j < r; ++j) { cout << " " << trans[i][j]; if(j == r - 1) cout << endl << endl; } return 0; } OUTPUT: Enter rows and column of matrix: 2 3 Enter elements of matrix: Enter elements a11: 1 Enter elements a12: 2
  • 12.
    Enter elements a13:9 Enter elements a21: 0 Enter elements a22: 4 Enter elements a23: 7 Entered Matrix: 1 2 9 0 4 7 Transpose of Matrix: 1 0 2 4 9 7
  • 13.
    8. Constructor AndDestructor #include <iostream> using namespace std; class Arithmetic { public: Circle(){ cout << "Constructor called"; } int sum(int a, int b){ return a + b; } ~Circle(){ cout << "Destructor called"; } }; int main() { Arithmetic arth; cout << “Sum is: ” << arth.sum(50 + 17) <<endl; return 0; } OUTPUT: Constructor called Sum is: 67 Destructor called
  • 14.
    9. Multiple Inheritance #include<iostream> usingnamespace std; class A{ public: A() { cout << "A's constructor called" << endl; } }; class B { CLASS A CLASS B CLASS C
  • 15.
    public: B() { cout <<"B's constructor called" << endl; } }; class C: public B, public A { public: C() { cout << "C's constructor called" << endl; } }; int main(){ C c; return 0; } OUTPUT: B's constructor called A's constructor called C's constructor called
  • 16.
    10. Static membersand functions #include <iostream> using namespace std; class Box { public: static int objectCount; // Constructor definition Box(double l = 2.0, double b = 2.0, double h = 2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; // Increase every time object is created objectCount++; } double Volume() { return length * breadth * height; } static int getCount() { return objectCount; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box };
  • 17.
    // Initialize staticmember of class Box int Box::objectCount = 0; int main(void) { // Print total number of objects before creating object. cout << "Inital Stage Count: " << Box::getCount() << endl; Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects after creating object. cout << "Final Stage Count: " << Box::getCount() << endl; return 0; } OUTPUT: Inital Stage Count: 0 Constructor called. Constructor called. Final Stage Count: 2
  • 18.
    11.Exception Handling #include <iostream> usingnamespace std; double division(int a, int b) { if( b == 0 ) { throw "Division by zero condition!"; } return (a/b); } int main () { int x = 50; int y = 0; double z = 0; try { z = division(x, y); cout << z << endl; }catch (const char* msg) { cerr << msg << endl; } return 0; } OUTPUT: Division by zero condition!
  • 19.
    12. File Handling #include<fstream> #include <iostream> using namespace std; int main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); cout << "Writing to the file" << endl; cout << "Enter your name: "; cin.getline(data, 100); // write inputted data into the file. outfile << data << endl; cout << "Enter your age: "; cin >> data; cin.ignore(); // again write inputted data into the file. outfile << data << endl; // close the opened file. outfile.close(); // open a file in read mode. ifstream infile; infile.open("afile.dat");
  • 20.
    cout << "Readingfrom the file" << endl; infile >> data; // write the data at the screen. cout << data << endl; // again read the data from the file and display it. infile >> data; cout << data << endl; // close the opened file. infile.close(); return 0; } OUTPUT: $./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9