+ 2
Just started with c++ today. Can you please help me.
#include <iostream> using namespace std; int main() { int a; int b; int c; cout << "How much did you get for your previous test or exam?\n"; cin >> a; cout << "What was it out of?\n"; cin >> b; c = (a / b) * 100; cout << "Your percentage was:" << c << "â \n"; }
6 Answers
+ 9
#include <iostream>
using namespace std;
int main()
{
int a = 6;
int b = 2;
int c;
cout << "How much did you get for your previous test or exam?\n";
// cin >> a;
cout << "What was it out of?\n";
// cin >> b;
c = (a / b) * 100;
cout << "Your percentage was: " << c << "% \n";
}
+ 4
I've executed your code here, and it's everything ok. maybe the problem is with this '%' sign
+ 3
What's wrong ?
The output ? if it's that, you may just add a space between the percent sign and the \n, like this: cout << "your percentage was: " << c << "% \n";
if you want a float number in the result, you can put the variable c with the data type float instead
+ 3
but there aren't anything wrong
+ 1
I'm guessing your problem is that for any result other than 100%, your output is 0%.
The simplest solution would be to change c's type to float and change the assignment to
c = a * 100.0 / b
otherwise you still have an integer division and c would be 0.0
0
it's not calculating percentage