











Coding Assignment 5 – Solution


Coding Assignment - 5 Solution
Solution of Coding Assignment 4 in C++ only.
Note : There might be some other possible solution of these questions. Solution provided here are just for learning purpose to get an idea about the possible logic used to solve the questions.
Ques . 1 Write a Program in C++ to swap two numbers using friend function
Sample Input and Output
Input : a = 5, b = 9
Output : a = 9, b = 5
Input : a = 4, b = 6
Output : a= 6, b = 4
#include<iostream>
using namespace std;
class Swap {
int num;
public:
Swap(int num)
{
this->num = num;
}
friend void swap(Swap&, Swap&);
};
void swap(Swap& s1, Swap& s2)
{
int temp;
cout << "\nBefore Swapping: " << s1.num << " " << s2.num;
temp = s1.num;
s1.num = s2.num;
s2.num = temp;
cout << "\nAfter Swapping: " << s1.num << " " << s2.num;
}
int main()
{
Swap s1(6), s2(10);
swap(s1,s2);
return 0;
}
Ques. 2 Write a C++ program to display the record of the student with the highest percentage using this pointer.
Sample Input and Output
Enter data
Name:Paul
Age:24
Roll:11
Percent:79
Enter data
Name:Reem
Age:21
Roll:9
Percent:87
Enter data
Name:Philip
Age:23
Roll:5
Percent:81
Student with highest percentage
Name:Reem
Age:21
Roll:9
Percent:87
#include <iostream>
using namespace std;
class student
{
char name[100];
int age,roll;
float percent;
public:
void getdata()
{
cout<<"Enter data"<<endl;
cout<<"Name:";
cin>>name;
cout<<"Age:";
cin>>age;
cout<<"Roll:";
cin>>roll;
cout<<"Percent:";
cin>>percent;
cout<<endl;
}
student & max(student &s1,student &s2)
{
if(percent>s1.percent && percent>s2.percent)
return *this;
else if(s1.percent>percent && s1.percent>s2.percent)
return s1;
else if(s2.percent>percent && s2.percent>s1.percent)
return s2;
}
void display()
{
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Roll:"<<roll<<endl;
cout<<"Percent:"<<percent;
}
};
int main()
{
student s,s1,s2,s3;
s1.getdata();
s2.getdata();
s3.getdata();
s=s3.max(s1,s2);
cout<<"Student with highest percentage"<<endl;
s.display();
getch();
return 0;
}
Ques. 3 Write a C++ Program to find out that a given number is odd or even using a unary function.
Sample Input and Output
Please enter a number: 2
Number 2 is even.
#include <iostream>
#include <functional>
struct IsOdd : public std::unary_function<int,bool> {
bool operator() (int number) {return (number%2!=0);}
};
int main () {
IsOdd IsOdd_object;
IsOdd::argument_type input;
IsOdd::result_type result;
std::cout << "Please enter a number: ";
std::cin >> input;
result = IsOdd_object (input);
std::cout << "Number " << input << " is " << (result?"odd":"even") << ".\n";
return 0;
}
Ques. 4 Write a C++ program to find whether the given 2 numbers are equal or not using binary function.
Sample Input and Output
Please enter first number: 2
Please enter second number: 33
Numbers 2 and 33 are not equal.
#include <iostream>
#include <functional>
struct Compare : public std::binary_function<int,int,bool> {
bool operator() (int a, int b) {return (a==b);}
};
int main () {
Compare Compare_object;
Compare::first_argument_type input1;
Compare::second_argument_type input2;
Compare::result_type result;
std::cout << "Please enter first number: ";
std::cin >> input1;
std::cout << "Please enter second number: ";
std::cin >> input2;
result = Compare_object (input1,input2);
std::cout << "Numbers " << input1 << " and " << input2;
if (result)
std::cout << " are equal.\n";
else
std::cout << " are not equal.\n";
return 0;
}
Ques. 5 Write a C ++ program to find out the highest of three numbers using a relational operator.
Sample Input and Output
Please enter first number: 2
Please enter second number: 33
Please enter Third number : 29
Largest Number : 33
Ques. 6 Write a C++ program to print the types of animal using run time polymorphism.
Sample Input and Output
Animal
Carnivorous Animals
Herbivorous Animals
Omnivorous Animals
Ques. 7 Write a C ++ program to find the factorial of a function
Sample Input and Output
Enter a number: 5
Factorial of entered number: 120
#include
using namespace std;
int factorial(int n){
if (n <= 1)
return 1;
else
return n*factorial(n-1);
}
int main(){
int num;
cout<<"Enter a number: ";
cin>>num;
cout<<"Factorial of entered number: "<<factorial(num);
return 0;
}
Ques. 8 Write a C++ Program to print the Fibonnaci series using recursion.
Sample Input and Output
Enter the number of elements: 10
0 1 1 2 3 5 8 13 21 34
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main(){
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
}
Ques. 9 Write a C++ program where you have to print the marks of two students in two subjects, and put the marks1 = marks2 using assignment operator overloading concept.
Sample Input and Output
Marks of first student :
Mark in 1st Subject : 45
Marks in 2nd Subject : 89
Marks of Second student :
Mark in 1st Subject : 36
Marks in 2nd Subject : 59
Marks of First student :
Mark in 1st Subject : 36
Marks in 2nd Subject : 59
#include<iostream>
using namespace std;
class Marks
{
private:
int m1;
int m2;
public:
//Default constructor
Marks() {
m1 = 0;
m2 = 0;
}
// Parametrised constructor
Marks(int i, int j) {
m1 = i;
m2 = j;
}
// Overloading of Assignment Operator
void operator=(const Marks &M ) {
m1 = M.m1;
m2 = M.m2;
}
void Display() {
cout << "Marks in 1st Subject:" << m1;
cout << "Marks in 2nd Subject:" << m2;
}
};
int main()
{
// Make two objects of class Marks
Marks Mark1(45, 89);
Marks Mark2(36, 59);
cout << " Marks of first student : ";
Mark1.Display();
cout << " Marks of Second student :";
Mark2.Display();
// use assignment operator
Mark1 = Mark2;
cout << " Mark in 1st Subject :";
Mark1.Display();
return 0;
}
Ques. 10 Write a C++ program to overload binary operator ‘+’ to add two complex numbers. Using binary operator overloading
Input and Output
Enter First Number : 5 6
Enter Second Number : 3 8
Result : 8 + 14i
#include<iostream>
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
Complex operator+(Complex obj) //Overloading '+' operator
{
Complex c;
c.num1=num1+obj.num1;
c.num2=num2+obj.num2;
return(c);
}
void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
int main()
{
Complex c1, c2, sum; //Created Object of Class Complex i.e c1 and c2
c1.accept(); //Accepting the values
c2.accept();
sum = c1+c2; //Addition of object
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display(); //Displaying user input values
cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display(); //Displaying the addition of real and imaginary numbers
return 0;
}
Ques. 11 Write a C++ program to find out of two number which one is greater than other or equal than other using relational operator
Input and Output
//if a = 25, b = 5;
A is Big
//if a = 5, b = 25;
B is Big
//if a = 5, b = 5;
B is Big
#include<iostream>
using namespace std;
int main ()
{
int a = 25, b = 5;
//int a = 5, b = 25;
//int a = 5, b = 5;
if( a>b )
cout<<"A is Big";
else if( a== b)
cout<<"A and B are Equal";
else
cout<<"B is Big";
return 0;
}
Ques. 12 Write a C++ program to increment the time by +1 and show the correct time using Overloading Increment ++ Operator
Input and Output
H: 12 M:0
H: 12 M:1
H: 10 M:41
H: 10 M:42
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int minutes;
public:
Time() {
hours = 0;
minutes = 0;
}
Time(int h, int m) {
hours = h;
minutes = m;
}
void displayTime() {
cout << "H: " << hours << " M:" << minutes <<endl;
}
Time operator++ () {
++minutes;
if(minutes >= 60) {
++hours;
minutes -= 60;
}
return Time(hours, minutes);
}
Time operator++( int ) {
Time T(hours, minutes);
++minutes;
if(minutes >= 60) {
++hours;
minutes -= 60;
}
return T;
}
};
int main() {
Time T1(11, 59), T2(10,40);
++T1;
T1.displayTime();
++T1;
T1.displayTime();
T2++;
T2.displayTime();
T2++;
T2.displayTime();
return 0;
}
Ques. 13 Write a C++ Program to Add Two Distances (in inch-feet) System Using Structures
Input and Output
Enter 1st distance,
Enter feet: 6
Enter inch: 3.4
Enter information for 2nd distance
Enter feet: 5
Enter inch: 10.2
Sum of distances = 12 feet 1.6 inches
#include <iostream>
using namespace std;
struct Distance{
int feet;
float inch;
}d1 , d2, sum;
int main()
{
cout << "Enter 1st distance," << endl;
cout << "Enter feet: ";
cin >> d1.feet;
cout << "Enter inch: ";
cin >> d1.inch;
cout << "\nEnter information for 2nd distance" << endl;
cout << "Enter feet: ";
cin >> d2.feet;
cout << "Enter inch: ";
cin >> d2.inch;
sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;
// changing to feet if inch is greater than 12
if(sum.inch > 12)
{
++ sum.feet;
sum.inch -= 12;
}
cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches";
return 0;
}
Ques. 14 Write a C++ Program to Calculate Difference Between Two Time Period
Input and Output
Enter hours, minutes and seconds respectively: 11
33
52
Enter stop time.
Enter hours, minutes and seconds respectively: 8
12
15
TIME DIFFERENCE: 11:33:52 – 8:12:15 = 3:21:37
#include <iostream>
using namespace std;
struct TIME
{
int seconds;
int minutes;
int hours;
};
void computeTimeDifference(struct TIME, struct TIME, struct TIME *);
int main()
{
struct TIME t1, t2, difference;
cout << "Enter start time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t1.hours >> t1.minutes >> t1.seconds;
cout << "Enter stop time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t2.hours >> t2.minutes >> t2.seconds;
computeTimeDifference(t1, t2, &difference);
cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":" << t1.seconds;
cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds;
cout << " = " << difference.hours << ":" << difference.minutes << ":" << difference.seconds;
return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *difference){
if(t2.seconds > t1.seconds)
{
--t1.minutes;
t1.seconds += 60;
}
difference->seconds = t1.seconds - t2.seconds;
if(t2.minutes > t1.minutes)
{
--t1.hours;
t1.minutes += 60;
}
difference->minutes = t1.minutes-t2.minutes;
difference->hours = t1.hours-t2.hours;
}
Ques. 15 Write a C++ Program for Addition of two n x n matrix in C++
Input and Output
Input
3
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
Output
12 14 16
18 20 22
24 26 28
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
return 0;
}
Ques. 16 Write a C++ program to print the sum of 2 digit number or 3 digit number depending on the input taken using polymorphism.
Sample Input and Output
// if number is 10, 20, 30
60
//If number if 15, 30
45
#include <iostream>
using namespace std;
class Sum {
public:
int add(int num1,int num2){
return num1 + num2;
}
int add(int num1, int num2, int num3){
return num1 + num2 + num3;
}
};
int main(void) {
//Object of class Sum
Sum obj;
//This will call the second add function
cout<<obj.add(10, 20, 30)<<endl;
//This will call the first add function
cout<<obj.add(11, 22);
return 0;
}
Ques. 17 Write a C++ program to convert decimal number to binary number
Sample Input and Output
Enter a decimal number: 109
109 in decimal = 1101101 in binary
#include <iostream>
#include <math.h>
using namespace std;
//function to convert decimal to binary
long convert(int n)
{
long binary = 0;
int i = 1;
//converting decimal to binary
while (n!=0)
{
int rem = n%2;
n /= 2;
binary += rem*i;
i *= 10;
}
return binary;
}
//main program
int main()
{
int decimal;
long binary;
cout << "Enter a decimal number: ";
//user input
cin >> decimal;
//calling function
binary = convert(decimal);
cout << decimal << " in decimal = " << binary << " in binary" << endl ;
return 0;
}
Ques. 18 Write a C++ program to change all the uppercase letters into lowercase and lowercase letters into uppercase.
Sample Input and Output
Input
PREPinsta placeMENt
Output
prepINSTA PLACEmenT
Ques. 19 Write a C++ program to add two fractions
Sample Input and Output
Enter numerator and denominator of first number: 9
7
Enter numerator and denominator of second number: 5
8
9/7 + 5/8 = 107/56
#include <iostream>
using namespace std;
//main Program
int findGCD(int n1, int n2)
{
int gcd;
for(int i=1; i <= n1 && i <= n2; i++)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
return gcd;
}
int main()
{
int num1,den1;
cout << "Enter numerator and denominator of first number: ";
//user input
cin >> num1 >> den1;
int num2,den2;
cout << "Enter numerator and denominator of second number: ";
//user input
cin >> num2 >> den2;
//finding lcm of the denominators
int lcm = (den1*den2)/findGCD(den1,den2);
//finding the sum of the numbers
int sum=(num1*lcm/den1) + (num2*lcm/den2);
//normalizing numerator and denominator of result
int num3=sum/findGCD(sum,lcm);
lcm=lcm/findGCD(sum,lcm);
//printing output
cout<<num1<<"/"<<den1<<" + "<<num2<<"/"<<den2<<" = "<<num3<<"/"<<lcm;
return 0;
}
Ques. 20 Write a C++ Program to check whether a number be expressed as a sum of two prime numbers
Sample Input and Output
Enter a positive integer: 15
15 = 2 + 13
#include<iostream>
using namespace std;
// Function to check prime number
int Prime(int num)
{
int div=0;
for(int i=1;i<=num;i++)
{
if(num%i==0)
div++;
}
if(div==2)
return 1;
return 0;
}
int main()
{
int check = 0, n;
cout<< "Enter a positive integer: ";
//user input
cin>>n;
for(int i = 1; i <= n/2;i++)
{
// condition for i to be a prime number
if (Prime(i))
{
// condition for n-i to be a prime number
if (Prime(n-i))
{
cout<<n <<" = "<< i <<" + " << n-i<< endl;
check = 1;
}
}
}
if (check == 0)
cout<<n<<" cannot be expressed as the sum of two prime numbers.";
return 0;
}