# Bài tập Java Full HD ko che

trantri2k2
java
3 years ago
4.9 kB
9
Indexable
Never
```import java.util.*;
import java.util.Arrays;
public class Main
{
// Ham
public static boolean laSoNguyenTo(int n){
if(n < 2) return false;
for(int i=2;i<=Math.sqrt(n);i++) {
if(n % i == 0) return false;
}
return true;
}

public static int laSoFib(int n) {
int f0 = 0;
int f1 = 1;
int fn = 1;

if (n < 0) {
return -1;
} else if (n == 0 || n == 1) {
return n;
} else {
for (int i = 2; i < n; i++) {
f0 = f1;
f1 = fn;
fn = f0 + f1;
}
}
return fn;
}
// Cau 8 den cau 18
public static void cau8(){
System.out.print("Nhap n:");
int n = sc.nextInt();

System.out.println(n+" so nguyen to dau tien la:");
int sum=0,count=0;
for(int i=0;i<100;i++){
if(count==n) break;
if(laSoNguyenTo(i)){
count++;
System.out.print(i+" ");
sum+=i;
}
}
System.out.println("\nTong "+n+" so nguyen to dau tien la:"+sum);
}

public static void cau9(){
System.out.print("Nhap mot chuoi:");
String str = sc.nextLine();
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i) == 'a') count++;
}
System.out.println("So luong chu 'a' trong chuoi la: "+count);
}

public static void cau10(){
System.out.print("Nhap mot chuoi:");
String str = sc.nextLine();
System.out.println("Chuoi sau khi tach la:");
int count=1;
for(int i=0;i<str.length();i++){
if(str.charAt(i) == ' ') count++;
}
String arrStr[] = new String[count];
arrStr = str.split(" ");
for(int i=0;i<count;i++){
System.out.println(arrStr[i]);
}
}

public static void cau11(){
System.out.print("Nhap mot chuoi:");
String str = sc.nextLine();
int count=0;
for(int i=0;i<str.length();i++){
if(Character.isDigit(str.charAt(i))) count+=1;
}
System.out.println("So ky tu la so trong chuoi la:"+count);
}

public static void cau12(){

int count=0,sum=0;
System.out.println("Nhap mot day so xen ke bat ky:");
while(count!=10){
int n = sc.nextInt();
if(n%2==0){
System.out.print(n+" ");
sum+=n;
count++;
}
}
System.out.println("\nTong cua 10 so chan dau tien la:"+sum);
}

public static void cau13(){
System.out.println("So le tu 1 den 99 la:");
for(int i=0;i<100;i++){
if(i%2==1) System.out.print(i+" ");
}
}

public static void cau14(){
System.out.println("Cac so la boi so cua 7 la:");
int rs=7,i=1,sum=0;
while(true){
rs = 7 * (i++);
if(rs>100) break;
sum+=rs;
}
System.out.println("\nTong cac so la boi cua 7 (1->100):"+sum);
}

public static void cau15(){
System.out.print("Nhap 1 day so:");
String str = sc.nextLine();
String myStr[] = str.split(" ");
int max = Integer.parseInt(myStr[0]);
int min = Integer.parseInt(myStr[0]);
for(int i=1;i<myStr.length;i++){
int num1 = Integer.parseInt(myStr[i]);
int num2 = Integer.parseInt(myStr[i]);
if(num1 > max) max = num1;
if(num2 < min) min = num2;
}
System.out.println("Min:"+min+"\nMax:"+max);
}

public static void cau16(){
System.out.print("Nhap mot so bat ky:");
int n = sc.nextInt();
if(n==0) System.out.println("zero");
System.out.println(n%2==0?"So chan":"So le");
}

public static void cau17(){
System.out.println("Boi cua so 3 (tu 300 den 3):");
int rs=3,i=1;
int a[] = new int[100]; //So luong phan tu mac dinh la 100 (tu hieu nha :)) )
while(true){
rs = 3*i;
if(rs>300) break;
a[i-1] = rs;
i++;
}
for(i=a.length-1;i>=0;i--){
System.out.print(a[i]+" ");
}
}

public static void cau18(){
System.out.print("Nhap n:");
int n = sc.nextInt();
System.out.println(n+" so fib dau tien la");
int i=1;
while(i <= n){
System.out.print(laSoFib(i++)+" ");
}
}
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//cau8();
//cau9();
//cau10();
//cau11();
//cau12();
//cau13();
//cau14();
//cau15();
//cau16();
//cau17();
//cau18();
}
}
```