C Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

Q.

Fibonacci sequence up to nth term


#include <iostream.h>
using namespace std;
int main() {
int n, firstTerm = 1, secondTerm = 1, nextTerm;
cout<< "Enter number of terms: ";
cin>> n;
cout<< "Fibonacci Series: " <<firstTerm<< " + " <<secondTerm<< " + ";
for (int i = 1; i <= n-2; ++i) {
nextTerm = firstTerm + secondTerm;
cout<<nextTerm<< " + ";
firstTerm = secondTerm;
secondTerm = nextTerm;
}
return 0;
}

OUTPUT
Enter number of terms: 11
Fibonacci Series: 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89

~1~

C++ program to check whether the given string is


Palindrome or not
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[50],m[50];
int p=1,i;
clrscr();
cout<<"Enter the string: ";
cin>>s;
strcpy(m,s);
strrev(m);
for(i=0;i<=strlen(s)-1;i++)
{
if(*(m+i)!=*(s+i))
{
p=0;
break;
}
}
if(!p)
cout<<"\n"<<s<<" is not a Palindrome";
else
cout<<"\n"<<s<<" is a Palindrome";
getch();
}

Output:
Enter the String: ROTATOR
ROTATOR is a Palindrome.

~2~

Q. WAP to find the product of two 3*3 matrices.

#include<iostream.h>
#include<conio.h>
void main( )
{
int mat1 [3][3], mat2[3][3],mat3[3][3], i ,j, k, sum;
clrscr( ) ;
cout<<"\nEnter values for first 3 x 3 matrix:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for (j = 0 ; j <= 2 ; j++ )
cin>>mat1 [i][j] ;
}
cout<<"\n Enter values for second 3 x 3 matrix:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cin>>mat2[i][j] ;
}
cout<<"\n The first 3 x 3 matrix entered by you is:\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<<"\t"<< mat1[i][j] ;
cout<<"\n";
}
cout<<"\n the second 3 x 3 matrix entered :\n";
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
cout<<"\t"<< mat2[i][j] ;
cout<<"\n";
}
for ( i = 0 ; i <= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )
{
sum = 0;
for ( k = 0 ; k <=2 ; k++ )
sum = sum + mat1 [i][k] *mat2[k][j];
mat3[i][j] = sum ;
}
}
cout<<"\nThe product of the above two matrices is:\n";
for ( i = 0 ;i<= 2 ; i++ )
{
for ( j = 0 ; j <= 2 ; j++ )

~3~

cout<<"\t"<<mat3[i][j] ;
cout<<"\n";
cout<<"\n Press any key to exit.";
getch( ) ;

Output

~4~

Q. WAP to show the use of virtual function.


#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show:";
}
void display()
{
cout<<"\n Base class display:" ;
}
};

classdrive:public base
{
public:
void display()
{
cout<<"\n Drive class display:";
}
void show()
{
cout<<"\n Drive class show:";
~5~

}
};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
cout<<"\n\n\t P points to drive:\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}

OUTPUT:
P points to Base
Base class display
Base class show
P points to Drive
Base class Display
Drive class Show
~6~

Q. WAP to show the use of Class template.


#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
using namespace std;
template<class T>
class Stack {
private:
vector<T>elems;

// elements

public:
void push(T const&); // push element
void pop();

// pop element

T top() const;

// return top element

bool empty() const{

// return true if empty.

returnelems.empty();
}
};
template<class T>
void Stack<T>::push (T const&elem)
{
// append copy of passed element
elems.push_back(elem);
}
~7~

template<class T>
void Stack<T>::pop ()
{
if (elems.empty()) {
throwout_of_range("Stack<>::pop(): empty stack");
}
// remove last element
elems.pop_back();
}
template<class T>
T Stack<T>::top () const
{
if (elems.empty()) {
throwout_of_range("Stack<>::top(): empty stack");
}
// return copy of last element
returnelems.back();
}
int main()
{
try {
Stack<int>intStack; // stack of ints
Stack<string>stringStack;

// stack of strings

// manipulate int stack


intStack.push(7);
~8~

cout<<intStack.top() <<endl;
// manipulate string stack
stringStack.push("hello");
cout<<stringStack.top() <<std::endl;
stringStack.pop();
stringStack.pop();
}
catch (exception const& ex) {
cerr<< "Exception: " <<ex.what() <<endl;
return -1;
}
}

