By GokiSoft.com| 19:31 12/04/2024|
Java Basic

Java Basic- Ôn luyện về vòng lặp for, while, do-while trong java

Bài 1 : 

Khai báo mảng số nguyên gồm 10 phần tử.

Yêu cầu :

- Nhập giá trị cho mảng đó

- Tính tổng giá trị các phần tử trong mảng và in ra màn hình

Bài 2 :

Khai báo mảng số nguyên gồm N phần tử (N nhập từ bàn phím)

Yêu cầu :

- Nhập dữ liệu cho mảng trên

- Tính tổng các phần tử chia hết cho 3 trong mảng và in ra màn hình kết quả.

Bài 3 :

In ra hình sau

*

**

***

****

*****

N = 5

Yêu cầu : Nhập N từ bàn phím, và in ra cây như hình trên

Bài 4 :

Cho biểu thức Fibonaci như sau

F(0) = 1

F(1) = 1

F(n) = F(n-1) + F(n-2)

Nhập từ bản phím số max. In ra tất cả các số Fibonaci có giá trị nhỏ hơn max.

Bài 5 :

Khai báo mảng gồm N số nguyên (N nhập từ bàn phím)

Sắp xếp mảng theo thứ tự tăng dần. và in mảng ra màn hình.

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

cuonglee [C1907L]
cuonglee

2020-03-18 15:07:47



bài 1:


package javabasic;
import java.util.Scanner;
/**
 *
 * @author Admin
 */
public class bai4 {
    public static void main(String[] args) {
        Scanner add = new Scanner(System.in);
       int n, sum = 0;
    do {
        System.out.println("Nhập vào số phần tử của mảng: ");
        n = add.nextInt();
    } while (n < 0);
         
    // khởi tạo và cấp phát bộ nhớ cho mảng
    int array[] = new int[n];
         
    System.out.println("Nhập các phần tử cho mảng: ");
    for (int i = 0; i < n; i++) {
        System.out.print("Nhập phần tử thứ " + i + ": ");
            array[i] = add.nextInt();
    }
     // Hiển thị mảng vừa nhập
    System.out.println("\nMảng ban đầu: ");
    for (int i = 0; i < n; i++) {
        System.out.print(array[i] + "\t");
    }
      
    

               
   }    

}       
bài 3

  public static void main(String[] args) {

     Scanner add = new Scanner(System.in);

      int i,j ,n;

     System.out.print("Nhập n: ");

      n = add.nextInt();

      

    for(i=1; i<n; i++){

        for(j=1; j<=i; j++){

            System.out.print("*");

        }

            System.out.println("");

        }


}       


package javabasic;

import java.util.Scanner;

/**

 *

 * @author Admin

 */

public class bai4 {

    public static void main(String[] args) {

        int i,n;

        Scanner add = new Scanner(System.in);

        System.out.println("Nhap so luong phan tu n: ");

        n = add.nextInt();

        if(n<=0){

            System.out.println("nhap lai n:");

        }

         int [] arr = new int [n];

        System.out.println("Nhap cac phan tu cua mang");

        for ( i = 0; i < n; i++) {

            System.out.printf("a[%d] = ", i);

            arr[i] = add.nextInt();

        }

        sapXep(arr);

        System.out.println("Dãy số được sắp xếp tăng dần: ");

        xuatMang(arr);

    }

     public static void sapXep(int [] arr) {

        int temp = arr[0];

        for (int i = 0 ; i < arr.length - 1; i++) {

            for (int j = i + 1; j < arr.length; j++) {

                if (arr[i] > arr[j]) {

                    temp = arr[j];

                    arr[j] = arr[i];

                    arr[i] = temp;

                }

            }

        }

    }

      public static void xuatMang(int [] arr) {

        for (int i = 0; i < arr.length; i++) {

            System.out.print(arr[i] + " ");

        }

    }

       

}


Trần Mạnh Dũng [T1907A]
Trần Mạnh Dũng

2020-03-16 13:02:18



package bt39;
import java.util.Scanner;
public class Bai1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Vui long nhap 10 phan tu:");
        int[] A = new int[10];
        int tong=0;
        for (int i=0; i < 10; i++) {
            System.out.format("A[%d] = ", i);
            A[i] = scan.nextInt();
            scan.nextLine();
            tong += A[i];
        }
        System.out.println("Tong cac so vua nhap la: "+tong);
    }
}



package bt39;
import java.util.Scanner;
public class Bai2 {
    public static void main(String[] agrs) {
        Scanner scan = new Scanner(System.in);
        int n,total=0;
        do{
        System.out.print("Nhập số phần tử của mảng: ");
        n = scan.nextInt();
        scan.nextLine();
        if (n>0){
        int[] A = new int[n];
        for (int i=0;i<n;i++) {
            System.out.format("A[%d] = ",i);
            A[i] = scan.nextInt();
            scan.nextLine();
            if (A[i]%3==0){
                total+=A[i];
            }
        }
        }
        else {
            System.out.println("Nhập n > 0");
        }
        }while(n<=0);
        System.out.println("Tổng các phần tử chia hết cho 3 trong mảng là " + total);
}



package bt39;
import java.util.Scanner;
public class Bai3 {
    public static void main(String[] agrs) {
        Scanner scan = new Scanner(System.in);
        int n;
        do{
        System.out.print("Nhập N = ");
        n = scan.nextInt();
        if (n>0) {
        for (int i=0; i<n; i++) {
            for (int t=0; t<=i; t++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        }
        else
            System.out.println("Nhập N > 0");
    
        }while(n<=0);
    } 
}



package bt39;
import java.util.Scanner;
public class Bai4 {
    public static void main(String[] agrs) {
        Scanner scan = new Scanner(System.in);
        int a=0,b=1,c=a+b;
        float max;
        do{
        System.out.print("Nhập max: ");
        max = scan.nextFloat();
        if (max > 0) {
        System.out.print(a + " ");
        while(c<max){
            System.out.print(c + " ");
            c=a+b;
            a=b;
            b=c;
        }
        }else 
            System.out.println("Nhập max > 0");
        }while(max<=0);
    }
}




package bt39;
import java.util.Scanner;
public class Bai5 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n,bn;
        do{
            System.out.println("Khai báo mảng gồm N số nguyên:");
            System.out.print("Nhập n: ");
            n = scan.nextInt();
            if(n>0){
            int[] A = new int[n];
            for (int i=0; i<n;i++) {
                System.out.format("Nhập A[%d] = ", i);
                A[i] = scan.nextInt();
                scan.nextLine();
            }
            for (int i=0; i<n-1;i++){
                for (int j= i+1; j < n; j++){
                    if(A[i]>A[j]){
                        bn = A[i];
                        A[i] = A[j];
                        A[j] = bn;
                    }
                }
            }
            System.out.println("Sắp xếp mảng theo thứ tự tăng dần:");
            for (int i=0; i<n;i++){
                System.out.print(A[i] + ", ");
            }
            }else
                System.out.println("Nhập n > 0");
        }while(n<=0);
    }
}




Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-03-16 12:20:00

Bài 1:

package bt39;
import java.util.Scanner;
public class BT391 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] t = new int[10];
        int tong=0;
        for (int i=0; i < 10; i++) {
            System.out.format("t[%d] = ", i);
            t[i] = scan.nextInt();
            scan.nextLine();
            tong += t[i];
        }
        System.out.println("Tong cac so vua nhap la: " + tong);
    }
}

Bài 2:
package bt39;
import java.util.Scanner;
public class BT392 {
    public static void main(String[] agrs) {
        Scanner scan = new Scanner(System.in);
        int n,tong=0;
        do{
        System.out.print("Nhap so phan tu cua mang: ");
        n = scan.nextInt();
        scan.nextLine();
        if (n>0){
        int[] t = new int[n];
        for (int i=0;i<n;i++) {
            System.out.format("t[%d] = ",i);
            t[i] = scan.nextInt();
            scan.nextLine();
            if (t[i]%3==0){
                tong+=t[i];
            }
        }
        }
        else {
            System.out.println("Vui long nhap n > 0");
        }
        }while(n<=0);
        System.out.println("Tong cac so chia het cho 3 = " + tong);
    }
}

Bài 3:
package bt39;
import java.util.Scanner;
public class BT393 {
    public static void main(String[] agrs) {
        Scanner scan = new Scanner(System.in);
        int n;
        do{
        System.out.print("Nhap n = ");
        n = scan.nextInt();
        if (n>0) {
        for (int i=0; i<n; i++) {
            for (int t=0; t<=i; t++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        }
        else
            System.out.println("Vui long nhap n > 0");
    
        }while(n<=0);
    }    
}

Bài 4:
package bt39; 
import java.util.Scanner;
public class BT394 {
    public static void main(String[] agrs) {
        Scanner scan = new Scanner(System.in);
        int a=0,b=1,c=a+b;
        float max;
        do{
        System.out.print("Nhap max: ");
        max = scan.nextFloat();
        if (max > 0) {
        System.out.print(a + " ");
        while(c<max){
            System.out.print(c + " ");
            c=a+b;
            a=b;
            b=c;
        }
        }else 
            System.out.println("Vui long nhap max > 0");
        }while(max<=0);
    }
}

Bài 5:
package bt39;
import java.util.Scanner;
public class BT395 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n,bn;
        do{
            System.out.print("Nhap n: ");
            n = scan.nextInt();
            if(n>0){
            int[] t = new int[n];
            for (int i=0; i<n;i++) {
                System.out.format("Nhap t[%d] = ", i);
                t[i] = scan.nextInt();
                scan.nextLine();
            }
            for (int i=0; i<n-1;i++){
                for (int j= i+1; j < n; j++){
                    if(t[i]>t[j]){
                        bn = t[i];
                        t[i] = t[j];
                        t[j] = bn;
                    }
                }
            }
            System.out.println("Cac phan tu mang sau khi da sap xep");
            for (int i=0; i<n;i++){
                System.out.print(t[i] + ", ");
            }
            }else
                System.out.println("Vui long nhap n > 0");
        }while(n<=0);
    }
}



Lê Trí Dũng [C1907L]
Lê Trí Dũng

2020-03-16 09:27:40



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package baitapjava;

import java.util.Scanner;
/**
 *
 * @author Dung
 */
