CPASS
CPASS
a) Write a C++ program to print 3 random numbers of length 6, 5, and 7 respectively. Now, add those
numbers which are even and subtract those numbers which are odd.
Example:
I/P: None
O/P: 1st random number = 195425
2nd random number = 24950
3rd random number = 998501
The sum is = -195425 + 24950 – 998501 = -1168976
Run your program thrice.
ALGORITHM
1. Start
2. T=3,r[3]
3. While(t!=0)
r[0]= randomNoGenerator(10000,99999)
PRINT “1st Random Number = " r[0]
r[1]= randomNoGenerator(100000,999999)
PRINT "2nd Random Number = " [1]
r[2]= randomNoGenerator(1000000,9999999);
PRINT"3rd Random Number = " r[2]
sum(r[0],r[1],r[2]);
Sleep(1000);
4. FUNCTION: int evenodd(long int a)
if(a%2 == 0)
return 1
else
return 0
5. FUNCTION: long int sum(long int a,long int b,long int c)
PRINT "The sum is = "
long int sum=0;
if(evenodd(a))
sum+=a
PRINT a
else
sum-=a
PRINT a
if(evenodd(b))
sum+=b
PRINT " + " b
else
sum-=b
PRINT" - "b
if(evenodd(c))
sum+=c;
1|Page
PRINT" + " c
else
sum-=c
PRINT " - " c
PRINT " = " sum
6. FUNCTION: long int randomNoGenerator(long int lower,long int upper)
time_t t;
srand((unsigned)time(&t));
long int r;
r = lower+rand() % static_cast<int>(upper-lower+1);
return r;
7. End
OUTPUT 1 A
1-A
QUESTION 1
b) Write a C++ program to find GCD of three numbers taking input from the keyboard.
ALGORITHM
1. Start
2. int x,y,z,d,i
3. PRINT "first number - "
4. INPUT x
5. PRINT "second number - "
6. INPUT y
2|Page
7. PRINT "third number - "
8. INPUT z
9. d=1, i=1
10. while(i<=x&&i<=y&&i<=z)
if(x%i==0&&y%i==0&&x%i==0)
d=i
i++
11. PRINT "GCD to : " x " " y " " z " is " d
12. End
PROGRAM 1 B
OUTPUT 1 B
1-B
3|Page
QUESTION 2
a)Given an alphanumeric string (of length <= 100) find out the numbers separated by any alphabet/word
and sum them up print the number and also print whether the sum is a prime number or not in the next
line.
Example:
i/p: “This5is4a2sam4plew2ord”
o/p: 5+4+2+4+2 = 17
17 is a prime number.
ALGORITHM
1. Start
2. FUNCTION: void isPrime(int n)
int i,flag
for(i = 2; i <= n/2; ++i)
if(n%i == 0)
flag = 1
Break
if (n == 1)
PRINT "1 is neither a prime nor a composite number."
Else
if (flag == 0)
PRINT n " is a prime number"
Else
PRINT n " is not a prime number"
3. FUNCTION: void findSum(string str)
string temp = "",sr=""
int i,sum = 0
for (i=0;i<str.length();i++)
if (isdigit(str.at(i)) )
temp += str.at(i)
sr=sr+" + "+str.at(i)
else
sum += atoi(temp.c_str())
temp =" "
sum = sum + atoi(temp.c_str())
PRINT sr " = " sum
isPrime(sum)
4. string str
INPUT str
findSum(str)
5. End
4|Page
OUTPUT 2 A
2-A
QUESTION 2
b) Write a C++ program to print all the prime no.s within a certain range. Take the lower and upper bound
from the keyboard
ALGORITHM
1 Start
2 FUNCTION: int isPrime(int num)
int i
for(i=2; i<=num/2; i++)
if(num % i == 0)
return 0
return 1
3 FUNCTION: void printPrimes(int lower, int upper)
PRINT “List of prime numbers between " lower " and " upper
while(lower <= upper)
if(isPrime(lower))
PRINT lower
lower++
4 int lower, upper
5 PRINT "Enter the lower and upper limit to list primes: "
6 INPUT lower
7 INPUT upper
8 printPrimes(lower, upper)
9 End
5|Page
QUESTION 3
Write a C++ program to take a n digit alphanumeric string (belongs to a class) and perform the following
operations
ALGORITHM
1 Start
2 class A
string s
public:
A(string x)
s=x
A()
string f1()
return s
3 FUNCTION: void isPalindrome(A x)\
string s1
s1=x.f1()
int i,k=s1.length()-1,flag=1
for(i=0;i<s1.length()/2;i++)
if(s1.at(i)!=s1.at(k))
flag=0
break
else
k—
if(flag==0)
PRINT s1 " is not a palindrome"
Else
PRINT s1 " is a palindrome"
4 FUNCTION: void isdigitsymbol(A x)
string s1
s1=x.f1()
int i,d=0,s=0,n=0
for(i=0;i<s1.length();i++)
if(isdigit(s1.at(i)))
d++
else if(isalpha(s1.at(i)))
n++
else
s++
PRINT "Number of digits : " d
PRINT "Number of alphabets : " n
PRINT "Number of symbols : " s
5 FUNCTION: void vowel(A x)
string s1
s1=x.f1()
int i,v=0,c=0
for(i=0;i<s1.length();i++)
char k = s1.at(i)
6|Page
if(k=='a'||k=='e'||k=='i'||k=='o'||k=='u'||k=='A'||k=='E'||k=='I'||k=='O'||k==
'U')
v++
Else
c++
PRINT "Number of vowels : " v
PRINT "Number of consonants : "c
6 A a
7 string x
8 INPUT x
9 a=A(x)
10 isPalindrome(a)
11 isdigitsymbol(a)
12 vowel(a)
13 End
OUTPUT
7|Page
QUESTION 4
Create a class complex with integer data members real and imaginary, use constructors for initialization
of data members. Also use one constructor which will copy one complex object passed as argument to
the invoking object with the use of alias variable concept. Take keyboard input for initialization of data
members and perform the following operations-
Find the counts of complex object for which both real and imaginary parts are multiples of four
Provide functions for addition, subtraction of two complex numbers taken as argument and the
result of the operation will as complex object. Use pointer to objects for calling the concerned
functions.
Write a function which will sort the list of complex numbers in ascending order where complex
numbers will be taken in array.
ALGORITHM
1 Start
2 class Complex
int real,img
static int count
public:
Complex()
Complex(int x,int y):real(x),img(y)
if(real%4==0 && img%4==0)
count++
Complex(Complex & c)
real=c.real
img=c.img
static void showcount()
PRINT "multiples of 4 : " count
void display()
PRINT real " + " img "i"
Complex sum(Complex)
Complex diff(Complex)
friend void swap(Complex &,Complex &)
friend int compare(Complex c1,Complex c2)
friend void sort(Complex c[],int n)
3 FUNCTION: Complex Complex::sum(Complex c1)
Complex c3
c3.real=c1.real + real
c3.img=c1.img + img
return(c3)
4 FUNCTION: Complex Complex::diff(Complex c1)
Complex c3
c3.real=c1.real – real
c3.img=c1.img – img
return(c3)
5 FUNCTION: void swap(Complex & c1,Complex & c2)
int real1,img1
real1=c1.real
img1=c1.img
c1.real=c2.real
c1.img=c2.img
8|Page
c2.real=real1
c2.img=img1
6 FUNCTION: int compare(Complex c1,Complex c2)
if(c1.real>c2.real && c1.img>c2.img)
return 1
else
return 0
7 FUNCTION: void sort(Complex c[],int n)
int i,j
for(j=0;j<n-1;j++)
for(i=0;i<n-j-1;i++)
if(compare(c[i],c[i+1]))
swap(c[i],c[i+1])
PRINT "The sorted array is : "
for(i=0;i<n;i++)
c[i].display();
8 int Complex::count
9 int x,y,n,i
10 PRINT “Enter real and imaginary part of first complex number : "
11 INPUT x y
12 Complex c1,c2,c3
13 c1=Complex(x,y)
14 PRINT "Enter real and imaginary part of second complex number : "
15 INPUT x y
16 c2=Complex(x,y)
17 c3=c2
18 c3.showcount()
19 c1.display()
20 c2.display()
21 PRINT "Displaying copied object"
22 c3.display()
23 Complex c4
24 Complex (Complex :: *pf)(Complex)=&Complex::sum
25 Complex (Complex :: *pff)(Complex)=&Complex::diff
26 Complex *op = &c1
27 c4=(op->*pf)(c2)
28 PRINT "sum of c1 and c2 is : "
29 c4.display()
30 c4=(op->*pff)(c2)
31 PRINT "difference between c1 and c2 is : "
32 c4.display()
33 PRINT "Enter the size of complex array : "
34 INPUT n
35 Complex c[n]
36 PRINT "Enter real and imaginary part of complex numbers in the array : "
37 for(i=0;i<n;i++)
38 PRINT i+1 " "
39 INPUT x y
40 c[i]=Complex(x,y)
41 sort(c,n)
42 End
9|Page
QUESTION 5
a) Write a program which will distinguish or resolve the name conflict of the variables declared with the
same name as global variable and static variable for the following structure-
int x =3;
class A
{
public:
static int x;
void f1(int x)
{
//print all x
}
}
ALGORITHM
1 Start
2 int x=3
3 class A
public:
static int x
void f1(int x)
PRINT "Local variable : " x
PRINT "Static variable : A::x
PRINT "Global variable : " ::x
4 int A::x=10
5 A a
6 a.f1(5)
7 End
OUTPUT 5 A
5-A
10 | P a g e
QUESTION 5
b) Create two classes Student and Teacher with Student class containing data members name, marks,
birth year. Teacher class contains data members name, joining year, specialization. Now use a friend
function which will share the data between these two classes such that the function takes two arrays as
the list of objects from these two classes and performs the following operations
Find the names of students and teachers that are identical and in such cases print the
differences between experience of teacher and the age of the student.
In such cases compute the sum of students age + teachers experience
ALGORITHM
1 Start
2 int pyear=2019
3 class Student
string name
int marks[3],byear
public:
Student()
Student(string na,int m1,int m2,int m3,int b):name(na)
marks[0]=m1
marks[1]=m2
marks[3]=m3
byear=b
friend void samename(Teacher t[],Student s[],int n1,int n2)
int age()
return (pyear-byear)
void display()
PRINT "STUDENT DETAILS : "
PRINT "NAME : " name
PRINT "BIRTH YEAR : " byear
PRINT "AGE : " age()
PRINT"MARKS : " marks[0] " " marks[1] " " marks[2]
4 class Teacher
string name,spcln
int jyear
public:
Teacher()
Teacher(string na,string s,int y):name(na),spcln(s),jyear(y)
friend void samename(Teacher t[],Student s[],int n1,int n2)
int experience()
return (pyear-jyear)
void display()
PRINT "TEACHER DETAILS : "
PRINT "NAME : " name
PRINT "JOIN YEAR : " jyear
PRINT "EXPERIENCE : " experience()
PRINT "SPECIALISATION : " spcln
5 FUNCTION: void samename(Teacher t[],Student s[],int n1,int n2)
int i,j
for(i=0;i<n1;i++)
for(j=0;j<n2;j++)
11 | P a g e
if(s[i].name==t[j].name)
PRINT "Difference of experience of teacher "
t[j].name " and age of student " s[i].name " is : "
(t[j].experience()-s[i].age())
PRINT "Sum of experience of teacher " t[j].name "
and age of student " s[i].name " is "
(t[j].experience()+s[i].age())
6 int i,n1,n2
7 string na,sp
8 int m1,m2, m3, b
9 PRINT "Enter number of students : "
10 INPUT n1
11 PRINT "Enter number of teachers : "
12 INPUT n2
13 Student s[n1]
14 Teacher t[n2]
15 PRINT "Enter student details name, marks in 3 subjects and birth year"
16 for(i=0;i<n1;i++)
PRINT i+1" "
INPUT na m1 m2 m3 b
s[i]=Student(na,m1,m2,m2,b);
PRINT "Enter teacher details name,specialisation and join year"
for(i=0;i<n2;i++)
PRINT i+1<<" "
INPUT na sp b
t[i]=Teacher(na,sp,b)
samename(t,s,n1,n2)
17 End
OUTPUT 5 B
12 | P a g e
QUESTION 6
Write a menu driven program such that the operations will be performed as per the following inputs
(use operator overloading to perform any arithmetic, relational operations for the object)
Compute the sum of complex object which are prime(that is both real and imaginary part is
prime)
Compute the sum of the complex objects which are equal.
ALGORITHM
1 Start
2 Function: int isPrime(int num)
int i
for(i=2; i<=num/2; i++)
if(num % i == 0)
return 0
return 1
3 class Complex
int real,img
public:
Complex()
Complex(int x,int y):real(x),img(y)
void display()
PRINT real " + " img "i"
Complex operator+(Complex c)
Complex temp
temp.real=real + c.real
temp.img=img + c.img
return (temp)
int operator==(Complex c)
if(real==c.real && img==c.img)
return 1
else
return 0
friend Complex primeSum(Complex c[],int n)
int i
Complex c1(0,0)
for(i=0;i<n;i++)
if(isPrime(c[i].real) && isPrime(c[i].img
c1=c1+c[i]
return (c1)
friend void equalsum(Complex c,Complex c1)
Complex x(0,0)
if(c1==c)
PRINT "Equal complex numbers"
x=c+c1;
PRINT "The sum is : "
13 | P a g e
x.display()
else
PRINT "Not equal"
4 int ch,i,n,x,y,t=1
5 char p
6 PRINT "Enter size of array : "
7 INPUT n
8 Complex c[n]
9 Complex c1
10 Complex c11,c12
11 PRINT "Enter real and imaginary part of complex numbers"
12 for(i=0;i<n;i++)
PRINT i+1 " "
INPUT x y
c[i]=Complex(x,y)
13 PRINT "1 : Print prime sum"
14 PRINT "2 : Print sum of equal numbers"
15 while(t)
PRINT "Enter your choice : "
INPUT ch
switch(ch)
case 1:
c1=primeSum(c,n)
PRINT "The sum of prime complex numbers is : ";
c1.display()
break
case 2:
PRINT "Enter two complex numbers : "
PRINT "1 "
INPUT x y
c11 = Complex(x,y)
PRINT "2 "
INPUT x y
c12=Complex(x,y)
equalsum(c11,c12)
break
default:
PRINT "Invalid input"
PRINT "Do you wish to continue?yes[y] or no[n]? "
INPUT p
if(p=='n')
t=0;
16 End
14 | P a g e
QUESTION 7
Create an employee class with data members e_id, name, birth year, join year , basic salary. Create
three subclasses of the employee class as faculty, technical assistant, staff. Here the data members of
these subclasses are designation, qualification, and grade.
Faculty
Assistant Prof
Sal = basicPay * 6000 * year of experience
Associate Prof
Sal = basicPay * 10000 * year of experience
Prof
Sal = basicPay * 15000 * year of experience
Technical assistant
(BCA/BSc)
Sal = basicPay * 3000 * year of experience
MCA/MSc
Sal = basicPay * 5000 * year of experience
Staff
LDC
Sal = basicPay * 1000 * year of experience
UDC
Sal = basicPay * 2000 * year of experience
User will supply eid and the respective details of the employee will be printed
User will supply designation and the salary amount. The employee with that designation and
salary greater than the amount will be printed.
User will supply designation and year of experience such that employees with that
designation and working experience greater than the supplied experience will be displayed.
ALGORITHM
1 Start
2 class employee
int e_id,dob,join,nsize
string name
double bs
public:
employee(int e,char n[],int b,int j,double sal):
e_id(e),dob(b),join(j),bs(sal)
name=""
nsize=sizeof(n)/sizeof(char)
int i
15 | P a g e
for(i=0;i<nsize;i++)
name=name+n[i]
employee()
double bsalary()
return bs
int exp()
return (2019-join)
int get_id()
return e_id
void display()
PRINT Employee id : " e_id
PRINT "Employee name : " name
PRINT "Employee Date of Birth : " dob
PRINT "Employee Join Date : " join
PRINT "Year of Experience : " exp()
PRINT "Basic Salary : " bs
3 class technicalAssistant:public employee
string qual
public:
technicalAssistant(int e,char n[],int b,int j,double sal,string q):
employee(e,n,b,j,sal),qual(q)
technicalAssistant()
double salary()
double sal
if(qual == "BCA" || qual == "BSc")
sal = bsalary() * 3000 * exp()
else if(qual == "MCA" || qual == "MSc")
sal = bsalary() * 5000 * exp()
else
PRINT "You have entered an invalid qualification"
return sal
void display()
PRINT "TECHNICAL ASSISSTANT"
employee::display()
PRINT "Qualification : " qual
PRINT "Category salary : " salary()
4 class faculty:public employee
string design
public:
faculty(int e,char n[],int b,int j,double sal,string s):
employee(e,n,b,j,sal),desig(s)
faculty()
double salary()
double sal
if(desig == "AssistantProf")
sal = bsalary() * 6000 * exp()
else if(desig == "AssociateProf")
sal = bsalary() * 10000 * exp()
else if(desig == "Prof")
sal = bsalary() * 15000 * exp()
else
PRINT "You have entered an invalid designation"
return sal
void display()
PRINT "FACULTY"
16 | P a g e
employee::display()
PRINT "Designation : " design
PRINT "Category salary : " salary()
friend void search(faculty f[],string d,double sy,int n2)
int i
for(i=0;i<n2;i++)
if(d==f[i].desig && f[i].salary()>=sy)
f[i].display()
friend void search1(faculty f[],string d,int ex,int n2)
int i
for(i=0;i<n2;i++)
if(d==f[i].desig && f[i].exp()>=ex)
f[i].display()
17 | P a g e
t[i]=technicalAssistant(e,name,b,j,sal,q)
19 PRINT "Enter employee id,name,birth year,join year,basic salary and
designation(AssistantProf/AssociateProf/Prof) of faculty "
20 for(i=0;i<n2;i++)
PRINT i+1 " "
INPUT e
gets(name)
fflush(stdin)
INPUT b j sal q
f[i]=faculty(e,name,b,j,sal,q)
21 PRINT "Enter employee id,name,birth year,join year,basic salary and
grade(LDC/UDC) of staff "
22 for(i=0;i<n3;i++)
PRINT i+1 " "
INPUT e
gets(name)
fflush(stdin)
INPUT b j sal q
s[i]=staff(e,name,b,j,sal,q)
23 int id,flag=0
24 PRINT "Enter id to search : "
25 INPUT id
26 for(i=0;i<n1;i++)
if(t[i].get_id()==id)
t[i].display()
flag=1
break;
27 for(i=0;i<n2;i++)
if(f[i].get_id()==id)
f[i].display()
flag=1
break
28 for(i=0;i<n3;i++)
if(s[i].get_id()==id)
s[i].display()
flag=1
break
29 if(flag==0){
30 PRINT "ID does not exist"
31 PRINT "Enter designation(AssistantProf/AssociateProf/Prof) and salary to
search : "
32 INPUT q sal
33 search(f,q,sal,n2)
34 PRINT "Enter designation(AssistantProf/AssociateProf/Prof) and experience to
search : "
35 INPUT q i
36 search1(f,q,i,n2)
37 End
18 | P a g e