C++ Old Solution Program
C++ Old Solution Program
Pulchowk
2062 Baisakh:
#include <iostream>
return a<b?a:b;
int main() {
int a=5,b=10;
cout << "a = " << a << endl;
smallest(a,b) = 50;
return 0;
#include <iostream>
class Complex {
float real,imag;
public:
Complex():real(0.0),imag(0.0) {}
void display() {
cout << "(" << real << "," << imag << ")";
};
int main() {
Complex c1(1,2);
c1.display();
return 0;
#include <iostream>
#include <cmath>
class complex {
float real,imag;
public:
real = r;
imag = i;
float magnitude() {
return sqrt(real*real+imag*imag);
}
if (this->magnitude()<c.magnitude())
return true;
return false;
if (this->magnitude()>c.magnitude())
return true;
return false;
if (this->magnitude()==c.magnitude())
return true;
return false;
void display() {
cout << "(" << real << "," <<imag << ")";
};
int main() {
complex c1(1,2),c2(2,3);
if (c1<c2) {
c1.display();
c2.display();
} else if (c1>c2) {
c1.display();
c2.display();
else if (c1==c2) {
c1.display();
c2.display();
return 0;
class Person {
char name[25];
int age;
public:
void input() {
void display() {
};
float salary;
public:
void input() {
cout << "Enter the details of the teacher: " << endl;
Person::input();
void display() {
Person::display();
};
float percentage;
public:
void input() {
Person::input();
}
void display() {
Person::display();
};
int main() {
student s;
s.input();
s.display();
teacher t;
t.input();
t.display();
return 0;
#include <iostream>
return out;
int main() {
int balance;
return 0;
#include <iostream>
class Stack {
int top;
int arr[size];
public:
Stack() {
top = -1;
void push(int x) {
if(top == size-1) {
throw stack_full();
top++;
arr[top] = x;
void pop() {
if(top == -1)
throw stack_empty();
top--;
class stack_full {
public:
char *message;
stack_full() {
};
class stack_empty {
public:
char *message;
stack_empty() {
};
};
int main() {
try {
Stack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.pop();
s.pop();
}
catch (Stack::stack_empty se) {
return 0;
2062 Poush:
#include <iostream>
class distances {
float feet,inches;
public:
void input() {
void display() {
cout << feet << " feet and " << inches << " inches";
};
int main() {
distances d;
d.input();
d.display();
return 0;
#include <iostream>
class length {
float feet,inches;
public:
length() {
feet = 0.0;
inches = 0.0;
length(float f,float i) {
feet = f;
inches = i;
operator float() {
float temp;
return temp;
void display() {
cout << "(" << feet << "," << inches << ")" << endl;
};
int main() {
length d(1,12);
d.display();
float inches;
inches = d;
return 0;
}
// Write a program to read and display Cartesian Coordinate using
stream operators.
#include <iostream>
class Cartesian {
int x,y;
public:
};
return is;
os<< "(" << c.x << "," << c.y << ")";
return os;
}
int main() {
Cartesian c;
cin >> c;
cout << c;
return 0;
2063 Baisakh:
// Hint:a complex number has real and imaginary part as a+ib where
i is (-1)^1/2.
#include <iostream>
class complex {
float real,imag;
public:
complex operator+(complex c) {
complex temp;
temp.real = real+c.real;
temp.imag = imag+c.imag;
return temp;
complex operator-(complex c) {
complex temp;
temp.real = real-c.real;
temp.imag = imag-c.imag;
return temp;
};
return is;
os << "(" << c.real << "," << c.imag << ")";
return os;
}
int main() {
complex c1,c2,c3,c4;
c3 = c1+c2;
cout <<c1 << "+" << c2 << "=" << c3 << endl;
c4 = c1-c2;
cout <<c1 << "-" << c2 << "=" << c4 << endl;
return 0;
2064 Jestha:
// and address) in a structure until user says "no". After reading the
data, write it to a file
// then display the content of the file in the proper format on your
output screen.
#include <iostream>
#include <fstream>
struct DOB {
int yr,mo,day;
};
struct student {
int student_id;
DOB d;
char department[25];
char address[30];
};
int main() {
student s;
char ch;
fstream fout;
do {
fout.write((char *)&s,sizeof(s));
cin>>ch;
while(ch =='y');
fout.close();
fstream fin;
fin.open("students.txt",ios::in | ios::binary);
while(fin.read((char*)&s,sizeof(s))) {
cout << "DOB: " << s.d.yr << "/" << s.d.mo << "/" << s.d.day
<< endl;
fin.close();
return 0;
2064 Falgun:
#include <iostream>
#include <string.h>
if(n>length || n<0) {
int main() {
display("kathmandu",3);
cout <<"\n";
display("pulchowk",4);
return 0;
2065 Shrawan:
#include <iostream>
enum bol{False,True};
class prime {
int n;
public:
void input() {
cin >> n;
}
bol check() {
if(n==2)
return True;
for(int i=2;i<=n/2;i++) {
if(n%i==0) {
flag = False;
break;
return flag;
void display(){
if(flag==1)
else
};
int main() {
prime p;
p.input();
p.display();
return 0;
2068 Baisakh:
AB
ABC
ABCD
*/
#include <iostream>
int main() {
char *c = "ABCD";
cout.write(c,i);
cout << endl;
return 0;
2069 Ashad:
// the result in ascending order. For sorting you can use the
vector member functions.
#include <iostream>
#include <vector>
void sorter(vector<int>&v) {
int temp;
for(int i=0;i<v.size();i++) {
for(int j=0;j<v.size()-i-1;j++) {
if(v[j]>v[j+1]) {
temp = v[j];
v[j] = v[j+1];
v[j+1] = temp;
void display(vector<int>&v) {
for(int i=0;i<v.size();i++)
int main() {
vector <int>v;
int n;
int elem;
cin >> n;
cout << "Enter the elements: ";
v.push_back(elem);
display(v);
sorter(v);
display(v);
return 0;
2070 Chaitra:
#include <iostream>
#include <string.h>
class Strings {
char *str;
public:
Strings() {}
Strings(char *s) {
strcpy(str,s);
Strings join(Strings s) {
Strings temp;
strcpy(temp.str,str);
strcat(temp.str,s.str);
return temp;
void display() {
};
int main() {
Strings s1("Amit"),s2("Chaudhary"),s3;
s3 = s1.join(s2);
s3.display();
return 0;