(wL) Forums

Full Version: C programming failed...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
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:
What is your program supposed to do exactly?
Its a simple program to calculate the percentage of 5 subjects and then display the rank or class i.e 1st division etc etc
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.
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]
Code:
per=((m1+m2+m3+m4+m5)/500*100));
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!!
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.
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!
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
Pages: 1 2 3