Nombre premier inferieur a un nombre N
khalilx0x
Messages postés
17
Statut
Membre
-
khalilx0x Messages postés 17 Statut Membre -
khalilx0x Messages postés 17 Statut Membre -
salut
voici mon petit code source qui ne marche pas malheureusement je sais pas ou erreur.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n, i,j;
char test = 'v';
printf("saisir le nombre :");
scanf("%d",&n);
if(n==1) printf("le nombre 1 n'est pas premier.");
for(j=2;j<=n;j++)
{
for(i=2;i<=j/2;i++)
{
if(j%i==0) test = 'f';
}
if(test=='v') printf("le nombre %d est premier.\n", j);
}
return 0;
}
ma question est ou est erreur??
quand j'utilise une procédure ça fonctionne parfaitement :
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void premier(int n)
{
int i;
char test = 'v';
for(i=2;i<=n/2;i++)
{
if(n%i==0) test = 'f';
}
if(test=='v') printf("le nombre %d est premier.\n", n);
}
int main(int argc, char *argv[]) {
int n, i,j;
printf("saisir le nombre :");
scanf("%d",&n);
if(n==1) printf("le nombre 1 n'est pas premier.");
for(j=2;j<=n;j++)
{
premier(j);
}
return 0;
}
Merci d'avance.
voici mon petit code source qui ne marche pas malheureusement je sais pas ou erreur.
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n, i,j;
char test = 'v';
printf("saisir le nombre :");
scanf("%d",&n);
if(n==1) printf("le nombre 1 n'est pas premier.");
for(j=2;j<=n;j++)
{
for(i=2;i<=j/2;i++)
{
if(j%i==0) test = 'f';
}
if(test=='v') printf("le nombre %d est premier.\n", j);
}
return 0;
}
ma question est ou est erreur??
quand j'utilise une procédure ça fonctionne parfaitement :
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void premier(int n)
{
int i;
char test = 'v';
for(i=2;i<=n/2;i++)
{
if(n%i==0) test = 'f';
}
if(test=='v') printf("le nombre %d est premier.\n", n);
}
int main(int argc, char *argv[]) {
int n, i,j;
printf("saisir le nombre :");
scanf("%d",&n);
if(n==1) printf("le nombre 1 n'est pas premier.");
for(j=2;j<=n;j++)
{
premier(j);
}
return 0;
}
Merci d'avance.
1 réponse
-
yg_be Messages postés 23437 Date d'inscription Statut Contributeur Dernière intervention Ambassadeur 1 588
tu ne nous expliques pas ce qui ne marche pas : ton programme boucle, donne une erreur, ?
et ainsi?int main(int argc, char *argv[]) { int n, i,j; printf("saisir le nombre :"); scanf("%d",&n); if(n==1) printf("le nombre 1 n'est pas premier."); for(j=2;j<=n;j++) { char test = 'v'; for(i=2;i<=j/2;i++) { if(j%i==0) test = 'f'; } if(test=='v') printf("le nombre %d est premier.\n", j); } return 0; }