Learn about C Program to Find Factors of a Number using recursion in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C Program to Find Factors of a Number using recursion
A number’s factors are numbers that split the original number evenly or perfectly. A factor is defined as a whole number that may divide a larger number equally. A fraction is not a factor. Each prime number has only two factors, namely 1 and the number itself, however all composite numbers include more than two factors, which include prime factors as well.
Source code:
#include<stdio.h>
void factors(int number, int i)
{
if ( i > number/2 ) return;
if(number%i == 0) printf("%d ", i);
factors(number, i+1);
}
int main()
{
int num;
printf("Enter number: ");
scanf("%d",&num);
printf("Factors of %d are:\n", num);
factors(num, 1);
return 0;
}
Output:
Enter number: 6
Factors of 6 are:
1 2 3
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C Program to Find Factors of a Number using a function
C program to find the sum of factors of a number