Using - C
#include<stdio.h>
void main() {
int num, i;
printf("Enter a Positive Integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for(i = 1; i <= num; ++i) {
if(num % i == 0) {
printf("%d ", i);
}
}
}
OUTPUT
Enter a Positive Integer: 12
Factors of 12 are: 1 2 3 4 6 12
Using - C++
#include<iostream>
using namespace std;
int main() {
int num;
cout<<"Enter a Positive Integer: ";
cin>> num;
cout<<"Factors of "<< num<< " are: ";
for(int i=1; i <= num; ++i) {
if(num % i == 0) {
cout<< i<< " ";
}
}
return 0;
}
OUTPUT
Enter a Positive Integer: 6
Factors of 6 are: 1 2 3 6