0% found this document useful (0 votes)
0 views

C++ external asgnmnt

The document contains solutions to five programming questions using C++. It includes programs to find the roots of a quadratic equation, determine the largest of three numbers, check if a number is positive, negative, or zero, calculate Fibonacci numbers, and demonstrate the use of the continue statement in a while loop.

Uploaded by

ravi8972535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

C++ external asgnmnt

The document contains solutions to five programming questions using C++. It includes programs to find the roots of a quadratic equation, determine the largest of three numbers, check if a number is positive, negative, or zero, calculate Fibonacci numbers, and demonstrate the use of the continue statement in a while loop.

Uploaded by

ravi8972535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Question 1- Write a program to find the roots of quadratic equation.

Solution:
#include <iostream>
#include <cmath>

using namespace std;

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;
}

Question 2- Write a program to find the largest number of any three


numbers.
Solution:
#include <iostream>

using namespace std;

int main() {
double num1, num2, num3;

cout << "Enter three numbers: ";


cin >> num1 >> num2 >> num3;

if (num1 >= num2 && num1 >= num3) {


cout << "The largest number is: " << num1 << endl;
} else if (num2 >= num1 && num2 >= num3) {
cout << "The largest number is: " << num2 << endl;
} else {
cout << "The largest number is: " << num3 << endl;
}

return 0;
}

Question 3- Write a program to cheak whether a number is positive,


negative, zero.
Solution:
#include <iostream>

using namespace std;

int main() {
double number;

cout << "Enter a number: ";


cin >> 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>

using namespace std;

int main() {
int n;
cout << "Enter the number of Fibonacci numbers you want to calculate: ";
cin >> n;

int fib1 = 0, fib2 = 1, nextTerm;

cout << "Fibonacci Series:" << endl;

for (int i = 1; i <= n; ++i) {


cout << fib1 << " ";

nextTerm = fib1 + fib2;


fib1 = fib2;
fib2 = nextTerm;
}

return 0;
}
Question 5- Write a program to demonstrate the use of continue statement
with in a while loop.
Solution:
#include <iostream>

using namespace std;

int main() {
int num = 1;

cout << "Printing odd numbers between 1 and 10 (excluding 5):" << endl;

while (num <= 10) {


if (num == 5) {
num++; // Increment num to avoid infinite loop, since continue is used
continue; // Skip the rest of the loop body and continue with the next iteration
}

if (num % 2 != 0) {
cout << num << " ";
}

num++;
}

cout << endl;

return 0;
}

You might also like