Basic Java exercises, with sample decoding

To serve your Java learning needs, TipsMake.com has synthesized some Java exercises from many sources, including sample code (for some articles). Hopefully it can be helpful to learn your Java programming language.

To serve your Java learning needs, TipsMake.com has synthesized some Java exercises from many sources, including sample code (for some articles). Hopefully it can be helpful to learn your Java programming language.

Basic Java exercises have a solution

Lesson 1 . Write a program to find the greatest common divisor, the smallest common multiple of two natural numbers a and b. See the article Lesson 1

Lesson 2. Write a program to convert a natural number of the base 10 system into numbers at any b-base system (1

Lesson 3 . Write a program that calculates the sum of the digits of any integer. For example: Number 8545604 has a total of 8 digits: 8 + 5 + 4 + 5 + 6 + 0 + 4 = 32. See Solution 3

Lesson 4 . Write a program that analyzes an integer into prime factors. Example: Number 28 is broken down into 2 x 2 x 7. See Lesson 4

Lesson 5 . Write a program that lists all primes smaller than n given. See Lesson 5

Lesson 6 . Write a program that lists the first n prime numbers. See Lesson 6

Lesson 7 . The Fibonacci number sequence is defined as follows: F0 = 1, F1 = 1; Fn = Fn-1 + Fn-2 with n> = 2. Write a program to find the Nth Fibonacci number. See Problem 7

Lesson 8. A number is called a reverse reversal number if we read from left to right or right to left number we still get the same number. Please list all six-digit malicious reversal numbers (Example: 558855). See Lesson 8

Lesson 9. Write a program that lists all binary strings of length n. See the lesson Lesson 9

Lesson 10. Write a program that lists all k subsets of 1, 2, ., n (k≤n). See Lesson 10

Lesson 11. Write a program that lists all permutations of 1, 2, ., n. See Lesson 11

Lesson 12. Calculate the value of the polynomial P (x) = a n x n + a n-1 x n-1 + . + a 1 x + a 0 according to the calculation of Horner: P (x) = (( ((a n x + a n- 1) x + a n-2 . + a 1 ) x + a 0

See Lesson 12

Lesson 13 . Enter data for 2 real numbers a 0 , a 1 , ., a m-1 and b 0 , b 1 , ., b n-1 . Suppose both of these sequences have been sorted in ascending order. Take advantage of the alignment of the two ranges and create the range c 0 , c 1 , ., c m + n-1 is the combination of the two ranges, so that the sequence c i also has an ascending order. See Lesson 13

Lesson 14 . Enter the data for the real number sequence a 0 , a 1 , ., a n-1 . Please list the elements that appear in the sequence exactly once. See Lesson 14

Lesson 15 . Enter the data for the real number sequence a 0 , a 1 , ., a n-1 . Please list the elements that appear in the sequence exactly 2 times. See Lesson 15

Lesson 16. Enter the data for the real number sequence a 0 , a 1 , ., a n-1 . Print to the screen the number of occurrences of the elements. See Lesson 16

Lesson 17. Enter the number n and the sequence of real numbers a 0 , a 1 , ., a n-1 . Do not change the elements and do not use any other real number array (you can use the integer array if necessary). See Lesson 17

Lesson 18. Enter a string of characters. Count the number of words of that string. For example, "School" has 2 words. See Lesson 18

Lesson 19. Write a program that lists all 5-digit primes so that the sum of the digits in each prime number is equal to the given S. See Lesson 19

Lesson 20. Enter a natural number n. Please list the number of Fibonacci smaller than n is the prime number. See Lesson 20

Lesson 21. Write a program to enter a positive integer n and perform the following functions:

  1. Calculate the sum of the digits of
  2. Analyze n into numerical factors.

See Lesson 21

Lesson 22. Write a program to enter a positive integer n and perform the following functions:

  1. List the divisors of n. There are many divisors.
  2. List the divisors that are prime of

See Lesson 22

Lesson 23. Write a program to enter a positive integer n and perform the following functions:

  1. List the first n prime numbers.
  2. List the first n Fibonacci numbers.

See Lesson 23

Lesson 24. Write a program to enter into matrix A with n lines, m columns, elements that are integers greater than 0 and less than 100 entered from the keyboard. Perform the following functions:

  1. Find the largest element of the matrix with the index of that number.
  2. Find and print the elements that are prime numbers of the matrix (non-prime elements are replaced by numbers 0).
  3. Sort all columns of the matrix in ascending order and print the results to the screen.

See Lesson 24

Exercise 25. Write a program that lists integers of 5 to 7 digits satisfying:

  1. Is a prime number.
  2. The number is reversible.
  3. Each digit is a prime number

See lesson 25

Lesson 26. Write a program that lists 7-digit integers that satisfy:

  1. Is a prime number.
  2. The number is reversible.
  3. The sum of the digits of that number is a reversible number

See lesson 26

Lesson 27. Write an input program into array A with n elements, elements that are integers greater than 0 and less than 100 entered from the keyboard. Perform the following functions:

  1. Find the largest and second largest element in the array with the index of those numbers.
  2. Arrange the array in descending order.
  3. Enter an integer x and insert x into array A so as to ensure descending sorting.

See lesson 27

Lesson 28. Write a program to enter into matrix A with n lines, m columns, elements that are integers greater than 0 and less than 100 entered from the keyboard. Perform the following functions:

  1. Find the largest element of the matrix with the index of that number.
  2. Find and print the elements that are prime numbers of the matrix (non-prime elements are replaced by 0).
  3. Find the row in the matrix with the most primes.

See lesson 28

Lesson 29. Write a program to enter the coefficients of the polynomial P of degree n (0

  1. Calculating the value of polynomial P according to Horner formula: P (x) = (((a n x + a n-1 ) x + a n-2 . + a 1 ) x + a 0
  2. Calculate the derivative of polynomial P. Print out the coefficients of the result polynomial.
  3. Enter more polynomial Q steps m. Calculate the sum of two polynomials P and Q

See lesson 29

Lesson 30. Write an input program into array A with n elements, elements that are integers greater than 0 and less than 100 entered from the keyboard. Perform the following functions:

  1. Find the largest and second largest element in the array with the index of those numbers.
  2. Arrange the array in descending order.
  3. Enter an integer x and insert x into array A so as to ensure descending sorting.

Lesson 31. Write a program to perform standardization of a string entered from the keyboard (remove extra spaces, convert the first character of each word to uppercase, other characters to lowercase)

See the article 31

Lesson 32. Write a program to execute a string and find the longest word in that string. Where does that word appear? (Note. If there are multiple words of the same length, select the first word found).

See solution 32

Lesson 33. Write a program that implements a structured string: they . buffer . name; convert that string to represent by name structure . they . buffer. See Lesson 33

The basic Java solution does not solve

Lesson 34. Write a program that lists all the elements of the set:

Basic Java exercises, with sample decoding Picture 1Basic Java exercises, with sample decoding Picture 1

Lesson 35. Write a program that lists all the elements of the volume

Basic Java exercises, with sample decoding Picture 2Basic Java exercises, with sample decoding Picture 2

Lesson 36. Write a program that lists all the elements of the set

Basic Java exercises, with sample decoding Picture 3Basic Java exercises, with sample decoding Picture 3

Lesson 37. Let two sets A consisting of n elements, B include m elements (n, m≤255), each of its elements is a string of characters. Example A = {'Lan', 'Hang', 'Ming', 'Shui'}, B = {'Meaning', 'Chinese', 'Ming', 'Shui', 'German'}. Write a program to do the following:

  1. Create data for A and B (from file or keyboard)
  2. Finding Basic Java exercises, with sample decoding Picture 4Basic Java exercises, with sample decoding Picture 4 .
  3. Finding Basic Java exercises, with sample decoding Picture 5Basic Java exercises, with sample decoding Picture 5 .
  4. Finding Basic Java exercises, with sample decoding Picture 6Basic Java exercises, with sample decoding Picture 6 .

Lesson 38. Give two polynomials Basic Java exercises, with sample decoding Picture 7Basic Java exercises, with sample decoding Picture 7 . Write a program to do the following:

  1. Create two polynomials (enter the coefficient for polynomial from the keyboard or file)
  2. Calculation Basic Java exercises, with sample decoding Picture 8Basic Java exercises, with sample decoding Picture 8
  3. Find the derivative of l ≤n of the polynomial.
  4. Finding Basic Java exercises, with sample decoding Picture 9Basic Java exercises, with sample decoding Picture 9
  5. Finding Basic Java exercises, with sample decoding Picture 10Basic Java exercises, with sample decoding Picture 10
  6. Finding Basic Java exercises, with sample decoding Picture 11Basic Java exercises, with sample decoding Picture 11 and residual polynomial

Lesson 39. Give two square matrices A level n. Write a program to do the following:

  1. Find the row, column or diagonal with the largest sum of elements.
  2. Find the displacement matrix of A
  3. Find the determinant of A
  4. Find the inverse matrix of A
  5. Solve the homogeneous linear equation n hidden AX = B by Gauss method

Lesson 40. Give a character buffer with n lines. Write a program to do the following:

  1. Create n lines of text for the buffer.
  2. Count the number of words in the Buffer.
  3. Find the frequency that appears from any X in the buffer.
  4. Encrypt the buffer with the Parity Bits technique
  5. Decode the buffer encoded by parity technique.
  6. Replace the word X with the word Y

Lesson 41. Write a program to do the following:

1. List the elements of the volume

Basic Java exercises, with sample decoding Picture 12Basic Java exercises, with sample decoding Picture 12

Inside Basic Java exercises, with sample decoding Picture 13Basic Java exercises, with sample decoding Picture 13 b is positive integers, Basic Java exercises, with sample decoding Picture 14Basic Java exercises, with sample decoding Picture 14

2. List the elements of the set:

Basic Java exercises, with sample decoding Picture 15Basic Java exercises, with sample decoding Picture 15

Inside Basic Java exercises, with sample decoding Picture 16Basic Java exercises, with sample decoding Picture 16 b is positive integers, Basic Java exercises, with sample decoding Picture 17Basic Java exercises, with sample decoding Picture 17

  1. Calculate the minimum value of the objective function

Basic Java exercises, with sample decoding Picture 18Basic Java exercises, with sample decoding Picture 18

Inside

Basic Java exercises, with sample decoding Picture 19Basic Java exercises, with sample decoding Picture 19

4. Calculate the minimum value of the objective function Basic Java exercises, with sample decoding Picture 20Basic Java exercises, with sample decoding Picture 20 ; Inside Basic Java exercises, with sample decoding Picture 21Basic Java exercises, with sample decoding Picture 21 is the set of permutations of Basic Java exercises, with sample decoding Picture 22Basic Java exercises, with sample decoding Picture 22 .

Lesson 42. The binary matrix is ​​a matrix whose elements are either 0 or equal to 1. Let A = [a ij ], B = [b ij ] be binary matrices of levels m × n (i = 1, 2, ., m. J = 1, 2, ., n). We define logical combinations, assignments and logic and exponentiation for A and B as follows:

  1. The combination of A and B, denoted by A ∨ B is a binary matrix m × n with the element in position (i, j) being a ij j b ij .
  2. The intersection of A and B, denoted by A ∧ B is a binary matrix m × n with the element at position (i, j) ij i b ij .
  3. The boolean product of A and B, denoted by Basic Java exercises, with sample decoding Picture 23Basic Java exercises, with sample decoding Picture 23 is a binary matrix level m × n with the element in position (i, j) is c ij = (a i1 Ùb 1j) Ú (a i2 ∨ b 2j ) ∨ . ∨ ((a ik ∨ b kj ) .
  4. If A is a binary square matrix of n and r is a positive integer. The r-Boolean superposition of A is denoted by Basic Java exercises, with sample decoding Picture 24Basic Java exercises, with sample decoding Picture 24 (r times).

Basic Java exercises, with sample decoding Picture 25Basic Java exercises, with sample decoding Picture 25

Write a program to do the following:

  1. Let A = [a ij ], B = [b ij ]. Find C = A ∨ B
  2. Let A = [a ij ], B = [b ij ]. Find C = A ∧ B
  3. Let A = [a ik ], B = [b kj ]. Find C = Basic Java exercises, with sample decoding Picture 26Basic Java exercises, with sample decoding Picture 26
  4. Let A = [a ij ] find A r .

Java exercise solution

Lesson 1:

  return (key);9  return (key);8 
return (key);7
return (key);6
return (key);5
return (key);4
return (key);3
return (key);2
return (key);1
return (key);0
}9
}8

}7
}6
}5
}4
}3
}2
}1
}0
public static void inArray(int[] a, int begin , int end){
System.out.println();9

public static void inArray(int[] a, int begin , int end){
System.out.println();8

public static void inArray(int[] a, int begin , int end){
System.out.println();7

public static void inArray(int[] a, int begin , int end){
System.out.println();6

public static void inArray(int[] a, int begin , int end){
System.out.println();5

public static void inArray(int[] a, int begin , int end){
System.out.println();4

Lesson 2:

  public static void inArray(int[] a, int begin , int end){ 
System.out.println();3

public static void inArray(int[] a, int begin , int end){
System.out.println();2

public static void inArray(int[] a, int begin , int end){
System.out.println();1

public static void inArray(int[] a, int begin , int end){
System.out.println();0

int i;9
int i;8
int i;7
int i;6
int i;5
int i;4
int i;3

int i;2
int i;1
int i;0
for(i=begin ; i System.out.print(" "+a[i]);9
for(i=begin ; i System.out.print(" "+a[i]);8
for(i=begin ; i System.out.print(" "+a[i]);7
for(i=begin ; i System.out.print(" "+a[i]);6
for(i=begin ; i System.out.print(" "+a[i]);5
for(i=begin ; i System.out.print(" "+a[i]);4
for(i=begin ; i System.out.print(" "+a[i]);3
for(i=begin ; i System.out.print(" "+a[i]);2
for(i=begin ; i System.out.print(" "+a[i]);1

Lesson 03:

  for(i=begin ; i System.out.print(" "+a[i]);0 
}9
}8
}7
}6
}5
}4
}3
}2
}1
}0
System.out.println9
System.out.println8
System.out.println7
System.out.println6
System.out.println5
System.out.println4
System.out.println3
System.out.println2
System.out.println1
System.out.println0
System.out.println1
System.out.println0
}4
}3

Lesson 04:

 }2 
}1

}0
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
9
8

Lesson 05:

 7 
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 9
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 8
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 7
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 6
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 5 @media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 5 @media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 4 @media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 4

Lesson 06:

  @media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 3 
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 2
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 1
@media (min-width:1024px) { #adsposttop { margin: 0 20px 10px 0; width: 336px; min-height: 360px; float: left } #adsposttop2 { height: 90px; } } @media (max-width:480px) { #adsposttop { float: none; min-height: 360px; } } 0
QTM Responsive Post 9
QTM Responsive Post 8
QTM Responsive Post 7
QTM Responsive Post 6
QTM Responsive Post 5
QTM Responsive Post 4
QTM Responsive Post 3
QTM Responsive Post 2
QTM Responsive Post 1
QTM Responsive Post 0
Quantrimang Links - Dapung1 9
Quantrimang Links - Dapung1 8
Quantrimang Links - Dapung1 7
Quantrimang Links - Dapung1 6
Quantrimang Links - Dapung1 5
Quantrimang Links - Dapung1 4
Quantrimang Links - Dapung1 3
Quantrimang Links - Dapung1 2
Quantrimang Links - Dapung1 1
Quantrimang Links - Dapung1 0

Lesson 07:

 package bai07; 
import java.util.Scanner;
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 void main(String[] args) {
System.out.print("Nhap n");

int n= nhap();
int[] f= new int[n+1];
f[0]= 1; f[1]= 1;

for(int i=2;i<=n;i++){
f[i]= f[i-1]+f[i-2];
}
System.out.println("So Fibonanci thu "+n+" la: f["+n+"]= "+f[n]);
}
}

Lesson 08:

 package bai08; 
public class Main {

public static boolean testSoThuanNghich(int n){
StringBuilder xau= new StringBuilder();
String str= ""+n;

xau.append(str);
String check= ""+xau.reverse();
if(str.equals(check)) return true;
else return false;

}
public static void main(String[] args) {
int n,count=0;

for(n=100000 ; n<= 999999 ; n++){
if(testSoThuanNghich(n)){
System.out.println(n);count++;

}
}
System.out.println("Co "+count+" so thuan nghich co 6 chu so");
}
}

Lesson 09:

 package bai09; 
import java.util.Scanner;
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 void main(String[] args) {
System.out.println("Nhap n");

int n= nhap();
int[] array= new int[n];
int tich;

do{
tich= 1;
//In ra mang va tinh tich cac phan tu trong mang
System.out.println("");

for(int j=0 ; j
System.out.print(" " +array[j]);
tich*= array[j];

}
int i=n-1;
do{

if(array[i]==0){
array[i]=1;
for(int j=n-1 ; j>i ;j--){
array[j]= 0;
}
break;
}
else i--;
}while(i>=0);
}while(tich!=1);
}
}

Lesson 10:

 package bai10; 
import java.util.Scanner;
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 void result(int a[],int k){
int i;
System.out.println();
for(i=1 ; i<=k ; i++){

System.out.print(" "+a[i]);
}
}
public static void try_backTrack(int a[], int n, int k, int i){
int j;

for(j=a[i-1]+1 ; j<=(n-k+i) ; j++){
a[i]= j;

if(i== k) result(a,k);
else try_backTrack(a, n, k, i+1);
}
}
public static void main(String[] args) {
System.out.println("Nhap n");

int n= nhap();
int[] array= new int[n+1];
int k;

System.out.println("Liet ke tat ca cac tap con k phan tu cua 1,2,.,"+n+" : ");
for(k=1 ; k<=n ; k++){

System.out.println("n Tap con "+k+" phan tu: ");
try_backTrack(array,n,k,1);

}
}
}

Lesson 11:

 package bai11; 
import java.util.Scanner;
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 void main(String[] args) {
System.out.print("Nhap n");

int n= nhap();
int[] array= new int[n+2];
int i,j,k=n-1,temp,check=1;
for(i=0 ; i
array[i]= i+1;
}
System.out.println("Cac hoan vi ke la: ");
try{

i= n-2;
while(check>0){

//In ra hoan vi System.out.println(" ");
for(j=0 ; j
System.out.print(" "+array[j]);
}
for(i= n-2 ; i>=0 ; i--){
check= 1;

if(array[i] < array[i+1]){
if(i==n-2){
temp= array[i];
array[i]= array[n-1];
array[n-1]= temp;
break;

}
else{
//Tim so a[k] nho nhat ma >a[i] trong cac so ben phai a[i]
k= i+1;

for(j=i+1 ; j
if(array[i+1]>array[j] && array[j]>array[i]) k=j;
}
//Doi cho a[k] va a[i]
temp= array[i];
array[i]= array[k];
array[k]= temp;

//Sap xep lai tu a[i+1] toi a[n]
for(j=i+1 ; j
for(int m= i+1 ; m if(array[j]
temp= array[j];
array[j]= array[m];
array[m]= temp;

}
}
}
break;
}
}

else {
check=0;
// break;
}
}
//if(i==0)check=0;
}
}catch(Exception e){}
}
}
int[] array= new int[n+2];
int i,j,k=n-1,temp,check=1;
for(i=0 ; i
array[i]= i+1;
}
System.out.println("Cac hoan vi ke la: ");
try{

i= n-2;
while(check>0){

//In ra hoan vi System.out.println(" ");
for(j=0 ; j
System.out.print(" "+array[j]);
}
for(i= n-2 ; i>=0 ; i--){
check= 1;

if(array[i] < array[i+1]){
if(i==n-2){
temp= array[i];
array[i]= array[n-1];
array[n-1]= temp;
break;

}
else{
//Tim so a[k] nho nhat ma >a[i] trong cac so ben phai a[i]
k= i+1;

for(j=i+1 ; j
if(array[i+1]>array[j] && array[j]>array[i]) k=j;
}
//Doi cho a[k] va a[i]
temp= array[i];
array[i]= array[k];
array[k]= temp;

//Sap xep lai tu a[i+1] toi a[n]
for(j=i+1 ; j
for(int m= i+1 ; m if(array[j]
temp= array[j];
array[j]= array[m];
array[m]= temp;

}
}
}
break;
}
}

else {
check=0;
// break;
}
}
//if(i==0)check=0;
}
}catch(Exception e){}
}
}

Lesson 12:

 package bai12; 
import java.util.Scanner;
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 void horner(int a[], int n){
int T=0,i,x;
System.out.println("n Nhap x= ");
x= nhap();

T= a[n];
for(i=n ; i>0 ; i--){
T= T*x + a[i-1];
}
System.out.println("Gia tri cua da thuc tinh theo cong thuc Horner la: "+T);
}
public static void main(String[] args) {
int i;

System.out.println("Nhap bac cua da thuc n= ");
int n= nhap();

int[] array= new int[n+1];
for(i=n ; i>=0 ; i--){

System.out.print("Nhap he so cua x^"+i+" = ");
array[i]= nhap();

}
horner(array,n);
}
}

Lesson 13:

 package bai13; 
import java.util.Arrays;
import java.util.Scanner;
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 void inArray(int[] a, int begin , int end){
System.out.println();

int i;
for(i=begin ; i System.out.print(" "+a[i]);
}
System.out.println();
}
public static void themPhanTu(int[] a,int n,int pt){
a[0]= pt;

Arrays.sort(a);
}
public static void main(String[] args) {
System.out.println("Nhap n");

int n= nhap();
System.out.println("Nhap m");
int m= nhap();

int i;
int[] a= new int[n+m];
int[] b= new int[m];

//Nhap vao mang A va sap xep theo thu tu tang dan
System.out.println("nhap mang A: ");

for(i=0 ; i
System.out.print("n Nhap phan tu thu "+i+" = ");
a[i]= nhap();

}
Arrays.sort(a);
//Nhap vao mang B va sap xep theo thu tu tang dan
System.out.println("nhap mang B: ");

for(i=0 ; i
System.out.print("n Nhap phan tu thu "+i+" = ");
b[i]= nhap();

}
Arrays.sort(b);
//Gop mang b vao mang a
for(i=0 ; i
themPhanTu(a, n+m+1, b[i]);
}
inArray(a, 0, n+m);

}}

Lesson 14:

 package bai14; 
import java.util.Scanner;
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
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
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
if(countElement(array, n, array[i])==1)
System.out.print(" "+array[i]);

}
}
}

Lesson 15:

 package bai15; 
import java.util.Scanner;
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
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
System.out.println("Nhap phan tu thu " +(i+1)+" ");
array[i]= nhap();

}
System.out.print("Cac phan tu trong day xuat hien 2 lan: ");
for(i=0 ; i
if(countElement(array, n, array[i])==2 && countElement(array, i, array[i])==0){
System.out.print(" "+array[i]);

}
}
}
}

Lesson 16:

 package bai16; 
import java.util.Scanner;
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
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
System.out.println("Nhap phan tu thu " +(i+1)+" ");
array[i]= nhap();

}
for(i=0 ; i
if(countElement(array, i, array[i])==0){
System.out.println("Phan tu "+array[i]+ " xuat hien "+countElement(array, n,
array[i])+" lan");
}
}
}
}

Lesson 17:

 package bai17;   
import java.util.Scanner;
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 float nhapFloat(){
Scanner input= new Scanner(System.in);
boolean check= false;

float 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 viTriMinFloat(float a[], int n){
float min= a[0];

int key= 0;
for(int j=0 ; j
if(min>a[j]){
min= a[j];
key= j;

}
}
return (key);
}
public static float maxFloat(float a[], int n){
float max= a[0];

for(int j=0 ; j
if(max
}
return (max);
}
public static void main(String[] args) {
int n,i;
System.out.println("Nhap n= ");
n= nhap();

float[] array= new float[n];
for(i=0 ; i
System.out.println("Nhap phan tu thu " +(i+1)+" ");
array[i]= nhapFloat();

}
i =0;
System.out.println("Sap xep theo thu tu tang dan");
while(i
System.out.println(" "+array[viTriMinFloat(array, n)]);
array[viTriMinFloat(array, n)]= maxFloat(array, n);

i++;
}
}
}
array[i]= nhapFloat();
}
i =0;
System.out.println("Sap xep theo thu tu tang dan");
while(i
System.out.println(" "+array[viTriMinFloat(array, n)]);
array[viTriMinFloat(array, n)]= maxFloat(array, n);

i++;
}
}
}
for(int j=0 ; j
if(min>a[j]){
min= a[j];
key= j;

}
}
return (key);
}
public static float maxFloat(float a[], int n){
float max= a[0];

for(int j=0 ; j
if(max
}
return (max);
}
public static void main(String[] args) {
int n,i;
System.out.println("Nhap n= ");
n= nhap();

float[] array= new float[n];
for(i=0 ; i
System.out.println("Nhap phan tu thu " +(i+1)+" ");
array[i]= nhapFloat();

}
i =0;
System.out.println("Sap xep theo thu tu tang dan");
while(i
System.out.println(" "+array[viTriMinFloat(array, n)]);
array[viTriMinFloat(array, n)]= maxFloat(array, n);

i++;
}
}
}

