0

From number to word converter

using System;

namespace ITEC102_PT1
 {
   public class Program
   {
     public static void Main(string[] args)
     {
      
      string[] first = {"Zero ", "One ", "Two ", "Three ", 
                        "Four ", "Five ", "Six ", "Seven ",
                        "Eight ", "Nine ","Ten ", "Eleven ", "Twelve ", 
                        "Thirteen ", "Fourteen ", "Fifteen ",
                        "Sixteen ", "Seventeen ", "Eighteen ",
                        "Nineteen "};
      string[] tens = {"","", "Twenty ", "Thirty ", "Forty ", 
                       "Fifty ", "Sixty ",
                       "Seventy ", "Eighty ", "Ninety "};
      string[] hundred = {"Hundred "};
      string[] thousand = {"Thousand "};
      
      int n, x;   
      string result = "";
           
      Console.WriteLine("•••NUMBER TO WORDS CONVERTER•••");
  
      Console.WriteLine();
   
      Console.Write("Please enter a number in the range of 0-9999 : ");
      x = int.Parse(Console.ReadLine());
   
      Console.WriteLine();
       
      Console.WriteLine(x + " in word/s is:");
      
      if (x >= 1000 && x <= 9999){
            n = x / 1000;
            result = first[n] + thousand[0];
            x = x% 1000;
      }if (x >= 100 && x <= 999){
            n = x / 100;
            result = first[n] + hundred[0];
            x = x% 100;
      }if (x >= 20 && x <= 99){
            n = x / 10;
            result = result + tens[n];
            x = x%10;
      }if (x >= 0 && x <= 19){
            result += first[x];
      }else if (x >= 10000){
            Console.WriteLine("More than 4 numbers is not allowed");
      }
      
      Console.WriteLine(result);
      
     }
   }
 }

Everytime that the number is ended within 100 to 9000 there's always a zero at the end and when the number from 1000 reach 1100 the word 'one thousand' disappear.

Please enter a number in the range of 0-9999 : 1100

1100 in word/s is:
One Hundred Zero 

I'm just a first year college student without any knowledge about coding before so please understand if I ask a basic problem. I'm still learning and want to learn more. I apologize if there's any mistake in my question this is my first time asking here. Thank you

Ray
  • 11
  • 4
  • this is common usecase. check this https://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp – Krishna Varma Nov 08 '21 at 14:14
  • Do a code walkthrough. For example, when you enter 1100, both the first and second `if` statements pass. Now check and see what happens when both pass. Then check if any other `if` statements. The second `if` statement overwrites the `result`, is that correct do you think? – CodingYoshi Nov 08 '21 at 14:19
  • You may think your question is programming related but it's not. If you were to write the steps using plain English, your steps will not make sense. So write the steps in plain English, test it and if it gives the Corey results, then turn from plain English to program code. – CodingYoshi Nov 08 '21 at 14:22
  • If you change `result =` to `result +=` and change the condition `if (x >= 0 && x <= 19)` to `if (x > 0 && x <= 19)`, that should fix some of the issues. You may have other issues though or may not, you need to figure that out yourself. – CodingYoshi Nov 08 '21 at 14:53
  • `if (x >= 10000)` IMHO you should check for failures first, then ensure the rest of the method is skipped. – Jeremy Lakeman Nov 09 '21 at 02:16

1 Answers1

0

This is the one that I've come up with though this is longer but it works for me.

using System;

namespace ITEC102_PT1
 {
   public class Program
   {
     public static void Main(string[] args)
     {
      
      string[] first = {"Zero ", "One ", "Two ", "Three ", 
                        "Four ", "Five ", "Six ", "Seven ",
                        "Eight ", "Nine ","Ten ", "Eleven ", "Twelve ", 
                        "Thirteen ", "Fourteen ", "Fifteen ",
                        "Sixteen ", "Seventeen ", "Eighteen ",
                        "Nineteen "};
      string[] tens = {"","", "Twenty ", "Thirty ", "Forty ", 
                       "Fifty ", "Sixty ",
                       "Seventy ", "Eighty ", "Ninety "};
      string[] hundred = {"Hundred "};
      string[] thousand = {"Thousand "};
      
      int n, x;   
      string result = "",
             result1 = "",
             result2 = "";
           
      Console.WriteLine("•••NUMBER TO WORDS CONVERTER•••");
  
      Console.WriteLine();
   
      Console.Write("Please enter a number in the range of 0-9999 : ");
      x = int.Parse(Console.ReadLine());
   
      Console.WriteLine();
       
      Console.WriteLine(x + " in word/s is:");
      
      // THOUSAND
      if (x > 999 && x < 10000){ 
            n = x / 1000;
            result1 = first[n] + thousand[0];
            Console.Write(result1);
            x = x% 1000;
                
              if (x >= 100 && x <= 999){
                 n = x / 100;
                 result = first[n] + hundred[0];
                 x = x% 100;
                   if (x >= 20 && x <= 99){
                       n = x / 10;
                       result += tens[n];
                       x = x%10; 
                       if (x < 20){
                       result += first[x];
                       }
                   }else{
                       result += first[x];
                   }
      
              }else if (x >= 20 && x <= 99){
                 n = x / 10;
                 result += tens[n];
                 x = x%10;
                 if (x < 20){
                     result += first[x];
                 }
            
              }else if (x > 0 && x < 20){
                 result += first[x];
              }
              Console.WriteLine(result);
      
      // HUNDRED
      }else if (x >= 100 && x <= 999){
            n = x / 100;
            result1 = first[n] + hundred[0];
            Console.Write(result1);
            x = x% 100;
            
            if (x >= 20 && x <= 99){
                 n = x / 10;
                 result += tens[n];
                 x = x%10;
                if (x > 0 && x < 20){
                    result += first[x];
                }
                
            }else if (x > 0 && x < 20){
              result += first[x];
            }
            Console.Write(result);
            
      // TENS
      }else if (x >= 20 && x <= 99){
            n = x / 10;
            result1 += tens[n];
            Console.Write(result1);
            x = x % 10;
            
            if (x > 0 && x < 20){
                    result += first[x];
                }
            Console.WriteLine(result);
            
      // 0 - 19
      }else if (x >= 0 && x <= 19){
            result1 += first[x];
            Console.WriteLine(result1);
      }else if (x >= 10000){
            Console.WriteLine("More than 4 numbers is not allowed");
      }else Console.WriteLine("Negative number is not allowed");
      
      
     }
   }
 }

Thank you very much for answering my question.

Ray
  • 11
  • 4