In: Computer Science
How would I write a C++ code that gives me the following out put:
Enter count of male students: 14
Enter count of female students: 17
Students registered
Total: 31
Male: 14 45.16%
Female: 17 54.84%
Enter count of male students: 7
Enter count of female students: 2
Students registered
Total: 9
Male: 7 77.78%
Female: 2 22.22% */
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Declaring Variables
int total, male, female ;
float male_per, female_per;
//Taking inputs like number of male n female
cout << endl;
cout << "Enter count of male students: ";
cin >> male;
cout << "Enter count of female students: ";
cin >> female;
cout << endl;
//calculate total
total = male + female;
//calculate percentage of male n female
male_per = (male / static_cast<float>(total)) * 100;
female_per = (female / static_cast<float>(total)) * 100;
// Printing percentage
cout << setprecision(2) << fixed;
cout << "Students Registered:" ;
cout << "\n total:" << total ;
cout << "\n Male:" << male <<"\t" <<
male_per <<"%";
cout << "\n Female:" << female <<"\t" <<
female_per <<"%"<< ".\n" <<endl ;
// program end
return 0;
}
Snapshot of Code:
Output: