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)




Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

hainguyen [T2008A]
hainguyen

2020-09-17 07:46: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[]) {
	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;
}



Đức Sơn [T2008A]
Đức Sơn

2020-09-14 06:44:11



#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;
	
	printf("\nNhap gia tri a=");
	scanf("%f,&a");
	
	printf("\nNhap gia tri b=");
	scanf("%f,&b");
	
	printf("\nNhap gia tri c=");
	scanf("%f,&c");
	
    if(a==0);
{
	if(b==0);
{
	if(c==0);
{
	
	printf("\nPhuong trinh vo so nghiem");
	}
	printf("\nPhuong trinh vo nghiem");
	}
	printf("\nPhuong trinh co nghiem duy nhat %f", -c/b);
	}
	{
	delta = b*b - 4*a*c;
	if(delta<0);
	printf("\nPhuong trinh vo nghiem");
	printf("\nPhuong trinh co hai nghiem phan biet");
	printf("\nPhuong trinh có nghiem kep %f",-b/(2*a));
	}
	
	return 0;
}



Nguyễn Anh Vũ [T2008A]
Nguyễn Anh Vũ

2020-09-13 11:01:14



#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[]) {
	//bat dau code;
	float a, b, c, delta;
	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("\nPhuong trinh vo so nghiem");
	printf("\nPhuong trinh vo nghiem");
		 }
	printf("\nPhuong trinh co nghiem duy nhat %f", -c/b);
}
{
        delta = b*b - 4*a*c;
          if(delta < 0)
    printf("\nPhuong trinh vo nghiem");
	printf("\nPhuong tri co hai nghiem phan biet");
	printf("\nPhuong trinh co nghiem kep %f", -b / (2*a));     
    
}
	//ket thuc code;
	return 0;
}



Do Trung Duc [T2008A]
Do Trung Duc

2020-09-12 14:00:45



#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 */
// Giai phuong trinh ax2 + bx + c = 0
int main(int argc, char *argv[]) {
	float a,b,c,x2,x1,x, delta;
		printf("Nhap gia tri a ="); 
		scanf("%f", &a);
		printf("Nhap gia tri b ="); 
		scanf("%f", &b);
		printf("Nhap gia tri c ="); 
		scanf("%f", &c);
		
	if (a == 0){
		if(b == 0){
			if(c==0){
				printf("phuong trinh vo so nghiem");
			}else{
				printf("phuong trinh vo nghiem");
			}
		}else{
			x = -c/b;
			printf("phuong trinh co nghiem duy nhat x = %f", x);
		}
	}else{
		delta = b*b - 4*a*c;
			
			if(delta<0){
				printf("phuong trinh vo nghiem");
			}else{
					if(delta==0){
						x = -b/2/a;
						printf("phuong trinh co mot nghiem x=%f", x);
					}else{
						x1 = -b/2/a + sqrt(delta)/2/a;
						x2 = -b/2/a - sqrt(delta)/2/a;
						printf("phuong trinh co  nghiem x1,x2  =%f,%f", x1,x2);
					}
			}
		
	}
	return 0;
}



Trần Thị Khánh Huyền [T2008A]
Trần Thị Khánh Huyền

2020-09-12 13:03:52



#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, x, x1, x2;
	printf("\nNhap gia tri a="); scanf("%f",&a);
	printf("\nNhap gia tri b="); scanf("%f",&b);
	printf("\nNhap gia tri c="); scanf("%f",&c);
	if (a==0){if(b==0){if(c==0){printf("PTVSN");}
	else {printf("PTVN");}}
	else {
	x=-c/b; printf("Phuong trinh co nghiem duy nhat x=%f",x);}}
	
	else{
		float delta;
		delta = (b/2)*(b/2)-a*c;
		if (delta>=0)
		{x1=(-b/2+sqrt(delta))/a;
		x2=(-b/2-sqrt(delta))/a;
		printf("\nPhuong trinh co nghiem x1 = %f",x1);
		printf("\nPhuong trinh co nghiem x2 = %f",x2);}
		else printf("PTVN");
	}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2020-09-12 07:53:35



