0% found this document useful (0 votes)
1 views6 pages

Unit 1 Programs

The document contains multiple C++ programs demonstrating various concepts including finding roots of quadratic equations, calculating factorials using recursion, implementing scope resolution and namespaces, and illustrating default arguments and access specifiers. Each program includes sample input/output to showcase functionality. The examples cover handling different types of roots, recursive function calls, namespace usage, and class access control.

Uploaded by

rohithmathala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Unit 1 Programs

The document contains multiple C++ programs demonstrating various concepts including finding roots of quadratic equations, calculating factorials using recursion, implementing scope resolution and namespaces, and illustrating default arguments and access specifiers. Each program includes sample input/output to showcase functionality. The examples cover handling different types of roots, recursive function calls, namespace usage, and class access control.

Uploaded by

rohithmathala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

To find the roots of a quadratic equation using C++.

To find the roots of a quadratic equation using


C++.

Program:

// Find the roots of a quadratic equation (ax2 + bx + c)


// This code handles real and distinct, real and equal, and complex roots.

#include <iostream>
#include <cmath> // for sqrt() function
using namespace std;

int main()
{
float a, b, c;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;

float discriminant = b*b - 4*a*c;


float root1, root2;

if (a == 0) {
cout << "This is not a quadratic equation (a cannot be 0)." << endl;
}
else if (discriminant > 0) {
// Real and distinct roots
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and distinct." << endl;
cout << "Root 1 = " << root1 << endl;
cout << "Root 2 = " << root2 << endl;
}
else if (discriminant == 0) {
// Real and equal roots
root1 = root2 = -b / (2*a);
cout << "Roots are real and equal." << endl;
cout << "Root = " << root1 << endl;
}
else {
// Complex roots
float realPart = -b / (2*a);
float imagPart = sqrt(-discriminant) / (2*a);
cout << "Roots are complex and imaginary." << endl;
cout << "Root 1 = " << realPart << " + " << imagPart << "i" << endl;
cout << "Root 2 = " << realPart << " - " << imagPart << "i" << endl;
}

return 0;
}

Output:

Sample Input/Output:
Enter coefficients a, b and c: 1 5 6
Roots are real and distinct.
Root 1 = -2
Root 2 = -3
Sample Input/Output:
Enter coefficients a, b and c: 1 4 4
Roots are real and equal.
Root = -2
Sample Input/Output:
Enter coefficients a, b and c: 1 2 5
Roots are complex and imaginary.
Root 1 = -1 + 2i
Root 2 = -1 - 2i

Find factorial of a given number using recursion

Programt:

//Find factorial of a given number using recursion

#include <iostream>

using namespace std;

// Recursive function to calculate factorial

long factorial(int n)

if (n == 0 || n==1)
return 1;

else

return n * factorial(n - 1);

int main()

int number;

cout << "Enter a positive integer: ";

cin >> number;

if (number < 0)

{ cout << "Factorial is not defined for negative numbers." << endl;

else

long result = factorial(number);

cout << "Factorial of " << number << " = " << result << endl;

return 0;

OUPUT:

Enter a positive integer: 5

Factorial of 5 = 120

Enter a positive integer: -3

Factorial is not defined for negative numbers.

Implement scope resolution and namespaces.


Program

// Implement Scope Resolution Operation and namespace

#include <iostream>

using namespace std; // using standard namespace

int x = 100; // Global variable

namespace MyNamespace

int value = 50;

void show()

{ cout << "Inside MyNamespace, value = " << value << endl;

int main()

{ int x = 10; // Local variable

cout << "Local x = " << x << endl;

cout << "Global x = " << ::x << endl; // Using scope resolution to access global x

// Accessing namespace variable and function

cout << "Namespace value = " << MyNamespace::value << endl;

MyNamespace::show();

return 0;

Illustrate the use of default arguments and access specifiers


Program:

#include <iostream>
using namespace std;
// Function with default arguments
void greet(string name = "Guest", int times = 1)
{
for (int i = 0; i < times; i++) {
cout << "Hello, " << name << "!" << endl;
}
}

// Class to illustrate access specifiers


class DemoAccess {
private:
int privateValue = 100;

protected:
int protectedValue = 200;

public:
int publicValue = 300;

void showValues() {
cout << "Private Value: " << privateValue << endl;
cout << "Protected Value: " << protectedValue << endl;
cout << "Public Value: " << publicValue << endl;
}
};

int main()
{
// Demonstrate default arguments
greet(); // Uses both default arguments
greet("Anil"); // Uses default for 'times'
greet("Ravi", 3); // No default used

// Demonstrate access specifiers


DemoAccess obj;
[Link](); // Access through public function
cout << "Direct Access to Public Value: " << [Link] << endl;

// The following would cause errors:


// cout << [Link];
// cout << [Link];
return 0;
}

Sample Input/Output:
Hello, Guest!
Hello, Anil!
Hello, Ravi!
Hello, Ravi!
Hello, Ravi!
Private Value: 100
Protected Value: 200
Public Value: 300
Direct Access to Public Value: 300

You might also like