Codes to find out Perfect numbers

  • Thread starter Thread starter itsmenikko
  • Start date Start date
  • Tags Tags
    Numbers
AI Thread Summary
The discussion revolves around a C program designed to identify perfect numbers based on user input. The code has multiple issues, including incorrect loop conditions and improper handling of array indices, which lead to incorrect outputs. Users express confusion about the program's logic and the purpose of certain variables, highlighting the need for clearer comments and explanations. The main problems identified include the misuse of the array index in loop conditions and the failure to reset the sum for each number being evaluated. Overall, the conversation emphasizes the importance of understanding algorithm structure and debugging practices in programming.
itsmenikko
Messages
4
Reaction score
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
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?
 
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.
 
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?
 
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.
 
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");
}
 
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. :(
 
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'
 
Back
Top