1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <iostream> using namespace std; // Function to check divisibility using pass-by-value bool isDivisibleByValue(int num) { return (num % 5 == 0 && num % 11 == 0); } // Function to check divisibility using pass-by-reference void isDivisibleByReference(int num, bool &divisibleBy5, bool &divisibleBy11, bool &divisibleByBoth) { divisibleBy5 = (num % 5 == 0); divisibleBy11 = (num % 11 == 0); divisibleByBoth = (num % 5 == 0 && num % 11 == 0); } int main() { int number; bool divisibleBy5, divisibleBy11, divisibleByBoth; // Input the number cout << "Enter a number: "; cin >> number; // Check divisibility using pass-by-value if (isDivisibleByValue(number)) { cout << "The number is divisible by both 5 and 11 (pass-by-value)." << endl; } else { cout << "The number is not divisible by both 5 and 11 (pass-by-value)." << endl; } // Check divisibility using pass-by-reference isDivisibleByReference(number, divisibleBy5, divisibleBy11, divisibleByBoth); cout << "Using pass-by-reference:" << endl; cout << "Divisible by 5: " << (divisibleBy5 ? "Yes" : "No") << endl; cout << "Divisible by 11: " << (divisibleBy11 ? "Yes" : "No") << endl; cout << "Divisible by both 5 and 11: " << (divisibleByBoth ? "Yes" : "No") << endl; return 0; } |
Explanation:
- Pass-by-Value (
isDivisibleByValue
):- Function Definition:
bool isDivisibleByValue(int num)
- This function takes a single integer
num
as input by value (i.e., a copy of the number). - It checks if
num
is divisible by both 5 and 11 using the modulus operator. - Returns
true
if divisible by both, otherwisefalse
.
- Function Definition:
- Pass-by-Reference (
isDivisibleByReference
):- Function Definition:
void isDivisibleByReference(int num, bool &divisibleBy5, bool &divisibleBy11, bool &divisibleByBoth)
- This function takes an integer
num
and three reference variables (divisibleBy5
,divisibleBy11
,divisibleByBoth
). - It calculates and assigns the results directly to the reference variables:
divisibleBy5
indicates ifnum
is divisible by 5.divisibleBy11
indicates ifnum
is divisible by 11.divisibleByBoth
indicates ifnum
is divisible by both 5 and 11.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input a number.
- It then checks if the number is divisible by 5 and 11 using both methods and prints the results.
Example Outputs:
Example 1:
Input:
- Number:
55
Output:
1 2 3 4 5 6 |
Enter a number: 55 The number is not divisible by both 5 and 11 (pass-by-value). Using pass-by-reference: Divisible by 5: Yes Divisible by 11: Yes Divisible by both 5 and 11: Yes |
Example 2:
Input:
- Number:
30
Output:
1 2 3 4 5 6 |
Enter a number: 30 The number is not divisible by both 5 and 11 (pass-by-value). Using pass-by-reference: Divisible by 5: Yes Divisible by 11: No Divisible by both 5 and 11: No |
Example 3:
Input:
- Number:
22
Output:
1 2 3 4 5 6 |
Enter a number: 22 The number is not divisible by both 5 and 11 (pass-by-value). Using pass-by-reference: Divisible by 5: No Divisible by 11: Yes Divisible by both 5 and 11: No |