#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, dt;
	printf("\nnhap a, b, c, dt;");
	scanf("%f%f%f", &a, &b, &c);
	if(a==0)
	{
		if(b==0)
		{
			if(c==0);
			printf("Phuong trinh vo so nghiem");
			printf("Phuong trinh vo nghiem");
		}
		printf("Phuong trinh co nghiem duy nhat %f ", -c/b);   		
	}
	{
		dt = b*b - 4*a*c;
		if(dt < 0)
		printf("phuong trinh vo nghiem!!!");
		printf("phuong co nghiem kep: %f", -b/(2*a));
		printf("phuong trinh co 2 nghiem pha biet");
	}
	return 0;
}



Nguyễn đình quân [T2008A]
Nguyễn đình quân

2020-09-12 05:19:40



#include <stdio.h>
#include <stdio.h>
 
int main() {
    float a, b, c;
    float delta;
    printf("Enter a, b and c:\n");
    scanf("%d%d%d", &a, &b, &c);
    delta = b * b - 4 * a * c;
    if(a == 0) {
        printf("You must enter a > 0\n");
        return 0; 
    }
    if(delta < 0) {
        printf("Impossible equation (No result for x)\n");
    } 
 
    if(delta == 0) {
        float x = -b / (2 * a);
        printf("x = %.2f\n", x);
    }
    if(delta > 0) {
        float x1 = (-b + sqrt(delta) ) / (2 * a);
        float x2 = (-b - sqrt(delta) ) / (2 * a);
        printf("x1 = %.2f\n", x1);
        printf("x2 = %.2f\n", x2);
    }
    return 0;
}



vuong huu phu [T2008A]
vuong huu phu

2020-09-12 03:45:06



#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,x,x1,x2,dt;
    printf("\n nhap so a = ");
    scanf("%f",&a);
    printf("\n nhap so b = ");
    scanf("%f",&b);
	printf("\n nhap so c = ");
    scanf("%f",&c);
	if(a==0) {
	if(b==0){
	if(c==0)
	printf("\n ptco vo so nghiem");
	else
	    printf("\n pt vo nghiem");
    }
		else
		printf("\nPhuong trinh co nghiem la x = %f",-c/b);
	}
	else{
		dt=b*b-4*a*c;
		
			if (dt<0){
			printf("\n pt vo nghiem");
			}
			
		    if(dt==0){
	        x=-b/(2*a);	
	        printf("\n pt co x = %f",x);
	        }
	        
			if (dt>0){
			x1= ((-b-sqrt(dt))/(2*a) );
			x2= ((-b+sqrt(dt))/(2*a) );
			
			printf("\n pt co 2 nghiem x1 = %f,x2 = %f",x1,x2);
			}
    }
	
	
	return 0;
}



Nguyên Phấn Đông [community]
Nguyên Phấn Đông

2020-09-11 16:42:00



#include<iostream>
#include <math.h> 
using namespace std;

int main(){

	float x, x1, x2, a,b,c, denta;
	
	cout<<"tim x biet ax^2+ bx+ c= 0"<<endl;
	cout<<"nhap a, b, c"<< endl;
	cin>> a >>b >>c ;
	if (a==0){
		if ( b==0){
 	       if( c==0){
 		    cout<<"Phuong trinh vo so ngiem";
 		}
 		else{ 
 		 cout<<"Phuong thi vo ngiem";
 		}
 	}
       else{
  c= -c;
  x= c/b; 
  cout<<"x= " <<static_cast<float>(b)/static_cast<float>(c); 
          }
		}
	
	else{
	denta= b*b - 4*a*c;	
	   if(denta >= 0){
	   	x1= (-b + sqrt(denta))/(2*a);
	   	x2= (-b - sqrt(denta))/(2*a);
	   	cout<<"x1= "<< x1<< "\nx2= " << x2;
	   	}
	   	else{ cout<< "phuong trinh vo nghiem";
		   }
	}
		
	return 0;
}



nguyễn Sử [T2008A]
nguyễn Sử

2020-09-11 15:52:38



#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
	int a, b, c;
	
	float delta, x1, x2;
	
	prentf("\nNhap a = ");
	scanf("%d, &a");
	
	prentf("\nNhap b = ");
	scanf("%d, &b");
	
	prentf("\nNhap c = ");
	scanf("%d, &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;
}



Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó