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

C++ Assignments

Uploaded by

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

C++ Assignments

Uploaded by

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

#include <iostream>#include <cmath> // Include the cmath library

for square root function


int main() {
// Declare variables
double x, result;

// Assign value to variable x


x = 9.0; // Example value

// Calculate square root of (x + 2)


result = sqrt(x + 2);

// Output the result


std::cout << "The square root of (x + 2) is: " << result <<
std::endl;

return 0;
}

2.x^2 + y^2:
#include <iostream>
int main() {
// Declare variables
double x, y, result;

// Assign values to variables


x = 3.0; // Example value
y = 4.0; // Example value

// Calculate x^2 + y^2


result = (x * x) + (y * y);

// Output the result


std::cout << "The result of x^2 + y^2 is: " << result <<
std::endl;

return 0;
}

3.(u + v)^(k-1):
#include <iostream>#include <cmath> // Include the cmath library for
pow function
int main() {
// Declare variables
double u, v, k, result;

// Assign values to variables


u = 2.0; // Example value
v = 3.0; // Example value
k = 4.0; // Example value

// Calculate (u + v)^(k-1)
result = pow(u + v, k - 1);

// Output the result


std::cout << "The result of (u + v)^(k-1) is: " << result <<
std::endl;

return 0;
}

3. start

Input two
numbers;char
Ie, num1,
num2

enter the
Operation
*,+,-,/

Add the numbers


With the
operations

End

3.
#include <iostream>
int main() {
double num1, num2;
char operation;

// Input two numbers and the desired operation


std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;

std::cout << "Enter the operation (+, -, *, /): ";


std::cin >> operation;

double result;
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
std::cout << "Error: Division by zero!" << std::endl;
return 1;
}
break;
default:
std::cout << "Invalid operation. Please enter +, -, *, or
/." << std::endl;
return 1;

You might also like