Learn C with example
Enter a number and show its
factor
In this example we are going to explain
how you can enter a number and how you can find its factor.
To enter a number we first define an
integer variable then use a scanf("%d",&a) method .This method is used to
take input from keyboard. We can enter number, float or
character by using this method. We have use %d,%f and %c
reactively at place of first argument. The second argument is used to store the
value at variable location.
To find factor we are using for loop which
iterate from 1 to a .We are checking the modulo of a and value from 1 to a. If
it return zero then this mean the it is factor of a and print it.
/* Enter a number and show its factor */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i;
clrscr();
printf("Enter a number\t");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(a%i==0)
printf("\nFactor of the number is\t=%d",i);
}
getch();
}
|
Output Of Example
Enter a number 12
Factor of the number is =1
Factor of the number is =2
Factor of the number is =3
Factor of the number is =4
Factor of the number is =6
Factor of the number is =12 |