By GokiSoft.com| 10:02 18/01/2021|
Học JS

Bài tập - Giải phương trình bậc 2 ax2+ bx + c = 0 BT2005

Thực hiện yêu cầu sau

Giải phương tình bậc 2 (ax2 + bx + c = 0)

B1: Nhập giá trị cho a, b, c bằng prompt

B2: Hiển thị các nghiệm của x ra màn hình


Tags:

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

https://gokisoft.com/2005

Bình luận

avatar
TRẦN VĂN ĐIỆP [Teacher]
2021-01-20 01:47:17



<!DOCTYPE html>
<html>
<head>
	<title>PT Bac 2</title>
	<meta charset="utf-8">
</head>
<body>
	<h1><label id="tagA">a</label>x<sup>2</sup> + <label id="tagB">b</label>x + <label id="tagC">c</label> = 0</h1>
	<p>
		<button onclick="nhapA()">Nhap A</button>
		<button onclick="nhapB()">Nhap B</button>
		<button onclick="nhapC()">Nhap C</button>
		<button onclick="giaiPT()">Tim Nghiem</button>
	</p>
	<p id="result"></p>

	<script type="text/javascript">
		var aTag = document.getElementById('tagA')
		var bTag = document.getElementById('tagB')
		var cTag = document.getElementById('tagC')
		var resultTag = document.getElementById('result')
		// console.log(aTag)
		// aTag.style.color = 'red'
		// aTag.style.fontSize = '100px'
		// aTag.innerHTML = '10'

		var a, b, c

		function nhapA() {
			a = prompt('Nhap A')
			aTag.innerHTML = a
		}

		function nhapB() {
			b = prompt('Nhap B')
			bTag.innerHTML = b
		}

		function nhapC() {
			c = prompt('Nhap C')
			cTag.innerHTML = c
		}

		function giaiPT() {
			if(a == 0) {
				//PT bac 1
				if(b == 0) {
					if(c == 0) {
						// alert('PTVSN')
						resultTag.innerHTML = 'PTVSN'
					} else {
						// alert('PTVN')
						resultTag.innerHTML = 'PTVN'
					}
				} else {
					x = -c/b;
					// alert('Nghiem x = ' + x)
					resultTag.innerHTML = 'Nghiem x = ' + x
				}
			} else {
				//PT bac 2
				delta = b*b - 4*a*c
				if(delta < 0) {
					// alert('PTVN')
					resultTag.innerHTML = 'PTVN'
				} else if(delta == 0) {
					x = -b/(2*a)
					// alert('Nghiem x = ' + x)
					resultTag.innerHTML = 'Nghiem x = ' + x
				} else {
					x1 = (-b+Math.sqrt(delta))/(2*a)
					x2 = (-b-Math.sqrt(delta))/(2*a)

					// alert('Nghiem PT x1 = ' + x1 + ', x2 = ' + x2)
					resultTag.innerHTML = 'Nghiem PT x1 = ' + x1 + ', x2 = ' + x2
				}
			}
		}
	</script>
</body>
</html>


avatar
Nguyễn Anh Vũ [T2008A]
2020-11-14 18:05:58


#pt b2.html


<!DOCTYPE html>
<html>
<head>
	<title>Giai pt b2</title>
	<meta charset="UTF-8">
</head>
<body>
	<script type="text/javascript">
		a = prompt("Nhap gia tri cua a")
		b = prompt("Nhap gia tri cua b")
		c = prompt("Nhap gia tri cua c")
		document.write('a = ' + a + '</br>')
		document.write('b = ' + b + '</br>')
		document.write('c = ' + c + '</br>')

		function timnghiem(a,b,c){
			delta = b*b-4*a*c;
			if (delta<0){
				return('pt vô nghiệm')
			} else if(delta==0){
				x1=-b/(2*a)
				return("Nghiem x =" + x1)
			} else{
				x2=(-b + Math.sqrt(delta))/(2*a)
				x3=(-b - Math.sqrt(delta))/(2*a)
				return("Nghiem x1=" + x2 + '</br>' + "Nghiem x2=" + x3) 
			}
		}

		if (a==0){
			if (b==0){
				if (c==0){
					document.write("pt vo so nghiem");
				} else {
					document.write("pt vo nghiem");
				}
			}else{
				document.write("Nghiem x =" + -c/b);	
			}
		} else {
			nghiem = timnghiem(a,b,c)
			document.write(nghiem)
		}
	</script>
