C - Calculating Perfect Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orangeworx
    New Member
    • Nov 2008
    • 18

    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)

    Code:
    #include <stdio.h>
    
    main()
    
    {
       // initialisation des variables
       int nbrMax;                        // Nbr d'entiers à saisir
       int tableauNbr[nbrMax-1];          // Tableau pour contenir les nombres entrés par l'usager
       int i;                             // Compteur boucle
       int j, k, somme;                   // variables pour calcul nbr parfait
       char condition;                    // Oui ou Non
    
       // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
       do
          {
          printf ("Entrez le nombre d'entier (Max 10):\n");
          scanf ("%d", &nbrMax);
    
          // boucle pour la saisie des nombres et remplissage le tableau.
          for (i = 0; i < nbrMax; i++)
              {
              scanf ("%d", &tableauNbr[i]);
              }
    
          // Calculs pour trouver les nombres parfaits
          somme = 0;
          for (j = 0; j < nbrMax; j++)
              {
                for (k = 1; k < tableauNbr[j]; k++)
                    {
    					if(tableauNbr[j]%k==0)
    						{
    						somme += k;
    						}
          }
    				// Affichage des resultats
    				    if (somme = tableauNbr[j])
    						printf("%d est un nombre parfait\n", tableauNbr[j]);
    					    else
    						printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
    				
    		  }	
          
    
    
    
          // Condition requise pour repartir du début
          printf ("\nVoulez vous faire un autre calcul, (o/n)?");
          fflush (stdin);                        // Fonction pour vider stdin
          condition = toupper(getchar());        // Capitaliser la letter et soumettre à condition
          }
       while ( condition == 'O');                // Repartir du début si O
    
       system("pause") ;
    }
  • orangeworx
    New Member
    • Nov 2008
    • 18

    #2
    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 ?

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Your program looks about right except that

      Code:
             somme = 0;
             for (j = 0; j < nbrMax; j++)
      setting somme to 0 should be inside the for j loop.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Banfa
        Your program looks about right except that

        Code:
               somme = 0;
               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():

        Code:
           int nbrMax;                        // Nbr d'entiers à saisir 
           int tableauNbr[nbrMax-1];          // Tableau pour contenir les nombres entrés par l'usager
        nbrMax isn't initialized (yet).

        kind regards,

        Jos

        Comment

        • orangeworx
          New Member
          • Nov 2008
          • 18

          #5
          Originally posted by Banfa
          Your program looks about right except that

          Code:
                 somme = 0;
                 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.

          Comment

          • orangeworx
            New Member
            • Nov 2008
            • 18

            #6
            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.

            Code:
            #include <stdio.h>
            #include <stdlib.h>
            
            main()
            {
               // initialisation des variables
               int nbrMax=0;                        // Nbr d'entiers à saisir
               int produit=0;
               int tableauNbr[nbrMax];            // Tableau pour contenir les nombres entrés par l'usager
               int i, j, k;                       // Compteur boucle
               int somme;                   // variables pour calcul nbr parfait
               char condition;                    // Oui ou Non
            
               // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
               do
                  {
                  printf ("Entrez le nombre d'entier (Max 10):\n");
                  scanf ("%d", &nbrMax);
            
                  // boucle pour la saisie des nombres et remplissage le tableau.
                  for (i = 0; i < nbrMax; i++)
                      {
                      scanf ("%d", &tableauNbr[i]);
                      }
            
                  // Calculs pour trouver les nombres parfaits
                  somme = 0;
                  for (j = 0; j < nbrMax; j++)			{
            
                        for (k = 1; k < tableauNbr[j]; k++)
                            {
            					if(!tableauNbr[j]%k)
            						somme += k;
            				}
            				// Affichage des resultats
            				    if (somme == tableauNbr[j])
            						printf("%d est un nombre parfait\n", tableauNbr[j]);
            					    else {
            						printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
            				        // produit *= tableaNbr[j];        // tableauNbr undeclared, first use in this function error...
                                    }
            		}	
                  
            
                  printf("le produit des nombres non-parfaits est: %d", produit);
            
                  // Condition requise pour repartir du début
                  printf ("\nVoulez vous faire un autre calcul, (o/n)?");
                  fflush (stdin);                        // Fonction pour vider stdin
                  condition = toupper(getchar());        // Capitaliser la letter et soumettre à condition
                  }
               while ( condition == 'O');                // Repartir du début si O
            
               system("pause") ;
            }
            /*

            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 . . .


            */
            Last edited by orangeworx; Nov 17 '08, 07:27 PM. Reason: pasted code many times ... oops

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              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

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by sicarie
                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

                Comment

                Working...