You will learn how to determine all the factors of an integer entered by the user in this example.
To follow along with this example, you should be familiar with the following C programming concepts:
- C if...else Statement.
- C for Loop.
- C while and do...while Loop.
- C break statement.
- C Continue statement.
Factors of a Positive Integer
This program accepts a positive integer from the user and displays all of the number's positive factors.
#include <stdio.h>
#include <conio.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);
}
}
getch();
}
Expected Output:-
Tags:
C if...else Statement.
C for Loop.
C while and do...while Loop.
C break and continue.
C Program to Display Factors of a Number
mskuthar