C++ Record For II-IV Sem 123
C++ Record For II-IV Sem 123
#include<iostream.h>
#include<conio.h>
class add
{
private:
int a,b,c;
public:
void read()
{
cout<<"enter Two Number "<<endl;
cin>>a>>b;
}
void display()
{
cout<<" A = "<<a<<endl;
cout<<" B = "<<b<<endl;
cout<<"Sum = "<<c<<endl;
}
void sum()
{
c= a+b;
}
};
void main()
{
add x; // x is a object off add
clrscr();
x.read();
x.sum();
x.display();
getch();
}
class Mathematics {
int x, y;
public:
void input() {
cout << "Input two integers\n";
cin >> x >> y;
}
void add() {
cout << "Result: " << x + y;
}
};
int main()
{
Mathematics m; // Creating an object of the class
m.input();
m.add();
return 0;
}
class programming {
public:
void output(); //function declaration
};
void programming::output() {
cout << "Function defined outside the class.\n";
}
int main() {
programming x;
x.output();
return 0;
}
Output of program:
int main()
{
int 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.
//function declaration
int addition(int a,int b);
int main()
{
int num1; //to store first number
int num2; //to store second number
int add; //to store addition
//read numbers
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
//call function
add=addition(num1,num2);
//print addition
cout<<"Addition is: "<<add<<endl;
return 0;
}
//function definition
int addition(int a,int b)
{
return (a+b);
}
Output
Enter first number: 100
Enter second number: 200
Addition is: 300
1 #include<iostream>
2 using namespace std;
3 class student
4 {
5 private:
6
7 char
8 name[20],regd[10],branch[10];
9 int sem;
1 public:
0 void input();
1 void display();
1
1
2 };
1 void student::input()
3 {
1 cout<<"Enter Name:";
4 cin>>name;
1 cout<<"Enter Regdno.:";
5 cin>>regd;
1 cout<<"Enter Branch:";
6 cin>>branch;
1 cout<<"Enter Sem:";
7 cin>>sem;
1 }
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5 void student::display()
2 {
6 cout<<"\nName:"<<name;
2 cout<<"\nRegdno.:"<<regd;
7 cout<<"\nBranch:"<<branch;
2 cout<<"\nSem:"<<sem;
8 }
2 int main()
9 {
3 student s;
0 s.input();
3 s.display();
1 }
3
2
3
3
3
4
3
5
3
6
3
7
3
8
Sample Input
Enter Name:Bikash
Enter Regdno.:123
Enter Branch:CS
Enter Sem:5
Sample Output
Name:Bikash
Regdno.:123
Branch:CS
Sem:5
#include<iostream>
#include<iomanip>
using namespace std;
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
int main()
{
int i, n;
/*Visit www.programmingwithbasics.com*/
for(i=0;i<n;i++)
{
cout << "\nEnter details of " << i+1 << " Employee"<<endl;
cout<<"\n\n------------------------------------------------------------\n";
cout << "Details of Employees";
cout<<"\n------------------------------------------------------------\n\n";
Output:-
9.
Write a C++ program to count the lines, words and characters in a given text.
#include(iostream.h)
#include(conio.h)
#include(stdio.h)
void main( )
{
char ch;
int sp=0, ct=0, line=0;
clrscr( );
cout<<"Enter the required lines of text:";
while((ch=getchar())!=EOF)
{
ct++;
if((ch==' ')
sp++;
if(ch=='\n')
line++;
}
cout<<"Total Lines:"<< line;
cout<<"Total words:"<< sp+line;
cout<<"Total Characters:"<< ct;
getch( );
}
Total Lines: 2
Total words: 5
10. Write a C++ program to compare two strings using string functions.
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char str1[100], str2[100];
cout<<"\n Enter First String : ";
gets(str1);
cout<<"\n Enter Second String : ";
gets(str2);
if(strcmp(str1, str2)==0) //strcmp() is a library function, used to
compare the two strings.
{
cout<<"\n Strings are Equal!!!";
}
else
{
cout<<"\n Strings are Not Equal!!!";
}
return 0;
}
Output:
11.
int main()
{
int n1, n2;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
12. Write a C++ program to calculate the area of rectangle, square using function overloading
#include<iostream>
#include<cstdlib>
using namespace std;
float area(float r)
{
return(3.14 * r * r);
}
float area(float b,float h)
{
return(0.5 * b * h);
}
float area(float l,float b)
{
return (l * b);
}
int main()
{
float b,h,r,l;
int ch;
do
{
cout<<"\n\n *****Menu***** \n";
cout<<"\n 1. Area of Circle";
cout<<"\n 2. Area of Triangle";
cout<<"\n 3. Area of Rectangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\n Enter the Radius of Circle : ";
cin>>r;
cout<<"\n Area of Circle : "<<area(r);
break;
}
case 2:
{
cout<<"\n Enter the Base & Height of Triangle : ";
cin>>b>>h;
cout<<"\n Area of Triangle : "<<area(b,h);
break;
}
case 3:
{
cout<<"\n Enter the Length & Bredth of Rectangle : ";
cin>>l>>b;
cout<<"\n Area of Rectangle : "<<area(l,b);
break;
}
case 4:
exit(0);
default:
cout<<"\n Invalid Choice... ";
}
}while(ch!=4);
return 0;
}
Output:
1. Area of Circle
2. Area of Triangle
3. Area of Rectangle
13.
14.
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
Output
Enter a positive integer: 12
Factorial of 12 = 479001600
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;
}
else if(arr[middle] == search)
{
cout<<search<<" found at location
"<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the
list.";
}
getch();
}
When the above C++ program is compile and executed, it will produce the following
result. Above C++ Programming Example Output (Element found):
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
getch();
}
Download Code
Output
Output
17. Write a C++ program to find the factorial of a given number using recursion
Calculate Factorial Using Recursion
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
Output
Enter an positive integer: 6
Factorial of 6 = 720
18. Write a C++ program to check whether a given number is even or odd.
Example 1: Check Whether Number is Even or Odd using if else
#include <iostream>
using namespace std;
int main()
{
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
Output
Enter an integer: 23
23 is odd.
#include <iostream>
using namespace std;
class operation
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
cin >> b;
}
int main()
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
Run on IDE
Output:
Enter first value: 45
Enter second value: 15
Addition of two numbers: 60
Difference of two numbers: 30
Product of two numbers: 675
Division of two numbers: 3
20. Write a C++ program to demonstrate parameter passing mechanism using pass by value
method.
Passing by Pointer:
// C++ program to swap two numbers using
// pass by pointer.
#include <iostream>
using namespace std;
int main()
{
int a = 45, b = 35;
cout << "Before Swap\n";
cout << "a = " << a << " b = " << b << "\
n";
swap(&a, &b);
#include <iostream>
using namespace std;
void swap(int& x, int& y)
{
int z = x;
x = y;
y = z;
}
int main()
{
int a = 45, b = 35;
cout << "Before Swap\n";
cout << "a = " << a << " b = " << b << "\
n";
swap(a, b);
Before Swap
a = 45 b = 35
After Swap with pass by reference
a = 35 b = 45
21. Write a C++ program to demonstrate parameter passing mechanism using pass by address
method.
void main() {
int x, y;
22. Write a C++ program to demonstrate the usage of a constructor and destructor in a class.
Types of Constructors
1. Default Constructors: Default constructor is the constructor which
doesn’t take any argument. It has no parameters.
// Cpp program to illustrate the
// concept of Constructors
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called
automatically
// when the object is created
construct c;
cout << "a: "<< c.a << endl << "b: "<<
c.b;
return 1;
}
2. Run on IDE
3. Output:
4.
5. a: 10
6. b: 20
String::String(char *c)
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
String::~String()
{
delete []s;
}
#include<iostream>
#include<conio.h>
using namespace std;
class staff
{
private:
char name[50];
int code;
public:
void getdata();
void display();
};
void staff::getdata()
{
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
}
void staff::display()
{
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
}
void typist::getdata()
{
cout<<"Speed:";
cin>>speed;
}
void typist::display()
{
cout<<"Speed:"<<speed<<endl;
}
int main()
{
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata();
t.getdata();
cout<<endl<<"Display data"<<endl;
t.staff::display();
t.display();
getch();
return 0;
}
24. Write a C++ program to calculate volume of cube, cylinder and rectangle using function
overloading
1 #include<iostream>
2 using namespace std;
3 float vol(int,int);
4 float vol(float);
5 int vol(int);
6
7 int main()
8 {
9 int r,h,a;
1 float r1;
0 cout<<"Enter radius and height of a
1 cylinder:";
1 cin>>r>>h;
1 cout<<"Enter side of cube:";
2 cin>>a;
1 cout<<"Enter radius of sphere:";
3 cin>>r1;
1 cout<<"Volume of cylinder is"<<vol(r,h);
4 cout<<"\nVolume of cube is"<<vol(a);
1 cout<<"\nVolume of sphere is"<<vol(r1);
5 return 0;
1 }
6 float vol(int r,int h)
1 {
7 return(3.14*r*r*h);
1 }
8 float vol(float r1)
1 {
9 return((4*3.14*r1*r1*r1)/3);
2 }
0 int vol(int a)
2 {
1 return(a*a*a);
2 }
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
Sample Input
Enter radius and height of a cylinder:8 12
Enter side of cube:2
Enter radius of sphere:3
Sample Output
Volume of cylinder is2411.52
Volume of cube is8
Volume of sphere is113.04
25. Write a C++ program to demonstrate the usage of friend function in a class.
class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
//friend function
friend int addFive(Distance);
};
int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}
Output
Distance: 5
26. Write a C++ program to demonstrate the usage of endl and setw manipulators.
#include <iostream.h>
#include <iomanip.h>
int main()
{
cout<<"USING setw() ..............\n";
cout<< setw(10) <<11<<endl;
cout<< setw(10) <<2222<<endl;
cout<< setw(10) <<33333<<endl;
cout<< setw(10) <<4<<endl;
27. Write a C++ program to display employee information using multiple inheritance
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
//create object of class employee
employee emp;
emp.getEmployeeInfo();
emp.printEmployeeInfo();
return 0;
}
Output
Enter employee's basic info:
Enter Name: Mickey
Enter Emp. Id: 1121
Enter Gender: F
Enter employee's department info:
Enter Department Name: Testing
Enter assigned work: Test Game OEM
Enter time in hours to complete work: 70
Employee's Information is:
Basic Information...:
Name: Mickey
Employee ID: 1121
Gender: F
Department Information...:
Department Name: Testing
Assigned Work: Test Game OEM
Time to complete work: 70
#include<iostream>
#include<conio.h>
class Father
{
protected:
int a;
public:
Father()
{
a = 5;
};
};
class Son : public Father
{
protected:
int b;
public:
Son() : Father()
{
b = 9;
};
};
class GrandSon : public Son
{
protected:
int c;
public:
GrandSon() :Son()
{
c = 0;
c = a + b;
}
void Show()
{
std::cout << " a + b = " << c<<"\n"<<"\n"<<"\n";
}
};
main()
{
GrandSon obj;
obj.Show();
getch();
}
29. Write a C++ program to create a file.
#include <iostream>
#include <fstream>
int main()
{
fstream file; //object of fstream class
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
return 0;
}
Output
File created successfully.
30. Write a C++ program to check whether a given number is a palindrome or not.
int main()
{
int n, num, digit, rev = 0;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome";
else
cout << " The number is not a palindrome";
return 0;
}
Output
Enter a positive number: 12321
The reverse of the number is: 12321
The number is a palindrome
31. Write a C++ program to generate the Fibonacci series using while loop.
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
32. Write a C++ program to overload + operator to add two complex numbers.
C++ program to add two complex number by using the concept of operator overloading using
member function
1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 int a,b;
7 public:
8 A(){};
9 A(int i,int j)
1 {
0 a=i;
1 b=j;
1
1 }
2 void show()
1 {
3 cout<<a<<"+i"<<b;
1 }
4 A operator +(A);
1 };
5 A A::operator +(A obj)
1 {
6 A temp;
1 temp.a=a+obj.a;
7 temp.b=b+obj.b;
1 return(temp);
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
}
2
int main()
6
{
2
A c1(5,6),c2(7,8),c3;
7
cout<<"The 1st no. is:";
2
c1.show();
8
cout<<"\nThe 2nd no. is:";
2
c2.show();
9
c3=c1+c2;
3
cout<<"\nSum is:";
0
c3.show();
3
}
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
Sample Output
The 1st no. is:5+i6
The 2nd no. is:7+i8
Sum is:12+i14
33. Write a C++ program to search for a given element in an array using linear search.
#include<iostream>
using namespace std;
int main() {
cout<<"Enter The Size Of Array: ";
int size;
cin>>size;
for(i=0;i<size;i++){
if(key==array[i]){
cout<<"Key Found At Index Number : "<<i<<endl;
break;
}
}
if(i != size){
cout<<"KEY FOUND at index : "<<i;
}
else{
cout<<"KEY NOT FOUND in Array ";
}
return 0;
}
Sample Output:
Output :
Terry
// C++ program to demonstrate read/write of
class
// objects in C++.
#include <iostream>
#include <fstream>
using namespace std;
// Function declaration of
output_highest_rated() to
// extract info from file Data Base
int output_highest_rated();
};
return 0;
}
// Function definition of
output_highest_rated() to
// extract info from file Data Base
int Contestant::output_highest_rated()
{
// Object to read from file
ifstream file_obj;
// Checking further
file_obj.read((char*)&obj,
sizeof(obj));
}
// Driver code
int main()
{
// Creating object of the class
Contestant object;
return 0;
}
Run on IDE
Output:
Terry
35. Write a C++ program to find the sum of natural numbers using for loop
int main()
{
int n, sum = 0;
36. Write a C++ program to create a simple class named Account and write methods to deposit
and withdraw amount from the account.
#include <iostream>
#include <string>
using namespace std;
class Account{
private:
string firstname;
string lastname;
string FullName;
int accountnumber;
double balance;
public:
Account(){
firstname = "Raaj";
lastname = "Lokanathan";
FullName = "Raaj Lokanathan";
accountnumber = 5671;
balance = 0.0;
}
Account(string fName,string lName,int accno,double
bal);
void setFullName(string fullname);
void setaccountnumber(int accno);
void setbalance(double bal);
void setwithdraw(int wdraw);
void setdeposit(int dep);
string getFullName();
int getaccountnumber();
double getbalance();
void display();
};
string Account::getFullName(){
return FullName;
}
int Account::getaccountnumber(){
return accountnumber;
}
double Account::getbalance(){
return balance;
}
void Account::display(){
cout<<"First Name: "<<firstname<<endl;
cout<<"Last Name: "<<lastname<<endl;
cout<<"Account Number: "<<accountnumber<<endl;
cout<<"Current Balance: "<<balance<<endl;
}
int main(){
Account Acc;
Acc.setFullName("Raaj Lokanathan");
Acc.setaccountnumber(5671);
Acc.setbalance(0.0);
Acc.display();
Account acc[50];
string full_name;
int acc_no;
double bAl;
int i;
for(i=0;i<50;i++){
cout<<"Please enter the details for the customer:
"<<i+1<<endl;
cout<<"Please enter your full name: ";
getline(cin,full_name);
cout<<"Please enter your account number: ";
cin>>acc_no;
cout<<"Please enter your current balance: ";
cin>>bAl;
acc[i].setFullName(full_name);
acc[i].setaccountnumber(acc_no);
acc[i].setbalance(bAl);
}
for(i=0;i<50;i++){
cout<<"Detail for the bank customers"<<i+1<<endl;
acc[i].display();
}
system("pause");
return 0;
int main ()
{
// Pointer initialization to null
int* p = NULL;
if (!p)
cout << "allocation of memory failed\
n";
else
{
for (int i = 0; i < n; i++)
q[i] = i+1;
return 0;
}
Run on IDE
Output:
Value of p: 29
Value of r: 75.25
Value store in block of memory: 1 2 3 4 5
38. Write a C++ program to demonstrate polymorphism by calculating area of rectangle and
triangle using a shape class
#include<iostream>
using namespace std;
int main() {
shape *ptr; // pointer to the 'shape' class
rectangle rect;
triangle tr;
39. Write a C++ program using Switch case to add, subtract, multiply and divide two numbers.
int main()
{
char op;
float num1, num2;
switch(op)
{
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
// If the operator is other than +, -, * or /, error
message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output
Enter operator either + or - or * or divide : -
Enter two operands:
3.4
8.4
3.4 - 8.4 = -5.0
40. Write a C++ program using class to implement basic operations on a stack using arrays
#include<iostream>
#include<stdlib.h>
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n) // constructor
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
41. Write a C++ program to display the sizes of various data types in c++ language.
int main()
{
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" <<
endl;
cout << "Size of double: " << sizeof(double) << " bytes" <<
endl;
return 0;
}
Output
Size of char: 1 byte
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
42. Write a C++ program to accept and display employee details using structures.
43. Write a C++ program to find the length of a given string using string functions
int main()
{
string str = "C++ Programming";
return 0;
}
Output
String Length = 15
Length of C-style string
To get the length of a C-string string, strlen() function is used.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[] = "C++ Programming is awesome";
return 0;
}
Output
String Length = 26
44. Write a C++ program to print the ASCII value of a user entered character
Program to Print ASCII Value
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
return 0;
}
Output
Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 2
46. Write a C++ program to overload + (plus) operator to perform concatenation of two strings
#include<conio.h>
#include<string.h>
#include<iostream.h>
class string {
public:
char *s;
int size;
void getstring(char *str)
{
size = strlen(str);
s = new char[size];
strcpy(s,str);
}
void operator+(string);
};
void main()
{
string ob1, ob2;
char *string1, *string2;
clrscr();
ob1.getstring(string1);
ob2.getstring(string2);
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[20],i,n,sum=0;
clrscr();
cout<<"How many elements you want to enter: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Sum of all Elements are: ";
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
for(i=0;i<n;i++)
{
}
cout<<sum;
getch();
}
Download Code
Output
return 0;
}
Output
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
49. Write a C++ program to demonstrate exception handling by dividing a number with zero.
Exception Handling Divide by zero Example Program
#include<iostream.h>
#include<conio.h>
void main() {
int a, b, c;
float d;
clrscr();
cout << "Enter the value of a:";
cin>>a;
cout << "Enter the value of b:";
cin>>b;
cout << "Enter the value of c:";
cin>>c;
try {
if ((a - b) != 0) {
d = c / (a - b);
cout << "Result is:" << d;
} else {
throw (a - b);
}
} catch (int i) {
cout << "Answer is infinite because a-b is:" << i;
}
getch();
}
Sample Output
int main()
{
long long n;