Coding Practice

Write a C program to find all factors of a number

Find all factors of a number
Sample Output
Find all factors of a number.

Number: 12
Factors are: 1 2 3 4 6 12
Source Code
#include<stdio.h>

int main()
{
    int i, number = 100;

    printf("Find all factors of a number.\n\nNumber: ");
    scanf("%d", &number);

    printf("Factors are: ");
    for(i = 1; i <= number; i++)
    {
        if((number % i) == 0)
        {
            printf("%d ", i);
        }
    }
    printf("\n\n");

    return 0;
}
Sample Output
Find all factors of a number.

Number: 100
Factors are: 1 2 4 5 10 20 25 50 100

No comments:

Post a Comment

Change Theme
X