Visual logic help

megan1989

Baseband Member
Messages
29
Location
USA
Write a program that inputs a number between 1 and 1000 and displays the number with the appropriate two-letter ending. I have been working on this problem for two days now and am getting nowhere. This is what need to do
FOR 1 to 1000
if the second-last character is 1 (Numbers 10-19) ad "th"
For all others if last character is 1 add "st"
Last character 2 add "nd"
Last Character 3 add "rd"
And for all others add "th"
I am at a loss of figuring out how to input it the information in a way that the program will understand, please help!
 
Have you got any code at all?

Much of your pseudo-code there, such as the loop, translates pretty directly into a statement for most languages. What language are you working with?
 
I'm guessing the thread title should have said Visual BASIC help, not Visual logic.

If you post your code attempt we can make suggestions on how to get it working but we're not going to write the code for you.
 
Welcome to CF!

As strollin pointed out, are you talking about Visual Basic? I've used Visual Logic before in school, but there's no real "writing" code in that... It's more GUI based. If that's the case though, post a screen shot so it jars my memory on what's available.

You could do something like:

Code:
string numberVarString = "What you're returning"; 
for(int x = 1; x <= 1000; x++) {
  if(x < 11) {
     numberVarString = x;
     continue; //Just sends to next iteration of loop
  else if(x < 20) {
        numberVarString = x + "th";
        continue;
   }
   else if(x < 100) {
       if(x.Substring(1,1) == 1))
           numberVarString = x + "st";
       else if(x.Substring(1,1) == 2))
           numberVarString = x + "nd";
       else if(x.Substring(1,1) == 3))
           numberVarString = x + "rd";
       else
           numberVarString = x + "th";
     }
   else if(x < 1000)
      //Repeat logic, but increase substring position (x.SubString(2,1))
}

That's a very short and incomplete answer, but knowing what you're using will help that.

EDIT: The above is C# code, just trying to demonstrate the nested logic statements that evaluate each iteration by groupings.
 
Last edited:
I'm guessing the thread title should have said Visual BASIC help, not Visual logic.

If you post your code attempt we can make suggestions on how to get it working but we're not going to write the code for you.

Yes, probably should have titled it that, I labeled it that because that is the program I am working with. Anyway this is how my problem looks now, and as you could tell I am probably doing in an extremely weird roundabout way, I am a beginner in learning how to work with the program.

Input: Number
Breakdown: Number MOD 10
IF STATEMENT: Breakdown>3 if true> Output: "Value is"&Number&"th"
if false: another IF> Breakdown=1>if true Output: "Value is" & Number&"st"
if false:Another IF>Breakdown=2>If true Output: "Value is"&Number&"nd"
and finally if false>Output: "Value is" &Number&"rd"

Like I said pretty screwed up and it doesn't account for the numbers like 11, 12, 13.
I am using visual logic 2.2.10, I hope that this can help clear up what I am having the problem with. As far as having the previous phrases I had in my initial post working well in the code there, I am not sure what statement I need to use for that i.e. FOR, WHILE, etc.
 
Yes, probably should have titled it that, I labeled it that because that is the program I am working with. Anyway this is how my problem looks now, and as you could tell I am probably doing in an extremely weird roundabout way, I am a beginner in learning how to work with the program.

Input: Number
Breakdown: Number MOD 10
IF STATEMENT: Breakdown>3 if true> Output: "Value is"&Number&"th"
if false: another IF> Breakdown=1>if true Output: "Value is" & Number&"st"
if false:Another IF>Breakdown=2>If true Output: "Value is"&Number&"nd"
and finally if false>Output: "Value is" &Number&"rd"

Like I said pretty screwed up and it doesn't account for the numbers like 11, 12, 13.
I am using visual logic 2.2.10, I hope that this can help clear up what I am having the problem with. As far as having the previous phrases I had in my initial post working well in the code there, I am not sure what statement I need to use for that i.e. FOR, WHILE, etc.

A for loop would be the normal loop of choice for incrementing one integer a set amount of times - there should be plenty of example on that online if you look around.

Ok, so sounds like one problem you have is getting the last digit of the number. There's two ways I can think of for doing this, the one I'd prefer would be to use mod. Mod is a % symbol and gives you the remainder (as in 150%2 would give you 0, 121%3 would give you 1.)

So to get the last digit, you can simply do a mod 10 (since whatever the remainder is after dividing the number by 10 will always be the last digit of the number.)

From there, you can apply your if statements to that number as you describe above :)
 
This may sound really stupid, but someone told me the only stupid question are the ones you don't ask. Aren't I making use of the mod already i.e. Number MOD 10, and for number such that end in 0, like 10 it doesn't give me the correct ending, it will loop through my IFs and give me 10rd. And it will not give the correct ending for numbers that there second last digit is 1 i.e 11, 12, 13. It will display for those number 11st, 12nd, 13rd. Maybe I'm not understanding something?
 
This may sound really stupid, but someone told me the only stupid question are the ones you don't ask. Aren't I making use of the mod already i.e. Number MOD 10, and for number such that end in 0, like 10 it doesn't give me the correct ending, it will loop through my IFs and give me 10rd. And it will not give the correct ending for numbers that there second last digit is 1 i.e 11, 12, 13. It will display for those number 11st, 12nd, 13rd. Maybe I'm not understanding something?

Doh, sorry, so you are. My mistake entirely.

The reason it won't work in that case is because 10 mod 10 will give you 0 (since it divides into itself exactly, with no remainder) - and that isn't covered in any of your if statements. So it'll fall through to the else case and go with the value specified there, which is "rd". Try adding a separate case in for 0 :)
 
Alrightly then, it looks like that is going to work just fine. As for the exceptions like 11,12 and 13 I think I am going to input a IF statement using the OR before the Number MOD 10 with an EXIT loop to account for those numbers. It will look something like this

(Number <13) OR (Number >11) etc for all the number that are like that. Thank you so much it really helped me understand the visual logic program better :)
 
Back
Top Bottom