GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Dec-2004, 03:49
noamfrie noamfrie is offline
New Member
 
Join Date: Dec 2004
Posts: 4
noamfrie is on a distinguished road

Amicable numbers program


Hi!
I need a litlle help with this program.
The input is a natural number and the output is all the amicalbe numbers that are smaller than this number.
Here is the code I wrote.
It doesn't give me any output.

Appriciate your help.

Thanks,

Noam.

CPP / C++ / C Code:
#include <stdio.h>
int sum_of_dividers (int n);
int haverim (int a, int b);
int main ()
{
int k,n,v;
scanf ("%d",&n);
for (k=1;k<n;k++)
if (haverim (k,v))
printf ("%d,%d\n",k,v);
return 0;
}
int haverim (int a, int b)
{
return (sum_of_dividers(a)==b && sum_of_dividers(b)==a);
}
int sum_of_dividers (int n)
{
int k,sum=1;
for (k=2;k*k<n;k++)
if (n%k==0)
sum+=k+n/k;
if (k*k==n)
sum+=k;
return sum;
}
Last edited by JdS : 06-Dec-2004 at 16:44. Reason: Please insert [c] & [/c] tags between your example C codes
  #2  
Old 06-Dec-2004, 08:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by noamfrie
Hi!
I need a litlle help with this program.
The input is a natural number and the output is all the amicalbe numbers that are smaller than this number.
Here is the code I wrote.
It doesn't give me any output.

Appriciate your help.

Thanks,

Noam.

There are a couple of things wrong with your post:

First: use code tags. Don't know what code tags are? Read the sticky at the top of this page. Click where it says Sticky:GIDForums enables ...

Second: Format your code to make it readable and to emphasize where your control structures are (use indentation)

Third: Tell what you get when you run your program. Tell what input you used and what output you expected and what you got.

One thing wrong with your program:

You read a value for n, but then you use v in your loop.
CPP / C++ / C Code:
if (haverim (k,v))



Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25
Need help with my programs, please help. agentxx04 C Programming Language 1 23-Sep-2004 19:02
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.