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)
![cuonglee [C1907L]](https://www.gravatar.com/avatar/b8c48ea2d04ab5a37bda2c50310d8a5c.jpg?s=80&d=mm&r=g)
cuonglee
2020-03-15 14:34:16
package javabasic;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class giaiphuongtrinh {
public static void main(String[] args) {
Scanner nhap = new Scanner(System.in);
System.out.println("moi nhap a: ");
int a = nhap.nextInt();
System.out.println("moi nhap b: ");
int b = nhap.nextInt();
if(a==0 || b==0){
System.out.println("phuong trinh vo so nghiem");
}else if(a==0 && b!=0){
System.out.println("Phuong trinh vo nghiem");
}else{
System.out.format("Phương trình có nghiệm duy nhất: %d\n", -b/a);
}
}
}
![nguyễn thị bích thủy [C1907L]](https://www.gravatar.com/avatar/8e36651cace2bad96b903e66c1f21057.jpg?s=80&d=mm&r=g)
nguyễn thị bích thủy
2020-03-15 13:38:55
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Nhap a = " );
int a = input.nextInt();
System.out.println("Nhap b = " );
int b = input.nextInt();
float x = -b/a;
System.out.format(" Nghiêm của phương trình ax + b = 0 là %f",x);
}
![Lê Trí Dũng [C1907L]](https://www.gravatar.com/avatar/720bc9428f32c3816dde6806f7d743c8.jpg?s=80&d=mm&r=g)
Lê Trí Dũng
2020-03-15 10:32:42
/*
* 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 baitapjava;
import java.util.Scanner;
/**
*
* @author Dung
*/
public class ptbac2 {
public static void main(String[] args){
float a;
float b;
float c;
Scanner scan = new scanner(System.in);
//nhap du lieu a,b,c
System.out.println("Nhập hệ số bậc 2, a = ");
a = scan.nextFloat();
System.out.println("Nhập hệ số bậc 1, b = ");
b = scan.nextFloat();
System.out.println("Nhập hằng số tự do, c = ");
c = scan.nextFloat();
ptbac2.giaiptbac2(a, b, c);
}
public static void giaiptbac2(float a, float b, float c){
//kiểm tra hệ số
if (a == 0){
if (b == 0){
System.out.println("Phương trình vô nghiệm !");
} else {
System.out.println("Phương trình có một nghiệm " + "x = " + (-c/b));
}
return;
}
//tính delta
float delta = b*b - 4*a*c;
float x1;
float x2;
//tính nghiệm
if (delta > 0){
x1 = (float) ((-b + Math.sqrt(delta)) / (2*a));
x2 = (float) ((-b - Math.sqrt(delta)) / (2*a));
System.out.println("Phương trình có 2 nghiệm phân biệt: " + "x1 = " + x1 + "x2 = " + x2);
} else if (delta == 0){
x1 = (-b / (2*a));
System.out.println("Phương trình có nghiệm kép: " + "x1 = x2 = " + x1);
} else{
System.out.println("Phương trình vô nghiệm !");
}
}
}
![Lê Trí Dũng [C1907L]](https://www.gravatar.com/avatar/720bc9428f32c3816dde6806f7d743c8.jpg?s=80&d=mm&r=g)
Lê Trí Dũng
2020-03-15 10:32:07
/*
* 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 baitapjava;
import java.util.Scanner;
/**
*
* @author Dung
*/
public class ptbac2 {
public static void main(String[] args){
float a;
float b;
float c;
Scanner scan = new scanner(System.in);
//nhap du lieu a,b,c
System.out.println("Nhập hệ số bậc 2, a = ");
a = scan.nextFloat();
System.out.println("Nhập hệ số bậc 1, b = ");
b = scan.nextFloat();
System.out.println("Nhập hằng số tự do, c = ");
c = scan.nextFloat();
ptbac2.giaiptbac2(a, b, c);
}
public static void giaiptbac2(float a, float b, float c){
//kiểm tra hệ số
if (a == 0){
if (b == 0){
System.out.println("Phương trình vô nghiệm !");
} else {
System.out.println("Phương trình có một nghiệm " + "x = " + (-c/b));
}
return;
}
//tính delta
float delta = b*b - 4*a*c;
float x1;
float x2;
//tính nghiệm
if (delta > 0){
x1 = (float) ((-b + Math.sqrt(delta)) / (2*a));
x2 = (float) ((-b - Math.sqrt(delta)) / (2*a));
System.out.println("Phương trình có 2 nghiệm phân biệt: " + "x1 = " + x1 + "x2 = " + x2);
} else if (delta == 0){
x1 = (-b / (2*a));
System.out.println("Phương trình có nghiệm kép: " + "x1 = x2 = " + x1);
} else{
System.out.println("Phương trình vô nghiệm !");
}
}
}
![Hoàng Quang Huy [C1907L]](https://www.gravatar.com/avatar/76e646e11f1674fba03bddd0ae020813.jpg?s=80&d=mm&r=g)
Hoàng Quang Huy
2020-03-15 06:08:20
import java.util.Scanner;
//[Java Basic] Giải phương trình bậc 2 trong java
public class Lab2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Nhập hệ số a: ");
float a = input.nextFloat();
System.out.println("Nhập hệ số b: ");
float b = input.nextFloat();
System.out.println("Nhập hệ số c: ");
float c = input.nextFloat();
float del = b * b - 4 * a * c;
if (a == 0) {
if (b == 0) {
System.out.println("Phương trình vô nghiệm");
} else {
System.out.println("Nghiệm của phương trình là: " + (-c / b));
}
}
if (del < 0) {
System.out.println("Phương trình vô nghiệm");
} else if (del == 0) {
System.out.println("Phương trình có nghiệm kép x = " + (-b / (2 * a)));
} else if (del > 0) {
float x1 = (float)((-b + Math.sqrt(del)) / (2 * a));
float x2 = (float)((-b - Math.sqrt(del)) / (2 * a));
System.out.println("Nghiệm phương trình x1 = " + x1);
System.out.println("Nghiệm phương trình x2 = " + x2);
}
}
}
![Vũ Việt Đức [C1907L]](https://www.gravatar.com/avatar/114894070fbd15fc0c29ffdeab37f4b5.jpg?s=80&d=mm&r=g)
Vũ Việt Đức
2020-03-13 14:45:43
package aptech;
import java.util.Scanner;
public class Homework {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Nhập các tham số của phương trình: ax + b = 0");
System.out.print("Nhập a: ");
int a = input.nextInt();
System.out.print("Nhập b: ");
int b = input.nextInt();
if(a == 0 && b != 0) {
System.out.println("Phương trình vô nghiệm.");
}
else if(a == 0 || b == 0) {
System.out.println("Phương trình có vô số nghiệm.");
}
else {
System.out.format("Phương trình có nghiệm duy nhất: %d\n", -b/a);
}
}
}
![Vũ Việt Đức [C1907L]](https://www.gravatar.com/avatar/114894070fbd15fc0c29ffdeab37f4b5.jpg?s=80&d=mm&r=g)
Vũ Việt Đức
2020-03-13 14:45:20
package aptech;
import java.util.Scanner;
public class Homework {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Nhập các tham số của phương trình: ax + b = 0");
System.out.print("Nhập a: ");
int a = input.nextInt();
System.out.print("Nhập b: ");
int b = input.nextInt();
if(a == 0 && b != 0) {
System.out.println("Phương trình vô nghiệm.");
}
else if(a == 0 || b == 0) {
System.out.println("Phương trình có vô số nghiệm.");
}
else {
System.out.format("Phương trình có nghiệm duy nhất: %d\n", -b/a);
}
}
}
![Nguyễn Hoàng Anh [C1907L]](https://www.gravatar.com/avatar/5b7bb435cae0d0a0fd414f6fdd0adc87.jpg?s=80&d=mm&r=g)
Nguyễn Hoàng Anh
2020-03-13 14:20:39
/*
* 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 Redmibook 14
*/
public class phuongtrinhbac1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Giai phuong trinh bac nhat");
System.out.println("Nhap vao so a : ");
float a = input.nextFloat();
System.out.println("Nhap vao so b : ");
float b = input.nextFloat();
float x = b / a;
System.out.format("Bieu thuc %.0fx + %.0f = 0 co x = -%.2f %n ", a, b, x);
}
}