الأربعاء، 4 فبراير 2015

C++ Program : calculating the periodic payments and the remaining balance of a loan

When you borrow money to buy a house, a car , or for some other purpose, then you typically repay it by making periodic payments. Suppose that the loan amount is L, r is the interest rate per year, m is the number of payments in a year, and the loan is for t years. Suppose that i =  ( r / m ) and r is in decimal. Then the periodic payment is: 



you can also calculate the unpaid loan balance after making cretin payments. For example the unpaid balance after making k payments:


Where R is the periodic payment.( Note that if the payments are monthly, then m =12).

source : Malik, D. (2009). User-defined functions 1. In C programming: From problem analysis to program design (4th ed.). Boston, MA: Course Technology.

***************************************************************************************************************************************************************************************************************************************************

                         

a sample run of the program 




The code : 

#include <iostream>
#include <cmath>

using namespace std;

void calculate_L(int L, int m, int t, int K, double R, double i)
{
double y;
int E = (m*t) - K;
double  j = (1 - pow(1 + i, -E ));

y = R*(j / i);
cout << " The unpaid balance after " << K << " payments is: " << y << endl;
}

double calculate_R(int L, int m, int t, int k, double i)
{
double R;
int E = -m*t;

R = ((L*i) / (1 - pow(1 + i, E)));

cout<<"The periodic payments is: "<< R ;

return R;
}


void main()
{

int L, m, t, k;
double r, i, R;

cout << " Enter the loan amount ( L ): ";
cin >> L;
cout << endl;

cout << " Enter the interest rate / year ( r ): ";
cin >> r;
cout << endl;

cout << " number of payments / year (m): ";
cin >> m;
cout << endl;

cout << " Enter the loan period ( t ) in years: ";
cin >> t;
cout << endl;

cout << " Enter the number of finished payments ( k ): ";
cin >> k;
cout << endl;

i = r / m;

R = calculate_R(L, m, t, k, i);

calculate_L(L, m, t, k, R, i);

system("pause");
}


الأحد، 25 يناير 2015

C++ Program : Ascending order array using bubble sort

This program takes 10 integers as an input , put them in an array and apply bubble sort algorithm .

The final result will be an array of 10 elements arranged in ascending order.

Sample:
a sample run of the program


The code: 

#include <iostream>

using namespace std;

void main()
{
int Num[10],index;
cout << " Enter five integers: ";
for (int i = 0; i < 10; i++)
{
cin >> Num[i];
}



cout << "The origional order is: " << endl;
for (int i = 0; i<10; i++)
{
cout << Num[i];
cout << endl;
}


for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (Num[j] >  Num[j + 1])
{
index = Num[j];
Num[j] = Num[j + 1];
Num[j+1] = index;
}

}
}

cout << endl;
cout << endl;
cout << "Ascending ordered Array is: " << endl;
for (int i = 0; i<10; i++)
{
cout << Num[i] << endl;
}



system("pause");
}

C++ program : decide if a number is positive,Negative or zero


This program written in C++, asks the user to enter a number , then it will decide if the number is positive , negative or zero.

The Code : 

#include <iostream>

using namespace std;

void main()
{
signed int x;

cout << "Enter an integer ";
cin >> x;
cout << endl;

if (x == 0)
cout << " Zero Number " << endl;
else if (x < 0)
cout << "Negative Number" << endl;
else if (x > 0)
cout << "Positive Number " << endl;

system("pause");
}