# Thread: Totaling up numbers in a for loop?

1. ## Totaling up numbers in a for loop?

I'm attempting another simple calculation program... a bank program to be specific. It asks how many deposits does the user want, user inputs x... program asks to input x amount of deposits. Same goes for withdraws.

What I can't figure out is that I want the program to display totals of a deposit. Here's what I have so far
Code:
```printf ("\nPlease enter your beginnnng balance:  \$");
scanf ("%f", &beg_bal);
printf ("\nYour beginning balance is:  \$%-10.2f", beg_bal);
cur_bal = beg_bal;
printf ("\n\nHow many deposits will you be making? ");
scanf ("%d", &depamt);

for (index = 1; index <= depamt; index++)
{
printf ("\nPlease enter a deposit: \$");
scanf ("%f", &deposit);
printf ("\nYou have deposited     : \$%6.2f", deposit);
cur_bal+=deposit;
printf ("\nYour current balance is: \$%6.2f\n", cur_bal);
}```
So basically I have that... but after that, I want to be able to display something like:

"You have made x deposits which totals \$x.xx."

I'd do something like:
printf ("You have made %d deposits which totals \$%f", depamt, ...);

But I've no idea what I can use for the "...". I'm guessing I can do some kind of a foruma for the for loop body but I've no idea what to do.

Since this is a for loop, it'd be dumb to include a float variable for each deposit... any way to accomplish this? Thanks.

2. I think if you just created a single new variable (cur_deposits) or something like that, and set it to zero before the loop, then add a statement in the loop like

cur_deposits = cur_deposits + deposit

then when you exited the loop, it would contain the total deposited...?

3. printf ("You have made %d deposits which totals \$%f\n", depamt, cur_bal-beg_bal );

Perhaps....

4. Your problem looks just like an on-going project I have in my "Introduction to C Programming" class. It involves asking for a current balance, amount of monthly payment, and the annual interest rate.

Every evolution of the project involves changing the way the information is input by the user and crunched by the program. The last one was using one dimensional arrays to store, crunch, and display the numbers received.

Popular pages Recent additions