54 lines
1.2 KiB
C
54 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int racineCarree(float nombre) {
|
|
int pasDeRacine = -1;
|
|
for(int i = 1; i<nombre-1; i++) {
|
|
if(nombre/i == i) {
|
|
return i;
|
|
}
|
|
}
|
|
return pasDeRacine;
|
|
}
|
|
|
|
void racineCarreeTab(float tab[], int tablength) {
|
|
float tabRacines[tablength];
|
|
printf("[ ");
|
|
for (int i=0; i < tablength; i++) {
|
|
tabRacines[i]=racineCarree(tab[i]);
|
|
printf(" %d,", tabRacines[i]);
|
|
}
|
|
printf(" ]");
|
|
return;
|
|
}
|
|
|
|
int main() {
|
|
float nombre;
|
|
int mode;
|
|
int tablength;
|
|
|
|
printf("Voulez vous entrer sous la forme d'un nombre (1) ou d'un tableau (2) ? \n");
|
|
scanf("%d", &mode);
|
|
if (mode==1) {
|
|
printf("Entrez le nombre dont vous voulez trouver la racine carrée : \n");
|
|
scanf("%f", &nombre);
|
|
printf("%d \n", racineCarree(nombre));
|
|
}
|
|
|
|
if (mode==2) {
|
|
printf("De combien d'éléments voulez-vous trouver leur racine carrée ? \n");
|
|
scanf("%d", tablength);
|
|
|
|
float tab[tablength];
|
|
printf("Donnez les nombres : \n");
|
|
|
|
for (int i = 0; i < tablength; i++) {
|
|
scanf("%f", &nombre);
|
|
tab[i] = nombre;
|
|
}
|
|
|
|
racineCarreeTab(tab, tablength);
|
|
}
|
|
|
|
|
|
}
|