Question Need help / advice with a loop

gravitycat5000

New member
Joined
Oct 26, 2020
Messages
4
Programming Experience
Beginner
How would I make a loop in which the user enters a number, that number is then multiplied by lets say 1, the results are outputted then it is then multiplied by 2 then the results are outputted then multiplied by 3, etc. etc. ?

I'm having trouble figuring out how I would go about doing this any advice / help is appreciated!
 
What have you tried so far? What have you thought about?
 
Welcome to the forums.

Write some code, and come back and tell us what is not working as intended. You need to show what you've tried. We are not a code writing service.
 
So i've figured out how do what i want, just not in like loop system for the bottom part.
What i want to do is have the user input a number and then see how much that number could be with various %'s of taxes. so ive got what what ive wanted it to do with this, but not in the way i want it, ya know?
 
Please post your code in code tags and post your code as text, and not as a screenshot. I've removed the attachment. We can not copy text from a screenshot without rewriting it. Which is illogical.

We will be happy to help you once you post your code using code tags. [CODE=csharp] Your code here [/CODE]

Thank you
 
C#:
using System;
using static System.Console;

namespace loop
{
    class Program
    {
        static void Main(string[] args)
        {
            string answer;
            double num;
            double tax;
            double markup;
            double price;
 

            WriteLine("Please enter a price");
            answer = ReadLine();
            while (double.TryParse(answer, out num) == false)
            {
                WriteLine("Oops, Thats not a numeric value");
                WriteLine("Please try again");
                answer = ReadLine();
            }

            tax = 0.05;
            markup = tax * num;
            price = markup + num;
            WriteLine("Marked up by 5% would be {0:C}", price);


            tax = 0.06;
            markup = tax * num;
            price = markup + num;
            WriteLine("Marked up by 6% would be {0:C}", price);

            tax = 0.07;
            markup = tax * num;
            price = markup + num;
            WriteLine("Marked up by 7% would be {0:C}", price);

            tax = 0.08;
            markup = tax * num;
            price = markup + num;
            WriteLine("Marked up by 8% would be {0:C}", price);

            tax = 0.09;
            markup = tax * num;
            price = markup + num;
            WriteLine("Marked up by 9% would be {0:C}", price);

            tax = 0.10;
            markup = tax * num;
            price = markup + num;
            WriteLine("Marked up by 10% would be {0:C}", price);
            ReadKey();


        }
          
    }
}

not sure if that worked, but there ya go
 
So what is keeping you from using a loop?
 
Im not sure how i would go about formatting it / writing the loop to begin with. Because im not quite sure how i would write something like a tax of like 0.05 to increase by like 0.01 every go around, and then display the result before doing it (going up by 0.01) if i could figure that part out i could probably figure out how to use the user given number to get multiplied fairly easily. but thats where im stumped
 
Because im not quite sure how i would write something like a tax of like 0.05 to increase by like 0.01 every go around
You don't know how to add a value to a variable? If you want something to happen before looping, e.g. initialise a variable, then you write the code to do it before the loop. If you want something to happen on each iteration of the loop, e.g. incrementing a variable, then you write the code to do it inside the loop. If you want something to happen after looping, e.g. displaying the final output, then you write the code to do it after the loop. Simple.

Now you need to think first, BEFORE writing the code, what do you want to happen before the loop, what do you want to happen on each iteration of the loop and what do you want to happen after the loop. If you're still not sure, pick up a pen and paper and do it manually first. You're allowed to do that. You don't have to go straight from an idea in your head to code on the screen. You're allowed to write down the logic and formalise it into an algorithm on paper first.
 
@gravitycat5000 : I suspect that the issue is because you are trying to do everything as a big bang rather than working incrementally. In programming, it's best to work in small incremental steps instead of trying to do everything in one go. Write a little code. Make it work correctly. Save a copy, or better yet, check it into source control. Add another thing, make it work, check it in. Lather. Rinse. Repeat.

To get you started consider the following loop that counts from 5 to 25:
C#:
for(int i = 5; i < 26; i++)
     Console.WriteLine(i);

Now what if we wanted it to print out values from 0.05 to 0.25? In the loop we'll have to compute a corresponding value:
C#:
for(int i = 5; i < 26; i++)
{
     decimal value = 5 / 100.0m;
     Console.WriteLine(value);
}

We can do better. Why compute the values on the inside when we can just do the values as part of the loop instead?
C#:
for(decimal value = 0.05m; value < 0.26m; value += 0.01m)
     Console.WriteLine(value);

Notice the incremental steps above that get us from point A to point C.
 

Latest posts

Back
Top Bottom