Program to find Factors of a number in C Language

program-to-find-all-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.

NumberFactors
151, 3, 5, 15
401, 2, 4, 5, 8, 10, 20, 40
301, 2, 3, 5, 6, 10, 15, 30
361, 2, 3, 4, 6, 9, 12, 18, 36

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.

program-to-find-factors-of-a-number-in-c-language-output

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.

program-to-find-factors-of-a-number-in-c-language-output-2

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.

You may also like...

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 […]

Leave a Reply