Output result:
7
hello
Exception: Stack<>::pop(): empty stack

~9~

Q. WAP to overload the extraction and insertion operators.


#include <iostream.h>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
friendostream& operator << (ostream&out, const Complex &c);
friendistream& operator >> (istream&in, Complex &c);
};
ostream& operator << (ostream&out, const Complex &c)
{
out<<c.real;
out<< "+i" <<c.imag<<endl;
return out;
}
istream& operator >> (istream&in, Complex &c)
{
cout<< "Enter Real Part ";
in>>c.real;
cout<< "Enter Imaginary Part ";
in>>c.imag;
return in;
}
int main()
{
Complex c1;
cin>> c1;
cout<< "The complex object is ";
cout<< c1;
return 0;
}

~ 10 ~

Output:
Enter Real Part 10
Enter Imaginary Part 20
The complex object is 10+i20

Q. WAP to define a class to add two integers using function


overloading.
#include <iostream.h>
using namespace std;
/* Function arguments are of different data type */
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout<< "Enter two integers\n";
cin>> a >> b;
x = add(a, b);
cout<< "Sum of integers: " << x <<endl;
cout<< "Enter two floating point numbers\n";
cin>> c >> d;
y = add(c, d);
cout<< "Sum of floats: " << y <<endl;
return 0;
}
~ 11 ~

long add(long x, long y)


{
long sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}

~ 12 ~

Q. Write a C++ program to implement an array by row-major method.


#include<iostream.h>
#include<conio.h>
void main()
{
int mat[3][3];
int i,j;
clrscr();
cout<<"Enter Elements of Matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat[i][j];
}
}
cout<<"\nRow major array: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat[i][j];
cout<<" row-[" <<i+ 1<<"]\n";
}
}
getch();
}
Output:

~ 13 ~

Q. WAP to add and subtract two matrices.

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,a[10][10],b[10][10],c[10][10],d[10][10],n,m;
clrscr();
cout<<"\nEnter the Number of Rows and Columns of Matrix A and B:";
cin>>n>>m;
cout<<"\nEnter the Elements of Matrix A: \n";
for(i=0; i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>a[i][j];
}
}
cout<<"\nEnter the Elements of Matrix B: \n";
for(i=0; i<n; i++)
{
for(j=0;j<m;j++ )
{
cin>>b[i][j];
}
}
for(i=0; i<n; i++)
{
for(j=0;j<m ;j++)
{
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}
cout<<"\nThe Resultant Matrix C=A+B is :\n";
for(i=0; i<n; i++)
{
for(j=0; j<m; j++ )
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
~ 14 ~

}
cout<<"\n The Resultant Matrix D=A-B is : \n";
for(i=0; i<n; i++)
{
for (j=0; j<m; j++ )
{
cout<<d[i][j]<<" ";
}
cout<<"\n";
}
getch();
}

Output:

~ 15 ~

Q. WAP to find average of N numbers.

#include<iostream.h>
#include<conio.h>
void main ( )
{
int n, a[50], i, s;
float av;
clrscr();
cout<<"\n Enter the Value of n : ";
cin>>n;
cout<<"\n Enter the Different "<<n<<" Values : ";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
s=0;
for(i=0; i<n; i++)
{
s=s+a[i];
}
cout<<"\nSum of " <<n <<" Value is : " <<s;
av=(float)s/n;
cout<<"\nAverage of "<<n <<" Value is : "<<av;
getch();
}

Output:

~ 16 ~

Q. C++ Program to Implement Bubble Sort using Templates


#include<conio.h>
#include<iostream.h>
template<class bubble>
void bubble(bubble a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
bubble element;
element = a[i];
a[i] = a[j];
a[j] = element;
}
}
}
}
void main()
{
int a[6]={1,2,3,4,4,3};
char b[4]={'s','b','d','e'};
clrscr();
bubble(a,6);
cout<<"\nSorted Order Integers: ";
for(int i=0;i<6;i++)
cout<<a[i]<<"\t";
bubble(b,4);
cout<<"\nSorted Order Characters: ";
for(int j=0;j<4;j++)
cout<<b[j]<<"\t";
getch();
}
Output:

~ 17 ~

Q. If (str1==str2) display two strings are same else display unequal str1 and
str2 are objects of string class.

#include<iostream.h>
#include<stdio.h>
#include<string.h>
using namespace std;
class String
{
char str[20];
public:
void getdata()
{
gets(str);
}
//operator function to overload comparison operator and compare two strings
int operator ==(String s)
{
if(!strcmp(str,s.str))
return 1;
return 0;
}
};
main()
{
String s1,s2;
cout<<"Enter first string:";
s1.getdata();
cout<<"Enter second string:";
s2.getdata();
if(s1==s2)
{
cout<<"\n Two strings are same\n";
}
else
{
cout<<"\n Strings are unqual\n";
}
return 0;
}

~ 18 ~

Output:
Enter first string : asansol
Enter second string : Durgapur
Strings are unequal.

Q. WAP that performs file read, write, update and display operations in
C++
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
staticint totrec=0; //totrec variable keep track for total variable entered//Initially Record scanned
are Zerovoid main()
{
int choice;
while(1)
{
clrscr();
cout<<"Choose your choice\nNOTE : one choice for one record(except viewing data)\n";
cout<<"1) Scanning intial records\n";
cout<<"2) Appending records\n";
cout<<"3) Modifying or append records\n";
cout<<"4) Viewing records\n";
cout<<"5) Exit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 : {
ofstream outfile;
outfile.open("emp",ios::out);
cout<<"\n\nPlease enter the details as per demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name;

~ 19 ~

outfile<<name<<endl;
cout<<"Enter Age : ";
int age;
cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by him\her : ";
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec= totrec + 1;
outfile.close();
}
break;
case 2 : {
ofstream outfile;
outfile.open("emp",ios::app);
cout<<"\n\nPlease enter the details as per demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name;
outfile<<name<<endl;
cout<<"Enter Age : ";
int age;
cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by him : ";
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec = totrec + 1;
outfile.close();
}
break;
case 3 : {
ofstream outfile;
outfile.open("emp",ios::ate);
cout<<"Are you interested in adding record\nenter y or n\n";
char ans;
cin>>ans;
if(ans=='y' || ans=='Y')
{
cout<<"\n\nPlease enter the details as per demanded\n";
cout<<"\nEnter the name : ";
char name[20];

~ 20 ~

cin>>name;
outfile<<name<<endl;
cout<<"Enter Age : ";
int age;
cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by him : ";
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec = totrec + 1;
}
outfile.close();
}
break;
case 4 : {
ifstream infile;
infile.open("emp",ios::in);
constint size=80;
char line[size];
int counter=totrec;
while(counter > 0)
{
infile.getline(line,size);
cout<<"\n\nNAME : "<<line<<endl;
infile.getline(line,size);
cout<<"AGE : "<<line<<endl;
infile.getline(line,size);
cout<<"LANGUAGE : "<<line<<endl;
counter--;
}
infile.close();
}
getch();
break;
case 5 : gotoout;
default : cout<<"\nInvalid Choice\nTRY AGAIN\n";
}
}
out:
}

~ 21 ~

OUTPUT:

~ 22 ~

Q. WAP to show sequence of execution of constructor and destructor


in multiple inheritances.
# include<iostreaam.h>
#include<constream.h>
Class A
{
Public:
{
cout<<\n Zero-argument constructor of base class A;}
~A()
{
cout<<\n Destructor of the class A;}
};
Class B
{
Public:
B()
cout<<\n Zero-argument constructor of base class B;}
~B(){cout<<\n Destructor of the class B;}
};
class c: public A, public B
{
Public:
C()
{
cout<<\n Zero argument constructor of derived class C;
}
~C(){ cout<<\n Destructor of the class C;}
};
int main()
{
clrscr();
c objc;
return 0;
}

OUTPUT:
Zero-argument constructor of base class A
Zero-argument constructor of base class B
Zero-argument constructor of derived class C
Destructor of the class C
Destructor of the class B
Destructor of the class A
~ 23 ~

