C++ Floating Type Program

C++ has three different float types : float, double and long double. This program shows the implementation of these data types. In the next program, operations are performed on these float data types.

#include<iostream>
using namespace std;
int main(){
	float x = 10.5f;
	double d = 22.5;
	long double l = 32.5;
	cout<<"x = "<<x<<endl;
	cout<<"d = "<<d<<endl;
	cout<<"l = "<<l<<endl;
	return 0;
}
output of the above image. C++ floating type program
output of the above image

Operations on floating-type arithmetic

#include<iostream>
using namespace std;
int main(){
	double x = 32.5;
	double y = 45.3;
	cout<<"x = "<<x<<" y = "<<y<<endl;
	cout<<"x + y = "<<x+y<<endl;
	cout<<"x - y = "<<x-y<<endl;
	cout<<"x * y = "<<x*y<<endl;
	cout<<"x / y = "<<x/y<<endl;
	return 0;
}
output for operations on float type
output for operations on float type

% operator does not work on float types.

One Reply to “C++ Floating Type Program”

Leave a Reply

Your email address will not be published. Required fields are marked *