C# Program to Convert Number in Characters - c# - c# tutorial - c# net



How to convert number into characters in C# Program ?

  • In C# language, we can 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.

Example 1:

using System;  
  	public class ConversionExample  
   {  
     public static void Main(string[] args)  
      {  
       int n,sum=0,r;     
       Console.Write("Enter the Number= ");    
       n= int.Parse(Console.ReadLine());     
       while(n>0)      
       {      
        r=n%10;      
        sum=sum*10+r;      
        n=n/10;      
       }      
       n=sum;      
       while(n>0)      
       {      
        r=n%10;      
        switch(r)      
        {      
         case 1:      
         Console.Write("one ");  
         break;      
         case 2:      
         Console.Write("two ");      
         break;      
         case 3:      
         Console.Write("three ");    
         break;      
         case 4:      
         Console.Write("four ");    
         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("tttt ");      
         break;      
        }//end of switch      
        n=n/10;      
       }//end of while loop       
   }  
  } 

C# examples - Output :

Enter the Number= 357546
three five seven five four six
learn c# tutorials - character to string in csharp - c# programs

Character to string in csharp

Example 2:

/*
 *  C# Program to Convert Digits to Words
 */
using System; 
 
public class ConvertDigitsToWords
{   
  public static void Main()
  { 
    int num; 
    int nextdigit; 
    int numdigits; 
    int[] n = new int[20]; 
 
    string[] digits = { "zero", "one", "two", 
                        "three", "four", "five", 
                        "six", "seven", "eight", 
                        "nine" }; 
 
 
    Console.WriteLine("Enter the number");
    num = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Number: " + num); 
    Console.Write("Number in words: ");    
    nextdigit = 0; 
    numdigits = 0; 
    do 
    { 
      nextdigit = num % 10; 
      n[numdigits] = nextdigit; 
      numdigits++; 
      num = num / 10; 
    } while(num > 0); 
    numdigits--; 
    for( ; numdigits >= 0; numdigits--) 
        Console.Write(digits[n[numdigits]] + " "); 
    Console.WriteLine();
    Console.ReadLine();
  }   
}

C# examples - Output :

Enter the number
1548
Number: 1548
Number in words: one five four eight

Related Searches to C# Program to Convert Number in Characters