Q. WAP to convert a decimal number into binary, octal and pental code in C++
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
long binary_code(int);
double pental_code(int);
double octal_code(int);
main()
{
clrscr();
int number;
cout<<"\n ********** Decimal Number *********"<<endl;
cout<<"\n For binary numbers ,enter from( 0 ---> 1023 ) "<<endl;
cout<<"\n\n Enter the Decimal number = ";
cin>>number;
cout<<"\n ********** Binary Number **********"<<endl;
cout<<"\t Binary Number = "<<binary_code(number)<<endl;
cout<<"\n *********** Pental Code ***********"<<endl;
cout<<"\t Pental Code = "<<pental_code(number)<<endl;
cout<<"\n *********** Octal Code ************"<<endl;
cout<<"\t Octal Code = "<<octal_code(number)<<endl;
getch();
return 0;
}
/*************************************************************************//******
*******************************************************************///----------------------- Function Definitions
-----------------------///****************************************************************
*********//***********************************************************************
**//*************************************************************************///-------------------------- binary_code(int)
------------------------///***************************************************************
**********/long binary_code(int number)
{
int count=0;
int resulting_array[25];

~ 24 ~

int remainder;
int qutiont;
int size=0;
long binary_number=0;
long sum=0;
longbase=1;
while(number>0)
{
qutiont=number/2;
remainder=number%2;
number=qutiont;
resulting_array[count]=remainder;
size++;
count++;
}
for(int j=0;j<size;j++)
{
sum=resulting_array[j]*base;
base=base*10;
binary_number+=sum;
}
return binary_number;
}
/*************************************************************************///-------------------------- pental_code(int)
------------------------///***************************************************************
**********/double pental_code(int number)
{
int count=0;
int resulting_array[25];
int remainder;
int qutiont;
int size=0;
double pental_number=0;
double sum=0;
doublebase=1;
while(number>0)
{
qutiont=number/5;
remainder=number%5;
number=qutiont;

~ 25 ~

resulting_array[count]=remainder;
size++;
count++;
}
for(int j=0;j<size;j++)
{
sum=resulting_array[j]*base;
base=base*10;
pental_number+=sum;
}
return pental_number;
}
/*************************************************************************///--------------------------- octal_code(int)
------------------------///***************************************************************
**********/double octal_code(int number)
{
int count=0;
int resulting_array[25];
int remainder;
int qutiont;
int size=0;
double octal_number=0;
double sum=0;
doublebase=1;
while(number>0)
{
qutiont=number/8;
remainder=number%8;
number=qutiont;
resulting_array[count]=remainder;
count++;
size++;
}
for(int j=0;j<size;j++)
{
sum=resulting_array[j]*base;
base=base*10;
octal_number+=sum;
}
return octal_number;
}

~ 26 ~

Output:

~ 27 ~

Q. WAP to illustrate over-riding of base class member function in a


derived class.
#include<iostream.h>
#include<conio.h>
/*********************************************************************
****///------------------------------ stack0
-------------------------------///***********************************************
**************************/class stack0
{
protected:
int st[25];
int top;
public:
stack0() { top=-1; }
void push(int item) { top++; st[top]=item; }
int pop();
};
/*********************************************************************
****///------------------------------ stack1
-------------------------------///***********************************************
**************************/class stack1:public stack0
{
public:
int pop();
void push(int);
};
/*********************************************************************
****///----------------------------- pop( )
--------------------------------///**********************************************
***************************/
stack0::pop()
{
int item;
item=st[top];
~ 28 ~

top--;
return item;
}
/*********************************************************************
****///----------------------------- pop( )
--------------------------------///**********************************************
***************************/
stack1::pop()
{
int n;
if(top==-1)
{
cout<<" Stack is empty "<<endl;
n=NULL;
}
else
n=stack0::pop();
return n;
}
/*********************************************************************
****///----------------------------- push(int)
-----------------------------///************************************************
*************************/void stack1::push(int item)
{
if(top==24)
cout<<" Stack is full "<<endl;
else
stack0::push(item);
}
main()
~ 29 ~

{
clrscr();
stack1 obj;
int n=0;
obj.push(10);
obj.push(20);
obj.push(30);
n=obj.pop();
cout<<"\n"<<n<<endl;
n=obj.pop();
cout<<n<<endl;;
getch();
return 0;
}

OUTPUT:

~ 30 ~

You might also like