Bài tập java cơ bản (P2)

Bài 1:
/**In ra man hinh tat ca cac hop so <100**/  
                           
public class bai05 {
                public static void main(String[] args)
                {
                                int k,n,dem;
                                for(k=1;k<=100;k++)
                                {
                                                dem=0; // dat ja tri cua bien dem=0 ung voi moi ja tri cua k
                                                for(n=2;n<=k;n++)
                                                {
                                                                if(k%n==0) // neu so du khi chia k cho n =0
                                                                                {
                                                                                                dem++; // thi bien dem duoc cong them 1 don vi
                                                                                }
                                                                }
                                                }
                                }
                                if(dem>1)
                                {
                                                System.out.println("hop so la"+k);
                                }
                }
}
Bài 06
/**In ra man hinh 15 so nguyen to dau tien**/
public class bai06 {
                public static void main(String[] args)
                {
                                int k=0,n,dem,j=0;// khai bao cac bien, dat ja tri ban dau cho cac bien can thiet
                                while (j<15) // trong khi so nguyen to da in < 15
                                {
                                                k++; // cong cho k 1 don vi
                                                dem=0; // reset gia tri cua bien dem =0 ung voi moi ja tri cua k
                                                for (n=2;n<=k;n++)
                                                {
                                                                if (k%n==0)
                                                                {
                                                                                dem++;
                                                                }
                                                }
                                                if (dem==1)
                                                {
                                                                System.out.print(k+" ");
                                                                j++;// so lan in duoc cong them 1
                                                                2
                                                }
                                }
                }
}
Kết quả
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Process completed.
Bài 07
/**In ra man hinh tat ca cac so nguyen to tu 1000 den 2000**/

public class bai7 {
                public static void main(String[] args) {
                                int k,n,dem;
                                for (k=1000;k<=2000;k++)
                                {
                                                dem=0;
                                                for (n=2;n<=k;n++)
                                                {
                                                                if(k%n==0)
                                                                dem++;
                                                }
                                                if(dem==1)
                                                System.out.println(k);
                                }
                }
}
Bài 08
/**In ra man hinh cac so <100 va chia het cho 3,7*/

public class bai08 {
                public static void main(String[] args) {
                                int k,n;
                                for (k=1;k<100;k++)
                                {
                                                if ((k%3==0) && (k%7==0))
                                                System.out.print(k+" ");
                                }
                }
}
Kết quả
21 42 63 84
Process completed.
Bài 09
/**In ra man hinh cac so nam giua 1000 va 200 dong thoi chia het cho 3,5,7*/

public class bai09 {
                public static void main(String[] args) {
                                int k;
                                for (k=1000;k<=2000;k++)
                                {
                                                if((k%3==0)&(k%5==0)&(k%7==0))
                                                System.out.print(k+" ");
                                }
                }
}
Kết quả
1050 1155 1260 1365 1470 1575 1680 1785 1890 1995
Process completed.
Bài 10
/**In ra man hinh 5 so hoan hao dau tien( so hoan hao la so co tong bang cac uoc so cua minh ke ca 1)**/

public class bai10 {
                public static void main(String[] args) {
                                int k=0,j=0,n,tong;
                                while(j<5)
                                {
                                                k++;
                                                tong=1;// vi 1 luon la uoc cua cac so hang
                                                for(n=2;n<k;n++) // ja tri ban dau cua n = 2 thay vi dat bang 1 vi da tinh 1 la uoc o phia tren
                                                {
                                                                if (k%n==0)
                                                                tong+=n;
                                                }
                                                if (k==tong)
                                                {
                                                                System.out.print(k+" ");
                                                                j++;
                                                }
                                }
                }
}
Kết quả
1 6 28 496 8128
Process completed.
Bài 11
/*Trong cac so tu nhien <=100 hay dem xem co bao nhiu so
*- Chia het cho 5
*- Chia 5 du 1
*- Chia 5 du 2
*- Chia 5 du 3
*/

public class bai11 {
                public static void main(String[] args) {
                                int dem0,dem1,dem2,dem3;
                                int d,k;
                                4
                                dem0=0;dem1=0;dem2=0;dem3=0;
                                for (k=5;k<=100;k++)
                                {
                                                switch(d=k%5)
                                                {
                                                                case 0 :
                                                                dem0++;
                                                                break;
                                                                case 1:
                                                                dem1++;
                                                                break;
                                                                case 2:
                                                                dem2++;
                                                                break;
                                                                case 3:
                                                                dem3++;
                                                                break;
                                                }
                                }
                                System.out.println("so cac so chia het cho 5 la:" +dem0);
                                System.out.println("So cac so chia 5 du 1 la:"+dem1);
                                System.out.println("So cac so chia 5 du 2 la:"+dem2);
                                System.out.println("So cac so chia 5 du 3 la:"+dem3);
                }
}
Kết quả
so cac so chia het cho 5 la:20
So cac so chia 5 du 1 la:19
So cac so chia 5 du 2 la:19
So cac so chia 5 du 3 la:19
Process completed.
Bài 12
/*** Cho so tu nhien N bat ki( da gan truoc do),tim va in ra uoc so nguyen to nho nhat cua N*/

public class bai12 {
                public static void main(String[] args)
                {
                                int N=40;
                                int k;
                                for (k=2;k<=N;k++)
                                {
                                                int dem=0;
                                                for(int x=2;x<=k;x++)
                                                if(k%x==0)
                                                                dem++;
                                                if ((N%k==0)&&(dem==1))
                                                {
                                                                5
                                                                System.out.print("uoc so nguyen to nho nhat la:"+k );
                                                                break;
                                                }
                                }
                }
}
Kết quả
uoc so nguyen to nho nhat la:2
Process completed.
Bài 13
/*Cho so tu nhien N > 1 bat ki ( da gan truoc do)
*In ra khai trien thanh tich cac so nguyen to tinh tu nho den lon
*Vd 9--> 3.3
*12--> 2.2.3
*/

//Cách 1: dùng for
public class bai13 {
                public static void main(String[] args) {
                                int N=12;
                                int k;
                                for (k=2;k<=N;k++)
                                {
                                                if (N%k==0)
                                                {
                                                                System.out.print(k+" ");
                                                                N=N/k;
                                                                k--;
                                                }
                                }
                }
}
//Cách 2: dùng while
public class bai13 {
                public static void main(String[] args) {
                                int N=12,k=2;
                                while(k<=N)
                                {
                                                for(k=2;k<=N;k++)
                                                {
                                                                if (N%k==0)
                                                                {
                                                                                System.out.print(k+" ");
                                                                                N=N/k;
                                                                                break;
                                                                }
                                                }
                                }
                }
}
Kết quả
2 2 3
6
Process completed.
Bài 14
/**Cho truoc so tu nhien N bat ki (da gan truoc do)
*In ra man hinh tat ca cac uoc so nguyen to khac nhau cua N*/

public class bai14 {
                public static void main(String[] args) {
                                int k,n,dem,N=1027;
                                for (k=1;k<=N;k++)
                                {
                                                dem=0;
                                                for (n=2;n<=k;n++)
                                                {
                                                                if (k%n==0)
                                                                {
                                                                dem++;
                                                                }
                                                }
                                                if (dem==1 & N%k==0)
                                                {
                                                                System.out.print(k+” “);
                                                }
                                }
                }
}
Kết quả
13 79
Process completed.

No comments:

Post a Comment