C program to display all factors of the input number in C programming.
Introduction:
This article will teach you how to write a C program that will display all factors of a given input number. The numbers that divide a given number without leaving a remainder are known as factors. By locating and displaying the factors, we can gain insight into a number's divisibility and use this information for a variety of mathematical operations.
What exactly are Factors?
Factors are numbers that divide a given number evenly, leaving no remainder. The factors of 12 are, for example, 1, 2, 3, 4, 6, and 12. These numbers divide 12 evenly without a remainder.
Coding:
#include <stdio.h>
int main()
{
int i, num;
printf("Enter any number : ");
scanf("%d", &num);
printf("All factors of %d are: \n", num);
for(i=1; i<=num; i++)
{
if(num % i == 0)
{
printf("%d, ",i);
}
}
}
Explanation of the program:
The above program is a simple C program that prints all the factors of a number entered by the user. The program first declares two variables, i and num, and initializes i to 1. Then, it enters a loop that will continue as long as i is less than or equal to num. Inside the loop, the program checks if num is divisible by i. If it is, the program prints i followed by a comma. The loop will terminate when i is equal to num + 1, at which point the program will exit.
Output:
Conclusion:
We looked at how to write a C program that displays all factors of a given input number in this article. We talked about the concept of factors and the steps involved in finding and displaying them. We can easily determine the factors of any positive integer and use this information for various mathematical calculations by implementing this program.
No comments:
Post a Comment