Tính diện tích hình chữ nhật

 Tính diện tích hình chữ nhật


class Baitap {
    protected int chieuDai, chieuRong;
    public static double tinhDienTich(double chieudai,double chieuRong) {
        return chieudai * chieuRong;
    }
    public static void main(String[] args) {
        double dai,rong;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhập chiều dài: ");
        dai = scanner.nextDouble();
        System.out.println("Nhập chiều rộng: ");
        rong = scanner.nextDouble();
        System.out.println("diện tích hình chữ nhật là: " + tinhDienTich(dai,rong));
    }
}


Tính tổng các số chia hết cho 5 trong khoảng từ 1 đến n

 Tính tổng các số chia hết cho 5 trong khoảng từ 1 đến n.


public class Demo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhập n:");
        int n = scanner.nextInt();
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            if (i % 5 == 0) {
                sum += i;
            }
        }
        System.out.println("Tổng các số chia hết cho 5: " + sum);
    }
}


Kiểm tra số hoàn hảo

 Kiểm tra số hoàn hảo


public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int sum = 0, a;
    System.out.println("\n\nNhập vào số cần kiểm tra: ");
    a = sc.nextInt();
    for(int i=1;i<=a/2;i++){
      if(a%i==0)
        sum+=i;
    }
    if(sum==a){
      System.out.println(a + " là số hoàn hảo");
    }
    else {
      System.out.println(a + " không phải là số hoàn hảo");
    }
  }
}


Kiểm tra số chính phương

 Kiểm tra số chính phương


class SoChinhPhuong {
    static boolean checkPerfectSquare(double x)
    {
        double sq = Math.sqrt(x);
        return ((sq - Math.floor(sq)) == 0);
    }
    public static void main(String[] args)
    {
        System.out.print("Nhập vào số cần kiểm tra:");
        Scanner scanner = new Scanner(System.in);
        double num = scanner.nextDouble();
        scanner.close();
        if (checkPerfectSquare(num))
            System.out.println(num+ " Là số chính phương");
        else
            System.out.println(num+ " Không phải là số chính phương");
    }
}


Kiểm tra số nguyên tố

 Kiểm tra số nguyên tố


class SoNguyenTo
{
    public static void main(String args[])
    {
        int temp;
        boolean isPrime=true;
        Scanner scan= new Scanner(System.in);
        System.out.println("Nhập vào số cần kiểm tra:");
        int num=scan.nextInt();
        scan.close();
        for(int i=2;i<=num/2;i++)
        {
            temp=num%i;
            if(temp==0)
            {
                isPrime=false;
                break;
            }
        }
        if(isPrime)
            System.out.println(num + " Là số nguyên tố!");
        else
            System.out.println(num + " Không phải là số nguyên tố!");
    }
}


Tìm ước của một số nguyên

 Tìm ước của một số nguyên


import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n;
    System.out.println("\n\nNhập vào số cần tìm ước số: ");
    n = sc.nextInt();
    System.out.printf("Các ước số của %d là: ",n);
    for(int i = 1; i <= n; i++){
      if(n % i == 0){
        System.out.print(i + "\t");
      }
    }
  }
}


Hoán vị hai số

 Hoán vị hai số


class Main {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      float a, b;
      System.out.println("\n\nNhập vào số a: ");
      a = sc.nextFloat();
      System.out.println("Nhập vào số b: ");
      b = sc.nextFloat();

      a = a - b;
      b = a + b;
      a = b - a;
      System.out.println("Sau khi hoán đổi\na = " + a + "\nb = " + b);

    }
  }


Sắp xếp các phần tử trong mảng tăng dần

 Sắp xếp các phần tử trong mảng tăng dần


