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.

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
void main()
{
  int NUM,i;
  printf("\nENTER ANY INTERGER AND FIND ITS DIVISORS : ");
  scanf("%d",&NUM);
  for(i=1;i<NUM;i++)
  {
   if(NUM%i==0)
   {
    printf("\n%d",i);
   }
  }
  getch();
}

[/codesyntax]

SCREEN SHOTS:-

C_program_Perfect_No

C_program_Perfect_No_Output

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

Leave a Reply