Number in Characters in C Sharp

Number in Characters

To convert a number in characters in C#, we can use a loop and switch case. After taking input from the user, the number is iterated and is divided by 10 until it becomes 0. The remainder so obtained is passed in switch case to retrieve the word for the number.

Example:

using System;  
  public class Example  
   {  
     public static void Main(string[] args)  
      {  
       int num, sum=0, x;     
       Console.Write("Enter a Number: ");    
       num = int.Parse(Console.ReadLine());     
       while(num>0)      
       {      
        x = num%10;      
        sum = sum*10 + x;      
        num = num/10;      
       }      
       num = sum;      
       while(num>0)      
       {      
        x = num%10;      
        switch(x)      
        {      
         case 1:      
         Console.Write("One ");  
         break;      
         case 2:      
         Console.Write("Two ");      
         break;      
         case 3:      
         Console.Write("Three ");    
         break;      
         case 4:      
         Console.Write("Four ");    
         break;      
         case 5:      
         Console.Write("Five ");    
         break;      
         case 6:      
         Console.Write("Six ");     
         break;      
         case 7:    
         Console.Write("Seven ");    
         break;    
         case 8:      
         Console.Write("Eight ");      
         break;      
         case 9:      
         Console.Write("Nine ");    
         break;      
         case 0:      
         Console.Write("Zero ");    
         break;      
         default:      
         Console.Write("Not_a_Digit ");      
         break;      
        }     
        num = num/10;      
       }      
   }  
  }

Output:

Explanation:

In the above example, we are converting a number in characters.

 

Please Share