Write a c++ program that displays the integers between 1 and 100 that are divisible
by 6 or 7 but not both
Answers
Answer:
Hope this will help you
Explanation:
For(n=1;n<=100;n++)
{
if((n%7==0)||(n%6==0)
{
if((n%7==0)&&(n%6==0))
{ count=0
continue;
}
else
cout<<n;
}
}
XOR Implementation - C++
This is a very specific question, where we need numbers from 1 to 100 that are divisible by 6 or by 7, but not by both.
This kind of operation is known as the Exclusive OR (XOR) Operation.
The XOR returns true when either one of the two conditions is true, but returns false when both conditions are true.
[This is different from OR. The OR operation returns true also when both conditions are true. But in this case, XOR returns false.]
So, XOR is the thing we are going to use.
In C++, the XOR Operator is denoted by the ^ (Caret) sign.
So, in general:
Condition 1 ^ Condition 2
is the XOR Operation.
We can check divisibility by using the Modulo Operator. It is denoted by % and returns the remainder of a division. So, num%6==0 would be a condition asking if the number num is divisible by 6 or not.
Similarly, num%7==0 would be a condition asking whether the number num is divisible by 7 or not.
Combining them with the XOR Operation would be:
num%6==0 ^ num%7==0
Here's a C++ program accomplishing the required task.
#include <iostream>
using namespace std;
int main()
{
cout << "The numbers between 1 and 100 which are divisible by 6 or 7, but not by both, are:" << endl;
//Run a loop from 1 to 100
for(int i=1;i<=100;i++)
{
//Check divisibility by 6 and by 7
//Combine the two by the XOR Operator ^
if(i%6==0 ^ i%7==0)
{
cout << i << endl;
}
}
return 0;
}