Monday, 30 June 2014

Factors of an Integer

Write a program to print all the factors of a positive integer A.

INPUT:
A single integer A

OUTPUT:
Factors of the number A, in ascending order, separated by whitespace. 1 and A are also factors of A.

CONSTRAINTS:
2 <= A <= 10000

Solution:

#include<stdio.h>
int main()
{
int number;
int i=1;
scanf("%d",&number);
printf("%d",i);
for(i=2;i<=number;i++) //iterating over all numbers from 2 to number
{
if(number%i==0) //checking whether 'i' is a factor of number or not
{
printf(" %d",i); //printing a factor with a preceding whitespace
}
}
return 0;
}

No comments: