Question

Write a program asking the user for an integer then find the lowest factor that is...

Write a program asking the user for an integer then find the lowest factor that is not a 2, 3, or 5. Hint: Use while or do-while with a flag. Think this way - you have found the answer when you find the lowest factor that is not divisible by 2, 3, or 5 (hint: %) but the number % factor = zero. Start factor at 2 then increment until the answer is found, then change the flag to stop the loop.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer actually is 1 for the question u have provided what ever be the input. because 1 divides any number.

Assuming you need a number greater than or equal to 2, I've written the following code. The code will return -1 if no factor with required conditions is found.

*** CODE STARTS HERE ***

#include <bits/stdc++.h>
using namespace std;

int fact(int n){
   int flag = 0;
   int ans = 0;
   for(int i=2; i<=n; i++){
       if(n%i == 0){
           if(i%2 ==0 || i%3 == 0 || i%5 == 0){
               continue;
           }
           else{
               flag = 1;
               ans = i;
               break;
           }
       }
   }
   if(flag == 1) return ans;
   else return -1;
}

int main() {
   int n;
   cin >> n;
   cout << fact(n) ;
   return 0;
}

***CODE ENDS HERE ***

Do comment if you have any queries.

Add a comment
Know the answer?
Add Answer to:
Write a program asking the user for an integer then find the lowest factor that is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program asking the user for an integer then find the lowest factor that is...

    Write a program asking the user for an integer then find the lowest factor that is not a 2, 3, or 5. Hint: Use while or do-while with a flag. Think this way - you have found the answer when you find the lowest factor that is not divisible by 2, 3, or 5 (hint: %) but the number % factor = zero. Start factor at 2 then increment until the answer is found, then change the flag to stop...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Write a complete C++ program that do following. 1) Read a positive integer from the user...

    Write a complete C++ program that do following. 1) Read a positive integer from the user with proper prompt. Assume that this user is nice and he/she would not start the integer with digit 0. 2) Create a size 10 array as counters for digits 0 - 9. 3) Use a while loop for processing a) Single out the current last digit using modular 10 technique b) Increment the corresponding counter through the index derived in a). 4) At the...

  • Write a program in a script file that finds the smallest odd integer that is also...

    Write a program in a script file that finds the smallest odd integer that is also divisible by 3 and whose cube is greater than 4000. Use a loop in the program. The loop should start from 1 and stop when the number is found. The program should print the message: The required number is: [and then prints the number]

  • Write a complete program that will prompt the user for an integer You must keep asking...

    Write a complete program that will prompt the user for an integer You must keep asking the user until they give you a number that is greater than 700. Once they give a valid number, display all multiples of 7 from 7 to that number inclusively). You may assume the user will give you correct type of input, but your code must continue to prompt them until they give a number greater than 700.

  • In Java Write a program that will ask the user for integers and print if the...

    In Java Write a program that will ask the user for integers and print if the integer is positive, negative or zero. The program should continue asking for integer until the user enters a negative value.

  • Write a program that prompts the user for positive integers, only stopping when a negative integer...

    Write a program that prompts the user for positive integers, only stopping when a negative integer or zero is given. The program should then print out how many of the positive integers were odd. Example: Enter a positive integer (0 or negative to stop): 9 Enter a positive integer (0 or negative to stop): 4 Enter a positive integer (0 or negative to stop): 7 Enter a positive integer (0 or negative to stop): -3 You entered 2 odd integers....

  • In this assignment we are asking the user to enter two number values, the starting and...

    In this assignment we are asking the user to enter two number values, the starting and ending numbers for our application. Having these values, we then construct a loop that will increment by one (1) from the starting number through (and including) the ending number. Within this loop we will check the current value of our number to see if it is evenly divisible by 3, then by 5, and then by both 3 and 5. We will output all...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT