hi, I'm trying to calculate the percentage of an ammount of boxes in C. Ex: 4400.
I have the next code:

``````#include<iostream.h>
#include<conio.h>
main()
{
double a=2400;
double b=8000;
double per=0;
per=2400*100/8000;
cout<<per;
getch();
}``````

and return a negative value. I try to asign values at differents variables like:

``````double confused=241541;
double moreconfused=145621;
long int percf=1452145;
cout<<confused<<endl<<moreconfused<<percf;``````

and return negatives values too,
please, somebody can help me?

## Recommended Answers

what compiler are you using? I compiled/ran with vc++ 2008 express and your first program worked ok.

## All 4 Replies

what compiler are you using? I compiled/ran with vc++ 2008 express and your first program worked ok.

Turbo C++ compiler

```````Inline Code Example Here`

main()
{int a,b,c;
printf("Marks obtained = \n");
scanf("%d",&a);
printf("Total marks = \n");
scanf("%d",&b);
c=(int)((a/b)*100);
printf("Percentage = %d",c);
getch();
}
``````

i am calculting the percentage but when i give the values it give me 0 percentage.can anyone give me some help plz.i am using Turbo c++ 3.0

line 9 is doing integer division, which means all declimals are discarded. With int division 1/2 is 0, not 0.5. To correct that you have to promote either numerator or denominator (or both) to float our double.
c=(int)((float)a/b*100.0);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.