Formula: Average=Obtain Marks * 100 / Total Marks will be used in this program to find Average of marks in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<iostream> using namespace std; int main() { float total,obt,avg; cout<<"Enter Your Obtained Marks : "; cin>>obt; cout<<"Enter Your Total Marks : "; cin>>total; avg=obt*100/total; cout<<"Your Average is : " <<avg; return 0; } |
This code calculates the average of a student’s marks based on their obtained marks and the total marks of the exam.
Here’s a brief breakdown of what the code does:
The #include<iostream>
line includes the input/output stream library in the program, which allows us to use input/output functions such as cin
and cout
.
The using namespace std;
line allows us to use the standard C++ library without having to specify the std::
prefix for every function.
The int main()
function is the entry point of the program. This is where the program begins executing.
The float
variables total
, obt
, and avg
are declared to store the total marks, obtained marks, and average, respectively.
The program prompts the user to enter their obtained marks by printing the message “Enter Your Obtained Marks : ” on the screen, and then reads the value entered by the user and stores it in the obt
variable using the cin
function.
The program then prompts the user to enter their total marks and reads the value entered by the user, storing it in the total
variable.
The average is calculated by dividing the obtained marks by the total marks and storing the result in the avg
variable.
The average is then printed on the screen using the cout
function.
The return 0;
statement at the end of the main()
function indicates that the program has completed successfully.
Output:
Add two number program
Here is a simple C++ program that adds two numbers and prints the result:
#include
using namespace std;
int main()
{
int a, b, sum;
cout << "Enter two integers: "; cin >> a >> b;
sum = a + b;
cout << "Sum = " << sum;return 0; }This program prompts the user to enter two integers, reads the values entered by the user, and stores them in the variables a and b. The sum of a and b is then calculated and stored in the sum variable. Finally, the sum is printed on the screen using the cout function.I hope this helps! Let me know if you have any questions.