2

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?

Edited by Nick Evan: Added code-tags.

3
Contributors
4
Replies
7
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0
`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

Edited by lll1klll

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);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.