C005 An interactive C program to generate the divisors of a given integer

By | August 8, 2013

Divisors are the numbers which can divide the number given and are less than the given number.
Let’s identify variables needed for this program.
First variable will be the one which will save the value entered by the user and it will be NUM. Other variable will be i which will be for FOR Loop. so in all two variables.

The identified variables are NUM, i.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

In this program there is a requirement of generating the divisors. Here we have to Check the values from 1 to the given number. It indicates LOOP is to be taken. Best Loop for such kind of condition is

FOR Loop.
Loop selected is FOR Loop.

Now give the user a message to enter a value and scan the input from the user and save it in NUM variable.
for(i=0;i<NUM;i++) this FOR loop will give you DIVIDE ERROR. So be careful while initializing i=1 instead of i=0.
For Loop will start with 1 to NUM-1.To identify whether the number is divisor or not. We have to divide that number by given number(NUM). If the remainder is zero than number entered is an divisor and if the remainder is not zero than number is not a divisor. This can be achieved by % (modulus) operator which is giving us the remainder.

The condition will be NUM%i in FOR Loop. If the remainder will be zero, we will go for printing the number divisible as output values at the end of this program.

  1. #include<stdio.h>
  2. void main()
  3. {
  4. int NUM,i;
  6. scanf("%d",&NUM);
  7. for(i=1;i<NUM;i++)
  8. {
  9. if(NUM%i==0)
  10. {
  11. printf("\n%d",i);
  12. }
  13. }
  14. getch();
  15. }




Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply