Thông tin tài liệu
Bài tập java về nhập liệu từ bàn phím! 1. Viết chương trình kiểm tra giá trị một số N nhập vào là chẵn hay lẻ. import java.io.*; public class ChanLe { public static void main (String[] args) throws Exception{ int n; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap n can xem chan hay le: "); n=Integer.parseInt(in.readLine()); if(n%2==0){ System.out.print("so :"+n+"la so chan"); } else{ System.out.print("so : "+n+" la so le"); } } } [=========> Bổ sung bài viết <=========] 2. Viết một chương trình hiển thị tổng các bội số của 7 nằm giữa 1 và 100. public class TongBoiSoCua7 { public static void main (String[] args){ int tong=0; int dem=0; for(int i=1;i<=100;i++) if(i%7==0){ tong= tong+i; dem=dem+1; } System.out.println("tong boi so cua 7 tu 1 den 100 la: " +tong); System.out.println("vay co '"+dem+"' so la boi so cua 7 tu 1 den 100\n"); System.out.println("gom cac so:\n"); for(int i=1;i<=100;i++) if(i%7==0) { System.out.print(","+i); } } } [=========> Bổ sung bài viết <=========] 3.Viết chương trình in ra tất cả các số nguyên từ 0 đến 10, bỏ qua 3, 4, 5. import java.io.*; public class InSoNguyen { public static void main (String[] args) throws Exception { int so; System.out.println("cac so nguyen tu 0 den 10 tru cac so 3,4,5 la: "); for(int i=0;i<=10;i++) if((i<=2)|(i>=6)) { so=i; System.out.print(","+so); } } } [=========> Bổ sung bài viết <=========] 4. Viết chương trình tính giai thừa một số. import java.io.*; public class GiaiThua { public static void main (String[] args) throws Exception { int gt=1; int gthua,n; BufferedReader in =new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap n de tinh giai thua: "); n=Integer.parseInt(in.readLine()); for(int i=1;i<=n;i++){ gt=gt*i; } gthua=gt; System.out.print("giai thua cua "+n+" la:"+gthua); } } [=========> Bổ sung bài viết <=========] 5. Viết chương trình để cộng bảy số hạng của dãy sau: 1!+2!+3!……… import java.io.*; public class Cong7soHang { public static void main (String[] args) throws Exception{ int n,tong=0; int gt=1; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap n de tinh tong cac gia thua tu 1-n:"); n=Integer.parseInt(in.readLine()); for(int i=1;i<=n;i++) { gt=gt*i; tong= gt+tong; } System.out.print("tong cac so tu 1! den "+n+ " ! la:"+tong); } } [=========> Bổ sung bài viết <=========] 6. Viết chương trình tính tổng: S = 1+1/2!+1/3!+ +1/N! import java.io.*; public class TinhTongGT { public static void main (String[] args) throws Exception{ int n; double gt=1; double tong=0; BufferedReader in= new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap n de tinh tong co dang 1+(1/2)!+ +(1/n!): "); n=Integer.parseInt(in.readLine()); for(int i=1;i<=n;i++) { gt = gt*i; tong=tong+(1/gt); } System.out.print("tong cac gt co dang 1+(1/2)!+ +(1/n!): "+tong); } [=========> Bổ sung bài viết <=========] 7. Viết chương trình tính tổng: S= 1 + 1/(1+2!) + 1/(1+2!+3!) + +1/(1+2!+ N!) import java.io.*; public class TongGTThapPhan { public static void main (String[] args) throws Exception{ int gt=1,n; double tong=0; double s=0; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap n de tinh tong co dang 1+(1/(1+2!)+ +1/(1+2!+3!):\n===>"); n=Integer.parseInt(in.readLine()); for(int i=1;i<=n;i++) { gt=gt*i; tong=tong+gt; s=s+(1/tong); } System.out.println("Vay tong cua '"+n+"'co dang 1+(1/(1+2!)+ +1/(1+2!+3!)la: \n===>"+s); } } [=========> Bổ sung bài viết <=========] 8. Viết chương trình tính tổng các số chẵn trong đoạn [a, b] (a, b nhập từ bàn phím,). import java.io.*; public class TongChan { public static void main (String[] args) throws Exception{ int a,b; int tong=0; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap a: "); a=Integer.parseInt(in.readLine()); System.out.print("Nhap b: "); b=Integer.parseInt(in.readLine()); for(int i=a;i<=b;i++) if(i%2==0) { tong=i+tong; } System.out.println("Tong cac so chan tu '"+a+" den '"+b+" la:\n "+tong); System.out.println("Gom cac so:"); for(int i=a;i<=b;i++) if(i%2==0) { System.out.print(","+i); } } [=========> Bổ sung bài viết <=========] 9. Viết chương trình kiểm tra xem N có phải là số nguyên tố không. import java.util.*; public class SoNguyenTo { public static void main (String[] args) throws Exception{ Scanner sc=new Scanner(System.in); int n; System.out.print("Nhap n de xem no co phai la so ng to khong:"); n=sc.nextInt(); if((n==0)|(n==1)) { System.out.print("so '"+n+"' khong phai la so ng to"); } if(n==2) { System.out.print("so 2 la so ng to"); } if((n>2)&&(n%2==0)) { System.out.print("so '"+n+"'khong phai la so ngyen to "); } else{ System.out.print("so '"+n+"'la so nguyen to "); } } } [=========> Bổ sung bài viết <=========] 10. Viết chương trình tính tổng các số nguyên tố nhỏ hơn 100. public class TongCacSoNguyenTo { public static void main (String[] args) throws Exception{ int tong=0; for(int i=2;i<=100;i++) if(i%2!=0) { tong=i+tong; } tong=tong+2; System.out.print("tong cac so nguyen to nho hon 100 la:"+tong); } } [=========> Bổ sung bài viết <=========] 11. Viết chương trình nhập vào mảng 1 chiều và tính tổng các số chẵn/lẽ trong mảng. import java.io.*; import java.util.*; public class TongMang { public static void main (String[] args) throws Exception{ int n; int tongchan=0,tongle=0; BufferedReader in= new BufferedReader (new InputStreamReader(System.in)); System.out.print("Nhap so phan tu trong mang n:"); n=Integer.parseInt(in.readLine()); int a[]=new int[n]; int i; for(i=0;i<n;i++) { System.out.print("nhap a["+i+"]: "); a[i]=Integer.parseInt(in.readLine()); } for(i=0;i<n;i++) if(a[i]%2==0) { tongchan=tongchan+a[i]; } else { tongle=tongle+a[i]; } System.out.println("Tong cac so 'chan' trong mang:"+tongchan); System.out.println("Tong cac so 'le' trong mang la:"+tongle); } } 12. Viết chương trình nhập mảng gồm n số nguyên, tìm số nguyên nhỏ nhất và lớn nhất có trong mảng import java.io.*; public class MinMax { public static void main (String[] args) throws Exception{ int n,min,max; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap so phan tu co trong mang n: "); n=Integer.parseInt(in.readLine()); int a[]=new int[n]; int i; for(i=0;i<n;i++) { System.out.print("Nhap a["+i+"]: "); a[i]=Integer.parseInt(in.readLine()); } min=a[0]; max=a[0]; for(i=0;i<n;i++) { if(a[i]<min) { min=a[i]; } else { max=a[i]; } } System.out.println("So nho nhat la: "+min); System.out.println("So lon nhat la: "+max); } } [=========> Bổ sung bài viết <=========] 13.Viết chương trình nhập mảng gồm n số nguyên và một số nguyên x, kiểm tra xem x có xuất hiện trong mảng hay không? import java.io.*; public class KiemTraX { public static void main (String[] args) throws Exception{ int x,n; int thoat=0; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("nhap so phan tu trong mang n:"); n=Integer.parseInt(in.readLine()); int a[]=new int[n]; int i; for(i=0;i<n;i++) { System.out.print("Nhap a["+i+"]: "); a[i]=Integer.parseInt(in.readLine()); } System.out.print("Nhap so can tim x: "); x=Integer.parseInt(in.readLine()); for(i=0;i<n;i++) { if(x==a[i]) { System.out.print("So '"+x+"' co trong day"); thoat=1; } } if(thoat==0){ System.out.print("So '"+x+"'khong co trong day"); } } } [=========> Bổ sung bài viết <=========] 14.Viết chương trình nhập vào mảng 1 chiều các số nguyên và sắp xếp mảng giảm/tăng dần. In mảng đã sắp ra màn hình. import java.io.*; public class TangGiamMang { static void tangdan(int a[],int n) { int t; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } static void giamdan(int a[],int n) { int t; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } public static void main (String[] args) throws Exception{ int n; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap so phan tu co trong mang: "); n=Integer.parseInt(in.readLine()); int a[]=new int[n+1]; int i,j; for(i=1;i<=n;i++) { System.out.print("Nhap a["+i+"]: "); a[i]=Integer.parseInt(in.readLine()); } tangdan(a,n); System.out.print("Day so tang dan: "); for(i=1;i<=n;i++) System.out.print(a[i]+" "); System.out.println(); giamdan(a,n); System.out.print("Day so giam dan: "); for(i=1;i<=n;i++) System.out.print(a[i]+" "); } [=========> Bổ sung bài viết <=========] 15. Viết chương trình nhập vào mảng 1 chiều và kiểm tra xem các phần tử có tăng/giảm dần không. import java.io.*; public class KiemTraTangGiam { public static void main (String[] args) throws Exception{ int n,kt=0,kt1=0; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap so phan tu de n :"); n=Integer.parseInt(in.readLine()); int a[] =new int[n+1]; int i,j; for(i=1;i<=n;i++) { System.out.print("Nhap phan tu thu "+i+":"); a[i]=Integer.parseInt(in.readLine()); } for (i=1;i<n;i++) { if(a[i]<= a[i+1]) { kt=1; } else{ kt=0; i=n; } } for (i=1;i<n;i++) { if(a[i]>= a[i+1]) { kt1=1; } else{ kt1=0; i=n; } } if(kt==1){ System.out.print("Day so vua nhap co gia tri tang dan "); } if(kt1==1){ System.out.print("Day so vua nhap co gia tri giam dan"); } if((kt==0)&&(kt1==0)){ System.out.print("Day vua nhap 'khong' co thu tu "); } } } [=========> Bổ sung bài viết <=========] 16. Viết chương trình tính tổng các số nguyên tố trong mảng 1 chiều. import java.io.*; public class TongNgToDenN { public static void main (String[] args) throws Exception{ int n,tong=0; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Nhap so phan tu trong mang n:"); n=Integer.parseInt(in.readLine()); int a[]=new int[n+1]; int i; for(i=1;i<=n;i++) { a[i]=i; } for(i=1;i<=n;i++) { if((a[i]>2)&&(a[i]%2!=0)) { tong=tong+a[i]; } } tong=tong+2; System.out.print("Tong cac so nguyen to trong mang tu 1 den "+n+" la: "+tong); [...]... for(i=1;i Bổ sung bài viết . Bài tập java về nhập liệu từ bàn phím! 1. Viết chương trình kiểm tra giá trị một số N nhập vào là chẵn hay lẻ. import java. io.*; public class ChanLe { public.
===>"+s); } } [=========> Bổ sung bài viết <=========] 8. Viết chương trình tính tổng các số chẵn trong đoạn [a, b] (a, b nhập từ bàn phím, ). import java. io.*; public class TongChan { public. la:"+tong); } } [=========> Bổ sung bài viết <=========] 11. Viết chương trình nhập vào mảng 1 chiều và tính tổng các số chẵn/lẽ trong mảng. import java. io.*; import java. util.*; public class TongMang
Ngày đăng: 04/07/2014, 06:47
Xem thêm: bài tập java về nhập liệu từ bàn phím, bài tập java về nhập liệu từ bàn phím