bai tap ja va
Cac so chia het cho 5
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class chiacho5 {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int n,i;
int a[]=new int[100];
System.out.print("nhap vao mang:");
n=input.nextInt();
for(i=1;i<=n;i++)
{System.out.print("nhap gia tri cho mang a["+i+"]:");
a[i]=input.nextInt();
}
System.out.print("mang la:
");
for(i=1;i<=n;i++)
{System.out.print(" "+a[i]);
System.out.print("
");
}
for(i=1;i<=n;i++)
if(a[i]%5==0)
System.out.println("cac so chia het cho 5 la:"+a[i]);
}
}
Cac so lon hon hoac bang m
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class cacsolonhonhoacbangm{
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int i,n,m,d;
int a[]=new int[100];
System.out.print("nhap vao n:");
n=input.nextInt();
for(i=1;i<=n;i++)
{System.out.print("nhap gia tri a["+i+"]:");
a[i]=input.nextInt();
}
System.out.println("day so la:
");
for(i=1;i<=n;i++)
{System.out.println(" "+a[i]);
System.out.println("
");
}
System.out.print("nhap vao m:");
m=input.nextInt();
d=0;
System.out.print(" cac so lon hon hoac bang m la:");
for(i=1;i<=n;i++)
{ if(a[i]>=m)
{ System.out.print(" "+a[i]);
System.out.print("
");
d++;
}
System.out.println("So luong cac so lon hon hoac bang m la:"+d);
}
}
}
Tong cac so am cac so duong
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class tongdayso {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int n,i,S,T;
int a[]=new int[100];
System.out.print("nhap vao so mang:");
n=input.nextInt();
for(i=1;i<=n;i++)
{System.out.print("nhap gia tri a["+i+"]:");
a[i]=input.nextInt();
}
System.out.print("mang la:
");
for(i=1;i<=n;i++)
{System.out.print(""+a[i]);
System.out.print("
");
}
S=0;T=0;
for(i=1;i<=n;i++)
{if(a[i]<0)
S=S+a[i];
else
T=T+a[i];
}
System.out.println("tong cac so khong am la:"+T);
System.out.println("tong cac so am la:"+S);
}
}
Day so tang dan
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class daysotang {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int i,n,j,x;
int a[]=new int[100];
System.out.print("Nhap vao so luong cua day so:");
n=input.nextInt();
for(i=1;i<=n;i++)
{System.out.print("nhap gia tri cac phan tu a["+i+"]:");
a[i]=input.nextInt();
}
System.out.print("day so la:
");
for(i=1;i<=n;i++)
{System.out.print(" "+a[i]);
System.out.print("
");
}
for (i=1;i<=n;i++)
for (j=i-1;j<=n;j++)
if (a[i]<a[j])
{
x=a[i];
a[i]=a[j];
a[j]=x;
}
System.out.println(" thu tu cua mang tang dan la:");
for(i=1;i<=n;i++)
System.out.println(a[i]+" ");
}
}
Giai he Phuong trinh
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class giaihept {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int a,b,c,d,e,f;
float S,S1,S2,x,y;
System.out.print("nhap vao a:");
a=input.nextInt();
System.out.print("nhap vao b:");
b=input.nextInt();
System.out.print("nhap vao c:");
c=input.nextInt();
System.out.print("nhap vao d:");
d=input.nextInt();
System.out.print("nhap vao e:");
e=input.nextInt();
System.out.print("nhap vao f:");
f=input.nextInt();
S=a*e-b*d;S1=c*e-f*b;S2=a*f-d*c;
x=S1/S;y=S2/S;
if(S==0)
System.out.println("phuong trinh co vo so nghiem");
else
System.out.println("nghiem cua phuong trinh la:"+x+","+y);
}
}
Giai Phuong trinh bac 2
import java.awt.*;
import java.io.*;
import java.util.Scanner;
public class giaiphuongtrinhbac4 {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int a,b,c,delta;
double x,x1,x2,x3,x4,x5,x6;
delta=b*b-a*4*c;
x1=(-b+Math.sqrt(delta))/(2*a);
x2=(-b-Math.sqrt(delta))/(2*a);
x3=Math.sqrt(x1);
x4=-Math.sqrt(x1);
x5=Math.sqrt(x2);
x6=-Math.sqrt(x2);
x=-b/(2*a);
System.out.print("nhap vao a:");
a=input.nextInt();
System.out.print("nhap vao b:");
b=input.nextInt();
System.out.print("nhap vao c:");
c=input.nextInt();
if(delta==0)
System.out.println("phuong trinh co nghiem:"+x);
if(delta<0)
System.out.println("phuong trinh vo nghiem");
else
{if(x1>0)
{if(x2>0)
System.out.println("phuong trinh co nghiem la:"+x3+",+x4+",+x5+","+x6);
else
System.out.println("phuong trinh co 2 nghiem:"+x3+","+x4);
}
else
System.out.println("phuong trinh co 2 nghiem:"+x5+","+x6);
}
}
}
Ma tran tinh tong cac hang cheo
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class matranvuong {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int i,j,n,S,N;
int a[][]=new int[100][100];
System.out.print("nhap gia tri:");
n=input.nextInt();
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{System.out.print("nhap gia tri a["+i+","+j+"]=");
a[i][j]=input.nextInt();
}
System.out.print("ma tran la:
");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{System.out.print("
"+a[i][j]);
System.out.print("
");
}
S=0;N=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{ if(i==j)
S=S+a[i][j];
else
N=N+a[i][j];}
System.out.println("tong cac phan tu tren duong cheo chinh la:"+S);
System.out.println("tong cac phan tu tren duong cheo phu la:"+N);
}
}
Nhap vao ngay thang nam
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class ngaythangnam {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int thang,nam;
System.out.print("nhap vao thang:");
thang=input.nextInt();
System.out.print("nhap vao nam:");
nam=input.nextInt();
if(nam%4==0)
switch(thang)
{case 1:case 3:case 5:case 7:case 8:case 10:case 12:
System.out.println("thang nay co 31 ngay");
break;
case 4:case 6:case 9:case 11:
System.out.println("thang nay co 30 ngay");
break;
case 2:System.out.println("thang nay co 28 ngay");
break;
}
else
switch(thang)
{case 1:case 3:case 5:case 7:case 8:case 10:case 12:
System.out.println("thang nay co 31 ngay");
break;
case 4:case 6:case 9:case 11:
System.out.println("thang nay co 30 ngay");
break;
case 2:System.out.println("thang nay co 29 ngay");
break;
}
}
}
Trung binh cong cac so khong am
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class tbcacsoduong {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int i,n,S,d;
float T;
int a[]=new int[100];
System.out.print("nhap so luong cua day so:");
n=input.nextInt();
for(i=1;i<=n;i++)
{
System.out.print(" nhap ca so cua day a["+i+"]:");
a[i]=input.nextInt();}
System.out.print("day so la:
");
for(i=1;i<=n;i++)
{System.out.print(" "+a[i]);
System.out.print("
");
}
S=0;d=0;T=0;
System.out.println("ca phan tu duong la:");
for(i=1;i<=n;i++)
if(a[i]>=0)
{System.out.println(" "+a[i]);
System.out.println("
");
d++;S=S+a[i];T=S/d;
}
System.out.println("trung binh cong cac so duong la:"+T);
}
}
Phuong trinh bac hai
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class phuongtrinhbac2 {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int a,b,c,delta;
double x1,x2,x;
System.out.print("nhap vao a:");
a=input.nextInt();
System.out.print("nhap vao b:");
b=input.nextInt();
System.out.print("Nhap vao c:");
c=input.nextInt();
delta=b*b-4*a*c;
x=-b/(2*a);x1=(-b+Math.sqrt(delta))/(2*a);x2=(-b-Math.sqrt(delta))/(2*a);
if(delta<0)
System.out.println("phuong trinh vo nghiem");
else if(delta==0)
System.out.println("phuong trinh co nghiem kep:"+x);
else
System.out.println("phuong trinh co 2 nghiem phan biet:"+x1+","+x2);
}
}
So luong cac so le trong day
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class soluongsole {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int i,n,S,d;
int a[]=new int[100];
System.out.print("nhap mang:");
n=input.nextInt();
for(i=1;i<=n;i++)
{System.out.print("nhap gia tri a["+i+"]:");
a[i]=input.nextInt();
}
System.out.print("day so la:
");
for(i=1;i<=n;i++)
{System.out.print(" "+a[i]);
System.out.print("
");
}
d=0;S=0;
System.out.print("cac phan tu le la:
");
for(i=1;i<=n;i++)
if(a[i]%2!=0)
{ System.out.print(" "+a[i]);
System.out.print("
");
d++;S=S+a[i];
}
System.out.println("so luong cac phan tu le la:"+d);
System.out.println("tong cac phan tu le la:"+S);
}
}
Tong cua S
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class tinhtongS {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int i,n;
double S=100;
System.out.print("nhap n:");
n=input.nextInt();
for(i=1;i<=n;i++)
{if(i%2!=0)
S=S+1/(10*i);
else
S=S-1/(10*i);
}
System.out.println("tong cac so la:"+S);
}
}
Uoc chung lon nhat cua 2 so
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class uocchunglonnhat {
public static int UCLN(int a, int b){
while(a!= b){
if(a>b) a= a-b;
else b= b-a;
}
return (a);
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a,b;
System.out.println("Nhap a:");
a=input.nextInt();
System.out.println("Nhap b:");
b=input.nextInt();
System.out.println("Uoc chung lon nhat cua "+a+" va "+b+" la: "+UCLN(a,b));
System.out.println("Boi chung nho nhat cua "+a+" va "+b+" la: "+((a*b)/UCLN(a,b)));
}
}
Trung binh cong max,min
import java.awt.*;
import java.io.*;
import java.util.Scanner;
class maxmin {
public static void main (String[] args) {
Scanner input=new Scanner(System.in);
int i,n,max,min;
float T=0;
int a[]=new int[100];
System.out.print("nhap vao mang:");
n=input.nextInt();
for(i=1;i<=n;i++)
{System.out.print("nhap gia tri a["+i+"]:");
a[i]=input.nextInt();
}System.out.print("mang la:
");
for(i=1;i<=n;i++)
{System.out.print(" "+a[i]);
System.out.print("
");
}
max=0;
max=max>a[i]?max:a[i];
min=a[i];
min=min<a[i++]?min:a[i++];
System.out.println("so lon nhat la:
"+max);
System.out.println("so nho nhat la:"+min);
T=(T+a[i])/n;
System.out.println("trung binh cong la:"+T);
}
}
Bạn đang đọc truyện trên: AzTruyen.Top