public class SapXepPhanTuTangDan
{
    public static void main(String[] args)
    {
        int count, temp;
        Scanner scan = new Scanner(System.in);
        System.out.print("Nhập vào số phần tử trong mảng: ");
        count = scan.nextInt();
        int num[] = new int[count];
        System.out.println("Các phần tử trong mảng là:");
        for (int i = 0; i < count; i++)
        {
            num[i] = scan.nextInt();
        }
        scan.close();
        for (int i = 0; i < count; i++)
        {
            for (int j = i + 1; j < count; j++) {
                if (num[i] > num[j])
                {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        System.out.print("Kết quả sau khi được sắp xếp: ");
        for (int i = 0; i < count - 1; i++)
        {
            System.out.print(num[i] + ", ");
        }
        System.out.print(num[count - 1]);
    }
}


Đảo ngược các phần tử trong mảng

 Đảo ngược các phần tử trong mảng


public class DaoNguocPhanTuTrongMang
{
    public static void main(String args[])
    {
        int counter, i=0, j=0, temp;
        int number[] = new int[100];
        Scanner scanner = new Scanner(System.in);
        System.out.print("Nhập vào số phần tử trong mảng: ");
        counter = scanner.nextInt();
        for(i=0; i<counter; i++)
        {
            System.out.print("Phần tử "+(i+1)+": ");
            number[i] = scanner.nextInt();
        }
        j = i - 1;
        i = 0;
        scanner.close();
        while(i<j)
        {
            temp = number[i];
            number[i] = number[j];
            number[j] = temp;
            i++;
            j--;
        }
        System.out.print("Mảng sau khi đảo ngược: ");
        for(i=0; i<counter; i++)
        {
            System.out.print(number[i]+ "  ");
        }
    }
}


Tính trung bình cộng các số trong mảng

 Tính trung bình cộng các số trong mảng



public class TinhTrungBinhCong {
    public static void main(String[] args) {
        System.out.println("Nhập vào số phần tử trong mảng: ");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        double[] arr = new double[n];
        double total = 0;
        for(int i=0; i<arr.length; i++){
            System.out.print("Nhập vào giá trị cho phần tử thứ "+(i+1)+": ");
            arr[i] = scanner.nextDouble();
        }
        scanner.close();
        for(int i=0; i<arr.length; i++){
            total = total + arr[i];
        }
        double average = total / arr.length;
        System.out.format("Kết quả là: %.3f", average);
    }
}

Tìm số nhỏ thứ hai trong mảng

 Tìm số nhỏ thứ hai trong mảng



public class SecondSmallestInArray {
 
    public static int getSecondSmallest(int[] a, int total) {
        int temp;
        for (int i = 0; i < total; i++) {
            for (int j = i + 1; j < total; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        return a[1];
    }
 
    public static void main(String args[]) {
        int a[] = {1, 2, 5, 6, 3, 2};
        int b[] = {44, 66, 99, 77, 33, 22, 55};
        System.out.println("Số nhỏ thứ 2 của Mảng a: " + getSecondSmallest(a, a.length));
    }
}


In các phần tử trùng nhau trong mảng

 In các phần tử trùng nhau trong mảng


Để in ra các phần tử trùng nhau thì chúng ta thực hiện trong 4 bước:

  • Bước 1: Duyệt từ đầu đến cuối mảng.
  • Bước 2: Triển khai vòng for j(ở trong vòng for i) bắt đầu tăng dần từ i đến hết.
  • Bước 3: Duyệt 2 vòng for, nếu arr[i] == arr[j] thì in arr[j]

public class Baitap {  
    public static void  main(String[] args) {    
            int[] arr =  new int[] { 1 ,  2 ,  3 ,  4 ,  2 ,  7 ,  8 ,  8 ,  3 };  
            System.out.println( "Chương trình này được đăng tại Freetuts.net" );  
            System.out.println( "Các phần tử trùng nhau trong mảng đã cho: " );  
            for(int i = 0; i < arr.length; i++) {  
                for(int j = i + 1; j < arr.length; j++) {  
                    if(arr[i] == arr[j])  
                        System.out.println(arr[j]);  
                }  
            }  
        }  
    }

Đảo ngược các từ trong chuỗi

 Đảo ngược các từ trong chuỗi


Ví dụ, nếu chúng ta nhập vào "HELLO" thì sau khi đảo ngược sẽ thành "OLLEH".
public class DaoChuoi
{
    public void reverseWordInMyString(String str)
    {
        String[] words = str.split(" ");
        String reversedString = "";
        for (int i = 0; i < words.length; i++)
        {
            String word = words[i];
            String reverseWord = "";
            for (int j = word.length()-1; j >= 0; j--)
            {
                reverseWord = reverseWord + word.charAt(j);
            }
            reversedString = reversedString + reverseWord + " ";
        }
        System.out.println(str);
        System.out.println(reversedString);
    }
    public static void main(String[] args)
    {
        DaoNguocChuoi obj = new DaoNguocChuoi();
        obj.reverseWordInMyString("HELLO");
    }
}

Kiểm tra chuỗi đối xứng

Kiểm tra chuỗi đối xứng

class KiemTraChuoiDoiXung {
    public static void main(String args[])
    {
        String reverseString="";
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhập vào chuỗi bạn muốn kiểm tra: ");
        String inputString = scanner.nextLine();
        int length = inputString.length();
        for ( int i = length - 1 ; i >= 0 ; i-- )
            reverseString = reverseString + inputString.charAt(i);
        if (inputString.equals(reverseString))
            System.out.println("Đây là chuỗi đối xứng!");
        else
            System.out.println("Đây không phải là chuỗi đối xứng!");
    }
}

Chuyển chữ hoa thành chữ thường

 Chuyển chữ hoa thành chữ thường



public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String message;
        System.out.println("\nNhập vào chuỗi cần in thường: ");
        message = sc.nextLine();
        System.out.println("Chuỗi sau khi in hoa: ");
        System.out.println(message.toLowerCase());
    }
}


Chuyển chữ thường thành chữ hoa

 Chuyển chữ thường thành chữ hoa



class Main {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      String message;
      System.out.println("\n\nNhập vào chuỗi cần in hoa: ");
      message = sc.nextLine();
      System.out.println("Chuỗi sau khi in hoa: ");
      System.out.println(message.toUpperCase());
    }
  }


Liệt kê số lần xuất hiện của các phần tử

 Liệt kê số lần xuất hiện của các phần tử


Nhập số liệu cho dãy số thực a
0, a1,..., an-1. In ra màn hình số lần xuất hiện của các phần tử.


public class Main {
    public static int nhap() {
        Scanner input = new Scanner(System.in);
        boolean check = false;
        int n = 0;
        while (!check) {
            System.out.print(" ");
            try {
                n = input.nextInt();
                check = true;
            } catch (Exception e) {
                System.out.println("Ban phai nhap so! hay nhap lai...");
                input.nextLine();
            }
        }
        return (n);
    }

    public static int countElement(int a[], int n, int i) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (a[j] == i)
                count++;
        }
        return (count);
    }

    public static void main(String[] args) {
        int n, i;
        System.out.println("Nhap n= ");
        n = nhap();
        int[] array = new int[n];
        for (i = 0; i < n; i++) {
            System.out.println("Nhap phan tu thu " + (i + 1) + " ");
            array[i] = nhap();
        }
        for (i = 0; i < n; i++) {
            if (countElement(array, i, array[i]) == 0) {
                System.out.println("Phan tu " + array[i] + " xuat hien " + countElement(array, n, array[i]) + " lan");
            }
        }
    }
}



Liệt kê các phần tử xuất hiện trong dãy đúng một lần

 Liệt kê các phần tử xuất hiện trong dãy đúng một lần


Nhập số liệu cho dãy số thực a0, a1,..., an-1. Hãy liệt kê các phần tử xuất hiện trong dãy đúng một lần.

public class Main {
    public static int nhap() {
        Scanner input = new Scanner(System.in);
        boolean check = false;
        int n = 0;
        while (!check) {
            System.out.print(" ");
            try {
                n = input.nextInt();
                check = true;
            } catch (Exception e) {
                System.out.println("Ban phai nhap so! hay nhap lai...");
                input.nextLine();
            }
        }
        return (n);
    }

    public static int countElement(int a[], int n, int i) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (a[j] == i)
                count++;
        }
        return (count);
    }

    public static void main(String[] args) {
        int n, i;
        System.out.println("Nhap n= ");
        n = nhap();
        int[] array = new int[n];
        for (i = 0; i < n; i++) {
            System.out.println("Nhap phan tu thu " + (i + 1) + " ");
            array[i] = nhap();
        }
        System.out.print("Cac phan tu trong day xuat hien 1 lan: ");
        for (i = 0; i < n; i++) {
            if (countElement(array, n, array[i]) == 1)
                System.out.print(" " + array[i]);
        }
    }
}



Đếm số từ của chuỗi ký tự

 Đếm số từ của chuỗi ký tự


Nhập một xâu ký tự. Đếm số từ của xâu ký tự đó. Thí dụ " Trường học " có 2 từ

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao 1 chuoi: ");
        String str = input.nextLine();
        StringTokenizer strToken = new StringTokenizer(str, " ");
        System.out.println("So cac tu trong chuoi la: " + strToken.countTokens());
    }
}