</body>
</html>


avatar
hainguyen [T2008A]
2020-10-19 03:05:29


#cs.html


<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
</head>
<body>
		<script type="text/javascript">
			var x, y, z, num
			var a = prompt('Nhap a');
			var b = prompt('Nhap b');
			var c = prompt('Nhap c');
			num = b*b - 4*a*c;

			if(num < 0){
				document.write('ptvn');
			}
			if(num == 0){
				var x = -b/(2*a);
				document.write('ptc1n')
			}
			else {
				var x1 = (-b + Math.sqrt(num))/(2*a);
				var x2 = (-b - Math.sqrt(num))/(2*a);
				document.write('x1');
				document.write('x2');
			}
		</script>
	</table>
</body>
</html>


avatar
nguyễn Sử [T2008A]
2020-10-17 11:31:58


#giaipuongtrinhbac2.html


<!DOCTYPE html>
<html>
<head>
	<title> Giai phuong trinh bac 2 </title>
	<meta charset="utf-8">
</head>
<body>
	<script type="text/javascript">
		var x, y, z, delta;
		var a = prompt('nhập giá trị của a', );
		var b = prompt('nhập giá trị của b', );
		var c = prompt('nhập giá trị của c', );
		delta = b*b - 4*a*c;

if (delta < 0 ){
	document.write('ptvn', );
}

if (delta==0) {
	var x = -b/(2*a);
	document.write('ptc1n', );
}else {
	var x1=(-b+Math.sqrt(delta))/(2*a);
	var x2= (-b-Math.sqrt(delta))/(2*a);
	document.write('x1', );
	document.write('x2', );
}
	</script>
</body>
</html>


avatar
Triệu Văn Lăng [T2008A]
2020-10-17 08:41:56


#ptbac2(2005).html


<!DOCTYPE html>
<html>
<head>
	<title>giai pt bac 2</title>
</head>
<body>
	<script type="text/javascript">
		var a, b, c,x1, x2, x;
		a=prompt("nhap a");
		b=prompt("nhap b");
		c=prompt("nhap c");

		var dental=(b*b)-(4*a*c);
		if (dental>0) {
			x1=(-b+Math.sqrt(dental))/(2*a);
			x2=(-b-Math.sqrt(dental))/(2*a);
			document.write("pt co 2 nghiem phan biet la x1= "+x1+" va x2= "+x2+"<br/>")
		} else 
			if (dental==0) {
				x=-b/2*a;
				document.write("pt co nghiem kep x= "+x+"<br/>")
			} else {
				document.write("pt vo nghiem")
			}

	</script>
</body>
</html>


avatar
Trần Thị Khánh Huyền [T2008A]
2020-10-17 03:49:08


#giaiPTbac2.html


<!DOCTYPE html>
<html>
<head>
	<title>giaiPTbac2</title>
	<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
	var a = prompt("Nhap gia tri a (a khac 0):");
	var b = prompt("Nhap gia tri b:");
	var c = prompt("Nhap gia tri c: ");
	var delta =b*b-4*a*c;
	if (delta<0){
		document.write('PTVN');
	}else if(delta==0){
		var x =-b/(2*a);
		document.write("Phuong trinh co nghiem duy nhat: ",x);
	}else{
		var x1=(-b+Math.sqrt(delta))/(2*a);
		var x2= (-b-Math.sqrt(delta))/(2*a);
		document.write("Phuong trinh co 2 nghiem, x1 la :"+ x1+"<br/>")
		document.write("x2 la: ",x2);
	}

</script>
</body>
</html>


avatar
vuong huu phu [T2008A]
2020-10-16 15:15:43



<!DOCTYPE html>
<html>
<head>
	<title></title>
	
</head>
<body>
<meta charset="utf-8">
<script type="text/javascript">
	var a = prompt("nhap so a");
	var b = prompt("nhap so b");
	var c = prompt("nhap so c");
	var x1 ;
	var x2 ;
	var dt ;
	var x ;

if(a==0){
x=(-b/a);
document.write(x);
}	
if(a!=0){
dt=((b*b)-(4*a*c));
if (dt<0) {
	document.write("pt vô nghiệm");
}if (dt==0) {
	x1 = x2 = (-b)/(2*a);
	document.write(x1);
}if (dt>0) {
x1 = (-b + dt)/2*a;
x2 = (-b - dt)/2*a;
document.write(x1 ,x2);
}
}
</script>
</body>
</html>


avatar
Nguyễn Hữu Hiếu [T2008A]
2020-10-16 08:54:46


#1606.html


<!DOCTYPE html>
<html>
<head>
	<title>JS</title>
</head>
<body>
	<script type="text/javascript">
		var a = prompt("Nhap a = ", 0);
		var b = prompt("Nhap b = ", 0);
		var c = prompt("Nhap c = ", 0);
		
		

		if (a == 0) {
			if (b == 0) {
				if (c == 0) {
					document.write("PT vo so nghiem");
				}
				else {
				document.write("PT vo nghiem");
				}	
			}
			else {
				var x = -c/b;
				document.write("x = " + x);
			}	
		}
		
		if (a != 0) {
			var d = b*b - 4*a*c;
			if (d < 0) {
				document.write("PT vo nghiem");
			}
			else if (d == 0) {
				var x1 = -b/(2*a);
				document.write("x1 = x2 = " + x1);
			}
			else if (d > 0) {
				var xx = (-b - Math.sqrt(d))/(2*a);
				var xxx = (-b + Math.sqrt(d))/(2*a);
				document.write("x1 = " + xx);
				document.write('<br>');
				document.write("x2 = " + xxx);
			}
		}

	</script>

</body>
</html>


avatar
Nguyễn Xuân Mai [T2008A]
2020-10-16 08:51:53



<!DOCTYPE html>
<html>
<head>
	<title>ptbac2</title>
	<meta charset="UTF-8">
</head>
<body>
	<script type="text/javascript">
		a = prompt("Nhap gia tri cua a")
		b = prompt("Nhap gia tri cua b")
		c = prompt("Nhap gia tri cua c")
		document.write('a = ' + a + '</br>')
		document.write('b = ' + b + '</br>')
		document.write('c = ' + c + '</br>')

		function timnghiem(a,b,c){
			delta = b*b-4*a*c;
			if (delta<0){
				return('pt vô nghiệm')
			} else if(delta==0){
				x1=-b/(2*a)
				return("Nghiem x =" + x1)
			} else{
				x2=(-b + Math.sqrt(delta))/(2*a)
				x3=(-b - Math.sqrt(delta))/(2*a)
				return("Nghiem x1=" + x2 + '</br>' + "Nghiem x2=" + x3) 
			}
		}

		if (a==0){
			if (b==0){
				if (c==0){
					document.write("pt vo so nghiem");
				} else {
					document.write("pt vo nghiem");
				}
			}else{
				document.write("Nghiem x =" + -c/b);	
			}
		} else {
			nghiem = timnghiem(a,b,c)
			document.write(nghiem)
		}
	</script>
</body>
</html>


avatar
Nguyễn Tiến Đạt [T2008A]
2020-10-16 08:44:13


#phuongtrinhbac2.html


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Giải phương trình bậc 2</title>
	<style>
		span{
			color: blue;
		}
	</style>
</head>
<body style="font-size: 30px;">
	<script>
		alert('Bài toán giải phương trình bậc 2!!!');
		a=prompt('Nhập a:');
		b=prompt('Nhập b:');
		c=prompt('Nhập c:');
		if(a==0){
			if(b==0){
				if(c==0){
					document.write('Phương trình có vô số nghiệm!!!')
				}
				else{
					document.write('Phương trình vô nghiệm!!!')
				}
			}
			else{
				x=-c/b
				document.write('Phương trình có 1 nghiệm duy nhất là:'+'<br>' +'<span class="dat">' + x + '</span>')
			}
		}
		else{
			delta=b*b - 4*a*c
			if(delta<0){
				document.write('Phương trình vô nghiệm!!!')
			}
			else if(delta==0){
				x1=-b/(2*a)
				document.write('Phương trình có 1 nghiệm kép là:'+ '<br>' +'<span class="dat">' + x1 + '</span>')
			}
			else{
				x2=(-b + Math.sqrt(delta))/(2*a)
				x3=(-b - Math.sqrt(delta))/(2*a)
				document.write('Phương trình có 2 nghiệm phân biệt là:'+ '<br>' +'<span class="dat">' + x2 + '</span>' + '<br><br>' +'<span class="dat">' + x3 + '</span>')
			}
		}
	</script>
</body>
</html>