0% found this document useful (0 votes)
2 views39 pages

Include

The document contains a series of C++ code snippets demonstrating various programming concepts such as input/output, arithmetic operations, conditional statements, loops, functions, and data types. Each code snippet includes a description of its functionality along with sample outputs. The examples cover topics like calculating grades, checking for prime numbers, and manipulating numbers, providing a comprehensive overview of basic C++ programming.

Uploaded by

dharanikolimi779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views39 pages

Include

The document contains a series of C++ code snippets demonstrating various programming concepts such as input/output, arithmetic operations, conditional statements, loops, functions, and data types. Each code snippet includes a description of its functionality along with sample outputs. The examples cover topics like calculating grades, checking for prime numbers, and manipulating numbers, providing a comprehensive overview of basic C++ programming.

Uploaded by

dharanikolimi779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

#include <iostream>
using namespace std;
int main()
{
cout<<"Welcome to C++ lab"<<endl;
return 0;
}
2. #include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two values: "<<endl;
cin>>a;
cin>>b;
cout<<"a+b = "<<a+b<<"\n"<<"a-b = "<<a-b<<"\n"<<"a*b = "<<a*b<<"\n"<<"a/b = "<<a/b;
return 0;
}

Output: Enter two values:


12
4
a+b = 16
a-b = 8
a*b = 48
a/b = 3

3. #include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter the value of a : "<<endl;
cin>>a;
if(a%2==0)
{
cout<<a<<" is even"<<endl;
}
else
{
cout<<a<<" is odd"<<endl;
}
}

Output:
Enter the value of a :
6
6 is even

Enter the value of a :


9
9 is odd

4. #include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter the values of a,b and c: "<<endl;
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<a<<" is the largest number"<<endl;
}
else if(b>a && b>c)
{
cout<<b<<" is the largest number"<<endl;
}
else
{
cout<<c<<" is the largest number"<<endl;
}
return 0;
}

Output:
Enter the values of a,b and c:
635
6 is the largest number

5. #include <iostream>
using namespace std;
int main()
{
int C,C_lab,Python,java,R,sum,avg,percentage;
cout<<"Enter the marks scored in each subject: "<<endl;
cin>>C>>C_lab>>Python>>java>>R;
sum=(C+C_lab+Python+java+R);
avg=sum/5;
percentage=avg;
cout<<"percentage = "<<percentage<<"\n";
if(percentage>94 && percentage<=100)
{
cout<<"Grade: O+"<<endl;
}
else if(percentage>84 && percentage<=95)
{
cout<<"Grade: O"<<endl;
}
else if(percentage>74 && percentage<=85)
{
cout<<"Grade: A+"<<endl;
}
else if(percentage>64 && percentage<=75)
{
cout<<"Grade: A"<<endl;
}
else if(percentage>54 && percentage<=65)
{
cout<<"Grade: B"<<endl;
}
else if(percentage>44 && percentage<=55)
{
cout<<"Grade: C"<<endl;
}
else
{
cout<<"Grade: F - Fail"<<endl;
}
return 0;
}

Output: Enter the marks scored in each subject:


45
78
68
99
68
percentage = 71
Grade: A

6. #include <iostream>
using namespace std;
int main()
{
int i,n,marks,sum=0,avg,percentage;
cout<<"Enter the number of subjects: "<<endl;
cin>>n;
cout<<"Enter the marks scored in each subject: "<<endl;
for(i=1;i<=n;i++)
{
cin>>marks;
sum+=marks;
}
avg=sum/n;
percentage=avg;
cout<<"percentage = "<<percentage<<"\n";
if(percentage>94 && percentage<=100)
{
cout<<"Grade: O+"<<endl;
}
else if(percentage>84 && percentage<=95)
{
cout<<"Grade: O"<<endl;
}
else if(percentage>74 && percentage<=85)
{
cout<<"Grade: A+"<<endl;
}
else if(percentage>64 && percentage<=75)
{
cout<<"Grade: A"<<endl;
}
else if(percentage>54 && percentage<=65)
{
cout<<"Grade: B"<<endl;
}
else if(percentage>44 && percentage<=55)
{
cout<<"Grade: C"<<endl;
}
else
{
cout<<"Grade: F - Fail"<<endl;
}
return 0;
}

Output: Enter the number of subjects:


5
Enter the marks scored in each subject:
57
89
90
99
99
percentage = 86
Grade: O

7. int addition(int x, int y)


{
return(x+y);
}

#include <iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter the values of a and b: ";
cin>>a>>b;
cout<<"a+b = "<<addition(a,b)<<"\n";
return 0;
}
Output:
Enter the values of a and b: 7
5
a+b = 12
8. int sumofdigits(int x)
{
int sum=0,rem;
while(x!=0)
{
rem=x%10;
sum=sum+rem;
x=x/10;
}
return(sum);
}

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the value of n: ";
cin>>n;
cout<<"Sum of Digits: "<<sumofdigits(n)<<"\n";
return 0;
}

Output:
Enter the value of n: 341
Sum of Digits: 8

9. int prime(int x)
{
int c=0,i;
for(i=1;i<=x;i++)
{
if(x%i==0)
c=c+1;
}
return(c);
}

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the value of n: ";
cin>>n;
if (prime(n)<=2)
{
cout<<"The number is prime"<<endl;
}
if (prime(n)>2)
{
cout<<"The number is not prime"<<endl;
}
}

Output:
Enter the value of n: 10
The number is not prime

10. int reverse(int x)


{
int rev=0,rem;
while(x!=0)
{
rem=x%10;
rev=rev*10+rem;
x=x/10;
}
return(rev);
}

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the value of n: ";
cin>>n;
cout<<"reverse of "<<n<<" is "<<reverse(n);
}

Output:
Enter the value of n: 124
reverse of 124 is 421
11. int sumofsquares(int x)
{
int i,sum=0;
for(i=1;i<=x;i++)
{
sum+= i*i;
}
return(sum);
}

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the value of n: ";
cin>>n;
cout<<"sum of squares of numbers till "<<n<<" is "<<sumofsquares(n);
}

Output:
Enter the value of n: 6
sum of squares of numbers till 6 is 91

12. int palindrome(int x)


{
int rev,rem;
rev=0;
while(x>0)
{
rem=x%10;
rev=rev*10+rem;
x=x/10;
}
return(rev);
}

#include <iostream>
using namespace std;
int main()
{
int n,m;
cout<<"Enter the value of n: ";
cin>>n;
m=n;
if(m==palindrome(n))
{
cout<<"The number is a palindrome"<<endl;
}
else
{
cout<<"The number is not a palindrome"<<endl;
}
}

Output:
Enter the value of n: 241
The number is not a palindrome

Enter the value of n: 1221


The number is a palindrome

13. #include <iostream>


using namespace std;

void multable(int x)
{
for(int i=1;i<=20;i++)
{
cout<<x<<"x"<<i<<"="<<x*i<<endl;
}
}

int main()
{
int n;
cout << "Enter the value of n: ";
cin >> n;

multable(n);

return 0;
}
Output:
Enter the value of n: 13
13x1=13
13x2=26
13x3=39
13x4=52
13x5=65
13x6=78
13x7=91
13x8=104
13x9=117
13x10=130
13x11=143
13x12=156
13x13=169
13x14=182
13x15=195
13x16=208
13x17=221
13x18=234
13x19=247
13x20=260

14. #include <iostream>


using namespace std;

void multable(int x, int y)


{
for(int i=1;i<=y;i++)
{
cout<<x<<"x"<<i<<"="<<x*i<<endl;
}
}

int main()
{
int n,m;
cout << "Enter the value of n: ";
cin >> n;
cout << "Enter the value of m: ";
cin >> m;
multable(n,m);
return 0;
}
Output:
Enter the value of n: 6
Enter the value of m: 10
6x1=6
6x2=12
6x3=18
6x4=24
6x5=30
6x6=36
6x7=42
6x8=48
6x9=54
6x10=60

C++ (from textbook)

 We use “::” for global variables


15. #include <iostream>


using namespace std;
int main()
{
cout<<"Hello world"<<endl;
return 0;
}

Output:
Hello world

16. #include <iostream>


using namespace std;
int main()
{
char gender;
char *msg = "C++ cout object";
int age;
float number;
gender = 'M';
age = 26;
number = 347.8;
cout<<gender;
cout<<" "<<age<<" "<<number;
cout <<"\n"<< msg << endl;
cout << 1 << 2 << 3 << endl;
cout << number+1;
cout <<"\n"<< 99.99;

return 0;
}

Output:
M 26 347.8
C++ cout object
123
348.8
99.99

17. #include<iostream>
using namespace std;
int main()
{
char name[50];
int age;
char address[50];
cout<<"Enter name : "<<endl;
cin>>name;
cout<<"Enter age: "<<endl;
cin>>age;
cout<<"Enter address: "<<endl;
cin>>address;
cout<<"\n"<<"The data entered are: "<<"\n"<<"Name: "<<name<<"\n"<<"Age: "<<age<<"\
n"<<"Address: "<<address<<"\n";
return 0;
}

Output:
Enter name :
Dharani
Enter age:
18
Enter address:
Vivekananda road

The data entered are:


Name: Dharani
Age: 18
Address: Vivekananda

18. #include<iostream>
using namespace std;
int main()
{
float p,t,r,SI,total;
cout<<"Enter principle amount: "<<endl;
cin>>p;
cout<<"Enter time: "<<endl;
cin>>t;
cout<<"Enter rate: "<<endl;
cin>>r;
SI=(p*t*r)/100;
cout<<"Simple interest: \n"<<SI<<endl;
total = p+SI;
cout<<"Total amout : "<<total<<endl;
return 0;
}

Output:
Enter principle amount:
10000
Enter time:
4
Enter rate:
10
Simple interest:
4000
Total amount : 14000

19. #include<iostream>
using namespace std;
const float PI = 3.1452;
int main()
{
float radius,area;
cout<<"Enter the value of radius: "<<endl;
cin>>radius;
area= PI*radius*radius;
cout<<"\nArea = "<<area;
return 0;
}

Output:
Enter the value of radius:
5

Area = 78.63

20. #include<iostream>
using namespace std;
int num=20;
int main()
{
int num=10;
cout<<"Local : "<<num;
cout<<"\nGlobal : "<<::num;

cout<<"\nLocal+Global :"<<num+::num<<endl;
}

Output:
Local : 10
Global : 20
Local+Global :30

21. #include<iostream>
using namespace std;
int counter=50;
int main()
{
int counter;
for(counter=1;counter<10;counter++)
{
cout<<::counter/counter<<"\n";
}
return 0;
}

Output:
50
25
16
12
10
8
7
6
5

22. #include<iostream>
using namespace std;
int main()
{
int a,b,c;
a=1;b=2;c=3;
int &z=a;
cout<<"a="<<a<<" b="<<b<<" c="<<c<<" z="<<z<<endl;
z=b;
cout<<"a="<<a<<" b="<<b<<" c="<<c<<" z="<<z<<endl;
z=c;
cout<<"a="<<a<<" b="<<b<<" c="<<c<<" z="<<z<<endl;
cout<<"&a="<<&a<<" \n&b="<<&b<<" \n&c="<<&c<<" \n&z="<<&z<<endl;
}

Output:
a=1 b=2 c=3 z=1
a=2 b=2 c=3 z=2
a=3 b=2 c=3 z=3
&a=0x7fff4345a1c4
&b=0x7fff4345a1c0
&c=0x7fff4345a1bc
&z=0x7fff4345a1c4

23. //reference test.


#include<iostream>
using namespace std;
int main()
{
int n=100;
int *p=&n;
int &m=*p;
cout<<"n="<<n<<" m="<<m<<" *p="<<*p<<endl;
int k=5;
p=&k;
k=200;
cout<<"n="<<n<<" m="<<m<<" *p="<<*p<<endl;
}

Output:
n=100 m=100 *p=100
n=100 m=100 *p=200

