C_C++

(C언어) 이차방정식의 해 구하기: 실근 허근 중근

고니자니 2022. 11. 10. 20:27
반응형

이차방적식의 실근, 허근, 중근을 구하는 C언어 코드입니다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

int main()
{
    int a, b, c;
    int d;
    double x1=0.0, x2=0.0;

    printf("이차방정식의 해: a b c값을 입력하세요: ");
    scanf("%d %d %d", &a, &b, &c);

    d = b * b - 4 * a * c;
    if (d > 0)  // 서로 다른 두 실근
    {
        x1 = (-b + sqrt(d)) / (2 * a);
        x2 = (-b - sqrt(d)) / (2 * a);
        printf("x1=%f\nx2=%f\n", x1, x2);
    }
    else if (d == 0)
        printf("중근\n");
    else  // (d < 0)
        printf("허근\n");

    return 0;
}

이차방정식: 실근
이차방정식: 허근

 

 

 

728x90
반응형