Sunday, August 2, 2009

I want to create a c++ program which shows the proper divisors of a number n.Can you help me?

i have tried this program but it does not work:





#include %26lt;iostream%26gt;


using namespace std;





int main()


{


int a1,b1,a2,b2;


cout %26lt;%26lt;"n= ";


cin %26gt;%26gt; n;


d=2;


x=n/2;


while ( d%26lt;=x )


{


int r;


r=n%d;


if(r=0)


{


cout %26lt;%26lt; "The proper divisors are " %26lt;%26lt; d;


}


d=d+1;


}





return 0;


}

I want to create a c++ program which shows the proper divisors of a number n.Can you help me?
your program is just fine. Good job!


Just a copule of things:


- You want to declare variables you are actually using (not a1,a2,b1,b2) - change the declaration line to


int w,d,xn;





- You want your "if (r=0)" statement look like "if (r==0)" or just if (!r)


'=0' is the asignment operation - it makes the value of r to be 0, and always evaulates to 0. To make a comparison, you want '=' to appear twice - r==0. Or, when comparing with 0 you can make a shortcut - 0 is the same as false, and everything else is true. So


if (!r) is quivalent to if (r==0), but shorter and safer (because it is not ambigous, and you can't mistake it for assignment be leaving out a '=' sign'





- Finally, you wand to add '%26lt;%26lt;endl' after 'd' in your cout statement - so that the divisors you find get printed one per line.





Logically, your program does the right thing. You got it right. Congratulations.





P.S. Do not use the program somebody gave you above. It has a logical error in it, unlike yours :-)
Reply:I've no clue about C++


Have you tried some programming forums?


Try http://rohitab.com/ - They do Programming, Making Viruses, Scripting %26amp; You have to read their rules! They're strict, but fair.


I'm on there as Nitrogen, lol.





Influx UK
Reply:Use the following program to know the divisors list.


# include %26lt;iostream%26gt;


int main()


{


int remainder, number, divisor, divisorcount = 0;


cout%26lt;%26lt;"Enter any number to know its divisors";


cin%26gt;%26gt;number;


divisor = number / 2;


while (divisor %26gt; 1)


{


remainder = number/divisor;


if (remainder == 0)


{


cout%26lt;%26lt;divisor%26lt;%26lt;" is a divisor"%26lt;%26lt;endl;


divisorcount ++;


}


divisor --;


}


cout%26lt;%26lt;"Total number of divisors " %26lt;%26lt; divisorcount;


}


No comments:

Post a Comment