24. //Strict type-checking........maximun of two numbers


#include <iostream>
using namespace std;
int max( int a, int b )
{
if ( a > b )
return a;
else
return b;
}
int main ()
{
int x, y;
cout << "Enter two integers: ";
cin >> x >> y;
cout << "Maximum = " << max ( x, y );
return 0;
}
Output:
Enter two integers: 17 13
Maximum = 17

25. #include <iostream>


using namespace std;
int swap( int * x, int * y )
{
int t;
t = *x;
*x = *y;
*y = t;

return(*x,*y);
}
int main()
{
int a, b;
swap( &a, &b );
cout<<"&a= "<<&a<<"\n"<<"&b= "<<&b<<endl;
}

Ouput:
&a= 0x7ffde8ac7a7c
&b= 0x7ffde8ac7a78

26. #include <iostream>


using namespace std;
int swap( int & x, int & y )
{
int t;
t = x;
x = y;
y = t;
return(x,y);
}
int main()
{
int a, b;
cout << "Enter two integers <a, b>: ";
cin >> a >> b;
swap( a, b );
cout << "On swapping <a, b>: " << a << " " << b;
}

Output:
Enter two integers <a, b>: 4 7
On swapping <a, b>: 7 4

27. //inline functions.......


#include<iostream>
using namespace std;
inline int square(int x)
{
x=x*x;
return(x);
}
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << “Its Square = ” << square( num );
}

Output:
Enter a number: 7
Its Square = 49

28. #include<iostream>
using namespace std;
void showstring(char *str= "Hello World\n")
{
cout<<str;
}
int main()
{
showstring("Here is an explicit argument \n");
showstring();
showstring("56");
}

Output:
Here is an explicit argument
Hello World
56

29. //if 'new' is declared, the memory is allocated dynamically


#include<iostream>
using namespace std;
void AddVectors(int *a,int *b,int *c,int size)
{
for(int i=0;i<size;i++)
{
c[i]=a[i]+b[i];
}
}
void ReadVector(int *vector,int size)
{
for(int i=0;i<size;i++)
{
cin>>vector[i];
}
}
void ShowVector(int *vector,int size)
{
for(int i=0;i<size;i++)
{
cout<<vector[i]<<" ";
}
}
int main()
{
int vec_size;
int *x,*y,*z;
cout<<"Enter the size of the vector: ";
cin>>vec_size;
x= new int[vec_size];
y= new int[vec_size];
z= new int[vec_size];
cout<<"Enter elements of vector x: ";
ReadVector(x,vec_size);
cout<<"Enter elements of vector y: ";
ReadVector(y,vec_size);
AddVectors(x,y,z,vec_size);
cout<<"Summation vector z: ";
ShowVector(z,vec_size);
delete x;
delete y;
delete z;
}

Output:
Enter the size of the vector: 3
Enter elements of vector x:
4
7
9
Enter elements of vector y: 3
9
6
Summation vector z: 7 16 15

30. #include<iostream>
using namespace std;
template <class T>
void transfer(T &acc1, T &acc2, T amt)
{
if (amt <= acc1)
{
acc1 = acc1 - amt;
acc2 = acc2 + amt;
cout << "\nAmount transferred\n";
}
else
{
cout << "\nCould not transfer the amount\n";
}
}

int main()
{
string name1,name2;
string acc_type1,acc_type2;
int acc_no1,acc_no2;
double bal1,bal2, amount;
cout << "Enter the name of user 1: ";
cin >> name1;
cout << "Enter the account type: ";
cin >> acc_type1;
cout << "Enter the account number: ";
cin >> acc_no1;
cout << "Enter the balance: ";
cin >> bal1;

cout << "\nEnter the name of user 2: ";


cin >> name2;
cout << "Enter the account type: ";
cin >> acc_type2;
cout << "Enter the account number: ";
cin >> acc_no2;
cout << "Enter the balance: ";
cin >> bal2;
cout << "\nBEFORE TRANSFER :\n";
cout << name1 << ": " << acc_type1 << ", acc no.: " << acc_no1 << ", Balance = " << bal1 <<
endl;
cout << name2 << ": " << acc_type2 << ", acc no.: " << acc_no2 << ", Balance = " << bal2 <<
endl;

cout << "\nEnter amount to transfer from " << name1 << " to " << name2 << ": ";
cin >> amount;

transfer(bal1, bal2, amount);


cout << "\nAFTER TRANSFER :\n";
cout << name1 << " Balance = " << bal1 << endl;
cout << name2 << " Balance = " << bal2 << endl;

return 0;
}

Output:
Enter the name of user 1: Dharani
Enter the account type: Market
Enter the account number: 21345
Enter the balance: 50890

Enter the name of user 2: Muskan


Enter the account type: Savings
Enter the account number: 67890
Enter the balance: 20580

BEFORE TRANSFER :
Dharani: Market, acc no.: 21345, Balance = 50890
Muskan: Savings, acc no.: 67890, Balance = 20580

Enter amount to transfer from Dharani to Muskan: 890

Amount transferred

AFTER TRANSFER :
Dharani Balance = 50000
Muskan Balance = 21470

31. #include<iostream>
using namespace std;
class counter
{
private:
int value;
public:
counter()
{
value=0;
}
counter( int val)
{
value = val;
}

int Getcounter()
{
return value;
}
void up()
{
value+=2;
}
~counter()
{
cout<<"Object destroyed"<<endl;
}
};

int main()
{
counter counter1;
counter counter2 (1);
cout<<"counter1 = "<<[Link]()<<endl;
cout<<"counter2 = "<<[Link]()<<endl;
[Link]();
[Link]();
cout<<"counter1 = "<<[Link]()<<endl;
cout<<"counter2 = "<<[Link]()<<endl;

Output:

counter1 = 0
counter2 = 1
counter1 = 2
counter2 = 3
Object destroyed
Object destroyed

32. #include<iostream>
#include<cstring>
using namespace std;
class student
{
private:
int roll_no;
char name[20];
public:
void setdata(int roll_no_in,char *name_in);
void outdata();
};

void student::setdata( int roll_no_in, char *name_in )


{
roll_no = roll_no_in;
strcpy( name, name_in );
}

void student::outdata()
{
std::cout << "Roll No = " << roll_no << std::endl;
std::cout << "Name = " << name << std::endl;
}
int main()
{
student s1;

student s2;

[Link]( 1, "Dharani" );

[Link]( 10, "Pradeepthi" );


std::cout << "Student details..." << std::endl;
[Link]();
s2 .outdata();
}

Output:
Student details...
Roll No = 1
Name = Dharani
Roll No = 10
Name = Pradeepthi

33. #include <iostream>


using namespace std;

class counter
{
protected:
int value;
public:
counter()
{
value = 0;
}
counter( int val )
{
value = val;
}
int GetCounter()
{
return value;
}
void up()
{
value = value + 1;
}
};

class NewCounter: public counter


{
public:
NewCounter(): counter()
{}
NewCounter( int val ) : counter( val )
{}
void down()
{
value = value - 1;
}
};
int main()
{
NewCounter counter1;
NewCounter counter2 (1);
cout << "counter1 initially = " << [Link]() << endl;
cout << "counter2 initially = " << [Link]() << endl;

[Link]();
[Link]();
cout << "counter1 on increment = " << [Link]() << endl;
cout << "counter2 on increment = "<< [Link]() << endl;

[Link]();
[Link]();
cout << "counter1 on decrement = " << [Link]() << endl;
cout << "counter2 on decrement = " << [Link]() << endl;

}
Output:
counter1 initially = 0
counter2 initially = 1
counter1 on increment = 1
counter2 on increment = 2
counter1 on decrement = 0
counter2 on decrement = 1

34. #include <iostream>


using namespace std;
class counter
{
private:
int value;
public:
counter()
{
value = 0;
}
counter( int val)
{
value = val;
}
int GetCounter()
{
return value;
}
void operator++()
{
value=value+2;
}
void operator--()
{
value=value-1;
}

};

int main()
{
counter counter1;
counter counter2 (1);
cout<<"counter1 initially: "<<[Link]()<<endl;
cout<<"counter2 initially: "<<[Link]()<<endl;
++counter1;
++counter2;
cout<<"counter1 on increment of (2): "<<[Link]()<<endl;
cout<<"counter2 on increment of (2): "<<[Link]()<<endl;
--counter1;
--counter2;
cout<<"counter1 on decrement of (1): "<<[Link]()<<endl;
cout<<"counter2 on decrement of (1): "<<[Link]();

Output:
counter1 initially: 0
counter2 initially: 1
counter1 on increment of (2): 2
counter2 on increment of (2): 3
counter1 on decrement of (1): 1
counter2 on decrement of (1): 2

35. //Single inheritance (Class in Public)


#include<iostream>
using namespace std;

class B
{
int a;
public:
int b;
void set_ab();
int Get_a();
void Show_a();
};

class D : public B
{
int c;
public:
void mul();
void display();
};
void B::set_ab()
{
a = 5;
b = 10;
}

int B::Get_a()
{
return a;
}

void B::Show_a()
{
cout << "a = " << Get_a() << endl;
}

void D::mul()
{
c = b * Get_a();
}

void D::display()
{
cout << "a = " << Get_a() << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}

int main()
{
D d;
d.set_ab();
[Link]();
[Link]();

d.b = 20;
[Link]();
[Link]();

return 0;
}
Output:
a=5
b = 10
c = 50
a=5
b = 20
c = 100

36. (class as private)


//Single inheritence
#include<iostream>
using namespace std;
class B
{
int a;
public:
int b;
void Get_ab();
int Get_a(void);
void Show_a(void);
};

class D: public B
{
int c;
public:
void mul();
void display(void);
};

void B :: Get_ab()
{
a=5;
b=10;
}

int B :: Get_a()
{
return a;
}

void B :: Show_a()
{
cout<<"a= "<<a<<endl;
}

void D::mul()
{
Get_ab();
c = b * Get_a();
}

void D :: display()
{
cout<<"a= "<<Get_a()<<endl;
cout<<"b= "<<b<<endl;
cout<<"c= "<<c<<"\n"<<endl;
}

int main()
{
D d;
d.Get_ab();
[Link]();
[Link]();

return 0;
}

Output:
a= 5
b= 10
c= 50

37. .
#include <iostream>
#include <string>
using namespace std;

class Animal
{
public:
string name;
int legs;
void Animal_name(string n)
{
name = n;
}

void no_of_legs(int l)
{
legs = l;
}
};

class Types : public Animal


{
public:
string type1 = "Domestic";
string type2 = "Wild";
string typein;

string Get_types()
{
cout << "Enter the Animal Type:: ";
cin >> typein;

if (typein == type1)
return type1;
else
return type2;
}

void display()
{
cout << "Animal Name: " << name << endl;
cout << "Legs: " << legs << endl;
cout << Get_types() << endl;
}
};

int main()
{
Types t;
t.Animal_name("Dog");
t.no_of_legs(4);
[Link]();
return 0;
}

Output:
Animal Name: Dog
Legs: 4
Enter the Animal Type:: domestic
domestic

38. #include<iostream>
using namespace std;
class space
{
int x,y,z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator-();
};

void space:: getdata(int a,int b,int c)


{
x=a;
y=b;
z=c;
}

void space:: display(void)


{
cout<<"x= "<<x<<" ";
cout<<"y= "<<y<<" ";
cout<<"z= "<<z<<"\n";
}

void space :: operator-()


{
x=-x;
y=-y;
z=-z;
}
int main()
{
space s;
[Link](10,-20,30);
cout<<"S: \n";
[Link]();
-s;
cout<<"-S: \n";
[Link]();
return 0;
}

Output:
S:
x= 10 y= -20 z= 30
-S:
x= -10 y= 20 z= -30

39. .
#include<iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex(){}
complex(float real, float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};

complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}

void complex:: display(void)


{
cout<<"("<<x<<")"<<"+i"<<"("<<y<<")"<<"\n";
}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(2.7,5.3);
c3=c1+c2;
cout<<"c1= ";
[Link]();
cout<<"c2= ";
[Link]();
cout<<"c3= ";
[Link]();
return 0;
}

Output:
c1= (2.5)+i(3.5)
c2= (2.7)+i(5.3)
c3= (5.2)+i(8.8)

40. Entering values for the complex numbers


#include<iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex(){}
complex(float real, float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};

complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}

void complex:: display(void)


{
cout<<"("<<x<<")"<<"+i"<<"("<<y<<")"<<"\n";
}
int main()
{
complex c1,c2,c3;
float a1,a2,b1,b2;
cout<<"Enter the values of a1,a2,b1 and b2: \n";
cin>>a1>>a2>>b1>>b2;
c1=complex(a1,b1);
c2=complex(a2,b2);
c3=c1+c2;
cout<<"c1= ";
[Link]();
cout<<"c2= ";
[Link]();
cout<<"c3= ";
[Link]();
return 0;
}

Output:
Enter the values of a1,a2,b1 and b2:
2.9
3.8
4.6
2.2
c1= (2.9)+i(4.6)
c2= (3.8)+i(2.2)
c3= (6.7)+i(6.8)
41. Multiplication
#include<iostream>
using namespace std;
class complex
{
float x,y;
public:
complex(){}
complex(float real, float imag)
{
x=real;
y=imag;
}
complex operator*(complex);
void display(void);
};

complex complex::operator*(complex c)
{
complex temp;
temp.x=(x*(c.x))-(y*(c.y));
temp.y=(x*(c.y))+(y*(c.x));
return temp;
}

