Lab With Solution
Lab With Solution
#include<iostream>
using namespace std;
int main( ) {
int s,n,n1;
s=0; //initialise s to zero
cout <<"\n enter the number : ";
do {
n1=n%10; // get remainder
s=s+n1; // add remainder to sum
n=n/10; // get quotient to reduce number by one digit
} while (n!=0); //check no of digit, if zero exit loop
#include <iostream>
int main( ) {
int r, n;
do {
r=n%10; // get remainder
n=n/10; //reduce digit by one digit
cout <<r; //print remainder
} while(n!=0);
#include <iostream>
int main( ) {
int i,f,n;
f=1; // initialization
cout <<"\n Program will find factorial of given Number";
cout <<"\n\t enter the number : ";
cin >>n; //read input
#include <iostream>
using namespace std;
int main() {
int n, first = 0, second = 1, next;
cout << "\n\t Enter the number of terms: ";
cin >> n;
cout << "\t Fibonacci Seq.: " << first << " " << second << " ";
int i = 2;
while (i < n) {
next = first + second;
first = second;
second = next;
cout << next << " ";
i++;
}
return 0;
} // end of program
#include <iostream>
using namespace std;
int main() {
int n, first = 0, second = 1, next;
cout << "\n\t Enter the number of terms: ";
cin >> n;
cout << "\t Fibonacci Seq.: " << first << " " << second << " ";
#include <iostream>
using namespace std;
//main function
int main() {
int n, first = 0, second = 1, next;
cout << "\n\t Enter the number of terms: ";
cin >> n;
fibSeries(n);
return 0;
} // end of program
#include <iostream>
using namespace std;
//main function
int main() {
int n ;
cout << "\n\t Enter the number of terms: ";
cin >> n;
#include <iostream>
using namespace std;
if((n == 1) || (n == 0))
return(n);
else
return(fib(n-1) + fib(n-2));
} // end of function fib( )
//main function
int main() {
int n ;
cout << "\n\t Enter the number of terms: ";
cin >> n;
#include <iostream>
#include <cmath> // to use pow(base, exp)
int main( ) {
int sum = 0, num, n1 = 0, rem= 0, len = 0;
cout << "\n\t Enter the number : ";
cin >> num; //read input to n
n1=num; // make copy of input number
while (n1) {
len++; //count digits
n1=n1/10;
} // end while
do {
rem = num % 10; // separate LSD
num = num / 10; // reduce digit by one
sum = sum + pow (rem, len); // get sum after taking power of number
// raise to no of digit
} while (num != 0); //test that all digit has been exhausted
int main( ) {
int l, i, j, k, n;
l=0;
} //endfor
} //end of program
int main( ) {
int num,n1=0,sum=0,rem=0;
do {
rem = num % 10; // find remainder
num = num / 10; // separate rhs digit
sum = sum * 10 + rem; // regenerate the number
} while(num != 0);
if(sum == n1)
cout << "\n\t The given number is " << n1 << " palindrome ";
else
cout << "\n\t The given number is " << n1 << " not palindrome ";
return 0;
} //end of program
int main( ) {
int fact=0, num;
cout << "\n\t Enter the number : ";
cin >> num; //read input
if (fact == num)
cout << "\n\t Number " << num << " is Perfect ";
else
cout << "\n\t Number " << num << " is not perfect";
} //end of program
//ex1.9_functionOverloading.cpp
/*
C++ program to find area of square, rectangle, circle and triangle by using
function overloading
*/
#include<iostream>
using namespace std;
//user define functions
int area(int s) {
return(s*s); // return area of square
}
float area(float r) {
return(3.14 * r * r); // return area of circle
}
float area(float bs, float ht) {
return((bs * ht)/2); // return area of triangle
}
//main function
int main() {
int s, l, b;
float r, bs, ht;
cout << "Enter side of a square:";
cin >> s;
cout << "Enter length and breadth of rectangle:";
cin >> l >> b;
cout << "Enter radius of circle:";
cin >> r;
cout << "Enter base and height of triangle:";
cin >> bs >> ht;
cout << "\n Area of square is " << area(s);
cout << "\n Area of rectangle is " << area(l, b);
#include <iostream>
using namespace std;
int main() {
int num, sum = 0;
cout << "\n\t Enter the number of terms : ";
cin >> num;
cout << "\n\t Summation of " << num << " term is " << sum;
}
// MatrixDiagonals.cpp
// Program name: MatrixDiagonals.cpp
// compiler : g++ under Linux
// g++ -Wall MatrixDiagonals.cpp -o MatrixDiagonals
// run: ./MatrixDiagonals
// C++ Program to print the Diagonals of a Matrix
#include <iostream>
using namespace std;
// main program
int main() {
int row = 4;
int arry[][COL] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 1, 2, 3 },
{ 4, 5, 6, 7 }
};
printPrincipalDiagonal(arry, row);
printSecondaryDiagonal(arry, row);
return 0;
} // end of program
// MatrixAdd.cpp
// Program name: MatrixAdd.cpp
// compiler : g++ under Linux
// g++ -Wall MatrixAdd.cpp -o MatrixAdd
// run: ./MatrixAdd
// C++ program for addition of two matrices
#include <iostream>
using namespace std;
#define ROW 4
#define COL 4
// main program
int main() {
int A[ROW][COL] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 }
};
int B[ROW][COL] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 }
};
return 0;
} // end of program
#include <iostream>
#include <cstring > // or we can use <string.h> also
using namespace std;
int main ( ) {
char s1[25], s2 [25]; // character array
// string s1, s2; // string variable
#include<iostream>
#include<cstring>
int main ( ) {
char s1[40], s2 [25]; // character array
// string s1, s2; // does not require <cstring>
cout << "\n\t string concatenation using character array \n\n";
cout << "\t Enter string s1 : ";
cin >> s1;
cout << "\t Enter string s2 : ";
cin >> s2;
strcat(s1, s2);
cout << "\t strcat (s1, s2 ) : " << s1;
// cout << "strcat (s1, s2 ) : " << s1 = s2;
} //end of program
int main ( ) {
char s1[25], s2 [25];
cout << "\n\t String Comparison \n\n";
cout << "\t Enter string s1 : " ;
cin >> s1;
cout << "\t Enter string s2 : ";
cin >> s2;
// compare str1 and str2 lexicographically
int status = strcmp ( s1, s2 ); // store comparison status
cout << "\n strcmp (s1, s2 ): ";
switch(strcmp (s1, s2 )) {
case -1 :
cout << s1 << " is less then " << s2;
break;
case 0 :
cout << s1 << " is equal to " << s2;
break;
case 1 :
cout << s1 << " is grater then " << s2;
break;
} // end of switch()
} // end of program
int main ( ) {
char str[25];
cout << "\n\t Reverse String \n\n" ;
cout << "\t Enter a string :" ;
cin >> str;
// cout << "\t strrev (str) : " << strrev(str); // or use following
// to lines
strrev(str);
cout << "\t strrev (str) : " << str;
}
// SingleInheritance01.cpp
// Example 8.1: Protected data member and accessing methods
// without arguments
#include<iostream>
using namespace std;
class A {
protected:
int a; // data members of class A
public:
void set_A() {
cout << "Enter the Value of a for class A = ";
cin >> a; // read value of a from KB
}
void disp_A() {
cout << endl << "Value of a for class A = " << a;
}
}; //end of class A (base class)
class B: public A {
int b, p; // data members of class A
public:
void set_B() {
set_A(); // call method from base class A
cout << "Enter the Value of b for class B = ";
cin >> b; // read value of a from KB
}
void disp_B() {
disp_A(); // call method from base class A
cout << endl << "Value of b of class B = " << b;
}
void cal_product() {
p = a * b;
cout << endl << "Product of " << a << " * "
<< b << " = " << p;
}
}; //end of sub class B
// main function
main() {
B obj_b; // drive class object
obj_b.set_B(); // call method of drive class
obj_b.cal_product(); // call method of drive class
return 0;
}
// MultipleInheritance.cpp
// Example 8.2 : C++ program to explain multiple inheritance
#include <iostream>
using namespace std;
public:
Vehicle() {
cout << "Executing constructor Vehicle\n";
}
}; // end of class Vehicle
}; //
// main function
int main() {
// Creating object of sub class will invoke
// the constructor of base classes.
Vehicle obj_V;
cout << endl;
// Car obj_C;
FourWheeler obj_FW;
cout << endl;
Car obj_C;
return 0;
}
// MultilevelInheritance.cpp
// example 11: C++ program to implement Multilevel Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() {
cout << "\t Executing constructor Vehicle\n";
}
}; //end of base class Vehicle
}
};
// main function
int main() {
// Creating object of sub class will invoke
// the constructor of base classes.
Vehicle obj_V;
cout << endl;
fourWheeler obj_fw;
cout << endl;
Car obj;
cout << endl;
return 0;
} //end of program
// HybridInheritance.cpp
// Example 8.2 : C++ program for Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle\n";
}
};
// base class
class Fare {
public:
Fare() {
cout << "Fare of Vehicle\n";
}
};
// main function
int main() {
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
} //end of program
// HierarchicalInheritance.cpp
// Example 12: C++ program to implement Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() {
cout << "\n\t Executing constructor Vehicle\n";
}
}; //end of class Vehicle
// main function
int main() {
// creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
class complex {
public:
int real;
int img;
complex() { // constructor
real = img = 0;
}
void show() {
cout<< "\n" << real << "+" << im << "i";
}
// friend function and over loading function declaration
friend complex operator+(complex c, complex d);
}; //end of class complex
// main function
int main() {
complex x(1, 2), y(0, 7);
complex c = x + y; //overloaded + is called here
c.show();
} // end of main
// VirtualFunctionOverriding.cpp
// example 05 : C++ program for virtual function overriding
#include <bits/stdc++.h>
using namespace std;
class base {
public:
virtual void print() {
cout << "print base class" << endl;
}
void show() {
cout << "show base class" << endl;
}
}; // end of base class
void show() {
cout << "show derived class" << endl;
}
}; // end of drive class
// main function
int main() {
base* bptr; // object pointer
derived d; // object
bptr = &d; // assign address od drived object to base pointer
// Virtual function, binded at runtime (Runtime polymorphism)
bptr->print();
return 0;
}