Problem Analysis
To develop a C program to find the factor of a number it is required to understand the meaning of the factor of a number. Factor of a number has following characteristics:
- A factor of a number on division leaves no remainder.
- A factor of a number can be even in number or odd in number.
- Square numbers have an odd number of factors.
- Prime number has only two factors the number itself and 1.
Factors
Factors divide numbers in such a way that it leaves no remainder. For example 12 has factors – 1,2,3,4,6 and 12.
If 12 is divided by any of the six factors the answer will be a whole number. For example – 12 / 3 = 4
Square Numbers
Square of a number is obtained by repeated multiplication of the number by itself. For example, a square of 36 is obtained by multiplying 6 two times (6 x 6) and 1 is by default.
1 x 6 x 6 = 36
As it can be seen that 1 is also a factor of 36 it makes the number of factors odd.
1 x 6 x 6 ( 3 factors ) = 36
Prime Number
A prime number has only two factors 1 and itself. Prime number cannot be divided by another number.
Examples of prime numbers are: 2,3,5,7,11,13,17,19,23, and 29.
1 has only one factor and it is not a prime number.
Problem Description
The problem is – “C Program to find factors of a number”.
From problem analysis it is evident that to obtain factors of a number, the number has to be divided and checked for remainder. If the remainder is 0 then the quotient is one of the facator. To obtain other factors than number has to be divided by number 1 and itself.
To repeatedly divide the number loop is to be used. There are 3 loop in C language:
- while loop
- for loop
- do while loop
From these 3 loops any one of the loops can be used.
Number after division has to be checked for the remainder. If the remainder is 0 then the quotient is a factor. To check this condition if statement of C language is to be used.
Solution to Problem
Following is the code to find factor of number:
C Program to find factor of a number
#include<stdio.h> int main() { int num_1,i; printf("Input positive integer"); scanf("%d", &num_1); printf("Obtained factors are: %d", num_1); for(i=1;i<=num_1;i++) { if(num_1 % i == 0) { printf(" %d", i); } } return 0; }
Output: Input positive integer234 Obtained factors of 234 are: 1 2 3 6 9 13 18 26 39 78 117 234
Code Analysis Input is taken from the user in integer variable num_1. for loop is executed until i is false. The number is divided by i and checked for remiander. Modulo divide is used to obtain remainder by using the following code: if(num_1 % i == 0) If the condition num % i is 0 then “i” is one of the factors. The value of “i” is incremented by 1.
Conclusion
The problem was to find the factor of a number by developing a “C” program. Problem analysis section discusses the meaning of a factor of a number. Descriptions of the problem section discuss “C” programming constructs used to develop the code. Code analysis section re-visits code and discusses the working of the code.
Leave a Reply