Write a function to compute the greatest non-trivial factor of a given natural n
ID: 3666729 • Letter: W
Question
Write a function to compute the greatest non-trivial factor of a given natural number. Note that a natural number can have multiple factors; for example, 1, 2, 3, 4, 6 and 12 are all factors of 12 Factors different from 1 and the number itself are said to be non-trivial. The function should be named greate3t_trivial_factor and be able to accept a natural number as argument. It returns the greatest non-trivial factor of the passed-in argument. If no greatest non-trivial factor can be found, your function should return None, a special value in Python Function specifications: Function name greatest_trivial_factor Input parameter A natural number return value the greatest non-trivial factor of the passed-in argument (note that if the greatest non-trivial factor does not exist, the return value should be None)Explanation / Answer
program in c++
#include<iostream>
using namespace std;
int greatest_trivial_factor(int num)
{
int greatestNum = 0;
for (int i = 2; i <num; ++i)
{
if (num % i == 0)
{
if(greatestNum < i)
{
greatestNum = i;
}
}
}
return greatestNum;
}
int main()
{
int naturalNumber;
cout<<"Enter the natural number"<<endl;
cin>>naturalNumber;
int greatest_trivial_fact = greatest_trivial_factor(naturalNumber);
cout<<"The greatest trivial factor is: "<<greatest_trivial_fact<<endl;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.