I have an exercise again and I got blocked again. The exercise is:
Daphne invests $100 at 10% simple interest. That is, every year, the investment earns
10% of the original investment, or $10 each and every year:
interest = 0.10 × original balance
At the same time, Cleo invests $100 at 5% compound interest. That is, interest is 5% of
the current balance, including previous additions of interest:
interest = 0.05 × current balance
Cleo earns 5% of $100 the first year, giving her $105. The next year she earns 5% of
$105, or $5.25, and so on. Write a program that finds how many years it takes for the
value of Cleo’s investment to exceed the value of Daphne’s investment and then displays
the value of both investments at that time.
And I wrote this code that is not working:

#include<iostream>
using namespace std;
int main()
 {
      int deposit1, deposit2, interest1 = 10, interest2= 5,  sum,sum1, sum2, year =1;
      cout<< " Enter your deposit for first choice "<< endl;
      cin>> deposit1;
      cout<< " Enter your deposit for the second choice "<< endl;
      cin>>deposit2;
      sum1 = deposit1 * interest1/100 + deposit1;
      sum2 = deposit2 * interest2/100 + deposit2;
      sum = sum2 * interest2 + sum2;
       if(sum1!=sum)
       {
                     year++;
                     }
       while(sum1 != sum)
       {
                  sum1++;
                  sum++;
                  }
       while (sum1 == sum)
       {
             cout<<year;
             }     
              
   system ("pause");
   return 0;
}

Recommended Answers

All 8 Replies

First thing, don't use system("pause").

Why sum1==sum, the question says when the investment of Celio exceeds Daphne's.
Also in loop, you are just incrementing sum1 and sum... do you think that's correct?

The only thing that I do not know is how to increment sum2. That's why I created the variable sum.

I am still just learning C++ so my observations may not be the best. It seems to me that you are using the wrong type for your variables. You are using integers. But when you divide the interest rate by 100 yo get a decimal that is less than one, NOT an integer. You need to change the type to float. In addition, your loop is probably infinite because you will never get an exact equality. The better exit condition is (simple investment - compound investment) < precision amount.

My field is business and I know finance. You are calculating the 'sums' incorrectly. The compound investment is: future balance = (original balance + interest rate) raised to the power of number of years. The simple investment is: future balance = original balance + (original investment * interest rate) * number of years.

N.B. In line 12, you haven't divided interest2 by 100 to make it a rate. As Vidit noted, you are not incrementing by the year.

In simple interest, you need to add fixed interest to the sum1 every time in the loop.

sum1=deposit1*interest1/100 + sum1;

In compound interest, you need to calculate interest every time in the loop and add it to the sum.

sum=sum*interest2/100 + sum;

Don't just increment it, you are just adding 1 to the sum1 and sum every time in the loop.

Thinking about this more, I would not have the sums at all.

while ((simple - compound) < 1.00) {
   simple = deposit1 + deposit1 * interest1/100 * years;
   compound = deposit2 * pow(1.00 + interest2/100, years);
   years++;
}

I tried also this but is still doesn't work

#include<iostream>
 #include<cmath>
using namespace std;
int main()
 {
      double deposit1, deposit2, interest1 = 10, interest2= 5,  simple, compound, years =1;
      cout<< " Enter your deposit for first choice "<< endl;
      cin>> deposit1;
      cout<< " Enter your deposit for the second choice "<< endl;
      cin>>deposit2;
       simple = deposit1 + deposit1 * interest1/100 * years;
    compound = deposit2 * pow(1.00 + interest2/100, years);
    years++;
     while (simple > compound) 
     {
        simple++;
        compound++;
        years++;
}

   cout<<years<<endl;

   system ("pause");
   return 0;
}

Why have you marked this thread as solved if your program doesn't yet run correctly? I ran a version of your program and it worked for most values of deposits.

int main()
{
double deposit1 = 0;
double deposit2 = 0;
double simple = 10;               // The two methods must have a difference large 
double compound = 0;              // enough to exceed the loop condition.
double interest1 = 0.10;
double interest2 = 0.05;
double years = 1;

cout << "Enter your deposit for first choice."<< endl;
cin >> deposit1;
cout<< "Enter your deposit for the second choice."<< endl;
cin >> deposit2;

while (abs(simple - compound) < 5.00) {
   simple = deposit1 + deposit1 * interest1 * years;
   compound = deposit2 * pow((1.00 + interest2), years);
   ++years;
}
cout <<"The simple amount is " << simple <<".\n";
cout <<"The compound amount is " << compound <<".\n";
cout <<"The time period is " <<years <<" years.\n";

keep_window_open();
return 0;
}

Because of the interest rates you have chosen, deposit2 should be larger than deposit1. Otherwise, the compound method might not catch up to the simple method quickly enough and the loop may end up infinite.

Sorry but I marked because I found the solution but I didn't mention it

#include<iostream>
 #include<cmath>
using namespace std;
int main()
 {
      double deposit1, deposit2, interest1 = 10, interest2= 5,  simple = 0, compound = 0, years=1;
      cout<< " Enter your deposit for first choice "<< endl;
      cin>> deposit1;
      cout<< " Enter your deposit for the second choice "<< endl;
      cin>>deposit2;
     simple=deposit1;
     compound=deposit2;
    while(simple > compound) 
     { 
                  simple = deposit1 + deposit1 * interest1/100 * years;
    compound = deposit2 * pow(1.00 + interest2/100, years);
                
                  years++;
    
        }

   cout<<years<<endl;


   system ("pause");
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.