Saturday, May 29, 2010

Divisors of a Number

Hello guys! I've received another email from one of my colleagues in an Online game called Trickster. Here is her email.

Hi Jerome,

Heya Jumi! My professor gave us a homework in programming. Here's the question:

Write a program in C that will list all the proper divisor of a number excluding itself.

Example:

Input: 8
Output: 1,2,4

I'm already picturing in my mind what I have to do but I can't write the proper code for it. Please help.

Much love,
Rose 

Well Rose, Instead of replying to your email, I've decided to post your question on my blog to help other people as well. And yeah, Thank you for emailing me this.

By the way, I've accidentally deleted my Turbo C on my PC and I have to download it again. I searched google and found a site that needs you to register to download Turbo C and I found that very unnecessary. So for you guys that needs Turbo C. Here it is. Turbo C.


Anyways. I assume you have Turbo C now so let's start!

What are the divisors of a number? They are the integers that divide exactly the number.

As stated above, 8. The divisors of 8 are 1,2,4 and 8. And since we need to exclude the number itself, we'll be down for only 1,2 and 4. Another example is 12, which has a divisor of 1,2,4 and 6 excluding the number it self.

In other words, Divisors of a number are simply the Factors of a number that when you divide the Number with that factor. The remainder is exactly 0.

Now for the Logic. Since we determine whether a Number is a Factor of Another number. We need to divide the Number to that another number and must have a remainder of 0. Then if we determine that it is a Divisor. We list it.

So to do that in C.

#include <stdio.h>
Include the Header Files of course.
main()
{
int num,ctr;
Declare the variables we will be using throughout the program.
clrscr();
Clear Screen so that we will not get junk outputs every time we run the program.
printf("Input Number: ");
scanf("%d", &num);
Input Number. And the number we input will be stored in the variable "num".

for(ctr=1;ctr < num;ctr++)
{
For-Loop to loop until the input-ed number.
if(num%ctr==0){
printf("\n %d", ctr);
}
Here it is! The condition that checks if the "num" divided by the current "ctr" has a remainder of 0. Notice the "%" sign. which signifies the Modulus division. Unlike normal division, The modulus division returns the Remainder. Not the Quotient of a number. And since we are looking for numbers that if divided to the Input-ed number, has a remainder of 0. We need to put this condition.


getch();
return 0;
}
Now signifies the end of our program. The getch() function simply makes the screen steady after the program is executed. While "return 0" simply makes our program more legit for the compiler because "main()" is a function and every function must have a return value. But it's still acceptable if we don't put that "return 0". Only we will have warnings by the compiler. Don't worry, Your program will still be working perfectly.

So to sum it up. Here's the code for the program.

#include <stdio.h>
main()
{
int num,ctr;
clrscr();
printf("Input Number: ");
scanf("%d", &num);
for(ctr=1;ctr < num ;ctr++)
{
if(num%ctr==0)
printf("\n %d", ctr);
}

getch();
return 0;
}

And Here's a Sample Output.


So Rose, I hope I Answered your Email and hope to receive more from you. I'm here to help don't worry!


By the way, Have fun coding. I'll also include the .c file of this. Download it Here.

Good Day and God Bless.

[/code]

No comments: