In: Computer Science
Read a number N and print all its divisors.
The code is in C++.
#include <iostream>
using namespace std;
int main()
{
int number; //To store the Input from user
//Message for the User to print the Number
cout<<"Enter any number(+ve): ";
cin>>number; //Storing the User's input in number
//Printing the Divisors of number
cout<<"Divisors of "<<number<<" are: ";
//For each i=1 that is less than equal to given number
//Checking if that i can divide given number or not.
//If i divides the number then its our Divisors, So print it.
//Else check for next i.
for (int i=1;i<=number;i++)
{
//Ch if i can divide number or not
if (number%i==0)
{
//Printing i as its a Divisors
cout<<i<<" ";
}
}
return 0;
}
Output Screenshot:
Code Screenshots: