loan calc

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

loan calc

Post by Face » Fri Nov 12, 2010 10:37 pm

I made a simple loan calc in C++.to calculate the premium for 3 loans.
here it is(I am still using IF only in my coding,because I am a beginner in C++)

Code: Select all

//My testing program in loan system

#include<iostream>

using namespace std;

int main()
{
     int loan,amount,time,premium,intrest;
     
     cout<<"Welcome to loan system made by G-sparkZ"<<endl<<endl;
     
     cout<<"loan code numbers"<<endl<<endl;
     cout<<"Housing loan   = 1"<<endl;
     cout<<"govenment loan = 2"<<endl;
     cout<<"forces loan    = 3"<<endl<<endl; 
     
     cout<<"Please enter your loan code - "<<endl;
     cin>>loan;
     if(loan==1)
     {cout<<endl<<"Normal Housing loan calculation Begings"<<endl<<endl;
     cout<<"Enter your loan amount - ";
     cin>>amount;
     cout<<endl<<"Enter your loan paying duration (years) - ";
     cin>>time;
     intrest=amount*14/100;
     time=time*12;
     premium=(amount+intrest)/time;
     cout<<endl<<"your Premium is "<<premium<<" per month "<<endl;
             }
             else if(loan==2)
                {cout<<endl<<"Govenment Housing loan calculation begins"<<endl<<endl;
                 cout<<"For govenment workers only"<<endl;
                 cout<<"Enter your loan amount - ";
                 cin>>amount;
                 cout<<endl<<"Enter your loan paying duration (years) - ";
                 cin>>time;
                 intrest=amount*9/100;
                 time=time*12;
                 premium=(amount+intrest)/time;
                 cout<<endl<<"your Premium is "<<premium<<" per month "<<endl;
                     }
                     else if(loan==3)
                     {cout<<"Speacial Forces loan calculation begins"<<endl<<endl;
                     cout<<"For Aramy,Air force,Navy,Police"<<endl;
                     cout<<"Enter your loan amount - ";
                     cin>>amount;
                     cout<<endl<<"Enter your loan paying duration (years) - ";
                     cin>>time;
                     intrest=amount*7/100;
                     time=time*12;
                     premium=(amount+intrest)/time;
                     cout<<endl<<"your Premium is "<<premium<<" per month "<<endl;
                          }
                          else
                          {cout<<"wrong code entered please try again"<<endl<<endl;
                          system("pause");
                          return 1;
                              }
     system("pause");
     return 1;
     }
     
     
I wanna make some changes in it.

1.I wanna add some values that user can Input.
Ex-Normal housing loan
amount 100 000 & 200 000 only,Time 1 or 2 years.

government housing loan
amount 100 000 , 200 000 , 300 000 only,Time 1 - 3 years.

forces loan
amount 100 000 , 200 000 , 250 000 only,Time 1 - 3 years.

2.If user input wrong amount or time I wanna ask my question again "enter your loan amount" or "Enter your loan paying duration" again.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: loan calc

Post by Nipuna » Sat Nov 13, 2010 1:45 pm

Cool Program Bro.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: loan calc

Post by Face » Sun Nov 14, 2010 6:08 pm

Thanks Machoo I wanna make some changes.I am waiting to NEO.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: loan calc

Post by Nipuna » Sun Nov 14, 2010 7:39 pm

I would help You If I Knew More. I didn't have time to learn after Global Variables. I learned till Global Variables.

I got two Freelancer Projects and I am Doing those. AS Soon As I finish them then I will start studying C++ and PHP.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: loan calc

Post by Neo » Mon Nov 15, 2010 11:17 am

Good progress.... :)
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: loan calc

Post by Face » Mon Nov 15, 2010 5:55 pm

Neo can you give me to make those changes.I wanna make some conditions for those amounts in my loan amounts.I show them in above post.Can you tell me how to make those conditions.Other thing is I wanna restart my calc after calculating one loan premium.I wanna make a exit code to my program.
G-sparkZ
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: loan calc

Post by Neo » Mon Nov 15, 2010 6:48 pm

ohh I see... now only I realised that you need help....

Regarding loan types... you are correct with "if", "else if" code blocks. You can also replace if, else if blocks with switch, case block.

Code: Select all


// get loan type from user to "loan"

