Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5Topics
    |
    6 Quizzes
  3. C++: Variables
    9Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8Topics
    |
    9 Quizzes
  7. C++: Vectors
    4Topics
    |
    3 Quizzes
Lesson 3, Topic 8
In Progress

C++: Test

Yasin Cakal 6 Nov 2021
Lesson Progress
0% Complete

Now that we know the basics of calculations in C++, let’s attempt some examples.

A teacher wants to calculate the percentages of students’ marks in a test to calculate their grades. The test was a total of 53 marks. She wants to make a calculator where she will enter the marks of one student and the program calculates the percentage of the marks for her to enter in the mark sheet.

The formula for calculating percentages for a test with total marks 53 is:

Percentage = (obtained marks/53)*100

Let’s write a program for this step by step:

  • First, we have to declare a double type variable named perc (for percentage) to enable the program to give outputs in decimal points.
  • We also have to declare an double type variable obt_marks to receive the input of the obtained marks by the teacher.
  • We will write a line to instruct the teacher to enter the obtained marks.
  • We will write a statement in the code to input the value of obt_marks from the teacher.
  • The next step is to calculate the percentage and assign the value to perc.
  • Now we have to output a line to help user identify that the value displayed is the calculated percentage.
  • Now output the calculated percentage.

Let’s solve the program for you:

#include <iostream>

int main() 
{
	double obt_marks;
	double perc;
	std::cout<<"Enter the marks of the student: \n";
	std::cin>>obt_marks;
	perc = (obt_marks/53)*100;
	std::cout<<"The percentage for the student's marks is: "<<perc<<"%.";
	return 0;
}

Output:

Enter the marks of the student: 
43
The percentage for the student's marks is: 81.1321%.

Things to Remember: Declaring different variable types and then using them in the same formula will give inaccurate results or errors.

Responses

Leave a Reply

%d bloggers like this: