(wL) Forums
C programming failed... - Printable Version

+- (wL) Forums (https://war-lords.net/forum)
+-- Forum: Resources (https://war-lords.net/forum/forum-31.html)
+--- Forum: Programming (https://war-lords.net/forum/forum-33.html)
+--- Thread: C programming failed... (/thread-9285.html)

Pages: 1 2 3


C programming failed... - Danger255 - Nov 27 2012

Hey guys I was learning c language then I dont know what's gone wrong here is my program
Code:
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5,per;
printf("Enter the marks of five subjects\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
per=(m1+m2+m3+m4+m5)/500*100;
if(per>=80)
printf("First Division\n");
else if(per>=60)
printf("Second Division\n");
else if(per>=50)
printf("Third Division\n");
else
printf("Fail");
return 0;
}
So now the problem is when I enter marks like 98,99,88,87,79. These are above 80% still it shows fail:mad: WHY??:confused:


RE: C programming failed... - Spartacus - Nov 27 2012

What is your program supposed to do exactly?


RE: C programming failed... - Danger255 - Nov 27 2012

Its a simple program to calculate the percentage of 5 subjects and then display the rank or class i.e 1st division etc etc


RE: C programming failed... - Spartacus - Nov 27 2012

your "if else" statements looks weird.

And you could just divide by 5 instead of /500 *100...

PS: my bad, nothing wrong with your if else...just your per equation...you need to bracket the whole equation.


RE: C programming failed... - Danger255 - Nov 27 2012

if u saying something like this that
Code:
per=(m1+m2+m3+m4+m5)/500*100;/CODE] convert this into
[FONT=Consolas][CODE](per=(m1+m2+m3+m4+m5)/500*100);
[/FONT]


RE: C programming failed... - Spartacus - Nov 27 2012

Code:
per=((m1+m2+m3+m4+m5)/500*100));



RE: C programming failed... - Danger255 - Nov 28 2012

Spartacus, post: 80988, member: 1060 Wrote:
Code:
per=((m1+m2+m3+m4+m5)/500*100));
I'll try and reply to thanks for helping me .......YOU ARE Cool!!


RE: C programming failed... - M. Bison - Nov 28 2012

98+99+88+87+79 = 451 which divided by 500 is less than 1. The datatype is an int which means anything beyond the integer is dropped, making it 0. The result is 0 * 100 = 0.

Try this:
Code:
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5,per;
printf("Enter the marks of five subjects\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
per=(int)(((float)(m1+m2+m3+m4+m5))/500.0*100.0);
if(per>=80)
printf("First Division\n");
else if(per>=60)
printf("Second Division\n");
else if(per>=50)
printf("Third Division\n");
else
printf("Fail");
return 0;
}

PS: If you intend on rounding to the nearest integer, add 0.5 before the cast to int.


RE: C programming failed... - Spartacus - Nov 28 2012

M. Bison, post: 81035, member: 359 Wrote:98+99+88+87+79 = 451 which divided by 500 is less than 1. The datatype is an int which means anything beyond the integer is dropped, making it 0. The result is 0 * 100 = 0.

Try this:
Code:
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5,per;
printf("Enter the marks of five subjects\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
per=(int)(((float)(m1+m2+m3+m4+m5))/500.0*100.0);
if(per>=80)
printf("First Division\n");
else if(per>=60)
printf("Second Division\n");
else if(per>=50)
printf("Third Division\n");
else
printf("Fail");
return 0;
}

I kneel to you O Great Hax King.

Btw Bison is right, i just divided by 5 and did not receive that error.

Man I'm rusty. keep the questions coming!


RE: C programming failed... - Dr. gkovr - Nov 28 2012

Ahh that mistake always screwed me over, the int type truncates any decimal. Solve by adding decimals and type casting to turn it into a value that accepts decimals such as a float or double, which is what bison did :p