c++ program for inflation rate of two numbers
Sure! Here is a C++ program for calculating the inflation rate of two numbers, along with explanations for each step:
#include <iostream>
using namespace std;
int main() {
float year1, year2, inflationRate;
// Input the values for year 1 and year 2
cout << "Enter the value for year 1: ";
cin >> year1;
cout << "Enter the value for year 2: ";
cin >> year2;
// Calculate the inflation rate
inflationRate = ((year2 - year1) / year1) * 100;
// Output the inflation rate
cout << "The inflation rate is: " << inflationRate << "%" << endl;
return 0;
}
Explanation:
We start by including the necessary header file
iostream
which allows us to perform input and output operations in C++.The
using namespace std;
statement is used to avoid having to writestd::
before certain standard library functions.In the
main()
function, we declare three variables:year1
,year2
, andinflationRate
. These variables will be used to store the values for year 1, year 2, and the calculated inflation rate, respectively.We prompt the user to enter the value for year 1 using the
cout
stream and accept the input using thecin
stream. The value entered by the user is stored in theyear1
variable. Similarly, we prompt the user for the value of year 2 and store it in theyear2
variable.The inflation rate is calculated using the formula:
(year2 - year1) / year1 * 100
. This formula calculates the difference between year 2 and year 1, divides it by year 1, and then multiplies the result by 100 to get the percentage value.Finally, we output the calculated inflation rate to the console using the
cout
stream. Theendl
manipulator is used to insert a newline character after the output.The
return 0;
statement is used to indicate that the program has executed successfully.
I hope this helps! Let me know if you have any further questions.