MY CODE THE RIGHT WAY( HOW I WANT MY CODE TO LOOK) #include <iostream> using nam
ID: 672760 • Letter: M
Question
MY CODE
THE RIGHT WAY( HOW I WANT MY CODE TO LOOK)
#include <iostream>
using namespace std;
int main()
{
// your code goes here
int mn,mx,i,j;
// dclaring the varibles
cout<<"Enter low interger: ";
cin>>mn;
//ask input for lower end
if(mn<1)
{
cout<<"number must be greater than 1";
//display error message
while(mn<1)
{
//while loop that itreates until positive value enterd
cin>>mn;
if(mn>1)
{
//if value is grater than 1 exit the loop
break;
}
cout<<"Number must be greter than 1";
}
}
cout<<"Enter high interger: ";
//enter maximum value
cin>>mx;
//take max value
if(mx<mn)
{
//if max value is grater tahn the min value display error message
cout<<"Number must be greater or equal to "<<mn;
while(mx<mn)
{
//while loop that iterates until appropriate value is entered
cin>>mx;
if(mx>=mn)
{
//if appropriate value is entered break the loop
break;
}
cout<<"Number must be greater or equal to "<<mn;
}
}
for(i=mn;i<=mx;i++){ //outer for loop that iterates from min value to max value
for(j=1;j<i;j++){//inner for loop that iterateas less tahn the max value
if(i %j==0){ //if the number is factor of given value print the numbewr
cout<<" Factors of all numbers in the range: [ "<<mn<<" , "<<mx<<" ]" " "
<<i<<":"<<j<<", ";
}
}
}
return 0;
}
Explanation / Answer
# include <iostream>
#include<math.h>
using namespace std;
void factors(int n)
{
int temp = n;
cout<<"1, ";
while (n%2 == 0)
{
cout<<2<<", ";
n = n/2;
}
for (int i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
cout<<i<<", ";
n = n/i;
}
}
if (n > 2)
cout<<n<<", ";
if( n != temp)
cout<<temp;
}
int main()
{
int low, high;
cin>>low;
cin>>high;
for(int i = low; i <= high; i++){
cout<<i<<": ";
factors(i);
cout<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.