473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C - Calculating Perfect Numbers

Ok so I've been away from math for a long while now (and programming for that matter) and I've got this assignment that i'm kinda stuck on

i'll paste my code at the bottom... it's not complete, i'll say that now but i just need some1 to steer me in the right direction.
As i'm not entirely familiar with the calculations of the perfect numbers ( i know what it means just not entirely sure of how to code it)

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. main()
  5.  
  6. {
  7.    // initialisation des variables
  8.    int nbrMax;                        // Nbr d'entiers à saisir
  9.    int tableauNbr[nbrMax-1];          // Tableau pour contenir les nombres entrés par l'usager
  10.    int i;                             // Compteur boucle
  11.    int j, k, somme;                   // variables pour calcul nbr parfait
  12.    char condition;                    // Oui ou Non
  13.  
  14.    // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
  15.    do
  16.       {
  17.       printf ("Entrez le nombre d'entier (Max 10):\n");
  18.       scanf ("%d", &nbrMax);
  19.  
  20.       // boucle pour la saisie des nombres et remplissage le tableau.
  21.       for (i = 0; i < nbrMax; i++)
  22.           {
  23.           scanf ("%d", &tableauNbr[i]);
  24.           }
  25.  
  26.       // Calculs pour trouver les nombres parfaits
  27.       somme = 0;
  28.       for (j = 0; j < nbrMax; j++)
  29.           {
  30.             for (k = 1; k < tableauNbr[j]; k++)
  31.                 {
  32.                     if(tableauNbr[j]%k==0)
  33.                         {
  34.                         somme += k;
  35.                         }
  36.       }
  37.                 // Affichage des resultats
  38.                     if (somme = tableauNbr[j])
  39.                         printf("%d est un nombre parfait\n", tableauNbr[j]);
  40.                         else
  41.                         printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
  42.  
  43.           }    
  44.  
  45.  
  46.  
  47.  
  48.       // Condition requise pour repartir du début
  49.       printf ("\nVoulez vous faire un autre calcul, (o/n)?");
  50.       fflush (stdin);                        // Fonction pour vider stdin
  51.       condition = toupper(getchar());        // Capitaliser la letter et soumettre à condition
  52.       }
  53.    while ( condition == 'O');                // Repartir du début si O
  54.  
  55.    system("pause") ;
  56. }
  57.  
Nov 17 '08 #1
7 2339
how about, if i try to do the calculation for 1 integer first and then build a table around it... coz then instead of dealing with 1 i'd be dealing with a for loop on a table list of elements, right ?
Nov 17 '08 #2
Banfa
9,065 Expert Mod 8TB
Your program looks about right except that

Expand|Select|Wrap|Line Numbers
  1.        somme = 0;
  2.        for (j = 0; j < nbrMax; j++)
setting somme to 0 should be inside the for j loop.
Nov 17 '08 #3
JosAH
11,448 Expert 8TB
Your program looks about right except that

Expand|Select|Wrap|Line Numbers
  1.        somme = 0;
  2.        for (j = 0; j < nbrMax; j++)
setting somme to 0 should be inside the for j loop.
There's more; look here near the start of main():

Expand|Select|Wrap|Line Numbers
  1.    int nbrMax;                        // Nbr d'entiers à saisir 
  2.    int tableauNbr[nbrMax-1];          // Tableau pour contenir les nombres entrés par l'usager 
  3.  
nbrMax isn't initialized (yet).

kind regards,

Jos
Nov 17 '08 #4
Your program looks about right except that

Expand|Select|Wrap|Line Numbers
  1.        somme = 0;
  2.        for (j = 0; j < nbrMax; j++)