void complex::display(void)
{
cout<<"("<<x<<")"<<"+i"<<"("<<y<<")"<<"\n";
}
int main()
{
complex c1,c2,c3;
float a,b,c,d;
cout<<"Enter the values of a,b,c,d: \n";
cin>>a>>b>>c>>d;
c1=complex(a,b);
c2=complex(c,d);
c3=c1*c2;
cout<<"c1= ";
[Link]();
cout<<"c2= ";
[Link]();
cout<<"c3= ";
[Link]();

return 0;
}

Output:
Enter the values of a,b,c,d:
6
3
4
5
c1= (6)+i(3)
c2= (4)+i(5)
c3= (9)+i(42)

42. .
#include<iostream>
using namespace std;
const int SIZE=4;
class vector
{
int v[SIZE];
public:
vector();
vector(int* x);
friend vector operator *(int a,vector b);
friend vector operator *(vector b,int a);
friend istream &operator>>(istream&,vector&);
friend ostream &operator<<(ostream&,vector&);
};

vector::vector()
{
for(int i=0;i<SIZE;i++)
{
v[i]=0;
}
}

vector::vector(int*x)
{
for(int i=0;i<SIZE;i++)
{
v[i]=x[i];
}
}

vector operator *(int a,vector b)


{
vector c;
for(int i=0;i<SIZE:i++)
{
c.v[i]=a*b.v[i];
}
return c;
}

vector operator *(vector b, int a)


{
vector c;
for(int i=0;i<SIZE;i++)
{
c.v[i]=b.v[i]*a;
}
return c;
}

istream & operator>>(istream &din, vector &b)


{
for(int i=0;i<SIZE;i++)
{
din>>b.v[i];
}
return din;
}

ostream & operator<<(ostream &dout, vector &b)


{
dout<<"("<<b.v[0];
for(int i=0;i<SIZE;i++)
{
dout<<","<<b.v[i];
dout<<")";
}
return dout;
}

int x[SIZE]={2,4,6};

int main()
{
vector m;
vector n=x;
cout<<"Enter elements of vector m"<<"\n";
cin>>m;
cout<<"\n";
cout<<"m= "<<m<<"\n";
vector p,q;
p=2*m;
q=n*2;
cout<<"\n";
cout<<"p= "<<p<<"\n";
cout<<"q= "<<q<<"\n";
return 0;
}

You might also like