By GokiSoft.com|
20:16 17/06/2022|
Java Basic
Java Basic- Giải phương trình bậc 2 trong java
Giải phương trình bậc nhất ax + b = 0;
Giái phương trình bậc 2 : ax2 + bx + c = 0
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-01-24 07:19:28
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class NewClass {
public static void main(String[] args) {
// Bai 1 :
int a, b;
double nghiem;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
Scanner scanner = new Scanner(System.in);
System.out.println("Nhap a: ");
a = scanner.nextInt();
System.out.println("Nhap b: ");
b = scanner.nextInt();
System.out.println("Phuong trinh nhap vao la : " + a + "x + " + b + " = 0.");
if (a == 0) {
if (b == 0) {
System.out.println("Phuong trinh VSN");
} else {
System.out.println("Phuong trinh VN");
}
} else {
nghiem = (double) -b / a;
System.out.println("Phuong trinh co nghiem x = " + decimalFormat.format(nghiem) + ".");
}
}
}
![nguyễn Sử [T2008A]](https://www.gravatar.com/avatar/47487be2776ac2ec915b0936ef7ab5ae.jpg?s=80&d=mm&r=g)
nguyễn Sử
2021-01-22 10:14:05
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bai6;
import java.util.Scanner;
/**
*
* @author WIN10
*/
public class Bai6 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("phương trình bậc nhất ax + b = 0");
Float a, b, x;
Scanner scan = new Scanner(System.in);
System.out.println("nhập số a = ");
a = scan.nextFloat();
System.out.println("nhập số b = ");
b = scan.nextFloat();
System.out.println("nhập số x = ");
x = scan.nextFloat();
if(a==0)
{
if(b==0)
System.out.println("phương trình vô số nhiệm");
} else{
System.out.println("phương trình vô số nhiệm");
}
else
System.out.println("phương trình có một nhiệm duy nhất x=: "+ (-b/a));
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-01-22 08:32:29
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson2;
import static java.lang.Math.sqrt;
import java.util.Scanner;
/**
*
* @author MyPC
*/
public class GiaiPhuongTrinhBac2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
float a , b , c , x;
a = scan.nextFloat();
b = scan.nextFloat();
c = scan.nextFloat();
if( a == 0 ){
if( b == 0 ){
if( c == 0 ){
System.out.println("PTVN");
}else{
System.out.println("PTVSN");
}
}else{
x = -c / b;
System.out.println("PT co 1 nghiem la: " + x );
}
}else{
float delta;
delta = b * b - 4 * a * c;
if(delta < 0){
System.out.println("PTVN");
}else if(delta == 0){
x = -b / ( 2 * a );
System.out.println("PT co 1 nghiem la: " + x );
}else{
float x1 , x2;
x1 =(float) (( -b + sqrt(delta) ) / ( 2 * a ));
x2 =(float) (( -b - sqrt(delta) ) / ( 2 * a ));
System.out.println("PT co 2 nghiem phan biet la: " + x1 + " " + x2);
}
}
}
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2021-01-22 08:10:11
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bai5;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Bai5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
System.out.println("Giai phuong trinh bac 2: ax^2+bx+c=0 (a#0)");
float a, b, c, delta, x1, x2;
System.out.println("Nhap a = ");
a = sc.nextFloat();
System.out.println("Nhap b = ");
b = sc.nextFloat();
System.out.println("Nhap c = ");
c = sc.nextFloat();
delta=b*b-4*a*c;
if(delta<0){
System.out.println("Phuong trinh vo nghiem");
}else if(delta==0){
System.out.println("Phuong trinh co nghiem duy nhat x = "+(-b/(2*a)));
}else{
x1 = (float)((-b+Math.sqrt(delta))/(2*a));
x2 = (float) ((-b-Math.sqrt(delta))/(2*a));
System.out.println("Phuong trinh co 2 nghiem x1 = "+x1+", x2 = "+x2);
}
}
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2021-01-22 08:08:41
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bai4;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Bai4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
float a, b;
System.out.println("Giai phuong trinh bac nhat: ax+b=0");
System.out.println("Nhap a = ");
a = scan.nextFloat();
System.out.println("Nhap b = ");
b = scan.nextFloat();
if (a==0){
if(b==0){
System.out.println("Phuong trinh vo so nghiem");
}
else{
System.out.println("Phuong trinh vo nghiem");
}
}
else{
System.out.println("Phuong trinh co nghiem duy nhat: "+(-b/a));
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-22 08:06:05
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Admin
*/
public class bai4 {
public static void main(String[] args) {
float a , b , x;
Scanner scan = new Scanner(System.in);
System.out.println("nhap so a");
a = scan.nextFloat();
System.out.println("Nhap so b");
b = scan.nextFloat();
if (a == 0){
System.out.println("nhap lai so a");
}else {
x = -b/a;
System.out.println("x = " + x);
}
}}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-01-22 08:05:40
import static java.lang.Math.sqrt;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Admin
*/
public class bai5 {
public static void main(String[] args) {
float a, b, c, x, x1, x2, dt;
Scanner scan = new Scanner(System.in);
System.out.println("Nhap a = ");
a = scan.nextFloat();
System.out.println("Nhap b = ");
b = scan.nextFloat();
System.out.println("Nhap c = ");
c = scan.nextFloat();
if (a == 0) {
System.out.println("NHAP LAI ");
} else {
dt = b * b - 4 * a * c;
if (dt > 0) {
x1 = (float) (-b - sqrt(dt) / (2 * a));
x2 = (float) (-b + sqrt(dt) / (2 * a));
System.out.println("phuong trinh co 2 nghiem ");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
} else if (dt < 0) {
System.out.println("phuong trinh vo nghiem");
} else if (dt == 0) {
x = -b / (2 * a);
System.out.println("phuong trinh co nghiem kep = " + x);
}
}
}
}
![nguyễn văn huy [T1907A]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
nguyễn văn huy
2020-03-20 10:11:05
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package PhuongTrinh;
import java.util.Scanner;
/**
*
* @author ASUS
*/
public class main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("nhập a");
float a = scan.nextFloat();
System.out.println("nhập b");
float b = scan.nextFloat();
System.out.println("nhập c");
float c = scan.nextFloat();
phuongTrinh(a, b, c);
}
public static void phuongTrinh(float a, float b, float c) {
if (a == 0) {
if (b == 0) {
System.out.println("phương trình voo nghiệm");
} else {
System.out.println("phương trình có 1 nghiệm" + "x=" + (-c / b));
}
return;
}
float denta = b * b - a * c * 4;
float x1, x2;
if (denta < 0) {
System.out.println("phương trình vô nghiệm");
} else if (denta == 0) {
System.out.println("phương trình có nghiệm kép"+"x1=x2="+(-b/2*a));
}else if(denta>0){
x1 =(float) ((-b+Math.sqrt(denta))/2*a);
x2=(float) ((-b-Math.sqrt(denta))/2*a);
System.out.println("phương trình có 2 nghiệm phân biệt:" +"X1="+x1+
"và x2="+x2);
}
}
}
![Đinh Vũ Anh [C1907L]](https://www.gravatar.com/avatar/3497c9d5bd990143221356aaab8c7819.jpg?s=80&d=mm&r=g)
Đinh Vũ Anh
2020-03-20 08:41:15
package Basic;
/**
*
* @author Admin
*/
import java.util.Scanner;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
float a;
float b;
float x;
Scanner input = new Scanner(System.in);
System.out.println("Giải phương trình bậc nhất ax + b = 0");
System.out.println("Input a: ");
a = input.nextFloat();
if(a == 0){
System.out.println("Unvalid value");
System.out.println("Input a: ");
a = input.nextFloat();
}
System.out.println("Input b: ");
b = input.nextFloat();
x = -b/a;
System.out.println("Phương trình có nghiệm x = " + x);
}
}
![Phạm Kim Anh [JavaFree]](https://www.gravatar.com/avatar/2a2cc19e3234e41e7f8a049d835aaac7.jpg?s=80&d=mm&r=g)
Phạm Kim Anh
2020-03-19 11:21:25
package gokisoft.com;
import java.util.Scanner;
public class GiaiPTBacNhat {
public static void main(String[] args) {
float a,b;
Scanner sc = new Scanner(System.in);
System.out.println("Nhap so a:");
a= Float.parseFloat(sc.nextLine());
System.out.println("Nhap so b: ");
b = Float.parseFloat(sc.nextLine());
System.out.println("Phuong trinh bac nhat nhap vao la: "+a+"x +"+b+" = 0");
if(a != 0 && b != 0) {
System.out.println("Phuong trinh co nghiem duy nhat: x = "+(-b)+"/"+a);
}else if(a!=0 && b==0) {
System.out.println("Phuong trinh co nghiem duy nhat: x = 0");
}else if(a==0) {
if(b == 0) {
System.out.println("Phuong trinh co vo so nghiem!");
}else {
System.out.println("Phuong trinh vo nghiem!");
}
}
}
}