C Assignment
C Assignment
C Assignment
OUTPUT
Enter number of terms: 11
Fibonacci Series: 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89
~1~
Output:
Enter the String: ROTATOR
ROTATOR is a Palindrome.
~2~
#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~
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~
// elements
public:
void push(T const&); // push element
void pop();
// pop element
T top() const;
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
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~
~ 10 ~
Output:
Enter Real Part 10
Enter Imaginary Part 20
The complex object is 10+i20
~ 12 ~
~ 13 ~
#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 ~
#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 ~
~ 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 ~
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 ~
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 ~