Bachelor of Technology in Information Technology


Write a C program to find the roots of a quadratic equation



Download 0,65 Mb.
bet6/14
Sana20.03.2022
Hajmi0,65 Mb.
#502184
1   2   3   4   5   6   7   8   9   ...   14
Bog'liq
PSP Lab Manual

Write a C program to find the roots of a quadratic equation.


ALGORITHM:

Step 1: Start.


Step 2: declare a, b, c, discriminant, root1, root2, realPart, imagPart. Step 3: print enter coefficients of a, b, c.
Step 4: read a, b, c.
Step 5: discriminant ← b*b-4*a*c. Step 6: if(discriminant > 0)
root1←(-b + sqrt(discriminant))/(2*a) root2←(-b - sqrt(discriminant))/(2*a)
print root1 and root2.
Step7: else if(discriminant == 0)
root1 ← root2 ← -b/(2*a) print root1 and root2.
Step 8: else
realPart← -b/(2*a)
imagPart←sqrt(-discriminant)/(2*a)
print real and imag part of root1 and root2.
Step 9: Stop.

Source Code


#include #include int main() {


double a, b, c, discriminant, root1, root2, realPart, imagPart; printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c); discriminant = b * b - 4 * a * c;
// condition for real and different roots if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);

}
// condition for real and equal roots else if (discriminant == 0) {


root1 = root2 = -b / (2 * a); printf("root1 = root2 = %.2lf;", root1);
} // if roots are not real else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}

  1. Calculator using switch-case


ALGORITHM:

Step 1: Start.


Step 2: declare a, b, result, option. Step 3: print enter values of a & b. Step 4: read a, b
Step 5: print Select an option
1.Addition. 2.Subtraction 3.Multiplication 4.Division.
Step 6: read option Step 7: switch to option
If option 1:result ←a+b
Print result Break
If option 2: result ←a-b
Print result Break
If option 3: result← a/b
Print result Break
If option 4: result ← a*b
Print result Break
Default case: print invalid option selected
Break



Download 0,65 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   14




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish