By GokiSoft.com|
19:57 07/12/2021|
Lập Trình C
Bài tập - Giải phương trình bậc 2 - Lập trình C
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 [community]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2020-09-11 10:52:39
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
float a, b, c, delta, x1, x2;
printf("\nNhap a =");
scanf("%f", &a);
printf("\nNhap b =");
scanf("%f", &b);
printf("\nNhap c =");
scanf("%f", &c);
if(a==0){
if(b==0){
if(c==0)
printf("\nPT vo so nghiem");
else
printf("\nPTVN");
}
}else{
delta = b*b - 4*a*c;
if(delta>0){
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
printf("\nPT co 2 nghiem phan biet la x1=%g, x2=%g", x1, x2);
}
else if(delta==0)
printf("\nPT co nghiem kep x=%g ", (-b/2*a));
else
printf("\nPTVN");
}
return 0;
}
![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
2020-09-11 10:15:47
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
float a, b, c, x1, x2, D;
printf("\nnhap a= "); scanf("%f",&a);
printf("\nnhap b= "); scanf("%f",&b);
printf("\nnhap c= "); scanf("%f",&c);
D= b*b-4*a*c;
if(D==0) {
x1=x2=-b/(2*a);
printf("\npt co nghiem kep x1=x2=%f",x1=x2);
} else {
if(D>0) {
x1=((-b-sqrt(D))/(2*a));
x2=((-b+sqrt(D))/(2*a));
printf("\npt co 2 nghiem x1=%f, x2=%f",x1, x2);
} else {
printf("\npt vo nghiem");
}
}
return 0;
}
![An Văn Minh [T2008A]](https://www.gravatar.com/avatar/e0f14efe4b11f7d9d5901e8802319c92.jpg?s=80&d=mm&r=g)
An Văn Minh
2020-09-11 10:04:44
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c;
printf("\n Nhap a: ");
scanf("%f", &a);
printf("\n Nhap b: ");
scanf("%f", &b);
printf("\n Nhap c: ");
scanf("%f", &c);
float x;
if(a==0){
if(b==0){
if(c==0){printf("\n PTVSN");}
else {
printf("\n PTVN");
}
}
else {
x=-c/b;
printf("\n Pt co nghiem x=%f", x);}
}
else {
float delta=b*b-4*a*c;
if (delta<0){
printf("\n PTVN");
}
else{
if(delta==0){
x=-b/(2*a);
printf("\n Pt co nghiem x=%f", x);
}
else{
float x1,x2;
x1=(-b+sqrt(delta))/2*a;x2=(-b-sqrt(delta))/2*a;
printf("\n Pt co nghiem x1=%f", x1);
printf("\n PT co nghiem x2=%f", x2);
}
}
}
return 0;
}
![Nguyễn Hữu Hiếu [T2008A]](https://www.gravatar.com/avatar/ca2884508b617fee77f000c7d99c219d.jpg?s=80&d=mm&r=g)
Nguyễn Hữu Hiếu
2020-09-11 09:58:33
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//Giai phuong trinh bac 2
float a,b,c,x,x1,x2,d,tren1, tren2,duoi;
printf("Nhap a= ");
scanf("%f", &a);
printf("Nhap b= ");
scanf("%f", &b);
printf("Nhap c= ");
scanf("%f", &c);
if (a==0) {
//PT bac 1
if (b==0) {
if (c==0) {
printf("PTVSN");
}
else {
printf("PTVN");
}
}
else {
x = -c/b;
printf("x = %f", x);
}
}
else {
d = b*b-4*a*c;
if (d<0) {
printf("PTVN");
}
if (d==0) {
x = -b/2*a;
printf("x = %f", x);
}
if (d>0) {
x1 = (-b-sqrt(d))/(2*a);
x2 = (-b+sqrt(d))/(2*a);
printf("x1 = %f, x2 = %f", x1, x2);
}
}
return 0;
}
![Nguyễn Xuân Mai [T2008A]](https://www.gravatar.com/avatar/d3d863d6f47708501814fb41e9c38f31.jpg?s=80&d=mm&r=g)
Nguyễn Xuân Mai
2020-09-11 09:24:24
float a,b,c,x,delta,x1,x2;
printf("\nNhap a = ");
scanf("%d",&a);
printf("\nNhap b = ");
scanf("%d",&b);
printf("\nNhap c = ");
scanf("%d",&c);
if (a==0){
if (b==0){
if (c==0){
printf("\npt vo so nghiem");
} else {
printf("\npt vo nghiem");
}
}else{
x = -c/b;
printf("\nNghiem x = %f",x);
}
} else {
delta = pow(b,2)-4*a*c;
if (delta==0){
x = -b/a;
printf("\nNghiem x = %f",x);
} else {
if (x>0){
printf("\npt vo nghiem");
} else {
x1 = (-b + pow(delta,1/2)) / (2*a);
x2 = (-b - pow(delta,1/2)) / (2*a);
printf("\nNghiem x1=%f, x2=%f",x1,x2);
}
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2020-09-11 09:16:21
#include<stdio.h>
int main(){
float a,b,c,delta,x1,x2;
printf("\nNhap he so a:");
scanf("%f",&a);
printf("\nNhap he so b:");
scanf("%f",&b);
printf("\nNhap he so c:");
scanf("%f",&c);
if (a==0){
if(b==0){
if(c==0)
printf("Phuong trinh co vo so nghiem");
else
printf("Phuong trinh vo nghiem");
}
else
printf("Phuong trinh co 1 nghiem la x=%g",-c/b);
}
else{
delta=b*b - 4*a*c;
if (delta>0){
x1= (-b + sqrt(delta)) / (2*a);
x2= (-b - sqrt(delta)) / (2*a);
printf("Phuong trinh co 2 nghiem phan biet x1=%g, x2=%g",x1,x2);
}
else if(delta==0)
printf("Phuong trinh co nghiem kep x=%g",(-b) / (2*a));
else
printf("Phuong trinh vo nghiem");
}
getch();
return 0;
}
![Đặ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
2020-09-11 09:05:47
#include "bits/stdc++.h"
int main() {
float a, b, c, x, d, xx;
printf("Solving Quadratic Equation: ax^2 + bx + c = 0\n");
printf("a = "); scanf("%f", &a);
printf("b = "); scanf("%f", &b);
printf("c = "); scanf("%f", &c);
if (a == 0) printf("Not A Quadratic Equation\n");
else {
d = b * b - 4 * a * c;
if (d < 0) printf("No Solution\n");
else if (d == 0) {
x = -b / (2 * a);
if (x == (int)x) printf("x = %.0f\n", x);
else printf("x = %.2f\n", x);
}
else if (d > 0) {
x = (-b + sqrt(d)) / (2 * a);
xx = (-b - sqrt(d)) / (2 * a);
if (x == (int)x) printf("x1 = %.0f\n", x);
else printf("x1 = %.2f\n", x);
if (xx == (int)xx) printf("x2 = %.0f\n", xx);
else printf("x2 = %.2f\n", xx);
}
}
return 0;
}