switch (loan) {
  case 1:
    // your code here for Loan type 1

    break;

  case 2:
    // your code here for Loan type 2

    break;

  case 3:
    // your code here for Loan type 3

    break;

  default:
    // your code here for Invalid option

    break;


    break;
}
Regarding restarting your code.... there is a method called unconditional jump where you can define a label and jump to that using "goto" keyword. But it is not a good practice use unconditional jumps in a code.
Ex:

Code: Select all

mylabel:
    // some code here


    // some code here

    if (myCodition == 0){     // if your condition is true then jump to start of program
         goto mylabel:
    }

Recommended method is to use an infinite while loop.

Code: Select all

while (1){
       // get loan type

       if (loan == 1){
             // your code here

             break;     // this will make your code to go out from main "while" loop
       }
       else if (loan == 2){
             // your code here

             break;     // this will make your code to go out from main "while" loop
.......
.......
.......
        else{
               // invalid input
               continue;    // this will make the while loop to restart
        }
} // end of while loop

// end of code. thanking message...

User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: loan calc

Post by Face » Mon Nov 15, 2010 7:14 pm

Thanks NEo.I will re correct it with WHILE LOOP.But I need another help to make conditions to apply loan amount.
Can you help me on it.I wanna make ranges to loan amount.I mentioned them in my first post.
1.I wanna add some values that user can Input.
Ex-Normal housing loan
amount 100 000 & 200 000 only,Time 1 or 2 years.

government housing loan
amount 100 000 , 200 000 , 300 000 only,Time 1 - 3 years.

forces loan
amount 100 000 , 200 000 , 250 000 only,Time 1 - 3 years.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: loan calc

Post by Neo » Mon Nov 15, 2010 7:17 pm

Use something like

Code: Select all

if (100000 <= loan_amount  && loan_amount <= 200000){
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: loan calc

Post by Face » Mon Nov 15, 2010 7:20 pm

Bro I made that while loop correct.I will make those conditions.. :D happy
thanks

Code: Select all

    //My testing program in loan system

    #include<iostream>

    using namespace std;

    int main()
    {
         int loan,amount,time,premium,intrest;
         
         cout<<"Welcome to loan system made by G-sparkZ"<<endl<<endl;
         
         cout<<"loan code numbers"<<endl<<endl;
         cout<<"Housing loan   = 1"<<endl;
         cout<<"govenment loan = 2"<<endl;
         cout<<"forces loan    = 3"<<endl<<endl;
         
         while(1){
         cout<<"Please enter your loan code - "<<endl;
         cin>>loan;
         if(loan==1)
         {cout<<endl<<"Normal Housing loan calculation Begings"<<endl<<endl;
         cout<<"Enter your loan amount - ";
         cin>>amount;
         cout<<endl<<"Enter your loan paying duration (years) - ";
         cin>>time;
         intrest=amount*14/100;
         time=time*12;
         premium=(amount+intrest)/time;
         cout<<endl<<"your Premium is "<<premium<<" per month "<<endl;
         break;
                 }
                 else if(loan==2)
                    {cout<<endl<<"Govenment Housing loan calculation begins"<<endl<<endl;
                     cout<<"For govenment workers only"<<endl;
                     cout<<"Enter your loan amount - ";
                     cin>>amount;
                     cout<<endl<<"Enter your loan paying duration (years) - ";
                     cin>>time;
                     intrest=amount*9/100;
                     time=time*12;
                     premium=(amount+intrest)/time;
                     cout<<endl<<"your Premium is "<<premium<<" per month "<<endl;
                     break;
                         }
                         else if(loan==3)
                         {cout<<"Speacial Forces loan calculation begins"<<endl<<endl;
                         cout<<"For Aramy,Air force,Navy,Police"<<endl;
                         cout<<"Enter your loan amount - ";
                         cin>>amount;
                         cout<<endl<<"Enter your loan paying duration (years) - ";
                         cin>>time;
                         intrest=amount*7/100;
                         time=time*12;
                         premium=(amount+intrest)/time;
                         cout<<endl<<"your Premium is "<<premium<<" per month "<<endl;
                         break;
                              }
                              else
                              {cout<<"wrong code entered please try again"<<endl<<endl;
                              system("pause");
                              continue;
                                  }
                                  }
         system("pause");
         return 1;
         }
         
         
Post Reply

Return to “C/C++ Programming”