Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do i make a program use recursive function to input salary every month and display the output of accumulated salary every month.

example:


Month 1: 10000 (input) accumulated salary: 10000 (output)
Month 2: 5000 (input) accumulated salary: 15000 (output)
Month 3: 7000 (input) accumulated salary: 22000 (output)
Month 4: 8000 (input) accumulated salary: 30000 (output)
...
Month 12:...

What I have tried:

I try use recursive function and loop (while(true)), didn't work (maybe iam wrong).
Posted
Updated 5-Dec-21 20:41pm
v2
Comments
Patrice T 6-Dec-21 0:58am    
Show your work.
Firdaus Nandu 6-Dec-21 1:08am    
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,x,y;

while(true){
cout<<"====================================="<<endl;
cout<<"="" "<<endl;
="" cout<<"month="" 1:="" ";cin="">>a; cout<<"\t\t\tAccumulated: "<<a;
cout<<"\nmonth="" 2:="" ";cin="">>b; cout<<"\t\t\tAccumulated: "<<a+b;
cout<<"\nmonth="" 3:="" ";cin="">>c; cout<<"\t\t\tAccumulated: "<<c+a+b;
cout<<"\nmonth="" 4:="" ";cin="">>d; cout<<"\t\t\tAccumulated: "<<a+b+c+d;
f="a+b+c+d;
" cout<<"\nmonth="" 5:="" ";cin="">>e; cout<<"\t\t\tAccumulated: "<<e+f;
cout<<"\nmonth="" 6:="" ";cin="">>g; cout<<"\t\t\tAccumulated: "<<e+f+g;
cout<<"\nmonth="" 7:="" ";cin="">>h; cout<<"\t\t\tAccumulated: "<<e+f+g+h;
i="e+f+g+h;
" cout<<"\nmonth="" 8:="" ";cin="">>j; cout<<"\t\t\tAccumulated: "<<j+i;
cout<<"\nmonth="" 9:="" ";cin="">>k; cout<<"\t\t\tAccumulated: "<<j+i+k;
cout<<"\nmonth="" 10:="" ";cin="">>l; cout<<"\t\t\tAccumulated: "<<j+i+k+l;
m="j+i+k+l;
" cout<<"\nmonth="" 11:="" ";cin="">>n; cout<<"\t\t\tAccumulated: "<<n+m;
cout<<"\nmonth="" 12:="" ";cin="">>o; cout<<"\t\t\tAccumulated: "<
Patrice T 6-Dec-21 2:06am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Write a function like, for instance
C++
int accumulate_salary(int month, int sum_of_salary)
{
  // insert here the stop condition, returning the accumulated sum of salaries
  // insert here the recursion step, calling the function itself for the next month, with the updated sum of salaries
}


and then, call it in you main function, e.g.
C++
int main()
{
  cout << "sum of salaries " << accumulate_salary(1, 0) << endl;
}
 
Share this answer
 
You can use the salary of 0 as inner abortion criteria. Use a struct or class to store the already computed data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900