Voici les corrections pour les exercices mentionnés :
Exercice 1 : Nombres premiers
import java.util.Scanner;
public class NombresPremiers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Entrez un nombre entier : ");
int n = scanner.nextInt();
int total = 0;
System.out.println("Nombres premiers entre 1 et " + n + " :");
for (int i = 2; i <= n; i++) {
if (estPremier(i)) {
System.out.print(i + " ");
total++;
System.out.println("\nNombre total de nombres premiers : " + total);
public static boolean estPremier(int nombre) {
if (nombre < 2) return false;
for (int i = 2; i <= Math.sqrt(nombre); i++) {
if (nombre % i == 0) return false;
return true;
}
---
Exercice 2 : Capital et intérêts composés
import java.util.Scanner;
public class InteretsComposes {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Entrez le capital initial : ");
double capital = scanner.nextDouble();
System.out.print("Entrez le taux d'intérêt annuel (en %) : ");
double taux = scanner.nextDouble();
System.out.print("Entrez le nombre d'années : ");
int annees = scanner.nextInt();
double[] capitaux = new double[annees];
double[] interets = new double[annees];
for (int i = 0; i < annees; i++) {
double interet = capital * taux / 100;
capital += interet;
capitaux[i] = capital;
interets[i] = interet;
}
System.out.println("Année\tCapital\t\tIntérêts");
for (int i = 0; i < annees; i++) {
System.out.println((i + 1) + "\t" + capitaux[i] + "\t" + interets[i]);
---
Exercice 3 : Factoriel
import java.util.Scanner;
public class Factoriel {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Entrez un nombre entier : ");
int nbre = scanner.nextInt();
if (nbre < 0) {
System.out.println("Le factoriel n'est pas défini pour les nombres négatifs.");
} else {
long factoriel = 1;
for (int i = 1; i <= nbre; i++) {
factoriel *= i;
System.out.println("Factoriel de " + nbre + " est : " + factoriel);
}
}
---
Exercice 4 : Classe Point et cercle
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
public double distance(Point autre) {
return Math.sqrt(Math.pow(autre.x - this.x, 2) + Math.pow(autre.y - this.y, 2));
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public class Cercle {
public static void main(String[] args) {
Point A = new Point(10, 5);
Point B = new Point(12, 7);
double distance = A.distance(B);
System.out.println("Distance euclidienne : " + distance);
double perimetre = 2 * Math.PI * distance;
double surface = Math.PI * Math.pow(distance, 2);
System.out.println("Périmètre : " + perimetre);
System.out.println("Surface : " + surface);
A.setX(2);
A.setY(2);
System.out.println("Nouvelle distance : " + A.distance(B));
---
Exercice 5 : Classe Fascicule
import java.util.Scanner;
class Fascicule {
private static int compteur = 0;
private int id;
private String titre, auteur;
private double prix;
public Fascicule(String titre, String auteur, double prix) {
this.id = ++compteur;
this.titre = titre;
this.auteur = auteur;
this.prix = prix;
}
public void afficher() {
System.out.println("Fascicule n°" + id + " : " + titre + " de " + auteur + " - Prix : " + prix);
public class TestFascicules {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Fascicule[] fascicules = new Fascicule[3];
for (int i = 0; i < 3; i++) {
System.out.print("Titre du fascicule n°" + (i + 1) + " : ");
String titre = scanner.nextLine();
System.out.print("Nom de l'auteur : ");
String auteur = scanner.nextLine();
System.out.print("Prix : ");
double prix = scanner.nextDouble();
scanner.nextLine(); // Pour consommer la nouvelle ligne
fascicules[i] = new Fascicule(titre, auteur, prix);
for (Fascicule f : fascicules) {
f.afficher();
}
---
Exercice 6 : Héritage Animal
class Animal {
public void communiquer() {
System.out.println("Un animal communique avec les autres.");
class Chat extends Animal {
@Override
public void communiquer() {
System.out.println("Le chat miaule.");
class Chien extends Animal {
@Override
public void communiquer() {
System.out.println("Le chien aboie.");
public class Test {
public static void main(String[] args) {
Animal animal = new Animal();
Animal chat = new Chat();
Animal chien = new Chien();
animal.communiquer();
chat.communiquer();
chien.communiquer();
Si vous avez besoin d'explications sur un exercice ou d'une personnalisation, faites-moi savoir !