Friday, April 9, 2021

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2. It allows one to be assigned to another array through the use of the assignment operator (e.g. cp1=cp2) 3. It supports a function that returns the size of the array. 4. It allows the reading or printing of array through the use of cout and cin

 #include<iostream>

using namespace std;

class CppArray

{

   private:

   int a[10],i,j,n,b[10],temp;

   public:

   void get();

   void print();

   void sort();

   void range();

   void exchange();

   int size();

};

void CppArray::get()

{

   cout<<"\n Enter the limit of array ";

   cin>>n;

   cout<<"\n Enter the array elements ";

   for(i=0;i<n;i++)

   {

       cout<<"\n a["<<i<<"]=";

       cin>>a[i];

   }

}

void CppArray::print()

{

   cout<<"\n The entered array is ";

   for(i=0;i<n;i++)

   {

       cout<<"\n a["<<i<<"]="<<a[i];

   }

}

void CppArray::sort()

{

   for(j=0;j<n;j++)

   {

      for(i=0;i<n-1;i++)

      {

         if(a[i]>a[i+1])

         {

            temp=a[i+1];

            a[i+1]=a[i];

            a[i]=temp;

         }

      }

   }

   cout<<"\n The sorted array is ";

   for(i=0;i<n;i++)

   {

       cout<<"\n a["<<i<<"]="<<a[i];

   }

}

void CppArray::range()

{

   cout<<"\n The range of array is from "<<a[0]<<" to "<<a[n-1];

}

void CppArray::exchange()

{

   for(i=0;i<n;i++)

   {

       b[i]=a[i];

   }

   cout<<"\n The exchanged array is ";

   for(i=0;i<n;i++)

   {

       cout<<"\n b["<<i<<"]="<<b[i];

   }

}

int CppArray::size()

{

   return n;

}

int main()

{

   int a;

   CppArray obj;

   obj.get();

   obj.print();

   obj.range();

   obj.exchange();

   obj.sort();

   a=obj.size();

   cout<<"\n The size of array is "<<a;

   return 0;

}

************output*****************


PS F:\harshal\MCA\opps\assignment 3> cd "f:\harshal\MCA\opps\assignment 3\" ; if ($?) { g++ ass3.cpp -o ass3 } ; if ($?) { .\ass3 }


 Enter the limit of array 4


 Enter the array elements 

 a[0]=3


 a[1]=2


 a[2]=4


 a[3]=5


 The entered array is

 a[0]=3

 a[1]=2

 a[2]=4

 a[3]=5

 The range of array is from 3 to 5

 The exchanged array is

 b[0]=3

 b[1]=2

 b[2]=4

 b[3]=5

 The sorted array is

 a[0]=2

 a[1]=3

 a[2]=4

 a[3]=5

 The size of array is 4 

**//

Friday, March 26, 2021

Title: Implement a class Complex which represents the Complex Number data type. Implement the following operations: 1. Constructor (including a default constructor which creates the complex number0+0i). 2. Overloaded operator+ to add two complex numbers. 3. Overloaded operator* to multiply two complex numbers. 4. Overload << and >> to print and read Complex Numbers.

 #include<iostream>

using namespace std;

class complex

{

public:

    float real,img;

    complex()

    {

    real=0;

    img=0;

    }

    complex operator +(complex);

    complex operator *(complex);

    friend ostream &operator<<(ostream&,complex&);

    friend istream &operator>>(istream&,complex&);

};


complex complex::operator +(complex obj)

{

    complex temp;

    temp.real=real+obj.real;

    temp.img=img+obj.img;

    return (temp);

}


complex complex::operator *(complex obj)

{

    complex temp;

    temp.real=(real*obj.real)-(img*obj.img);

    temp.img=(real*obj.img)+(img+obj.img);

    return (temp);

}


istream &operator>>(istream& is,complex& obj)

{

    is>>obj.real;

    is>>obj.img;

    return is;


}


ostream &operator<<(ostream& os,complex& obj)

{

    os<<obj.real;

    os<<"+"<<obj.img<<"i";

    return os;

}


int main()

{

    complex a,b,c,d;

    //cout<<"\n Enter first complex number"<<endl;

    cout<<"\n Enter real and imaginary part of first complex number:";

    cin>>a;


    //cout<<"\n Enter second complex number"<<endl;

    cout<<"\n Enter real and imaginary part of second complex number:";

    cin>>b;


    cout<<"\n Arithmetic operations are :";

    c=a+b;

    cout<<"\n Addition is:"<<c;

   

    d=a*b;

    cout<<"\n Multiplication is:"<<d<<"\n";

    return 0;

}


*****************OUTPUT***************************

Enter real and imaginary part of first complex number:3 

3


 Enter real and imaginary part of second complex number:4

5


 Arithmetic operations are :

 Addition is:7+8i

 Multiplication is:-3+23i

Monday, March 22, 2021

to compute 1:square root of a numnber 2:square of a number 3:cube of a number 4)Check for prime 5)factorial of a number6)Prime factor of a number

To accept the number and compute using python

a)Square root a Number

>>> num = float(input('Enter a Number:'))

Enter a Number:77

>>> num_sqrt = num ** 0.5

>>> print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))

The square root of 77.000 is 8.775

>>> 

b)Squar of a Number

>>> num = float(input('Enter a Number:'))

Enter a Number:12

>>> square = num * num

>>> print('The square of %0.3f is %0.3f'%(num ,square))

The square of 12.000 is 144.000

>>>

c)Cube of a Number

>>> num = float(input('Enter a Number:'))

Enter a Number:4

>>> cube= num * num *num

>>> print('The cube of %0.3f is %0.3f'%(num ,cube))

The cube of 4.000 is 64.000

d)Check for Prime Number

>>> num = int(input("Enter a number: "))

Enter a number: 23

>>> if num > 1:

...     for i in range(2, num):

...             if(num % i) == 0:

...                     print(num,"IS NOT APRIME NUMBER")

...                     break

...             else:

...                     print(num,"IS A PRIME NUMBER")

...     else:

...             print(num,"IS NOT A PRIME NUMBER")

...

23 IS A PRIME NUMBER

5) factorial of number

>>> num = int(input('Enter a Number:'))

Enter a Number:23

>>> f = 1

>>> if(num < 0):

...     print("THE NUMBER IS NEGATIVE")

... elif(num == 0):

...     print("The factorial of 0 is 1")

... else:

...     for i in range (1, num + 1):

...             f =f * i

...     print("the factorial of",num,"is",f)

...

the factorial of 23 is 25852016738884976640000

>>>

6)Prime Factore of a Number
>>> Number = int(input(" Please Enter any Number: "))
 Please Enter any Number: 23
>>> i = 1
>>> while(i <= Number):
...     count = 0
...     if(Number % i== 0):
...             j = 1
...             while(j <= i):
...                     if(i % j== 0):
...                             count = count + 1
...                     j = j+ 1
...             if(count == 2):
...                     print("%d is a Prime Factor of a Given Number %d" %(i, Number))
...     i = i + 1
...
23 is a Prime Factor of a Given Number 23
>>>

Sunday, March 21, 2021

 Addiition of two Number using cpp


#include<iostream.h>

using namespace std;

int main()

{

        int a,b,c;

        cout<<"Enter the value of a:"<<endl;

        cin>>a;

        cout<<"Enter the value of b:"<<endl;

        cin>>b;

        c=a+b;

        cout<<"The addition of a and b is:"<<endl;

        cin>>c;

        return 0;

}