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.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Đỗ Minh Quân [T2008A]](https://www.gravatar.com/avatar/fa40264d7c4b4209c87a9e9451d2b9f0.jpg?s=80&d=mm&r=g)
Đỗ Minh Quân
2021-01-24 16:02:59
/*
* 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 t2008a;
import static java.time.Clock.system;
import java.util.Scanner;
import javax.sound.midi.Soundbank;
/**
*
* @author PC
*/
public class T2008A {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Bai 2");
Scanner input = new Scanner(System.in);
System.out.println("Nhap N tu ban phim");
int N = input.nextInt();
int [] q = new int [N];
for (int i=0;i<N;i++){
System.out.println("nhap phan tu" +(i+1)+"la");
q[i]=input.nextInt();
}
int sum = 0;
for (int i=0;i<N;i++){
if (q[i]%3==0)
sum = sum + q[i];
}
System.out.println("tong phan tu trong mang chia het cho 3"+sum);
}
}
![Đỗ Minh Quân [T2008A]](https://www.gravatar.com/avatar/fa40264d7c4b4209c87a9e9451d2b9f0.jpg?s=80&d=mm&r=g)
Đỗ Minh Quân
2021-01-24 15:52:42
/*
* 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 t2008a;
import static java.time.Clock.system;
import java.util.Scanner;
import javax.sound.midi.Soundbank;
/**
*
* @author PC
*/
public class T2008A {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Bai 1");
int[] n = new int [10];
for (int i=0;i<10;i++){
System.out.println("nhap phan tu thu" + (i+1) + "la");
n[i] = input.nextInt();
}
int sum = 0;
for (int i=0;i<10;i++){
sum = sum + n[i];
}
System.out.println("tong cac phan tu la"+sum);
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-01-24 07:51:37
/*
* 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 newpackage;
import static java.lang.Math.sqrt;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class NewClass {
public static void main(String[] args) {
// Bai 4:
Scanner scan = new Scanner (System.in);
int n;
System.out.println("Nhap so max : ");
n = scan.nextInt();
System.out.println("Day so Fobansi la : ");
int F1 = 0;
int F2 = 1;
int Fn = 1;
while(Fn <= n) {
System.out.println("" + Fn);
F1 = F2;
F2 = Fn;
Fn = F1 + F2;
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-01-23 14:55:17
/*
* 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 bai1179;
import java.util.Scanner;
/**
*
* @author MTLS
*/
public class bt1179 {
public static void main(String[] args) {
int f0=0, f1=1, max, fn;
Scanner scan = new Scanner(System.in);
System.out.println("Nhap max = ");
max = scan.nextInt();
System.out.print(f0 + " " + f1 + " " + 1);
fn = f0 + f1;
while(fn <= max) {
f0 = f1;
f1 = fn;
fn = f0 + f1;
if(fn>max) {
break;
}
System.out.print(" " + fn + " ");
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-01-23 14:54:31
/*
* 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 bai978_3;
import java.util.Scanner;
/**
*
* @author MTLS
*/
public class bt978_3 {
public static void main(String[] args) {
int n, i;
Scanner scan = new Scanner(System.in);
System.out.print("Nhap chieu cao cua tam giac: ");
n = scan.nextInt();
for (i=0; i<n; i++) {
for (int j=0; j<=i; j++)
System.out.print("* ");
System.out.println(" ");
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-01-23 14:35:24
/*
* 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 bai978_1_2;
import java.io.PrintStream;
import static java.lang.Double.sum;
import static java.lang.Float.sum;
import static java.lang.Integer.sum;
import java.util.Scanner;
/**
*
* @author MTLS
*/
public class bt978_1_2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Nhap so phan tu cua mang: ");
int n = scan.nextInt();
int[] arr = new int[n];
System.out.println("Nhap cac phan tu cua mang: ");
for (int i=0; i<n; i++) {
PrintStream printf = System.out.printf(" arr[%d] = ", i+1);
arr[i] = scan.nextInt();
}
System.out.print("cac phan tu cua mang la: ");
for (int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
int sum = 0;
for (int i=0; i<n; i++) {
if (arr[i]%3==0) {
sum = sum + arr[i];
}
}
PrintStream printf = System.out.printf("\ntong cac so chia het cho 3 la: %d", sum);
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-23 09:30:51
/*
* 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 pkg2_bai_1;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class bai_5 {
public static void main(String[] args) {
int n, i, j;
Scanner scan = new Scanner(System.in);
System.out.print("Nhap so phan tu: ");
n = scan.nextInt();
int[] a = new int[n];
for (i = 0; i < n; i++) {
System.out.format("nhap gia tri cho a[i] = ", i);
a[i] = scan.nextInt();
}
int sx;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (a[i] < a[j]) {
sx = a[i];
a[i] = a[j];
a[j] = sx;
}
}
}
for (i = 0; i < n; i++) {
System.out.format(" %d ", a[i]);
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-23 06:49:23
/*
* 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 pkg2_bai_1;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class bai_4 {
public static void main(String[] args) {
int n, i;
String tong = "";
Scanner scan = new Scanner(System.in);
System.out.println("nhap n");
n = scan.nextInt();
for (i = 0; i < n; i++) {
tong = tong + "*";
System.out.println(tong);
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-23 06:32: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 pkg2_bai_1;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class bai2 {
public static void main(String[] args) {
int n = 0, tong = 0, i;
Scanner scan = new Scanner(System.in);
System.out.println("nhap n");
n = scan.nextInt();
int[] a = new int[n];
for (i = 0; i < n; i++) {
a[i] = i;
if (a[i] % 3 == 0) {
tong = tong + a[i];
}
}
System.out.println("tong = " + tong);
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2021-01-22 15:02:00
/*
* 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 javalesson2;
import java.util.Scanner;
/**
*
* @author W10-2004
*/
public class ex07 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int N;
// ArrayList<Integer> a = new ArrayList<>();
Scanner r = new Scanner(System.in);
N = r.nextInt();
int a[] = new int[N];
for (int i = 0; i < N; i++) {
a[i] = r.nextInt();
}
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < N; i++) {
System.out.print(a[i] + " ");
}
}
}