# Thread: How to write a percentage in this problem?

1. ## How to write a percentage in this problem?

Hi again,

I almost finished my code for an assignment but I have trouble outputting a percentage. Thanks in advance.

Need the program to output "[number] percent are passing grades.":

We want to count how many passinggrades are entered. We dont know how many grades there will be. Use a sentinel controlled while loop that will ask the user toenter student grades until a value of -1 is entered. Use a counter variable to count all thegrades that are passing grades, where 70 is the minimum passing grade. If there are any grades that are out of therange 0  100, present an error message to the user, and do not count thatgrade as passing. We also would like to see what percentage ofthe valid grades are passing.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main() {
double percentage;

printf("Enter the grades. Enter -1 to stop. \n");
printf("That is not a valid grade. \n");}
{
{
}
else
{
}
else
{
}
}
}

/* Need to add calculation of percentage! */
system("pause");

}``` 2. Have you done a search for how to calculate percentage? It's pretty easy.

You should also ensure your code is neatly formatted and indented:

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
double percentage;

{
printf("Enter the grades. Enter -1 to stop. \n");

{
printf("That is not a valid grade. \n");
}

{
{
}
{
}
else
{
}
}
}

/* Need to add calculation of percentage! */
system("pause");
}``` 3. Thank you for the reply.
I attempted to do what I pasted below, and the percents are off :/ Did I add in the code in the correct places?

Edit: I fixed the indents but its not showing correctly, whelp.

Code:
```
#include <stdio.h>
#include <stdlib.h>

int main()
{
double percentage;

{

// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");

// If the user enters anything lower than -1 or higher than 100, code displays an error.
printf("That is not a valid grade. \n");}
{
{
}
else
{
// Calculates the number of passing grades
percentage = (passingGrades / total) * 100;
}
else
{
// Calculates the total and percentage
}
}
}
// Prints results to user
printf("%.2lf percent are passing grades. \n", percentage);

// printf("%lf percent are passing grades. \n", percentage);
// Need percentage calculation and print results
// percentage = (passingGrades / total) * 100; ?

system("pause");
}``` 4. put this in the matching code, with the added printf to see what your value is wihin that var
Code:
```if (grades <= 100 && grades >= 70)
{
// Calculates the number of passing grades

percentage = (passingGrades / total) * 100;
}
else``` 5. I attached the output, hopefully I did so correctly.

Code:
```#include <stdio.h>
#include <stdlib.h>
int main()
{
double percentage;
{

// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");

// If the user enters anything lower than -1 or higher than 100, code displays an error.
printf("That is not a valid grade. \n");}
{
{
}
else
{
// Calculates the number of passing grades
printf("%i = %i + 1 \n", passingGrades);
percentage = (passingGrades / total) * 100;
}
else
{
// Calculates the total and percentage
}
}
}
// Prints results to user
printf("%.2lf percent are passing grades. \n", percentage);
// Need to fix percentage calculation...

system("pause");
}``` 6. moded
Code:
```\$ ./a.out
\$ ./a.out
Enter the grades. Enter -1 to stop.
70
0 = 547313808 + 1
1 passingG 0, failingG 0
2 passingG 0, failingG 0
Floating point exception
userx@slackwhere:~/bin```
you code
Code:
```#include <stdio.h>
#include <stdlib.h>
int main()
{
double percentage;
passingGrades = 0; // stays 0 no assinging it anything after you

{

// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");

// If the user enters anything lower than -1 or higher than 100, code displays an error.
{
printf("That is not a valid grade. \n");
}
{ //opens here -
{
}
{
// Calculates the number of passing grades
printf("%i = %i + 1 \n", passingGrades);

//stays (0/0) * 100
percentage = (passingGrades / total) * 100;
}
else
{
// Calculates the total and percentage
}
} // closes here :: what need is theses brackets?
} // end while
// Prints results to user
printf("%.2lf percent are passing grades. \n", percentage);
// Need to fix percentage calculation...

system("pause");
}``` 7. So failingGrades isn't getting a value properly? I'm not sure if I follow, I'm sorry. 8. Originally Posted by Kayla Hoyte So failingGrades isn't getting a value properly? I'm not sure if I follow, I'm sorry.
you got the right idea here;
Code:
`  total = passingGrades + failingGrades;`
assigning a value to another variable and adding to it at the same time. But you did not do that with your input first
that gives you that starting point so that the variable called, passingGrades, will even have a value inside of it to pass it into
the next variable you are using called, total. you missed your first very important step.

in other words,
what variable are you using to get the starting value in the first place? you are not using that one to pass it to any other variable
to keep its value and do the math on it. Your percentage calculation should be after the loop, when all the values are finished being entered. 10. I think I understand userxbw, and thank you Matticus. So passingGrades will start at 0 and so will failingGrades. total is failingGrades added to passingGrades. percentage is passingGrades divided by total then timed by one hundred.

My program isn't running correctly --- typing -1 does not stop from entering values. I think the problem is at line 23? I am really bad at figuring this stuff out, jesus.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()
{
double percentage;
{
// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");

// If the user enters anything lower than -1 or higher than 100, code displays an error.
printf("That is not a valid grade. \n");
}
{
{
}
else
{
// Calculates the number of passing grades
}
else
{
}
{
}
}
}
// Calculates the total and percentage
percentage = (passingGrades / total) * 100;
// Prints results to user
printf("%.2lf percent are passing grades. \n", percentage);
system("pause");
}``` 11. Originally Posted by Kayla Hoyte I think I understand userxbw, and thank you Matticus. So passingGrades will start at 0 and so will failingGrades. total is failingGrades added to passingGrades. percentage is passingGrades divided by total then timed by one hundred.

My program isn't running correctly --- typing -1 does not stop from entering values. I think the problem is at line 23? I am really bad at figuring this stuff out, jesus.
Your indentation and bracket use are way off. It is hard to find a problem when reading the code becomes difficult.
Your problem lies in line 37.

Here's the code properly indented:
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
double percentage;

{
// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");

// If the user enters anything lower than -1 or higher than 100, code displays an error.
{
printf("That is not a valid grade. \n");
}

{
{
}
{
// Calculates the number of passing grades
}
{
}
{
}
}
}
// Calculates the total and percentage
percentage = (passingGrades / total) * 100;

// Prints results to user
printf("%.2lf percent are passing grades. \n", percentage);
system("pause");

return 0;
}```
You should also have else ifs for every case of 'grades', so that when one if statement is valid it doesn't keep checking. (If grades is > 100 or < -1 it is clearly never failing nor passing nor -1).

Also, make sure of compiling with -Wall, main returns an int, but there is no return in the function. This is made clear by the compiler if you compile with -Wall.

Also it is:
Code:
`int main(void)`
rather than
Code:
`int main()`
since it is a function definition. 12. shouldn't grades be from 0 to 100 0 = F, fail 100 = 'A' pass. so anything between is an b c d , therefore take in what ever number up to 100, then total/how many taken in gives average, so do the math for percent. because the numbers being taken in represent a percent. yes?

Code:
```If you want to know what percent A is of B,
you simply divide A by B, then take that number
and move the decimal place two spaces to the right.
Code:
```printout
input percentages, hit q to printout total percentage gained```
that is the basic set up. right? 13. this does not complete your code, but I hope it helps you in writing it more logically.
Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
double percentage;

{
// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");

// If the user enters anything lower than -1 or higher than 100, code displays an error.
// no need to check for both
// just check for over 100
// your while uses -1 to quit it will check that value then quit.
{
printf("That is not a valid grade. \n");
}

{
// is there such a grade as negative 1 ?
{
} // it looks like your spliting them into two separate
// categories 70 and less fail, over 70 pass. one check
// is all you need to do, the rest automatically goes
// to the other batch
// if one thing put them over there
// else it is the other put them somewhere else.
//
// or it is one way or the other.
//
// if the magic number or greater then put them in that
//pile. else put them in the other pile.
//
{
// Calculates the number of passing grades
}
{
}
{
}
}
}
// Calculates the total and percentage
percentage = (passingGrades / total) * 100;

// Prints results to user
printf("%.2lf percent are passing grades. \n", percentage);
system("pause");

return 0;
}```
oh yeah, way are you pausing the system when you're quitting the program?

this is what I am getting after I fixed it to my specs, I do not know what yours are, but I think I got them right.
I did not mess with your math computations.
Code:
```userx@slackwhere:~/bin
Enter the grades. Enter -1 to stop.
30
Enter the grades. Enter -1 to stop.
30
Enter the grades. Enter -1 to stop.
43
Enter the grades. Enter -1 to stop.
50
Enter the grades. Enter -1 to stop.
100
Enter the grades. Enter -1 to stop.
100
Enter the grades. Enter -1 to stop.
73
Enter the grades. Enter -1 to stop.
-1
total 8
just for a little , just so you know,
Code:
`  passingGrades += 1;  //  passingGrades + 1;`
their is a different way you can add them up as well. 14. (I should note that I am using Dev-C++)

I fixed some of the issues I made. I tested and saw that when I put in two, three, four numbers that are passing it displays the percentage correctly (at 100 as expcted). Anything else does not compute the percentage. I still do not see the issue.

I apologize for testing your patience.

Code:
```#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double percentage;

{
printf("Enter the grades. Enter -1 to stop. \n");
{
printf("That is not a valid grade. \n");
}
else
{
{
}
{
}
else
{
}
}
}

percentage = ((passingGrades / total) * 100);

printf("%.2lf percent are passing. \n", percentage);

return 0;
}``` 15. Originally Posted by Kayla Hoyte (I should note that I am using Dev-C++)

I fixed some of the issues I made. I tested and saw that when I put in two, three, four numbers that are passing it displays the percentage correctly (at 100 as expcted). Anything else does not compute the percentage. I still do not see the issue.

I apologize for testing your patience.

Code:
```#include <stdio.h>
#include <stdlib.h>
int main(void)
{
// change everything to use float, or double
// then format it properly  everywhere it is applicable
double percentage;

{
printf("Enter the grades. Enter -1 to stop. \n");
{
printf("That is not a valid grade. \n");
}
// invalid code, because it never gets used,
// what else is using -1
else
{
{
}
{
}
else
{
}
}
}

percentage = ((passingGrades / total) * 100);

printf("%.2lf percent are passing. \n", percentage);

return 0;
}```
Code:
```failingGrades 4.000000
total 9.000000
Code:
```.....// you already have preventive measures to not get
// anything over 100 set prior to getting to this code. so....

if (grades  equal to or are greater than magic number )
{
what ever is the magic number or greater gets added here
}
else
{
apply process of elimination
so the rest just get added here.
}
........```
worse yet,
Code:
`grades <= 100 && grades >= 70`
a number less than or equal to 100 get taken, and a number greater than or equal to 70 get taken.

so what is left between 0 thru 100?

just a few little fixes and can end up with it looking like this
Code:
```-1
total 9 Popular pages Recent additions count, grades, passing, passinggrades, percentage 