you will learn to find all the factors of an integer entered by the user.
To understand this example, you should have the knowledge of the following C programming topics:
- C Programming Operators
- C if...else Statement
- C for Loop
This program takes a positive integer from the user and displays all the positive factors of that number.
Factors of a Positive Integer
#include <stdio.h>
int 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);
}
}
return 0;
}
Output
Enter a positive integer: 60 Factors of 60 are: 1 2 3 4 5 6 10 12 15 20 30 60
In the program, a positive integer entered by the user is stored in num.
The for
loop is iterated until i is false.
In each iteration, whether num is exactly divisible by i is checked. It is the condition for i to be a factor of num.
if (num % i == 0) {
printf("%d ", i);
}
Then the value of i is incremented by 1.
HTML tutorial or HTML 5 tutorial provides basic and advanced concepts of HTML. Our HTML tutorial is developed for beginners and professionals. In our tutorial, every topic is given step-by-step so that you can learn it in a very easy way. If you are new in learning HTML, then you can learn HTML from basic to a professional level and after learning HTML with CSS and JavaScript you will be able to create your own interactive and dynamic website. But Now We will focus on HTML only in this tutorial.
ReplyDelete