setting somme to 0 should be inside the for j loop.
i don't want to reset my sum every j loop because i need to verify if the sum = table member then it's a perfect number.
Nov 17 '08 #5
that's the code i have for now... it's like the calculations are doing nothing... i can "see" what the loop is doing, i'll put some prinft to see how the variables are acting up.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. main()
  6. {
  7.    // initialisation des variables
  8.    int nbrMax=0;                        // Nbr d'entiers à saisir
  9.    int produit=0;
  10.    int tableauNbr[nbrMax];            // Tableau pour contenir les nombres entrés par l'usager
  11.    int i, j, k;                       // Compteur boucle
  12.    int somme;                   // variables pour calcul nbr parfait
  13.    char condition;                    // Oui ou Non
  14.  
  15.    // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
  16.    do
  17.       {
  18.       printf ("Entrez le nombre d'entier (Max 10):\n");
  19.       scanf ("%d", &nbrMax);
  20.  
  21.       // boucle pour la saisie des nombres et remplissage le tableau.
  22.       for (i = 0; i < nbrMax; i++)
  23.           {
  24.           scanf ("%d", &tableauNbr[i]);
  25.           }
  26.  
  27.       // Calculs pour trouver les nombres parfaits
  28.       somme = 0;
  29.       for (j = 0; j < nbrMax; j++)            {
  30.  
  31.             for (k = 1; k < tableauNbr[j]; k++)
  32.                 {
  33.                     if(!tableauNbr[j]%k)
  34.                         somme += k;
  35.                 }
  36.                 // Affichage des resultats
  37.                     if (somme == tableauNbr[j])
  38.                         printf("%d est un nombre parfait\n", tableauNbr[j]);
  39.                         else {
  40.                         printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
  41.                         // produit *= tableaNbr[j];        // tableauNbr undeclared, first use in this function error...
  42.                         }
  43.         }    
  44.  
  45.  
  46.       printf("le produit des nombres non-parfaits est: %d", produit);
  47.  
  48.       // Condition requise pour repartir du début
  49.       printf ("\nVoulez vous faire un autre calcul, (o/n)?");
  50.       fflush (stdin);                        // Fonction pour vider stdin
  51.       condition = toupper(getchar());        // Capitaliser la letter et soumettre à condition
  52.       }
  53.    while ( condition == 'O');                // Repartir du début si O
  54.  
  55.    system("pause") ;
  56. }
  57.  
/*

Résultats:
==========
Entrez le nombre d'entier (Max 10):
2
6
28
6 n'est pas un nombre parfait
28 n'est pas un nombre parfait
le produit des nombres non-parfaits est: 0
Voulez vous faire un autre calcul, (o/n)?
Press any key to continue . . .


*/
Nov 17 '08 #6
sicarie
4,677 Expert Mod 4TB
orangeworx-

I have merged your threads as the code is exactly the same between the two. Please confine yourself to one question, in one thread, at a time.

Thanks,

sicarie
Nov 18 '08 #7
sicarie
4,677 Expert Mod 4TB
orangeworx-

I have merged your threads as the code is exactly the same between the two. Please confine yourself to one question, in one thread, at a time.

Thanks,

sicarie
orangeworx-

I definitely made a mistake there, my apologies. I have split the threads again, please let me know if I did not get a post in the right thread between this and this.

Thanks,

sicarie
Nov 18 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: | last post by:
I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. What I need to know is, is there any method in Java to...
13
by: racygirl | last post by:
Hi all, I was just wondering, is it possible to write a solution to the following problem with the following criteria: 1. it must be as efficient as possible at run-time 2. be in O(1) 3. and...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
38
by: jdcrief | last post by:
Complier: Visual C++ 2005 Express Edition The program I wrote will compile and execute, but the output is always the same, no matter what number is entered in for the radius of the circle. ...
8
by: sajid | last post by:
The CSS 2.1 Specification describes how to sort a list of selectors in order of specificity, but it doesn't provide a method to calculate the specificity of a single selector in isolation. I've...
10
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: ...
2
by: wkid87 | last post by:
Using C# Microsft Visual C#, Console Application I'm needing help with the user entering in a number to tell if it perfect or not. I have the perfect number figure out. Here in the instruction: ...
12
by: hipfunkyjive | last post by:
Basically my program has to find perfect numbers in a range 1- 1000 it works , kinda but it displays a few numbers that are not perfect for example the first Perfect numbers are : 6 28 496 my...
17
by: nickels | last post by:
-Alright im supposed to make a program that tests for perfect numbers. I have to use 2 methods and 3 methods if you count the main method. -My first method has to return a boolean value of true...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.