Write a a c++ program to find all Even factor of a number
Answers
The given problem is solved using language - C++.
#include <iostream>
using namespace std;
int main() {
int n,i;
cout<<"Enter a number: ";
cin>>n;
if(n%2!=0)
cout<<"Number is odd. There is no even factors.";
else{
cout<<"The even factors of the number are: \n";
for(i=2;i<=n;i+=2){
if(n%i==0)
cout<<i<<" ";
}
}
return 0;
}
- Accept the number from the user.
- If the number is odd, Display message - "No even factors."
- If the number is even - Loop through each even numbers in the range and check if the number is divisible by any even number or not. If true, display it.
See attachment for output.
Answer:
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a positive number:";
cin >> num;
if(n%2!=0)
cout<<"Number is odd. There is no even factors.";
else{
cout << "Factors of " << num << " are : " << endl;
}
for (int i = 1; i <= num; i++)
{
if (num % i == 0 && i%2==0)
cout << i << "\t";
}
return 0;
}
Explanation:
Hope it helps pls like if you are able to understand this program