English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

why this program isnt working the way it shld??
y isnt it storing pi=3.1428... into pi?? it is storing only the integer part of it. why so?? can u guys plz explain?????

void main()
{
float pi=22/7,rad,area;
printf("Enter the radius:");
scanf("%f",&rad);
area=pi*rad*rad;
printf("Area=%f",area);
getch();
}

OUTPUT:
Enter radius: 3
Area=27.000000

2007-01-19 20:45:40 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

The expression '22/7' is the quotient of two integer variables, so in the C language it is evaluated TO AN INTEGER (in this case 3) by truncating the division. The float it is being assigned to is not seen till later, i.e. too late to go back and pick up the remainder . . .

You need to make one of the integers a float. Either '22/7.0' or '22.0/7' will do the trick. '355.0/113' will be even more accurate.

2007-01-20 07:50:51 · answer #1 · answered by Anonymous · 0 0

PI should already be an integrated function in C/C++, provided you included the correct H files. Try use the function for PI instead.

Try also to separate the pi variable from the other two, since you're giving it a value. But I doubt that's the problem... Worse comes to worse, change the line: area=pi*rad*rad; to area=rad*rad*22/7. Or use 22.0 and 7.0 as values instead of 22 and 7.

2007-01-19 20:58:29 · answer #2 · answered by Jack 3 · 1 1

(I think) when you initialize a float, it does it with integer values.

When you have pi=22/7, it takes 22 and 7 as integers and divides them, and then stores the value 21 in pi.

Try this

float pi, a=22, b=7, rad, area;
pi=a/b;

2007-01-19 20:55:58 · answer #3 · answered by Anonymous · 0 1

Start with System Restore back a few weeks ago. If that doesn't work, go to Microsoft website and download another copy of the Installer.

2016-05-24 00:06:25 · answer #4 · answered by Anonymous · 0 0

try inputting pi=22.0/7.0
it'll work

2007-01-19 20:54:05 · answer #5 · answered by yashwanth 3 · 2 0

fedest.com, questions and answers