public class loop {
    
//    //Bài 1:
    public static void main(String[] args){
        int [] arr = {0;1;2;3;4;5;6;7;8;9};
        int sum = 0
            for (int i = 0; i <= 10;i++){
            sum += arr[i];
            System.out.println("Tổng của dãy số trên là: " + sum);
    }
}

//    //Bài 2
    public static void main(String[] args){
        int n;
        int sum = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhập vào số phần tử của mảng: ");
        n = scanner.nextInt;
        
    //Khởi tạo và cấp phát bộ nhớ cho mảng
    int array[] = new int[n];
    System.out.println("Nhập các phần tử cho mảng: ");
    for (int i = 0; i < n; i++){
    System.out.println("Nhập phần tử thứ " + i + ": ");
    array[i] = scanner.nextInt;
}
    //Hiển thị mảng vừa nhập
    System.out.println("\nMảng vừa nhập: ")
    for (int i = 0; i < n; i++){
    System.out.println("\t" + array[i]);
}
    //tính tổng mảng vừa nhập
    for (int i = 0; i < n; i++){
    sum += array[i];
}
    System.out.println("\n Tổng các phần tử có trong mảng = " + sum);

//Bài 3:

//Bài 4:
    public static void main(String[] args){
            System.out.println("Dãy số fibonacci: ");
            for (int i = 0; i <= n; i++) {
                System.out.print(fibonacci(i) + " ");
            }
    }
    public static int fibonacci(int n) {
        if (n < 0) {
            return -1;
        } else if (n == 0 || n == 1) {
                return n;
        } else {
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    }

//Bài 5:



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-16 06:48:48



package BaiTapNgay26_2_2020.bai39;

import java.util.Scanner;

public class bai39 {

    //bài 1
  /*  public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int sum=0;
        int array[] = new int[10];
        System.out.println("Nhập giá trị cho từng phần tử: ");
        for (int i=0;i<10;i++){
            System.out.print("Phần tử thứ "+ i +": " );
            array[i] = input.nextInt();
            sum += array[i];
        }
        System.out.println("Tổng các phần tử của mảng là :" +sum);
    }*/

    //bài 2
   /* public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
        int n, sum = 0, i = 0;
        System.out.print("Nhập vào số phần tử của mảng: ");
        n = input.nextInt();
        int array[] = new int[n];
        if (n <= 0) {
            System.out.println("Nhập lỗi !!!");
        } else {
            System.out.println("Nhập giá trị cho từng phần tử:");
            for (i = 0; i < n; i++) {
                System.out.print("Phần tử thứ " + i + ": ");
                array[i] = input.nextInt();
            }

            for (int j = 0; j < n; j++) {
                if (array[j] % 3 == 0) {
                    sum += array[j];
                }
            }
            System.out.println("Tổng các sổ chia hết cho 3 là: "+sum);
        }
    }*/

   //bài 3
   /*public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       int n, i , j ;
       System.out.print("Nhập số n: ");
       n = input.nextInt();
       for (i=0;i<n;i++){
           for (j=0;j<=i;j++){
           System.out.print("*");
           }
           System.out.println("");

       }
   }*/
}




Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-03-16 05:53:04



package bt;

import java.util.ArrayList;
import java.util.Scanner;

public class Bt2 { 
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.println("Bai so 1:");
        int[] arr1 = new int[10];
        int tong=0;
        System.out.println("Nhap vao mang co 10 so nguyen: ");
        for(int i=0;i<10;i++){
            arr1[i] = input.nextInt();
            tong=tong+arr1[i];
        }
        System.out.println("Tong la: "+tong);
        
        System.out.println("\nBai so 2:");
        int n;
        System.out.println("Nhap vao n: ");
        n = input.nextInt();
        int[] arr2 = new int[n];
        System.out.println("Nhap vao mang co "+n+" so nguyen: ");
        int so3=0;
        for(int i=0;i<n;i++){
            arr2[i] = input.nextInt();
            if(arr2[i]%3==0){
                so3++;
            }
        }
        System.out.println("So cac So chia het cho 3 la: "+so3);

        System.out.println("\nBai so 3:");
        int sao;
        System.out.println("Nhap vao n: ");
        sao = input.nextInt();
        for(int i=1;i<=sao;i++){
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
               System.out.print("\n");
        }
        
        System.out.println("\nBai so 4:");
        System.out.print("Nhap vao Max: ");
        int max = input.nextInt();
        ArrayList<Integer> f = new ArrayList<>();
        f.add(1);
        f.add(1);
        if(max>=1){
            System.out.print("Day Fibonaci t/m:\n1 1 ");
            for(int i=1;f.get(i)<max;i++){
                int a= f.get(i) + f.get(i-1);
                if( a<max){
                    f.add(a);
                    System.out.print(a+" ");
                }else{
                    System.out.println();
                    break;
                }
            }
        }else{
            System.out.println("Ko co day Fibonaci t/m.");
        }
        
        System.out.println("\nBai so 5:");
        System.out.print("Nhap vao N: ");
        int num = input.nextInt();
        int[] mang = new int[num];
        System.out.println("Nhap vao mang co "+num+" phan tu: ");
        for(int i=0;i<num;i++){
            mang[i] = input.nextInt();
        }
        for(int i=0;i<num-1;i++){
            for(int j=i+1;j<num;j++){
                if( mang[i]>mang[j]){
                    int tmp=mang[i];
                    mang[i]=mang[j];
                    mang[j]=tmp;
                }
            }
        }
        System.out.println("Mang sau khi sap xep tang dan la:");
        for(int i=0;i<num;i++){
            System.out.print( mang[i]+" ");
        }
    }
}



trung [C1907L]
trung

2020-03-15 16:23:53



import java.util.Scanner;
import java.lang.Math;
import java.util.ArrayList;
/**
 *
 * @author prdox
 */

//Viết chương trình hiển thị thông tin cá nhân của bạn bao gồm (tên, tuổi, địa chỉ, email, sđt)
public class Main {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
//        //Bài 1 : 
//        final int ARRAY_SIZE = 10;
//        Scanner input = new Scanner(System.in);
//        int arr[] = new int[ARRAY_SIZE];
//        int tong = 0;
//        //Nhap gia tri
//        for (int i = 0; i < ARRAY_SIZE; i++){
//            System.out.format("Arr[%d]:= ",i+1);
//            arr[i] = Integer.parseInt(input.nextLine());
//            tong += arr[i];
//        }
//        System.out.format("Tong cac phan tu trong mang la: %d",tong);

//        //Bai 2
//        Scanner input = new Scanner(System.in);
//        System.out.println("So phan tu cua mang la:= ");
//        int N = Integer.parseInt(input.nextLine());
//        int tong = 0;
//        ArrayList<Integer> arr = new ArrayList<Integer>();
//        for (int i = 0; i < N; i++){
//            System.out.format("Arr[%d]:= ",i+1);
//            arr.add(Integer.parseInt(input.nextLine()));
//            if (arr.get(arr.size()-1) % 3 == 0) tong+= arr.get(arr.size()-1);
//        }
//        System.out.format("Tong cac phan tu chia het cho 3 trong mang la: %d",tong);

//        //Bai3
//        Scanner input = new Scanner(System.in);
//        System.out.println("N:= ");
//        int N = Integer.parseInt(input.nextLine());
//        for (int i = 0; i < N; i++){
//            for (int j = 0; j <= i; j++){
//                System.out.print("*");
//            }
//            System.out.print("\n");
//        }

//        //Bai4
//        Scanner input = new Scanner(System.in);
//        System.out.println("Max:= ");
//        int max = Integer.parseInt(input.nextLine());
//        if (max < 1) return;
//        int x = 0;
//        int y = 1;
//        while (y < max){
//            System.out.println(y);
//            y = x + y;
//            x = y - x;
//        }

        //Bài 5 :
        Scanner input = new Scanner(System.in);
        System.out.println("N:= ");
        int N = Integer.parseInt(input.nextLine());
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for (int i = 0; i < N; i++){
            System.out.format("Arr[%d]:= ",i+1);
            arr.add(Integer.parseInt(input.nextLine()));
        }
        for (int i = 0; i < N-1; i++){
            for (int j = i+1; j < N; j++){
                if (arr.get(i) > arr.get(j)){
                    int temp = arr.get(i);
                    arr.set(i,arr.get(j));
                    arr.set(j,temp);
                }
            }
        }
        for (int i = 0; i < N; i++){
            System.out.format("Arr[%d]:= %d\n",i+1,arr.get(i));
        }
    }

}



nguyễn thị bích thủy [C1907L]
nguyễn thị bích thủy

2020-03-15 13:40:22

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

