C Sharp Program to Convert Number in Characters

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# Program to Convert Number in Characters

In C# language, we can easily convert number in characters by the help of loop and switch case. In this program, we are taking input from the user and iterating this number until it is 0. While iteration, we are dividing it by 10 and the remainder is passed in switch case to get the word for the number.

Let's see the C# program to convert number in characters.

 
  1. using System;  
  2.   public class ConversionExample  
  3.    {  
  4.      public static void Main(string[] args)  
  5.       {  
  6.        int n,sum=0,r;     
  7.        Console.Write("Enter the Number= ");    
  8.        n= int.Parse(Console.ReadLine());     
  9.        while(n>0)      
  10.        {      
  11.         r=n%10;      
  12.         sum=sum*10+r;      
  13.         n=n/10;      
  14.        }      
  15.        n=sum;      
  16.        while(n>0)      
  17.        {      
  18.         r=n%10;      
  19.         switch(r)      
  20.         {      
  21.          case 1:      
  22.          Console.Write("one ");  
  23.          break;      
  24.          case 2:      
  25.          Console.Write("two ");      
  26.          break;      
  27.          case 3:      
  28.          Console.Write("three ");    
  29.          break;      
  30.          case 4:      
  31.          Console.Write("four ");    
  32.          break;      
  33.          case 5:      
  34.          Console.Write("five ");    
  35.          break;      
  36.          case 6:      
  37.          Console.Write("six ");     
  38.          break;      
  39.          case 7:    
  40.          Console.Write("seven ");    
  41.          break;    
  42.          case 8:      
  43.          Console.Write("eight ");      
  44.          break;      
  45.          case 9:      
  46.          Console.Write("nine ");    
  47.          break;      
  48.          case 0:      
  49.          Console.Write("zero ");    
  50.          break;      
  51.          default:      
  52.          Console.Write("tttt ");      
  53.          break;      
  54.         }//end of switch      
  55.         n=n/10;      
  56.        }//end of while loop       
  57.    }  
  58.   }  

Output:

Enter the Number= 357546
three five seven five four six