Codes to find out Perfect numbers

  • Thread starter itsmenikko
  • Start date
  • Tags
    Numbers
In summary: The semicolon tells the computer to stop executing the code within the loop and move onto the next line. Without the semicolon, your code will keep running and printing out the values in the array.
  • #1
itsmenikko
4
0
#include <stdio.h>
#include <conio.h>

main()
{
int limit;
int arrayname[ 90 ];
int i;
int sum=0;
int b;

printf("Enter how many times: ");
scanf("%d", &limit);


for(i = 1 ; i <= limit ; i++)
{
scanf("%d", &arrayname);
}
printf("\n");


for(i=1;i<arrayname;i++)
{
b=arrayname%i;
if(b==0)

sum=sum+i;

if(sum==arrayname)

printf("Perfect number");

else

printf("Not perfect");

printf("\n");
}

getch();
return 0;
}









I have created this source codes to find out perfect numbers. The problem is it prints not perfect even if its perfect. And I am using arrays. The program will ask the user how many numbers to input and after the user satisfies the condition, the program will identify if it's a perfect number or not. Please help me with this.
 
Technology news on Phys.org
  • #2
I have put HTML code tags ([ code] and [ /code] but without the spaces) around your code and have also indented it to make it easier to understand.

When you include code for a problem you're asking about, include the code tags.
itsmenikko said:
Code:
#include <stdio.h>
#include <conio.h>

main()
{
   int limit; 
   int arrayname[ 90 ]; 
   int i;
   int sum=0;
   int b;

   printf("Enter how many times: ");
   scanf("%d", &limit);


   for(i = 1 ; i <= limit ; i++) 
   {
      scanf("%d", &arrayname[i]);
   }
   printf("\n");


   for(i=1;i<arrayname[i];i++)
   {
      b=arrayname[i]%i;
      if(b==0)
         sum=sum+i;

      if(sum==arrayname[i])
         printf("Perfect number");

      else
         printf("Not perfect");

      printf("\n");
   }

   getch();
   return 0;
}

I have created this source codes to find out perfect numbers. The problem is it prints not perfect even if its perfect. And I am using arrays. The program will ask the user how many numbers to input and after the user satisfies the condition, the program will identify if it's a perfect number or not. Please help me with this.

I don't understand your algorithm at all, and you haven't helped, because there is not a single comment in your code. You first ask for user input for limit. What is that used for? Then you ask the user to enter all of the values of your array arrayname. Why are you doing that? How is the user supposed to know what values to put into the array?

Your program should determine that 6 and 28 are perfect numbers. These are the smallest perfect numbers. Do you understand what a perfect number is?
 
  • #3
The purpose of why I am using arrays sir for that all the inputs inside of the loop will be red and that's the only way i know all inputs can be red inside the loop. sorry sir I am just a beginner in programming. And it's my first time in this forums. sorry.
 
  • #4
itsmenikko said:
The purpose of why I am using arrays sir for that all the inputs inside of the loop will be red and that's the only way i know all inputs can be red inside the loop.
That doesn't get me any closer to understanding what you're trying to do. If I were running your program I wouldn't have any idea what numbers I was supposed to put into the array. When you run your program, what numbers do you enter?
itsmenikko said:
sorry sir I am just a beginner in programming. And it's my first time in this forums. sorry.

I can't give any help because I don't have a clue what your code is supposed to do? Can you explain to me in English what you are attempting to do?
 
  • #5
The program will ask the user if how many numbers will he/she wants to input. the opening line of the program is "Enter how many times". The user will then input how many numbers he/she will input. after that for example if he/she input's 10, the scanf will loop 10 times and the user must input 10 numbers. after inputing 10 numbers, the program will then identify each of the 10 numbers the user inputed if its a perfect number or not perfect number.
 
  • #6
OK, suppose I type in 3 for the first input, and enter 6, 8, and 15 for the three numbers. What is this code (below) going to do for the first value in the array? It should report that the first number is perfect, and the second and third aren't perfect.

Code:
for(i=1;i<arrayname[i];i++)
{
   b=arrayname[i]%i;
   if(b==0)
      sum=sum+i;

   if(sum==arrayname[i])
      printf("Perfect number");

   else
      printf("Not perfect");

   printf("\n");
}
 
  • #7
That's my problem sir . the codes that i wrote for this program is wrong. i need someone to tell me what's wrong with my codes. :(
 
  • #8
itsmenikko... The thing is this code has more than one problem, so it's less a "why isn't it working?" problem and more a "why do you expect it to work?" problem. What I think Mark is saying is that it would help you to step through the code in your head line by line and ask "What does this actually do?"

What it sort of looks like is that you (or whoever gave you your algorithm) meant to make two nested loops that iterate over each item in arrayname, and for each one sets sum to 0 then iterates over each number 1..arrayname adding to sum. Instead you have one loop that iterates until "i<arrayname" (which is nonsense, you're comparing the index to the value in the array... this will probably crash) and adds to a single sum variable the entire time, which isn't what you want. Also, by the way, the first index in an array is 0 not 1.

There are enough problems in this code that asking "what is wrong with the code" is like asking "do it for me"... In which case,
Code:
perl -pe '$b=$_*2;for$a(1..$_){$_%$a or$b-=$a}$_=!$b'
 

Related to Codes to find out Perfect numbers

1. What is a Perfect number?

A Perfect number is a positive integer that is equal to the sum of its proper divisors (positive divisors excluding the number itself). For example, 6 is a Perfect number because its proper divisors are 1, 2, and 3, and 1+2+3=6.

2. How do you find Perfect numbers?

One way to find Perfect numbers is by using a code or algorithm. This involves checking all the numbers below the given number and adding up the proper divisors. If the sum is equal to the given number, then it is a Perfect number.

3. What is the most efficient code for finding Perfect numbers?

The most efficient code for finding Perfect numbers is the Euclid-Euler theorem, which states that a number of the form 2^(p-1)(2^p - 1), where p and 2^p - 1 are both prime numbers, will always result in a Perfect number. This code is efficient because it only checks for prime numbers, which significantly reduces the number of calculations needed.

4. Are there any other methods for finding Perfect numbers?

Yes, there are other methods for finding Perfect numbers such as the Mersenne prime method, the Lucas-Lehmer test, and the Perny primality test. Each method has its own advantages and disadvantages in terms of efficiency and ease of implementation.

5. What are some real-world applications of Perfect numbers?

Perfect numbers have been studied for centuries and have interesting mathematical properties. They have also been used in cryptography and coding theory, as well as in the creation of musical scales and rhythms. In addition, they have been used in the optimization of computer algorithms and in the analysis of complex systems.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
752
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
911
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
2
Views
952
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
Back
Top