C++ real world examples

In this page, two programs are given. In which real-world formulas have been used,

a simple demonstration to find out the simple interest

In this program, we will find a simple interest

As we know that we use its formula to find out the simple interest. like

si = (principle*rate*time/100)

In this formula, we have input period, rate and time from the user.

Here is the program,

``````#include<iostream>
using namespace std;

int main()
{
int si,principal,rate,time;
cout<<"Enter Principal, rate, time: ";

cout<<"\nPrincipal: ";
cin>>principal;

cout<<"Rate(in %) : ";
cin>>rate;

cout<<"Time(days) : ";
cin>>time;

si = (principal*rate*time/100);

cout<<"Simple interest: "<<si;

return 0;
}``````

OUTPUT

``````Enter Principal, rate, time:

Principal : 100
Rate(in %) : 5
Time(days) : 2
Simple interest: 10``````

Explanation:

In the program, we have declared three variables. such as,

``int principal, rate, time, si``

Where, the user has been asked for input for variables period, rate and time. suppose these inputs are

``````principal = 100
rate = 5
time = 2``````

and variable si store these variable’s result,

``si = (principal*rate*time)/100``

means,

``si = (100*5*2)/1005``

And in the last statement, the value of variable si is printed which is 10.

Thus the program is executed successfully

Here is another one program,

C++ convert Fahrenheit degree to celsius

in this program, we convert Fahrenheit degree to celsius

Like the program above, we will use the formula here

``celsius = (farhenheit-32)*5/9;``

Here is the program,

``````#include<iostream>
using namespace std;

int main()
{
int fahrenheit,celsius;

cout<<"Enter Fahrenheit Degree: ";
cin>>fahrenheit;

celsius = (fahrenheit-32)*5/9; // using formula

cout<<"\nIn Celsius: "<<celsius;

return 0;
}``````

OUTPUT

``````Enter Fahrenheit degree: 23
In Celsius: -5``````

Explanation

In the program, we have asked the user to enter the degree Fahrenheit. Which will be stored in the variable Fahrenheit, such as,

``fahrenheit = 23``

After this we have used the formula to get celsius degree, so

``celsius = (23-32)*5/9``

And in the last statement, the value of the variable calsius is printed.

Similarly in C++, we can also create many more programs where we can use the formulas.

here is another example where we solve a 10th class problem arithmetic progression program in C++

C++ Examples list

Like it?