answersLogoWhite

0


Best Answer

In C, decimal values are called "floating point" because the number of digits after the decimal point can vary. There are two types used for floating point operations: float and double.

Although the decimal point can shift anywhere within the number, C only allows a certain number of consecutive digits worth of precision. The float type only allows 7 consecutive digits, and the double type allows 15. Any digits before and after those consecutive digits are generally displayed as zero (0).

Here's an example program that uses the float type with arithmetic operators:

#include

void main()

{

float myfloat=0.0, counter;

printf("* perform addition on the float:\n");

for (counter=5; counter<10; counter++) {

myfloat+=(counter/10);

printf("myfloat is: %0.7f\n", myfloat);

}

printf("* perform subtraction on the float:\n");

for (counter=0; counter<5; counter++) {

myfloat-=(counter/10);

printf("myfloat is: %0.7f\n", myfloat);

}

printf("* perform multiplication on the float:\n");

for (counter=5; counter<10; counter++) {

myfloat*=(counter/10);

printf("myfloat is: %0.7f\n", myfloat);

}

printf("* perform division on the float:\n");

for (counter=2; counter<8; counter++) {

myfloat/=(counter/10);

printf("myfloat is: %0.7f\n", myfloat);

}

}

The %0.7f inside the printf() format string tells the compiler you want to display a minimal number of integer places, and no more than 7 decimal places. Play around with this value and see what happens.

If you compile and run this program, you may notice something odd. The last (right-most) decimal place has an errant number popping in. This is a result of the low precision of the float type. The following output is an example:

* perform addition on the float:

myfloat is: 0.5000000

myfloat is: 1.1000000

myfloat is: 1.8000001

myfloat is: 2.6000001

myfloat is: 3.5000002

* perform subtraction on the float:

myfloat is: 3.5000002

myfloat is: 3.4000003

myfloat is: 3.2000003

myfloat is: 2.9000003

myfloat is: 2.5000002

* perform multiplication on the float:

myfloat is: 1.2500001

myfloat is: 0.7500001

myfloat is: 0.5250000

myfloat is: 0.4200000

myfloat is: 0.3780000

* perform division on the float:

myfloat is: 1.8900001

myfloat is: 6.3000002

myfloat is: 15.7500000

myfloat is: 31.5000000

myfloat is: 52.5000000

myfloat is: 75.0000000

However, if you're displaying only a couple of decimal points, the discrepancy is generally unseen.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to you write a program in with decimal values?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write a c program to accept a numbers and generate square root cube and exponential values?

write a c program to accept a number and generate a square root cube and exponential values


Write a c program for function with no arguments and no return values?

void mynullfunction () {;}


How do you write 723 percent as a decimal?

You divide percent values by 100 (or move the decimal two places to the left) to get the decimal equivalent: 723 / 100 = 7.23


How do you write 135.2 percent as a decimal?

You divide percent values by 100 (or move the decimal two places to the left) to get the decimal equivalent: 135.2 / 100 = 1.352


How do you write out 1.83 percent as a decimal?

You divide percent values by 100 (or move the decimal two places to the left) to get the decimal equivalent: 1.83/ 100 = 0.0183


How do you write 72 percent as a decimal in simplest form?

You divide percent values by 100 (or move the decimal two places to the left) to get the decimal equivalent: 72 / 100 = 0.72


How do you write 131.66 percent?

You divide percent values by 100 (or move the decimal two places to the left) to get the decimal equivalent: 131.66 / 100 = 1.3166


How do you write 17 percent?

You divide percent values by 100 (or move the decimal two places to the left) to get the decimal equivalent: 17 / 100 = 0.17


How you write 625 percent?

You divide percent values by 100 (or move the decimal two places to the left) to get the decimal equivalent: 625 / 100 = 6.25


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


Write a C program to convert hexadecimal number into decimal number?

pongada punda vayanungala ..................