 // bài 1:

        /*int[] n = new int[10];

        for (int i = 0; i < 10; i++) {

            System.out.println("Nhập phần tử thứ " + (i + 1) + " là:");

            n[i] = input.nextInt();

        }

        int sum = 0;

        for (int i = 0; i < 10; i++) {

            sum = sum + n[i];

        }

        System.out.println("Tổng các phần tử của mảng n là " + sum);

        */

// bài 2:

       /* System.out.println("\nNhập N = ");

        int N= input.nextInt();

        int[] m= new int [N];

        for (int i = 0; i < N; i++) {

            System.out.println("Nhập phần tử thứ " + (i + 1) + " là:");

            m[i] = input.nextInt();

        }

        int sum1=0;

         for (int i = 0; i < N; i++){

             if(m[i]%3==0) sum1=sum1+m[i];

         }

           System.out.println("Tổng các phần tử trong mảng chia hết cho 3 là " + sum1);

           */

    //bài 3:

        /*System.out.println("\nNhập N = ");

        int N= input.nextInt();

        System.out.println("\n *");

         System.out.println("\n **");

          System.out.println("\n ***");

           System.out.println("\n ****");

            System.out.println("\n *****");

             System.out.println("\n N = "+ N);

             */

     // bài 4:

        System.out.println("\nNhập N = ");

        int N= input.nextInt();

        int[] m= new int [N];

        for (int i = 0; i < N; i++) {

            System.out.println("Nhập phần tử thứ " + (i + 1) + " là:");

            m[i] = input.nextInt();

        }

        int num;

        for (int i = 0; i < N-1; i++){

            for (int j = 1; j < N; j++){

                if(m[j]<m[i]){

                    num=m[i];

                    m[i]=m[j];

                    m[j]=num;

                }

            }                       

        }

        System.out.format("\nCác phần tử đã sáp xếp là:");

        for (int i = 0; i < N; i++){

         System.out.format("" +m[i]);

        }

    }


Hoàng Quang Huy [C1907L]
Hoàng Quang Huy

2020-03-15 08:09:47



import java.util.Scanner;

public class Lab2_ex2 {


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Bài 1 :
		// Khai báo mảng số nguyên gồm 10 phần tử.
		// Yêu cầu :
		// - Nhập giá trị cho mảng đó
		// - Tính tổng giá trị các phần tử trong mảng và in ra màn hình
		Scanner input = new Scanner(System.in);
		System.out.println("Bài 1: ");
		int[] a = new int[10];
		for (int i = 0; i < a.length; i++) {
			System.out.println("Nhập giá trị a[" + i + "] = ");
			a[i] = input.nextInt();
		}
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum += a[i];
		}
		System.out.println("Tổng giá trị phần tử trong mảng là: " + sum);

		// Bài 2 :
		// Khai báo mảng số nguyên gồm N phần tử (N nhập từ bàn phím)
		// Yêu cầu :
		// - Nhập dữ liệu cho mảng trên
		// - Tính tổng các phần tử chia hết cho 3 trong mảng và in ra màn hình kết quả.
		System.out.println("Bài 2:");
		System.out.println("Nhập số phần tử của mảng: ");
		int n = input.nextInt();
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			System.out.println("Nhập giá trị b[" + i + "] = ");
			b[i] = input.nextInt();
		}
		int sum2 = 0;
		for (int i = 0; i < n; i++) {
			if (b[i] % 3 == 0) {
				sum2 += b[i];
			}
		}
		System.out.println("Tổng các phần tử chia hết cho 3 là: " + sum2);

//		Bài 3 :
//			In ra hình sau
//			*
//			**
//			***
//			****
//			*****
//			N = 5
//			Yêu cầu : Nhập N từ bàn phím, và in ra cây như hình trên
		System.out.println("Bài 3:");
		System.out.println("Nhập N: ");
		int n1 = input.nextInt();
		int n2 = 1;
		while (n2 <= n1) {
			System.out.println(String.format("%0" + n2 + "d", 0).replace("0", "*"));
			n2++;
		}

//		Bài 4 :
//		Cho biểu thức Fibonaci như sau
//			F(0) = 1
//			F(1) = 1
//			F(n) = F(n-1) + F(n-2)
//			Nhập từ bản phím số max. In ra tất cả các số Fibonaci có giá trị nhỏ hơn max.
		System.out.println("Bài 4: ");
		int f1 = 1, f2 = 1, f = 0;
		System.out.println("Nhập giá trị max: ");
		int max = input.nextInt();

		while (max <= 1) {
			System.out.println("Giá trị max không hợp lệ");
			System.out.println("Nhập lại giá trị max: ");
			max = input.nextInt();
		}
		System.out.print("Dãy Fibonacci nhỏ hơn max là: 1 1 ");
		while (true) {
			f = f1 + f2;
			f1 = f2;
			f2 = f;
			if (f >= max) {
				break;
			}
			System.out.print(f + " ");
		}
		// Bài 5 :
		// Khai báo mảng gồm N số nguyên (N nhập từ bàn phím)
		// Sắp xếp mảng theo thứ tự tăng dần. và in mảng ra màn hình.
		System.out.println("\nBài 5:");
		System.out.println("Nhập n: ");
		int n5 = input.nextInt();
		int[] c = new int[n5];
		int temp;
		for (int i = 0; i < c.length; i++) {
			System.out.println("Nhập giá trị c[" + i + "]= ");
			c[i] = input.nextInt();
		}
		for (int i = 0; i < n5 - 1; i++) {
			for (int j = i + 1; j < n5; j++) {
				if (c[i] > c[j]) {
					temp = c[i];
					c[i] = c[j];
					c[j] = temp;
				}
			}
		}
		System.out.print("Mảng sắp xếp tăng dần: ");
		for (int i = 0; i < n5; i++) {
			System.out.print(c[i] + " ");
		}
	}
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-03-14 02:44:12



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package loop;

import java.util.Scanner;
import java.util.ArrayList;

/**
 *
 * @author Redmibook 14
 */
public class Bai1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] n = new int[10];
        int sum = 0;

        for (int i = 0; i < n.length; i++) {
            System.out.format("Nhap phan tu thu %d : ", i + 1);
            n[i] = input.nextInt();
            sum += n[i];
        }
        System.out.format("Sum : %d : ", sum);

        System.out.format("Nhap so phan tu : ");
        int N = input.nextInt();
        int[] arr = new int[N];
        for (int i = 0; i < arr.length; i++) {
            System.out.format("Moi nhap phan tu thu  %d : ", i + 1);
            arr[i] = input.nextInt();
        }
        int Sum3 = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 3 == 0) {
                Sum3 += arr[i];
            }
        }
        System.out.format("Tong cac phan tu chia het cho 3 la  %d : ", Sum3);
        System.out.print("Moi nhap so phan tu : ");
        int n3 = input.nextInt();
        String[] arr3 = new String[n3];
        String chars = "*";
        String printc = "";
        for (int i = 0; i < arr3.length; i++) {
            printc += chars;
            System.out.printf(printc + "%n");
        }
        System.out.print("Moi nhap so phan tu : ");
        int n4 = input.nextInt();
        int[] arr4 = new int[n4];
        int sUm = 1;
        arr4[0] = 1;
        arr4[1] = 1;
        for (int i = 2; i < arr4.length; i++) {
            arr4[i] = arr4[i - 1] + arr4[i - 2];
            sUm += 1;

        }
        System.out.printf("Day fibonaci : %n");
        for (int i = 0; i < arr4.length; i++) {
            System.out.printf(arr4[i] + "%n");
        }
        System.out.format("Nhap so phan tu : ");
        int n5 = input.nextInt();

        int[] arr5 = new int[n5];

        for (int i = 0; i < arr5.length; i++) {
            System.out.format("Nhap phan tu thu %d : ", i + 1);
            arr5[i] = input.nextInt();

        }
        System.out.printf("Sap xep : %n");
        for (int i = 0; i < arr5.length; i++) {
            for (int j = i + 1; j < arr5.length; j++) {
                int t;
                if (arr5[i] > arr5[j]) {
                    t = arr5[i];
                    arr5[i] = arr5[j];
                    arr5[j] = t;
                }
            }
            System.out.printf(arr5[i] + "%n");
        }

    }
}