[ create a new paste ] login | about

Link: http://codepad.org/VJ1GXxdx    [ raw code | output | fork | 5 comments ]

C, pasted on Sep 11:
// ax^2 + bx + c = 0
#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
	float a, b, c; // Khai báo.

	printf("\nNhap vao a = ");
	scanf("%f", &a);

	printf("\nNhap vao b = ");
	scanf("%f", &b);

	printf("\nNhap vao c = ");
	scanf("%f", &c);

	if (a == 0) // pt có dạng: bx + c = 0
	{
		if (b == 0) // pt có dạng: c = 0
		{
			if (c == 0)
			{
				printf("\nPhuong trinh co vo so nghiem");
			}
			else
			{
				printf("\nPhuong trinh vo nghiem");
			}
		}
		else
		{

			float x = -c / b;

			printf("\nPhuong trinh co nghiem duy nhat x = %f", x);
		}
	}
	else
	{
		float Denta = b * b - 4 * a * c;

		if (Denta < 0)
		{
			printf("\nPhuong trinh vo nghiem");
		}
		else if (Denta == 0)
		{
			float x = -b / (2 * a);

			printf("\nPhuong trinh co nghiem kep x1 = x2 = %f", x);
		}
		else // Denta > 0
		{
			float x1 = (-b + sqrt(Denta)) / (2 * a);
			float x2 = (-b - sqrt(Denta)) / (2 * a);

			printf("\nPhuong trinh co 2 nghiem phan biet:\nx1 = %f\nx2 = %f", x1, x2);

		}
	}


	getch();
	return 0;
}


Output:
1
Line 18: error: conio.h: No such file or directory


Create a new paste based on this one


Comments:
posted by hieunv1996 on Nov 19
Tôi đến từ khóa học lập trình C miễn phí của anh Hiếu
https://nguyenvanhieu.vn/khoa-hoc-lap-trinh-c/
reply
posted by yonnon on Jul 12
#include<bits/stdc++.h>

using namespace std;
int main(){
int a;
int b;
int c;
int delta=0;
float x1;
float x2;
do{
cout <<"Enter a: ";
cin >> a;
cout <<"Enter b: ";
cin >> b;
cout <<"Enter c: ";
cin >> c;
if(a==0 && b==0){
cout<<"Many solutions";
return 0;
}
if (a==0 ){
cout << "No solution ";
return 0;
}

}while(a==0 || (a==0 && b==0));
delta=pow(b,2)-(4*a*c);
if(delta<0){
cout << "No solution";
return 0;
}
else if(delta == 0){
cout<< "Phuong trinh co nghiem kep x1 = x2 = "<<-b/(2*a);
return 0;
}
else if(delta>0){
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
cout<<"Phuong trinh co 2 nghiem phan biet"<<
endl;
cout<<"x1 = "<<x1<<
endl;
cout<<"x2 = "<<x2<<
endl;
return 0;

}
cin.ignore();
return 0;

}
reply
posted by lehoangan01 on Jan 31

reply
posted by lehoangan01 on Jan 31
#include <iostream>
#include <math.h>
using namespace std;

int main(){
float a, b, c;
float delta;
printf("nhap vao so a = ");
scanf("%f", &a );
printf("nhap vao so b = ");
scanf("%f", &b );
printf("nhap vao so c = ");
scanf("%f", &c );

if (a == 0){
if(b == 0){
if(c == 0){
cout << "phuong trinh co vo so nghiem"<< endl;
}
else {
cout << "phuong trinh vo nghiem" << endl;
}
}
}
else{
delta = b * b - 4 * a * c;
if (delta > 0){
float x1 = (-b + sqrt(delta)) / 2 * a;
float x2 = (-b + sqrt(delta)) / 2 * a;
cout <<" phuong trinh co hai nghiem x1 " << x1 << "va x2 " << x2 << endl;
}
else if ( delta == 0){
float x = -b / (2 * a);
cout << " phuong trinh co nghiem kep x = " << x << endl;
}
else{
cout << "phuong trinh vo nghiem" << endl;
}

}
return 0;
}


reply
posted by 20223768 on Sep 27
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a, b, c;
cin >> a>>b >>c;
if (a==0)
{
if (b==0)
{
if (c==0) cout << "Phuong trinh co vo so nghiem";
else cout << "Phuong trinh co nghiem duy nhat x=" << -c/b;
}
else cout << "Phuong trinh co nghiem duy nhat x=" << -c/b;
}
else
{
float d;
d=b*b-4*c*a;
if (d>=0) cout << "Phuong trinh co hai nghiem x1=" << (-b+sqrt(d))/(2*a) <<", x2=" << (-b-sqrt(d))/(2*a);
else cout << "Phuong trinh khong co nghiem thuc va co hai nghiem phuc la x1=" << -b/(2+a) <<"+" << sqrt(abs(d))/(2*a) << "i" << ", x2=" << -b/(2*a) << "-" << sqrt(abs(d))/(2*a) << "i";
}
return 0;
}
reply