I used tu work with Code::Blocks under windows.
I try now to use it under linux.
I've got a problem; It seems that the compilation is OK (I have installed the g++ ) but after this ... nothing.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define tailleMax 100000
double PGCD(double nb1,double nb2);
double PPCM(double nb1, double nb2);
int main()
{
double nb1, nb2;
int nb3;
int l,choix;
double encore =0;
int tab[tailleMax] = {0};
int tab2[tailleMax] = {0};
printf("Ce programme permet de :\n\n");
do
{
printf("1-calculer le PGCD de deux nombres.\n");
printf("2-calculer le PPCM de deux nombres .\n");
printf("3-trouver tous les diviseurs d'un nombre (jusqu'a 2.147.483.647).\n");
printf("4-decomposer un nombre en produit de facteurs premiers (jusqu'a 2.147.483.647).\n");
printf("5-rechercher les nombres premiers entre 2 valeurs (jusqu'a 2*10^6 en gros).\n\n");
printf("Votre choix : ");
for (l=0; l<=tailleMax; l++)
{
tab[l]=0;
tab2[l]=0;
}
scanf("%d",&choix);
switch (choix)
{
case 1 :
{
printf("\nEntrez le 1er nombre : ");
scanf("%lf",&nb1);
printf("Entrez le 2e nombre : ");
scanf("%lf",&nb2);
printf("\nLe PGCD de %.0lf et %.0lf est %.0lf\n", nb1,nb2,PGCD(nb1,nb2));
if (PGCD(nb1,nb2)==1)
printf("%.0lf et %.0lf sont premiers entre eux.",nb1,nb2);
break;
}
case 2 :
{
printf("\nEntrez le 1er nombre : ");
scanf("%lf",&nb1);
printf("Entrez le 2e nombre : ");
scanf("%lf",&nb2);
printf("\nLe PPCM de %.0lf et %.0lf est %.0lf", nb1,nb2,PPCM(nb1,nb2));
break;
}
case 3 :
{
int i,k;
printf("\nEntrez le nombre : ");
scanf("%d",&nb3);
printf("\nLes diviseurs de %d sont ",nb3);
for ( i=1 ; i<= sqrt(nb3); i++)
{
if ((nb3%i)==0)
{
tab[i-1]=i;
if (i*i != nb3)
tab[tailleMax-i]=nb3/i;
}
}
k=0;
printf("%d",tab[0]);
for (i=1; i<tailleMax; i++)
if(tab[i]!=0)
{
tab2[k]=tab[i];
printf(" - %d",tab[i]);
k++;
}
printf("\nIl y a %d diviseurs",k+1);
if (k==1)
printf(" : -----> %d est un nombre premier.",nb3);
break;
}
case 4 :
{
int i,j,k,x;
printf("\nEntrez le nombre : ");
scanf("%d",&nb3);
for ( i=1 ; i<= sqrt(nb3); i++)
{
if ((nb3%i)==0)
{
tab[i-1]=i;
if (i*i != nb3)
tab[tailleMax-i]=nb3/i;
}
}
k=0;
for (i=1; i<tailleMax; i++)
if(tab[i]!=0)
{
tab2[k]=tab[i];
k++;
}
printf("\n");
if (k==1)
printf("-----> %d est un nombre premier.",nb3);
else
{
j=0;
printf("%d = %d",nb3, tab2[0]);
x=nb3/tab2[0];
for (j=0; j<k; j++)
while ((x%tab2[j])==0)
{
printf(" x %d",tab2[j]);
x=x/tab2[j];
}
}
break;
}
case 5 :
{
int i,k,max,min,x;
int *entier=NULL;
printf("\nEntrez le plus petit nombre : ");
scanf("%d",&min);
do
{
printf("Entrez le plus grand nombre (il doit etre superieur ou egal a %d) : " ,min+2);
scanf("%d",&max);
}
while (max<=min+1);
entier = malloc(max*sizeof(int)); // allocation dynamique d'un tableau ; il aura max cases de int.
if (entier==NULL)
exit(0);
for (i=0; i<max; i++)
entier[i]=i;
i=1;
do
{
i++;
if (entier[i]!=0)
{
k=2;
while (k*i<=max-max%entier[i])
{
entier[k*i]=0;
k++;
}
}
}
while (i*i<max);
printf("\nLes nombres premiers compris entre %d et %d sont : ",min,max);
k=0;
for (i=min; i<=max; i++)
if ((entier[i]!=0)&&(i>1))
{
printf("%d ",entier[i]);
k++;
}
if (k==0)
printf("\n\nIl n'y a aucun nombre premier.\n");
else
printf("\n\nIl y a %d nombre(s) premier(s).\n",k);
if (min<=0)
min=2;
FILE *fichier=NULL;
fichier=fopen("nombrespremiers.txt","w+");
if(fichier!=NULL)
{
for(i=min; i<max; i++)
if (entier[i]!=0)
fprintf(fichier,"%d ",entier[i]);
rewind(fichier);
for(i=0; i<10; i++)
{
fscanf(fichier,"%d",&x);
printf("%d ",x);
}
}
else
printf("Impossible d'ouvrir le fichier Nombres.txt");
free(entier);
break;
}
default :
exit (0);
}
printf("\n\nVoulez-vous recommencer ? (oui = 1 , non = autre touche) : ");
scanf("%d",&choix);
if (choix==1)
encore = 1;
else
encore = 0;
printf("\n--------------------------------------------------------------------------------\n");
printf("--------------------------------------------------------------------------------\n");
printf("\n");
}
while (encore==1);
return 0;
}
double PGCD(double nb1,double nb2)
{
double dividende, diviseur, reste;
dividende = nb1;
diviseur = nb2;
do
{
reste = dividende - diviseur*floor(dividende/diviseur);
dividende = diviseur ;
diviseur = reste;
}
while (reste != 0);
return dividende;
}
double PPCM(double nb1, double nb2)
{
return nb1*nb2/PGCD(nb1,nb2);
}
Sorry it's in french but maybe you can guess what it means.