Sunteți pe pagina 1din 5

RUTHIE B.

TABANGUIL BES 103 COMPUTER FUNDAMENTALS AND PROGRAMMING

CE 2-2

MODULE 6: FUNCTIONS 6.1: Predefined Functions

Application

To utilize the general knowledge, you have gained in this lesson, create a program that

will find all roots of a quadratic equation. Following the quadratic equations below,

y = ax2 + bx + c

your program should ask your user an input value for the coefficients a, b and c. The program should
display all roots of a quadratic equation using the formula,

𝒚 = −𝒃±√𝒃𝟐−𝟒𝒂𝒄/𝟐𝒂

The program should also display “Roots are real and different” if the determinant is

greater than 0, “Roots are real and same” if the determinant is equal to 0, and “Roots

are complex and different” if the determinant is less than 0.

//Ruthie Tabanguil
//MODULE 6 MACHINE PROBLEM ASYNCHRONUOUS CLASS
//M6L1APPP1

#include <iostream>
#include <cmath>
using namespace std;

int main() {

float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;


cout << "Enter coefficient a: ";
cin >> a;
cout << "Enter coefficient b: ";
cin >> b;
cout << "Enter coefficient c: ";
cin >> c;
discriminant = b*b - 4*a*c;

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}

else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}

return 0;
}
MODULE 6: FUNCTIONS 6.2 Void Functions

Application

Write a C++ code for a void function that receives an input value from the main program. This function
should compute and display the equivalent factorial number.

Note the following conditions:

If the input value is less than 0 it should display "Wrong value", if the input value is equal to 0 it should
display "Terminating condition", otherwise it should compute and display the equivalent factorial
number.

//Ruthie Tabanguil
//MODULE 6 MACHINE PROBLEM ASYNCHRONUOUS CLASS
//M6L2APPP1
#include<iostream>
using namespace std;

//void function for factorial//


void factorial(int n)
{
int fact = 1;
if (n<0)
cout << "Wrong value.";
else if (n == 0)
cout << "Terminating condition";
else
{
for (int i=1; i<= n; i++)
fact = fact * i;
cout << "Factorial of " << n << " = " << fact;
}
}
int main()
{
int n;
cout << "Enter any number: ";
cin >> n;

//function calling//
factorial (n);
return 0;
}
MODULE 6: FUNCTIONS 6.3 Value Returning Functions

Write a C++ code for a value returning function that receives an input values from the main program.
This function should compute and display depending on the basic mathematical operations chosen by
the user. The program should not be case sensitive for an input options. If the user input a wrong
character for the option, it should display “Wrong options!”.

//Ruthie Tabanguil
//MODULE 6 MACHINE PROBLEM ASYNCHRONUOUS CLASS
//M6L3APPP1
//program that will compute and display an area of a rectangle//
#include <iostream>
using namespace std;

// function prototype
double area(double length, double width);

int main(int argc, char** argv) {


// declaring and initializing variables
double answer, x, y;

// input prompt for the user


cout << "Area of a reactangle" << endl << endl;
cout << "Input length: ";
cin >> x;
cout << "Input width: ";
cin >> y;

// call the function


answer = area(x, y);

// display the area


cout << "Area : " << answer << ". " << endl;
return 0;
}
double area(double length, double width)
{
// declare and initiale a variable
double result;
// compute the area
result = length * width;
return result;
}

DRIVE LINK:

https://drive.google.com/drive/folders/1qie-yu8bvLxRmj3r23OSRexKHc0tw1WD?usp=sharing

S-ar putea să vă placă și