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)
![Nguyen Tri Duc [java1_online]](https://www.gravatar.com/avatar/9ad80c7352fd64a202fbfb45d9ca9fb1.jpg?s=80&d=mm&r=g)
Nguyen Tri Duc
2023-04-15 15:22:27
import java.util.Scanner;
public class PhuongTrinhbac1 {
public static void main(String[] args) {
// ax + b = 0
Scanner sc = new Scanner(System.in);
double nghiem;
System.out.println("Nhap so a: ");
int a = sc.nextInt();
System.out.println("Nhap so b: ");
int b = sc.nextInt();
// a = 0 va b = 0
if (a == 0) {
if (b == 0) {
System.out.println("Phuong trinh vo so nghiem");
}else {
System.out.println("Phuong trinh vo nghiem");
}
}else {
nghiem = (double) -b / a;
System.out.printf("Phuong trinh co nghiem x = %.2f", nghiem);
}
}
}
////////////////////////////
import java.util.Scanner;
public class PhuongTrinhBac2 {
public static void main(String[] args) {
// ax2 + bx + c = 0
Scanner sc = new Scanner(System.in);
System.out.println("Nhap he so a: ");
int a = sc.nextInt();
System.out.println("Nhap he so b:");
int b = sc.nextInt();
System.out.println("Nhap hang so c:");
int c = sc.nextInt();
// kiem tra a = 0
if (a == 0) {
if (b == 0) {
System.out.println("Phuong trinh vo nghiem");
}else {
System.out.println("Phuong trinh co 1 nghiem x = " + (-c / b));
}
}
// cong thuc delta = b2 - 4ac
double delta = b*b - 4*a*c;
double x1;
double x2;
if (delta > 0) {
x1 = (double)((-b + Math.sqrt(delta)) / (2*a));
x2 = (double)((-b - Math.sqrt(delta)) / (2*a));
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}else if (delta == 0) {
x1 = -b / 2*a;
System.out.println("Nghiem kep x1 = x2 = " + x1);
}else {
System.out.println("Phuong trinh vo nghiem");
}
}
}
![Trần Nhựt Linh [java1_online]](https://www.gravatar.com/avatar/6e945f8e29edcd38ec9b7492c0265f02.jpg?s=80&d=mm&r=g)
Trần Nhựt Linh
2023-03-26 07:18:42
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.javabasic;
import java.util.Scanner;
/**
*
* @author mymem
*/
public class JavaBasic {
public static void main(String[] args) {
double a;
double b;
double c;
double x;
double x1;
double d;
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so a:");
a = scan.nextDouble();
System.out.println("Nhap so b:");
b = scan.nextDouble();
System.out.println("Nhap so c:");
c = scan.nextDouble();
d = b*b-4*a*c;
if(d<0){System.out.println("Phuong trinh vo nghiem");}
else if(d==0){x=-b/(2*a);
System.out.println("Phuong trinh co nghiem kep:" + x);}
else {x=(-b + Math.sqrt(d))/(2*a);
x1=(-b - Math.sqrt(d))/(2*a);
System.out.println("nghiem cua phuong trinh x1:" + x);
System.out.println("nghiem cua phuong trinh x2:" + x1);
}
}
}
![Trần Nhựt Linh [java1_online]](https://www.gravatar.com/avatar/6e945f8e29edcd38ec9b7492c0265f02.jpg?s=80&d=mm&r=g)
Trần Nhựt Linh
2023-03-26 06:48:57
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.javabasic;
import java.util.Scanner;
/**
*
* @author mymem
*/
public class JavaBasic {
public static void main(String[] args) {
int a;
int b;
int x;
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so a:");
a = scan.nextInt();
System.out.println("Nhap so b:");
b = scan.nextInt();
if(a==0&b==0){System.out.println("Phuong tring vo so nghiem");}
else if(a==0&b!=0){System.out.println("Phuong trinh vo nghiem");}
else {x=-b/a;
System.out.println("nghiem cua phuong tring la:" + x);
}
}
}
![le anh tuan [java1_online]](https://www.gravatar.com/avatar/0c79bbaaa43a903799a613fb8a163e0a.jpg?s=80&d=mm&r=g)
le anh tuan
2022-09-01 05:07:48
package javabasic;
import java.util.Scanner;
/**
*
* @author Skynet
*/
public class FirstSecond_Degree_Equation {
// giai he phuong trinh bac 1: ax + b = 0
public static void First(){
Scanner scanner = new Scanner(System.in);
System.out.print("Nhap gia tri a: ");
float a = scanner.nextFloat();
System.out.print("Nhap gia tri b: ");
float b = scanner.nextFloat();
if(a==0){
System.out.println("Phuong trinh vo nghiem");
}else if(b==0){
System.out.println("Phuong trinh vo so nghiem");
}else {
float x = -b/a;
System.out.println("Gia tri x la: "+x);
}
}
// giai he phuong trinh bac 2: ax2 + bx + c = 0
public static void Second(){
Scanner scanner = new Scanner(System.in);
System.out.print("Nhap gia tri a: ");
float a = scanner.nextFloat();
System.out.print("Nhap gia tri b: ");
float b = scanner.nextFloat();
System.out.print("Nhap gia tri c: ");
float c = scanner.nextFloat();
float delta = b*b - 4*a*c;
if(delta > 0){
float x1 = (float) (-b + Math.sqrt(delta)/(2*a));
float x2 = (float) (-b - Math.sqrt(delta)/(2*a));
System.out.println("Phuong trinh co 2 nghiem: x1 = "+x1+" x2 = "+x2);
}else if(delta == 0){
float x1 = -b/2*a;
System.out.println("Phuong trinh co nghiem kep: x1 = x2 = "+x1);
}else {
System.out.println("Phuong trinh vo nghiem");
}
}
public static void main(String[] args){
// giai he phuong trinh bac 1: ax + b = 0
First();
// giai he phuong trinh bac 2: ax2 + bx + c = 0
Second();
}
}
![Hoàng Anh [C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2022-06-17 14:35:04
/*
* 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 c2108l;
import java.util.Scanner;
/**
*
* @author Hoàng Anh aka bom1
*/
public class bai4A {
public static void main(String[] args) {
// giải phương trình bậc nhất ax2 + bx + c = 0
System.out.println("phương trình ax2 + bx + c = 0");
Scanner input = new Scanner(System.in);
System.out.println("nhập giá trị a = ");
int a = Integer.parseInt(input.nextLine());
System.out.println("nhập giá trị b = ");
int b = Integer.parseInt(input.nextLine());
System.out.println("nhập giá trị c = ");
int c = Integer.parseInt(input.nextLine());
if (a == 0) {
if (b == 0) {
System.out.println("Phương trình vô nghiệm.");
} else {
double x = -c / b;
System.out.println("Phương trình có một nghiệm kép = " + x);
}
}else{
// 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 là: "
+ "x1 = " + x1 + " và 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 Anh [C2010G]](https://www.gravatar.com/avatar/e73c31efff649e599b1e4320b6bfb1a9.jpg?s=80&d=mm&r=g)
Hoàng Anh
2022-06-17 14:27:27
/*
* 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 c2108l;
import java.util.Scanner;
/**
*
* @author std
*/
public class bai4 {
public static void main(String[] args) {
// giải phương trình bậc nhất ax + b = 0
System.out.println("phương trình ax + b = 0");
Scanner input = new Scanner(System.in);
System.out.println("nhập giá trị a = ");
int a = Integer.parseInt(input.nextLine());
System.out.println("nhập giá trị b = ");
int b = Integer.parseInt(input.nextLine());
if (a == 0) {
if (b == 0) {
System.out.println("Phương trình này có vô số nghiệm.");
} else {
System.out.println("Phương trình vô nghiệm.");
}
}else{
double x = -b / a;
System.out.println("phương trình có nghiệm x = " + x);
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-03-02 14:16:58
/*
* 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 bai977.ax2.giaipt2;
import static java.lang.Math.sqrt;
import java.util.Scanner;
/**
*
* @author MTLS
*/
public class giaipt2 {
public static void main(String[] args) {
int a, b, c;
float x1, x2, detail;
Scanner scan = new Scanner(System.in);
System.out.println("Nhap a= ");
a = scan.nextInt();
System.out.println("Nhap b= ");
b = scan.nextInt();
System.out.println("Nhap c= ");
c = scan.nextInt();
detail = (b*b - (4*a*c))/(2*a);
if (detail<0) {
System.out.println("PT vo nghiem");
} else if(detail==0) {
x1=x2= (-b/(2*a));
System.out.println("PT co nghiem kep x1 = x2= " + x1);
}
else {
x1 = (float) ((-b + sqrt(detail))/(2*a));
x2 = (float) ((-b - sqrt(detail))/(2*a));
System.out.println("PT co 2 nghiem x1 = " + x1 + "x2 = " + x2);
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-01-25 08:50:55
/*
* 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 bai977.ax;
import java.util.Scanner;
/**
*
* @author MTLS
*/
public class giaipt1 {
public static void main(String[] args) {
int a, b;
double x;
Scanner scan = new Scanner(System.in);
System.out.print("nhap a = ");
a = scan.nextInt();
System.out.print("nhap b = ");
b = scan.nextInt();
if(a==0) {
if(b==0) {
System.out.println("PT VSN");
} else {
System.out.println("PT VN");
}
} else {
x = -b/a;
System.out.println("PT co nghiem x= " + x);
}
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2021-01-25 02:46:36
package javalesson2;
import java.util.Scanner;
public class ex08 {
public static void main(String[] args) {
double a, b, c, x1, x2, delta;
String ketQua = "";
Scanner scanner = new Scanner(System.in);
do {
System.out.print("Nhập a (a # 0): ");
a = scanner.nextDouble();
} while (a == 0);
System.out.print("Nhập b: ");
b = scanner.nextDouble();
System.out.print("Nhập c: ");
c = scanner.nextDouble();
delta = Math.pow(b, 2) - 4 * a * c;
if (delta < 0) {
ketQua = "Phương trình vô nghiệm!";
} else if (delta == 0) {
x1 = x2 = -b/ (2*a);
System.out.println("Phương trinh có nghiệm kép là x1 = x2 = "+x1);
} else {
x1 = (-b + Math.sqrt(delta)) / (2 * a);
x2 = (-b - Math.sqrt(delta)) / (2 * a);
ketQua = "Phương trình có 2 nghiệm x1 = " + x1 + " và x2 = " + x2;
}
System.out.println(ketQua);
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-01-24 07:36:37
/*
* 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 static java.lang.Math.sqrt;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class NewClass {
public static void main(String[] args) {
// Bai 2 :
Scanner scan = new Scanner(System.in);
float a, b, c, x;
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) {
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 duy nhat : " + 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 mot 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);
}
}
}
}