Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C programming failed...
#11
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;
}

PS: If you intend on rounding to the nearest integer, add 0.5 before the cast to int.
THANKS about it MAN!!! THANKS AGAIN!!

Double post

M. Bison, post: 81035, member: 359 Wrote:PS: If you intend on rounding to the nearest integer, add 0.5 before the cast to int.
Please can u explain this further?
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#12
Danger255, post: 81078, member: 14867 Wrote:Please can u explain this further?
An integer does not store anything beyond the decimal. That means it always rounds down.

(int)15.9999 = 15
(int)15.4999 = 15
(int)15.5 = 15
(int)15.1 = 15

Real example: 99+99+99+99+98 = 494 / 500 = 0.988 * 100 = 98.8
(int)98.8 = 98

If you intend on rounding to the nearest integer (whole number) then adding 0.5 remedies this situation:

(int)(15.9999 + 0.5) = 16
(int)(15.4999 + 0.5) = 15
(int)(15.5 + 0.5) = 16
(int)(15.1 + 0.5) = 15

Real example: 99+99+99+99+98 = 494 / 500 = 0.988 * 100 = 98.8
(int)(98.8 + 0.5) = 99

Watch out for negative numbers!
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
#13
why not just define the number variables as

float m1,m2,m3,m4,m5,per;
be the best version of yourself, that's all you can do.
#14
Spartacus, post: 81083, member: 1060 Wrote:why not just define the number variables as

float m1,m2,m3,m4,m5,per;
I was thinking like the same.........

Double post

Thanks Bison it worked AND NOW A BIG SALUTE TO PROGRAMMING KING!!Big Grin

Double post

Okay guys moved on to another problem could you get me outta here??
THE CODE:
Code:
#include <stdio.h>
int main()
{
char another;
int num;
do
{
printf("Enter a number");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n");
scanf("%c",&another);
} while(another=='y');
return 0;
}
Now the problem is here
Code:
printf("Enter a number");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n");
scanf("%c",&another);
after this executed
Code:
printf("Square of %d is %d\n",num,num*num);
Nothing HAPPENS IT DOESNT ASK!! Y/N!
I mean the square PROGRAM STOPS!
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#15
Here's what is happening: After inputting the number, you're pressing enter. Enter is a newline character and doesn't actually disappear. The subsequent scanf is using the character to terminate the loop.

The easiest solution is to add a space before %c. I've also added some newlines to cleanup your output.

Try this:
Code:
#include <stdio.h>
int main()
{
char another;
int num;
do
{
printf("Enter a number\n");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n\n");
scanf(" %c",&another);
} while(another=='y');
return 0;
}

Why isn't anyone else replying anymore? Rolleyes
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
#16
M. Bison, post: 81277, member: 359 Wrote:Why isn't anyone else replying anymore? Rolleyes

Because I've been busy and I am unfamiliar with C syntax :p
[Image: sigix.png]
#17
M. Bison, post: 81277, member: 359 Wrote:
Code:
#include <stdio.h>
int main()
{
char another;
int num;
do
{
printf("Enter a number\n");
scanf("%d",&num);
printf("Square of %d is %d\n",num,num*num);
printf("Want to enter another number? y/n\n");
scanf(" %c",&another);
} while(another=='y');
return 0;
}
REALLY MAN A S P A C E can change the world!!:eek:

Thanks Bison thanks once again
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#18
Danger, just remember you can't let Bison keep on doing your homework for you.

You have to figure some things out yourself if you want to learn the language better.

i miss programming..too bad i'm too much of a noob to do anything special or unique.
be the best version of yourself, that's all you can do.
#19
Spartacus, post: 81319, member: 1060 Wrote:Danger, just remember you can't let Bison keep on doing your homework for you.

You have to figure some things out yourself if you want to learn the language better.

i miss programming..too bad i'm too much of a noob to do anything special or unique.
lol I dont have homework I am doing it for my interest.....Rolleyes
[Image: 629965_101.png][Image: dav8ugf75d010z8vovm.gif]
#20
GAHHH CHANGE THE 500*100 TO JUST BE 5!


per=(m1+m2+m3+m4+m5)/5;

that was said like 2 times in this thread then you kept going and making it more and more complicated. the /5 is all you need. why were you doing 500 in the first place?

Users browsing this thread: 8 Guest(s)