Explore BrainMass
Share

C Programming - for and while loops

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Create a program that asks the user to enter a number from 1 to 50.

Print the statement "Programming is fun" the number of times entered by the user using a for loop.

Print the statement "Spring break is fun" the same number of times using a while loop.

© BrainMass Inc. brainmass.com October 25, 2018, 6:27 am ad1c9bdddf
https://brainmass.com/computer-science/c/c-programming-for-and-while-loops-460458

Solution Preview

Please see the attachment <I>loops.c</I> for the complete program.

As usual, we start with the standard empty program:

<I>
#include <stdio.h>

int main()
{

return 0;
}
</I>

Since we have to get a number (between 1 and 50) as input from the user, declare an integer <I>n</I>. Now, we have to use two loops (one after the other), and this means we need a <I>loop counter</I> variable (conventionally named <I>i</I>):

<I>
int n, i;
</I>

Get the input from the user. The problem statement tells us to specify that the number should be between 1 and 50, but nothing is mentioned about actually checking whether this is so. So I have included an if statement for checking whether <I>n</I> is greater than 50, and if so, setting it equal to 50. But I have commented this, in case it is not required. You can either uncomment or delete it.

The general syntax of a for loop is:

<I>
for(</I>initialization expression<I>; </I>condition<I>; </I>update expression<I>)
{
</I>body<I>
}
</I>

Here, <I>initialization expression</I> is a statement that is executed exactly once just before the loop starts; <I>condition</I> is an expression with true/false (that is, zero or non-zero) value that is evaluated at the start ...

Solution Summary

The same task is done using both a for loop and a while loop in C. This illustrates the difference in syntax and the similarity in the functioning of the two loops.

$2.19
See Also This Related BrainMass Solution

Write a C++ program that will tell you how many months it will take you to pay off the loan, as well as the total amount of interest paid over the life of the loan.

You have just purchased a stereo system that costs $1000 on the following credit plan: no down payment, an interest rate of 18% per year (and hence 1.5% per month), and monthly payments of $50. The monthly payment of $50 is used to pay the interest, and whatever is left is used to pay part of the remaining debt. Hence, the first month you pay 1.5% of $1000 in interest. That is $15 in interest. The remaining $35 is deducted from your debt, which leaves you with a debt of $965.00. The next month you pay interest of 1.5% of $965.00, which is $14.48. Hence, you can deduct $35.52 (which is $50-$14.48) from the amount you owe.

Write a C++ program that will tell you how many months it will take you to pay off the loan, as well as the total amount of interest paid over the life of the loan. Use a loop to calculate the amount of interest and the size of the debt after each month.

The final program need not output the monthly amount of interest paid and remaining debt, but you may want to write a preliminary version of the program that does output these values.

The last payment may be less than $50 if the debt is small, but do not forget the interest. If you owe $50, then your monthly payment of $50 will not pay off your debt, although it will come close. One month's interest on $50 is only 75 cents.

View Full Posting Details