/**recursive function to find square root of a double to a precision of
maxDepth = 10 decimal places
returns -1.0 when given a negative number*/
#define maxDepth 10
/*firstly find the rational part*/
double getSqrt(double numIn){
/*cant take the square root of a negitive,
and a square root should not be negative
so return -1*/
int candidate = 0;
if (numIn <0)
return -1.0;
/*try every integer until you get one whose square is higher than
the number required, and this clearly one more than the
integer part of your square root*/
do{
if (candidate *candidate *1.0 numIn)
return testVal;
if (testVal * testVal >numIn)
/*most square roots are irrational, and theirfore a maximum number of recursions
must be set, otherwise infinite recursion will occur*/
if (myDepth <maxDepth)
return getIrrational(numIn, testVal-1/10^myDepth, myDepth +1);
else
return testVal-1/10^myDepth;
}
//this can probably be improved on in terms of conciseness, but the logic is the only //way to find a sqrt without going into calculous
Use a template function to calculate the square of any value, then sum the squares. The following example demonstrates how to sum the squares of integers 3 and 5.
#include<iostream>
template <class T>
T sq(T& value)
{
return( value*value );
}
int main()
{
#using namespace std;
int x=3, y=5;
cout<<"The sum of "<<x<<" squared plus "<<y<<" squared is "
<<sq(x)+sq(y)<<endl;
return(0);
}
#include<iostream>
template<typename T>
struct foo
{
T data;
foo (const T num): data (num) {}
T square() const { return data * data; }
T cube() const { return data * square(); }
};
int main()
{
foo<int> a {3};
foo<double> b {3.14};
std::cout << a.data << " squared = " << a.square() << " and cubed = " << a.cube() << std::endl;
std::cout << b.data << " squared = " << b.square() << " and cubed = " << b.cube() << std::endl;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
cout << "Enter a number: ";
cin >> x;
cout << "The squareroot of " << x << " is " << sqrt(x) << endl;
char wait;
cin >> wait;
return 0;
}
#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
double a;
double b;
double c;
cout << "Enter number for a: ";
cin >> a;
cout << "Enter number for b: ";
cin >> b;
cout << "Enter number for c: ";
cin >> c;
if (a 0)
{
cerr << "0 is an invalid input" << endl;
}
else
{
double root_1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
double root_2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
cout << "Root 1: " << root_1 << endl;
cout << "Root 2: " << root_2 << endl << endl;
}
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num = 0;
cout << "Enter a number: ";
cin >> num;
if(num % 2 != 0)
{
cout << "Number is odd and the squareroot is: " << sqrt(num);
}
return 0;
}
#include
#include
#include
int main()
{
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::sqrt;
using std::getline;
using std::stringstream;
double input;
string str = "";
while (true)
{
cout << "Enter a positive decimal number: ";
getline (cin, str);
stringstream ss (str);
if (ss >> input && input > 0)
break;
cout << "Invalid input, please try again..." << endl;
}
double squared = input * input;
double square_root = sqrt (input);
cout << input << " squared is " << squared << endl;
cout << "The square root of " << input << " is " << square_root << endl;
}
Example output
Enter a positive decimal number: 1.234
1.234 squared is 1.52276
The square root of 1.234 is 1.11086
That's what function sqrt is good for, prototype in math.h.
#include <iostream> using namespace std; int main() { int i,sum; // variables sum = 0; // initialize sum /* recursive addition of squares */ for (i = 1; i <= 30; i++) sum = sum + (i * i); cout << sum <<" is the sum of the first 30 squares." << endl; return 0; }
Using its Taylor-series.
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
This is not a question.
Functions are used to reduce the lines of code and the complexity of the code. For an instance let us suppose that you want to calculate the factorial of numbers at different times in a program. There are two ways to do this 1. Write a 4-5 line code every time you want to calculate factorial. 2. Write a function of 4-5 lines which calculates the factorial and call that function every time you need to calculate factorial by just writing a single line. In C++ you can pass the variable, address of the variable or a reference to the variable in a function
give an example of calculation of mathematics
#include <iostream> using namespace std; int main() { int i,sum; // variables sum = 0; // initialize sum /* recursive addition of squares */ for (i = 1; i <= 30; i++) sum = sum + (i * i); cout << sum <<" is the sum of the first 30 squares." << endl; return 0; }
float income_tax (float income, float tax_percent) { return income * tax_percent / 100; }
using function we can call the function in the program any where. by using functions we can reduce the code redundancy
cubic function cubic function
Using its Taylor-series.
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
#include<
By using the =MOD function.
use the rate function
C programs do not function without functions.
It can be done via its address, for example: void function (void (*callback)(void)) { (*callback)(); }