0% found this document useful (0 votes)
9 views3 pages

Exjavacontrole

Uploaded by

banouniyassine1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Exjavacontrole

Uploaded by

banouniyassine1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercice 1 : Somme des éléments d’un tableau

public static int sum(int[] arr) {


int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

Exercice 2 : Recherche du plus grand élément dans un tableau

public int getMax(int[] array) {


int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}

Exercice 3 : Palindrome

public static boolean isPalindrome(String str) {


int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}

Exercice 4 : Compter les voyelles

public static int countVowels(String str) {


int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch =='O'
|| ch == 'U') {
count++;
}
}
return count;
}
Exercice 5 : Table de multiplication

public static void multiplicationTable(int N) {


for (int i = 1; i <= 10; i++) {
System.out.println(N + " x " + i + " = " + (N * i));
}
}

Exercice 6 : Moyenne d’un tableau

public static double average(int[] arr) {


int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return (double) sum / arr.length;
}

Exercice 7 : Inversion d’un tableau

public static void reverse(int[] arr) {


int i = 0, j = arr.length - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}

Exercice 8 : Suppression des espaces

public static String removeSpaces(String str) {


return str.replaceAll("\\s", "");
}

Exercice 9 : Nombre premier

public static boolean estPremier(int n) {


if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
Exercice 10 : Fusion de tableaux
public static int[] Fusionner(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
int i = 0;
for (int element : arr1) {
result[i] = element;
i++;
}
for (int element : arr2) {
result[i] = element;
i++;
}
return result;
}

You might also like