problem 1
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 plus plus
Program :
#include<iostream>
using namespace std;
int main() {
int num, i;
cout<<"Enter a number:";
cin>>num;
cout << "The factors of " << num << " are : ";
for(i=2; i <= num; i++) {
if (num % i == 0)
{
cout<<i<<" ";
num=num/i;
i=i-1;
}
}
return 0;
}
Output :
Thank you Have a great day .Please do like!!!
Get Answers For Free
Most questions answered within 1 hours.