C++ external asgnmnt
C++ external asgnmnt
Solution:
#include <iostream>
#include <cmath>
int main() {
double a, b, c;
double discriminant, root1, root2;
cout << "Enter the coefficients of the quadratic equation (a, b, c): ";
cin >> a >> b >> c;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << root1 << endl;
cout << "Root 2 = " << root2 << endl;
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
cout << "Roots are real and same." << endl;
cout << "Root 1 = Root 2 = " << root1 << endl;
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" << endl;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
}
return 0;
}
int main() {
double num1, num2, num3;
return 0;
}
int main() {
double number;
if (number > 0) {
cout << "The number is positive." << endl;
} else if (number < 0) {
cout << "The number is negative." << endl;
} else {
cout << "The number is zero." << endl;
}
return 0;
}
Question 4- Write a program to calculate successive fibonacci numbers.
Solution:
#include <iostream>
int main() {
int n;
cout << "Enter the number of Fibonacci numbers you want to calculate: ";
cin >> n;
return 0;
}
Question 5- Write a program to demonstrate the use of continue statement
with in a while loop.
Solution:
#include <iostream>
int main() {
int num = 1;
cout << "Printing odd numbers between 1 and 10 (excluding 5):" << endl;
if (num % 2 != 0) {
cout << num << " ";
}
num++;
}
return 0;
}