# Program to find Factors of a number in C Language

## Program Description:

Write a Program to find all Factors of a number in C programming language. The program should accept number from the user and find all Factors of the number. Make sure program should display error on Invalid inputs ( like negative numbers)

## What are the Factors of a Number?

The factors of a number are All numbers that evenly divide the given number without any remainder.

Letâ€™s take number 20 , The factors of number 20 are 1, 2, 4, 5, 10, 20. Since 20 is evenly divisible by all the above numbers. So we can say 1, 2, 4, 5, 10, 20 as factors of number 20.

Here is a few Numbers and Their Factors.

## Expected Input and Output of the program:

Input:

Enter a Number to calculate factors: 10

Output:

Factors of 10 are : 1 2 5 10

## Program Pre-Requisites:

It is recommended to have the knowledge of the C Loops and modulus operator. Please go through following articles to learn more about these topics.

## Factors of a number in C Program Algorithm:

1. Take the input from the user and store it in a variable, Letâ€™s say we are using variable num
2. Check if num is positive or negative. If num is negative number, Display an error message and exit the program.
3. If the num is positive continue further.
4. To be factor of a number num, The factor must evenly divide the num. So all the numbers from 1 to num, which evenly divide the num are the Factors of the give number num.
5. Start the loop, The loop goes from 1 to num. At each iteration
• check if the num is evenly divided by i using Modulus operator â€“ num % i == 0, If this condition is True, Then i is Factor of num, So display the i
• Increment the value of i by 1 and go to next iteration.
6. The Step-5 continues until the loop condition i <= num becomes False. And we will get All Factors of the number

## Factors of a number in C using while loop:

### Program Output:

Compile and run the above program. We are using the GCC compiler.

To compile the program using the GCC compiler use the following command.

gcc factors.c

The above command creates the executable file named a.out. So run it using the following command.

./a.out

Letâ€™s try few more examples.

What if the user enters a Negative number.

As you can see from the above outputs, The program is giving the All factors of the number and Also doing the error check and displaying the error notice on Invalid input.

## Factors of a number in C using for loop and goto statement:

Letâ€™s try to write the above program using the for loop and we also add the functionality to allow the user to provide the input again incase of the invalid input.

To do that, We are going to use the Goto Statement, Which is a Jump statement used to jump from one statement to another statement in the C program.

Please look at the following program.

### Program Output:

Save the above program to file with the extension .c and compile and run the program.

Positve number test cases.

Program is providing the desired output.

Letâ€™s test the negative number test case.

As you can see from the above output, The program is displaying the error message Invalid Input, Please try again on Negative number (-30). And program also displaying the instruction to provide the number again. So that user can provide new number. The above process continues until user enters a valid number.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 4 Responses

1. [â€¦] have already discussed about the Program to find all Factors of a number in earlier [â€¦]

2. [â€¦] C Program to Find all Factors of a Number [â€¦]

3. [â€¦] have looked at the Factors of number program, and GCD Program in earlier posts, In todayâ€™s post we are going to write a program to [â€¦]

4. [â€¦] C Program to Find all Factors of a Number [â€¦]