Ask a Teacher



Write a C++ program to input a number and display all of its factors. (For example, if the input is 28, the output should be 1, 2, 4, 7, 14, 28).

#include<iostream.h>
#include<stdio.h>
int main()
{
  int n,i;
  cout<<"Enter a positive integer: "<<endl;
  cin>>n;
  cout<<"Factors of <<n<<"are :"<<endl;
  for(i=1;i<=n;++i)
  {
      if(n%i==0)
         printf("%d ",i);
  }
  return 0;
}


comments powered by Disqus