By GokiSoft.com| 20:16 17/06/2022|
Java Basic

Java Basic- Giải phương trình bậc 2 trong java BT977

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

Liên kết rút gọn:

https://gokisoft.com/977

Bình luận

avatar
Nguyen Tri Duc [java1_online]
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");
		}
	}
}


avatar
Trần Nhựt Linh [java1_online]
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);
        }
    }
}


avatar
Trần Nhựt Linh [java1_online]
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);
        }
    }
}


avatar
le anh tuan [java1_online]
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();
    }
}


avatar
Hoàng Anh [C2010G]
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!");
         }
        }
    }
}


avatar
Hoàng Anh [C2010G]
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);
        }
    }
}


avatar
Triệu Văn Lăng [T2008A]
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);
        } 
                
            
    } 
}


avatar
Triệu Văn Lăng [T2008A]
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);
        }
    }
}


avatar
Đặng Trần Nhật Minh [T2008A]
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);
    }

}


avatar
hainguyen [T2008A]
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);
            }
        }
    }
}