C++ Double

C++ Double
Techiio-author
Written by Sagar RabidasJanuary 25, 2022
7 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion on C++ Double

C++ Double Data Type

C++ double is a versatile statistics kind that is used internally for the compiler to outline and hold any numerically valued statistics type, particularly any decimal oriented value. C++ double statistics kind may be either fractional in addition to complete numbers with values. Those styles of decimal-oriented information type values can comprise numbers to fifteen digits and might either be prefixed with the decimal factor or may be suffixed with the decimal point. The double facts kind is tailored often via programmers when in comparison with glide and is often kept as the default information type at the time of operating with numbers associated with a big number with decimals.

Syntax:

C++ double data type has a syntax which is represented as follows:

double var_name;

datatype considered here is double followed by a variable name which is var_name in this syntax. Also, a series of var_name somewhat in the following manner can be considered to optimize the code representation:

double var_name1, var_name2, var_name3…...var_nameN

Examples

Lets us discuss examples of C++ Double.

Example #1

This program demonstrates the C++ program for the double keyword where the user wants to make a conversion from Celsius temperature to Fahrenheit as shown in the output.

Code:

#include <iostream>
using namespace std;
int main()
{
double  c_tmp, f_tmp;
cout << "Take an input in celsius and then convert it into some value: ";
cin >> c_tmp;
f_tmp = (c_tmp * 1.8) + 32;
cout << "Temperature in Fahreinheit: " << f_tmp;
return 0;
}

Example #2

This program demonstrates the addition, multiplication, and division of two numbers and provides the output where both the input numbers will be of type double as shown in the output.

Code:

#include <iostream>
using namespace std;
int main()
{
double n_1, n_2;
cout<<"First_Number to be entered as a double type: ";
cin>>n_1;
cout<<"Second_Number to be entered as a double type: ";
cin>>n_2;
cout<<"Sum of both the numbers entered: "<<(n_1+n_2);
cout<<"  Product or multiplication of both the numbers entered: "<<(n_1*n_2);
cout<<"  Result_for division of two numbers: " <<(n_1/n_2);
return 0;
}

Example #3

This program demonstrates function overloading where the function considering two integer numbers gets overridden by the function consisting of the data type with both the parameters as double as shown in the output.

  • Note: Both the function has integer and double value as user input to get the result shown in the particular format.

Code:

#include <iostream>
using namespace std;
int sum_num(int, int);
double sum_num(double, double);
int main(){
int n_1, n_2, p;
double n_3, n_4, q;
cout<<" Two Integer_numbers: ";
cin>>n_1>>n_2;
cout<<"Rslt: "<<sum_num(n_1, n_2)<< endl;
cout<<" Two double_numbers: ";
cin>>n_3>>n_4;
cout<<"Rslt: " <<sum_num(n_3, n_4)<< endl;
}
int sum_num(int p, int q){
return p+q;
}
double sum_num(double p, double q){
return p+q;
}

Example #4

This program demonstrates a quadratic equation: x2 -4.0500000x + 2.999999 = 0, where the roots are just round off to 10 significant digits with root values as , r1 = 4.056785645 and r2 = – 6.0089767987. The output is shown as follows.

Code:

#include <stdio.h>
#include <math.h>
void doubl_sol(double p, double q, double r)
{
double d_b = q*q - 8.0*p*r;
double sol_d = sqrt(d_b);
double r_1 = (-q + sol_d) / (4.0*p);
double r_2 = (-q - sol_d) / (4.0*p);
printf("%.8f\t%.8f\n", r_1, r_2);
}
void flt_sol(float p, float q, float r)
{
float d_b = q*q - 4.0f*p*r;
float sol_d = sqrtf(d_b);
float r_1 = (-q + sol_d) / (6.60f*p);
float r_2 = (-q - sol_d) / (8.0f*p);
printf("%.8f\t%.8f\n", r_1, r_2);
}
int main(void)
{
float f_a = 1.02f;
float f_b = -4.0500000f;
float f_c = 2.99999f;
double d_a = 3.0;
double d_b = -6.0000000;
double d_c = 3.9989999;
flt_sol(f_a, f_b, f_c);
doubl_sol(d_a, d_b, d_c);
return 0;
}

c
c plus plus
c plus plus double
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance