0% found this document useful (0 votes)
5 views7 pages

Lab 05

Uploaded by

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

Lab 05

Uploaded by

u2108026
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Electronics & Telecommunication Engineering

Experiment Name: Friend Function and Friend Class in C++.

Name : Mushfiquzzaman Sarker


Id :2108026
Course Code : CSE 284
Course Title : Object Oriented Programming
Experiment No. : 05
Level : 02
Term : 02
Date of Experiment: 14/11/24
Date of Submission: 28/11/24 Group: 01

Remarks:
Experiment Name:
Friend Function and Friend Class in C++.

Objectives:

• To get familiarized with friend class and function in C++.


• To solve some problems using the friend function.
Practice Exercise:

1. Write a C++ Program to display the reverse of a number using the friend
function.

Code:
#include<iostream>
using namespace std;

class Number{
private:
int value;
public:
//constructor
Number(int val):value(val){}
friend void reverseNumber(const Number &num);
};
void reverseNumber(const Number &num){
int reversed = 0, temp = num.value;

while(temp!=0){
int digit = temp % 10;
reversed = reversed * 10 + digit;
temp /= 10;
}
cout << "The reverse of "<< num.value << " is: " << reversed <<endl;
}
int main(){
int num;
cout<<"Enter number: ";
cin>>num;

Number number(num);

reverseNumber(number);

return 0;
}
Output:

2. Write a C++ program to find the number and sum of all integer between 100
and 200 which are divisible by 11 with friend function.

Code:
#include<iostream>
using namespace std;

class Range{
private:
int start,end;
public:
//constructor
Range(int s, int e):start(s),end(e) {}

friend void DivisibleBy11(const Range &range);


};

void DivisibleBy11(const Range &range){


int count=0, sum=0;

cout <<"Numbers divisible by 11 between" << range.start <<" & "


<<range.end << " are " <<endl;

for (int i= range.start;i<=range.end;++i){


if (i%11==0){
cout<< i << " ";
sum += i ;
++count ;
}
}
cout << "\nTotal count: "<<count<<endl;
cout <<"Sum of these numbers: "<< sum << endl;
}

int main(){
Range range(100,200);

DivisibleBy11(range);
return 0;
}

Output:

3. Write a program in C++ to Check Whether a Number can be expressed as


Sum of Two Prime Numbers using the friend function.
Code:
#include<iostream>
#include<cmath>
using namespace std;

class Number{
private:
int value;
public:
Number(int val) : value(val){}

friend void checkSumOfTwoPrimes(const Number &num);


};
bool isPrime(int n){
if(n<=1) return false;
for (int i=2;i<=sqrt(n);++i){
if (n%i==0)return false;
}
return true;
}
void checkSumOfTwoPrimes(const Number &num){
bool found = false ;
cout << "Checking if " << num.value << " can be expressed as the sum of
two prime numbers "<<endl;

for (int i = 2; i<=num.value / 2 ; ++i){


if (isPrime(i) && isPrime(num.value - i)){
cout << num.value << " = " << i << " + " << (num.value-i) <<endl;
found = true;
}
}
if (!found){
cout << num.value << " cannot be expressed as the sum of the two prime
numbers."<< endl;
}
}
int main(){
int input;

cout<<"Enter number: ";


cin >> input;

Number number(input);

checkSumOfTwoPrimes(number);

return 0;
}

Output:
Discussion:
1. This experiment introduces the concept of friend functions and friend classes in
C++, demonstrating their role in providing controlled access to private data.

2. The program utilizes a helper function, isPrime, to verify the primality of


numbers, ensuring only valid prime numbers are considered while finding pairs
that sum to the given input.
3. The friend function checkSumOfTwoPrimes effectively accesses the private
data of the Number class, allowing the program to determine whether a number
can be expressed as the sum of two primes. It successfully identifies cases where
even numbers like 34 have multiple valid pairs, while odd numbers like 23 may
not.

You might also like