Learn about Program to find factor Using while loop in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Program to find factor Using while loop
The below program takes a number as input from the user. Then using a while loop we will iterate over the numbers from 1 to n/2 and find the factors.
Source code:
#include<stdio.h>
int main()
{
int num, i=1;
printf("Enter number: ");
scanf("%d",&num);
printf("Factors of %d are:\n", num);
while (i<=num/2) {
if(num%i==0) printf("%d\t",i);
i++;
}
return 0;
}
Output:
Enter number: 16
Factors of 16 are: 1 2 4 8
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Program to find factors of a number in C using for loop
C Program to Find Reverse of a Number using for loop