Hey guys, I'm not really a beginner in this, i can read and write code with enough ease but this is a whole new thing for me... i've written some code to give me the exact number of change (coins) when giving a number between 0 (inclusive) and 5 (non-inclusive)
the problem i'm having is, for example, when i put certain numbers, i'm getting a wrong count of change... off by 1 penny only
it's very bizarre...
I hope some1 can give me some insight on this
Thanks in advance
Marc
the problem i'm having is, for example, when i put certain numbers, i'm getting a wrong count of change... off by 1 penny only
it's very bizarre...
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") ;
}
Thanks in advance
Marc
Comment