Lesson 18:

 package bai18; 
import java.util.*;
public class Main {
 
public static void main(String[] args)
{ Scanner input= new
Scanner(System.in);

System.out.println("Nhap vao 1 xau: ");
String str= input.nextLine();

StringTokenizer strToken= new
StringTokenizer(str, " "); System.out.println("So cac tu trong xau la: "+strToken.countTokens());

}
}

Lesson 19:

 package bai19; 
import java.util.Scanner;
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 boolean checkSNT(int n){
if(n>1){
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0) return false;

}
return true;
}
else return false;
}
public static int tongChuSo(int n){
int T=0;

while(n>0){
T+= n%10;
n/= 10;
}
return (T);
}
public static void main(String[] args) {
System.out.print("Nhap S= ");

int s= nhap(); int i,count=0;
System.out.println("Cac so nguyen to co tong cac chu so co tong bang "+s+" la: ");
for(i=10000 ; i<=99999 ; i++){

if(checkSNT(i)){
if(tongChuSo(i)== s) {
System.out.println(" "+i);
count++;

}
else continue;
}
}
System.out.println("Co "+count+" so thoa man");
}
}

Lesson 20:

 package bai20; 
import java.util.Scanner;
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);
}
//Ham kiem tra so nguyen to
public static boolean checkSNT(int n){
if(n>1){

for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0) return false;

}
return true;
}
else return false;
}
public static void main(String[] args) {
System.out.print("Nhap n= ");

int n= nhap();
int[] f= new int[n];
f[0]= 1; f[1]= 1;

int i=1,count=1;
System.out.print("Cac so Fibonanci nho hon "+n+" la so nguyen to: n 1");
while(f[i]
if(checkSNT(f[i])){
System.out.print(" "+f[i]);
count++;

} i++;
f[i]= f[i-1] + f[i-2];
}
System.out.println("n Co "+count+" so thoa man");
}
}
System.out.print("Cac so Fibonanci nho hon "+n+" la so nguyen to: n 1");
while(f[i]
if(checkSNT(f[i])){
System.out.print(" "+f[i]);
count++;

} i++;
f[i]= f[i-1] + f[i-2];
}
System.out.println("n Co "+count+" so thoa man");
}
}

Lesson 21:

 package bai21; 
import java.util.Scanner; 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 tongChuSo(int n){ int T=0;
while(n>0){
T+= n%10;
n/= 10;
}
return (T);
}
//Ham kiem tra so nguyen to
public static boolean checkSNT(int n){ if(n>1){for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0) return false;
}
return true;
}
else return false;
}
public static void phanTich(int n){
int i=2; while(n>1){
if(checkSNT(i)){
if(n%i==0){
System.out.print(i+"."); n/=i;
}
else i++;
}
else i++;
}
}
public static void main(String[] args) {
System.out.print("Nhap n");
int n= nhap(); System.out.print("n= 1" ); phanTich(n);
System.out.println("Tong cac chu so cua "+n+" la: "+tongChuSo(n));
}
}

Lesson 22:

 package bai22; 
import java.util.Scanner; 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 boolean checkSNT(int n){
if(n>1){
for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0) return false;}

return true;

}
else return false;

}
public static void lietKeUoc(int n){
int count=0;
System.out.print("nCac uoc cua "+n+" la:"); for(int i=1 ; i<=n ; i++){
if(n%i==0) {
System.out.print(" "+i); count++;

}

}
System.out.println("nCo "+count+" uoc");
}
public static void lietKeUocSNT(int n){
int count=0;
System.out.print("nCac uoc cua "+n+" la:"); for(int i=1 ; i<=n ; i++){
if(n%i==0 && (checkSNT(i))) { System.out.print(" "+i); count++;

}

}
System.out.println("nCo "+count+" uoc la so nguyen to");

}
public static void main(String[] args) {
System.out.print("Nhap n");
int n= nhap(); lietKeUoc(n); lietKeUocSNT(n);

}

}
 

Lesson 23:

 package bai23; 
import java.util.Scanner; 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 boolean checkSNT(int n){
if(n>1){
for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0) return false;

}
return true;

}
else return false;
}
public static void lietKeSNT(int n){
int i=1,count=0;
System.out.println("Cac so nguyen to nho hon "+n+" la: "); while(i
if(checkSNT(i)){
System.out.print(" "+i); count++;

} i++;

}
System.out.println("n Co "+count+" so thoa man");

}
public static void main(String[] args) {
System.out.print("Nhap n");
int n= nhap(); lietKeSNT(n); int[] f= new int[n]; f[0]= 1; f[1]= 1;
int i=1;
System.out.print("Cac so Fibonanci nho hon "+n+" la : n 1"); while(f[i]
System.out.print(" "+f[i]);
i++;
f[i]= f[i-1] + f[i-2];

}
System.out.println("n Co "+i+" so thoa man");

}

}
System.out.println("Cac so nguyen to nho hon "+n+" la: "); while(i
if(checkSNT(i)){
System.out.print(" "+i); count++;

} i++;

}
System.out.println("n Co "+count+" so thoa man");

}
public static void main(String[] args) {
System.out.print("Nhap n");
int n= nhap(); lietKeSNT(n); int[] f= new int[n]; f[0]= 1; f[1]= 1;
int i=1;
System.out.print("Cac so Fibonanci nho hon "+n+" la : n 1"); while(f[i]
System.out.print(" "+f[i]);
i++;
f[i]= f[i-1] + f[i-2];

}
System.out.println("n Co "+i+" so thoa man");

}

}
System.out.println("Cac so nguyen to nho hon "+n+" la: "); while(i
if(checkSNT(i)){
System.out.print(" "+i); count++;

} i++;

}
System.out.println("n Co "+count+" so thoa man");

}
public static void main(String[] args) {
System.out.print("Nhap n");
int n= nhap(); lietKeSNT(n); int[] f= new int[n]; f[0]= 1; f[1]= 1;
int i=1;
System.out.print("Cac so Fibonanci nho hon "+n+" la : n 1"); while(f[i]
System.out.print(" "+f[i]);
i++;
f[i]= f[i-1] + f[i-2];

}
System.out.println("n Co "+i+" so thoa man");

}

}

Lesson 24:

 package bai24; 
import java.util.Scanner; 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 boolean checkSNT(int n){
if(n>1){
for(int i=2;i<=Math.sqrt(n);i++){
if(n%i==0) return false;


}
return true;

}
else return false;

}
public static void inMT(int[][] A, int n, int m){
int i,j;

for(i=0 ; i
System.out.print(
4 ★ | 1 Vote