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 28-Nov-2004, 11:16
koool_kid koool_kid is offline
Invalid Email Address
 
Join Date: Nov 2004
Posts: 8
koool_kid is on a distinguished road
Lightbulb

Program Needed: Perfect Number Program in C language


I need a program in simple C language for generating perfect numbers or checking if the entered number is perfect or not. It is needed urgently.

A perfect number is one whose sum of factors is equal to the number, like 6. 6 has 3 factors 1,2,3 when we add 1+2+3 it is equal to 6; hence 6 is perfect number. Likewise 28 is another perfect number. Can I get this program please?

I'm using Borland c++ assembler to compile the programs.
  #2  
Old 28-Nov-2004, 12:26
koool_kid koool_kid is offline
Invalid Email Address
 
Join Date: Nov 2004
Posts: 8
koool_kid is on a distinguished road
Hello again.

I wrote a c program for above question. I don't know how far it's right and I haven't even checked it for myself because I don't have a compiler. It will be great if anyone of you just check this program, thank you.

CPP / C++ / C Code:
  
  #include<stdio.h>
  #include<conio.h>
  main()
  {
  int a,b,c,d;
  printf("Enter a Number");
  scanf("%d", &a);
   a=d;
  for (int i=1;i<=a-1;i++)
  { 
    b=n%i; // im not sure if its b= n%i or b=i%n i cant remember which is the right one
    if(b==0)
      {
         c=c+i;
       }
  }
     if(d==a)
       printf("Entered Number  is perfect number");
     else
       printf("Entered Number is not perfect");
     getch();
}
  #3  
Old 28-Nov-2004, 12:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I'd recommend the following:
1) Never use urgent in your posts. People are not likely to treat your post as urgent. They'll answer when they read it and some will even avoid it.
2) Use code tags as described in the sticky
3) Download the free borland compiler and test your program Get it here

CPP / C++ / C Code:
a=d;
You've just erased the value you entered. Reverse it.

CPP / C++ / C Code:
 b=n%i; // im not sure if its b= n%i or b=i%n 
You have it correct.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #4  
Old 28-Nov-2004, 12:58
koool_kid koool_kid is offline
Invalid Email Address
 
Join Date: Nov 2004
Posts: 8
koool_kid is on a distinguished road
Thanks walt
  #5  
Old 28-Nov-2004, 14:39
koool_kid koool_kid is offline
Invalid Email Address
 
Join Date: Nov 2004
Posts: 8
koool_kid is on a distinguished road
GOTCHA
thats the right program but it is having one typo there "b=n%i" its wrong it shd be "b=a%i" well thanks
  #6  
Old 28-Nov-2004, 14:40
koool_kid koool_kid is offline
Invalid Email Address
 
Join Date: Nov 2004
Posts: 8
koool_kid is on a distinguished road

Correct Program for finding Perfect Number


Here is the correct program for finding a Perfect Number.

CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,d;
printf("Enter a Number");
scanf("%d", &a);
d=a;
for (int i=1;i<=a-1;i++)
{ 
b=a%i; // im not sure if its b= n%i or b=i%n i cant remember which is the right one
if(b==0)
{
c=c+i;
}
}
if(d==a)
printf("Entered Number is perfect number");
else
printf("Entered Number is not perfect");
getch();
}
Last edited by koool_kid : 28-Nov-2004 at 14:41. Reason: wrongly typed
  #7  
Old 28-Nov-2004, 22:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by koool_kid
GOTCHA
thats the right program but it is having one typo there "b=n%i" its wrong it shd be "b=a%i" well thanks
You're right. I didn't look at it that close. Sorry.

Next thing to look at it this tutorial since code tags didn't help at all.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #8  
Old 02-Dec-2005, 02:29
zanyzanny zanyzanny is offline
New Member
 
Join Date: Nov 2005
Posts: 6
zanyzanny is on a distinguished road

Re: [Program]Urgently Needed Perfect Number Program in C lang


hi
how would you go about modifying this so when you put a number in the nearest perfect number can be found?
  #9  
Old 02-Dec-2005, 05:24
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Arrow

Finding the nearest perfect number in a C program using functions


Hi Zany,

Did you try running that program?
It is wrong!!!!

First, lets try to develop the correct way of finding whether a number is perfect or not:
  • 1. First, get the input from the user. Let that number be num.
  • 2. Create another variable say sum to store the factors one by one.
  • 3. Next, in a loop that varies 'i' from 2 to num/2( Why num/2??? ), check whether num is divisible by i.
  • 4. If it is, then add 'i' to sum. (Why?)
  • 5. After the end of the loop, check whether sum is equal to num.
  • 6. If it is, then the number is a perfect number.
  • 7. Else, the number is not a perfect number...

Now, to your question:
Quote:
Originally Posted by zany
how would you go about modifying this so when you put a number in the nearest perfect number can be found?

You can create a function say int is_perfect(int number) which returns 1 if the number is a perfect number, and 0 if the number is not perfect.
You can use the logic we've developed above.

And, in the main function:
  • 1. Create a variable say j, which is initialized to 0.
  • 2. In a loop, first check whether number + j is a perfect number.
  • This can be done by using the is_perfect function, like this:
    CPP / C++ / C Code:
            if(is_perfect(num + i))
    
  • 3. If it is, then print the perfect number and break the loop.
  • 4. Else, check whether number - j is a perfect number.
  • 5. If it is, then print the perfect number and break the loop.
  • 6. Increment j in the end of the loop, to check for the next number...
Best Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #10  
Old 02-Dec-2005, 07:47
zanyzanny zanyzanny is offline
New Member
 
Join Date: Nov 2005
Posts: 6
zanyzanny is on a distinguished road

Re: [Program]Urgently Needed Perfect Number Program in C lang


(the orange, is just trying to get the user within set limits)

Code:
#include <stdio.h> #include <stdlib.h> void main(void) { int number,y=1,high_limit=1000,calculation,a,b=0,c,e; do { printf("enter a number between 1 and %d:",high_limit); scanf("%d",&number); if(number>high_limit) printf("your number is too high!\n"); else if(number<y) printf("your number is too low\n"); else printf("\nYour entered number is %d!\n\nThe perfect numbers up to %d are:\n\n",number, number); } while (number>high_limit || number<=0); system("PAUSE"); for(e=number;e>0;e--) { for(a=e-1; a>0; a--) { calculation=e%a; if(calculation==0) { "%d\n",a; b=b+a; } } if(b==e) printf("\n%d\n\n",e); { b=b-b; } } system("PAUSE"); return ; }

I don't know where to go from here. (I hope I got the scrolly thing to work).
I think I need to take each perfect number and - the input number then compare, see which is closest.
I was wondering if I could put all the perfect numbers into an array then work with them from that. Thanks for replying so quickly I'm really struggling with this subject. I have limited knowledge
I am quite enjoying it at the same time tho
 


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
C++ Perfect Number Program lpwilliams21 C++ Forum 5 06-Oct-2008 20:49
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 11:38
Help with interactive program, please nika1p2 C Programming Language 1 09-May-2005 00:22
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:18.


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