Question

In: Computer Science

Write a program that asks the user for an integer and then prints out all its...

Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++

Solutions

Expert Solution

#include<iostream>
#include<limits>
using namespace std;
void printFactors(int n)
{
//printing all factors of n
while (n % 2 == 0)
{
cout << 2 << " ";
n = n/2;
}
for (int i = 3; i*i <= n; i = i + 2)
{
while (n % i == 0)
{
cout << i << " ";
n = n/i;
}
}
if (n > 2)
cout << n << " ";
}

int main()
{
   //reading input and validating it
   int n;
   do{
   cout<<"Enter a number:";
   cin>>n;
   if(!cin.fail())
   break;
   else
   cin.clear();
   cin.ignore(numeric_limits<streamsize>::max(),'\n');
   cout<<"Enter number only\n";
   }
   while(1);
   printFactors(n);
   cout<<endl;
   return 0;
}

output:

Enter a number:s
Enter number only
Enter a number:84
2 2 3 7


Process exited normally.
Press any key to continue . . .



Related Solutions

Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Write a program which prompts the user for a positive integer, and then prints out the...
Write a program which prompts the user for a positive integer, and then prints out the prime factorization of their response. Do not import anything other than the Scanner. One way you might go about this using nested loops: Start a "factor" variable at 2 In a loop: repeatedly print the current factor, and divide the user input by it, until the user input is no longer divisible by the factor increment the factor This plan is by no stretch...
Write a short program that asks the user for a sentence and prints the result of...
Write a short program that asks the user for a sentence and prints the result of removing all of the spaces. Do NOT use a built-in method to remove the spaces. You should programmatically loop through the sentence (one letter at a time) to remove the spaces.
Code in C ++ that asks the user for an integer and that prints whether or...
Code in C ++ that asks the user for an integer and that prints whether or not the number is divisible by three in the integers. If the number is not divisible by three, the program must write a message indicating what the remainder is obtained by dividing the number by three.
Q9) Write a function that asks the user for a number and prints out the multiplication...
Q9) Write a function that asks the user for a number and prints out the multiplication table for that number. Python3 Q10)Write a function that takes two integer inputs for width and length, and prints out a rectangle of stars. (Use * to represent a star). Q11)Write a program that reads in a string and prints whether it: [Hint: given a character like ‘H’ you can apply to it the method ‘H’.isalpha() to check if it is a letter or...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> For this project, you are to: 1) You should validate any data coming from the...
Write a program that prompts a user for an integer from 1 to 99 and prints...
Write a program that prompts a user for an integer from 1 to 99 and prints it as an amount in words. The program will loop in case the user wants to input an additional number. If the user enters -99, the program will exit. Example: Input: 89 Output: Eighty nine Input: 45 Output: Fourty five Input: -99 Output: Have a nice day. <program exits> c++ project. need help.
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT