Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C programming failed...
#1
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:
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#2
What is your program supposed to do exactly?
be the best version of yourself, that's all you can do.
#3
Its a simple program to calculate the percentage of 5 subjects and then display the rank or class i.e 1st division etc etc
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#4
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.
be the best version of yourself, that's all you can do.
#5
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]
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#6
Code:
per=((m1+m2+m3+m4+m5)/500*100));
be the best version of yourself, that's all you can do.
#7
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!!
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#8
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.
Steam Wrote: 4:02 PM - George, of the jungle: was out
4:02 PM - George, of the jungle: bison, dude
4:02 PM - Brawl Bashin’ Bison: ???
4:02 PM - George, of the jungle: you're very rude towards alina
4:02 PM - George, of the jungle: how about unbanning her friend?
4:02 PM - George, of the jungle: I mean
4:02 PM - George, of the jungle: it's only gamebanana skins
4:02 PM - Brawl Bashin’ Bison: LOL
4:02 PM - George, of the jungle: ^^
4:02 PM - Brawl Bashin’ Bison: LOLOL
4:02 PM - George, of the jungle: lol
#9
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!
be the best version of yourself, that's all you can do.
#10
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
[Image: sigix.png]

Users browsing this thread: 2 Guest(s)