Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Average percentage formula is not working

+1 vote
asked Feb 12 by Kamran Shah (130 points)

I am writing a program where student input their name, Id, and five subjects marks, after that out put showing the average percentage of the subjects, but when i divide obtained marks by total marks output is showing zero.

This is my code. i coloured the code with blue font which is making trouble.

#include <iostream>

using namespace std;

int main() {
    // Input Name from student
    string Nm;
    cout << "Enter Your First Name: ";
    cin >> Nm;
    //Input VUID from student
   string VUID;
    cout << "Enter Your VUID: ";
    cin >> VUID;

    
  int Science =100, Maths=100, SST=100, Computer=100, Accounting=100, TtlMrks,Avg; 
  int AllSubMrks=Science+Maths+SST+Computer+Accounting;
  
  cout << "Enter Science Marks: "; // Type a number and press enter
  cin >> Science; // Get user input from the keyboard
  cout << "Enter Maths Marks: "; // Type a number and press enter
  cin >> Maths; // Get user input from the keyboard
  cout << "Enter SST Marks: "; // Type a number and press enter
  cin >> SST; // Get user input from the keyboard
  cout << "Enter Computer Marks: "; // Type a number and press enter
  cin >> Computer; // Get user input from the keyboard
  cout << "Enter Accounting Marks: "; // Type a number and press enter
  cin >> Accounting; // Get user input from the keyboard
  cout << "Student Name: " << Nm <<"\n";
  cout<< "Student VUID: " << VUID<<"\n";
  
  TtlMrks = Science+Maths+SST+Computer+Accounting;
 
  cout << "Obtain Marks: " <<TtlMrks<<"/"<<AllSubMrks<<"\n";
  Avg = TtlMrks/AllSubMrks;
  Avg=Avg*100;
  cout <<"Average Marks:"<<Avg <<"\n";

  if (TtlMrks> 60) {
  cout << "Division: First";
} else if (TtlMrks>=45 && TtlMrks<=60) {
  cout << "Division: Second";

else if (TtlMrks>=33 && TtlMrks<=45) {
  cout << "Division: Second";
} else if (TtlMrks<33) {
  cout << "Division: Fail";

  
    return 0;
}

2 Answers

0 votes
answered Feb 21 by prasanna (140 points)

Hello,

Your code works fine you just had to declare the   Avg ,AllSubMrks ,TtlMrks in float .

And your blue marked area works fine. 

Thank You .

#include <iostream>

using namespace std;

int main() {
    // Input Name from student
    string Nm;
    cout << "Enter Your First Name: ";
    cin >> Nm;
    //Input VUID from student
   string VUID;
    cout << "Enter Your VUID: ";
    cin >> VUID;

    
  int Science =100, Maths=100, SST=100, Computer=100, Accounting=100; 
  float AllSubMrks=Science+Maths+SST+Computer+Accounting,TtlMrks,Avg;      // I just  declared Avg,AllSubMrks,TtlMrks in float 
  cout << "Enter Science Marks: "; // Type a number and press enter
  cin >> Science; // Get user input from the keyboard
  cout << "Enter Maths Marks: "; // Type a number and press enter
  cin >> Maths; // Get user input from the keyboard
  cout << "Enter SST Marks: "; // Type a number and press enter
  cin >> SST; // Get user input from the keyboard
  cout << "Enter Computer Marks: "; // Type a number and press enter
  cin >> Computer; // Get user input from the keyboard
  cout << "Enter Accounting Marks: "; // Type a number and press enter
  cin >> Accounting; // Get user input from the keyboard
  cout << "Student Name: " << Nm <<"\n";
  cout<< "Student VUID: " << VUID<<"\n";
  
  TtlMrks = Science+Maths+SST+Computer+Accounting;
 
  cout << "Obtain Marks: " <<TtlMrks<<"/"<<AllSubMrks<<"\n";
  Avg = TtlMrks/AllSubMrks;
  Avg=Avg*100;
  cout <<"Average Marks:"<<Avg <<"\n";
  if (TtlMrks> 60) {
  cout << "Division: First";
} else if (TtlMrks>=45 && TtlMrks<=60) {
  cout << "Division: Second";

else if (TtlMrks>=33 && TtlMrks<=45) {
  cout << "Division: Second";
} else if (TtlMrks<33) {
  cout << "Division: Fail";

  
    return 0;
}

0 votes
answered Feb 22 by Peter Minarik (55,660 points)
edited Feb 23 by Peter Minarik

Your variable Avg is of type int, i.e. an integral type. It does not support fractional numbers. Use float or double (can hold larger numbers and more precision than float, but takes double the memory) instead, when you calculate your average as it most probably will contain fractional parts as well.

float Avg = (float)TtlMrks / AllSubMrks;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...