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) -
-
#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") ;
-
}
-
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 ?
Banfa 9,065
Expert Mod 8TB
Your program looks about right except that - somme = 0;
-
for (j = 0; j < nbrMax; j++)
setting somme to 0 should be inside the for j loop.
Your program looks about right except that - 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(): -
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
Your program looks about right except that - 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.
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. -
-
#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 . . .
*/
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 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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. ...
|
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...
|
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:
...
|
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:
...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
| |