Advertisements
Home > Java > Java Program to print a given number in words till at least 7 digits – Q10

Java Program to print a given number in words till at least 7 digits – Q10


Q10: Java Program to print a given number in words till at least 7 digits

import javax.swing.*;

class num_word{
	public static void main(String args[]){
		int num, y, i, j;
		int arr[] = new int[10];
		boolean show = false;
		String input = JOptionPane.showInputDialog("Enter a Number: ");
		num = Integer.parseInt(input);

        	System.out.println("\n You Entered: ");

		i = 0;
		while(num != 0){
			arr[i++] = num % 10;
			num /= 10;
		}
		i--;

		if((i > 2) && (i % 2 == 1) ){
			y = arr[i];
			System.out.print(" " + word(y));
			y = (int)Math.pow(10, i);
			System.out.print(" " + word(y));
			i--;
		}

		for(j=i; j>=3; j=j-2){
			if(arr[j] != 0 || arr[j-1] != 0)
				show = true;
			if((j % 2) == 0){
				if(arr[j-1] == 1 && arr[j] != 0){
					y = arr[j-1];
					System.out.print(" " + word(y + 10));
				}
				else
					y = arr[j] * 10;
			}
			else
				y = arr[j];
			if(arr[j-1] != 1 || arr[j] == 0){
				System.out.print(" " + word(y));
				y = arr[j-1];
				System.out.print(" " + word(y));
			}
			if(show == true){
				y = (int)Math.pow(10, j-1);
				System.out.print(" " + word(y));
			}
			show = false;
		}

		if(arr[2] != 0){
			y= arr[2];
			System.out.print(" " + word(y) + " " + word(100));
		}

		if(arr[1] != 0){
			if(arr[1] == 1){
				y = arr[0];
				System.out.print(" " + word(y + 10));
				arr[0] = 0;
			}
			else{
				y = arr[1];
				System.out.print(" " + word(y * 10));
			}
		}

		if(arr[0] != 0){
			y = arr[0];
			System.out.print(" " + word(y));
		}

		System.out.println("\n Dev By: ManojPandey \n");
	}

	public static String word(int _num){
		switch(_num){
			case 1:
				return "One";
			case 2:
				return "Two";
			case 3:
				return "Three";
			case 4:
				return "Four";
			case 5:
				return "Five";
			case 6:
				return "Six";
			case 7:
				return "Seven";
			case 8:
				return "Eight";
    			case 9:
    				return "Nine";
    			case 10:
    				return "Ten";
    			case 11:
    				return "Eleven";
    			case 12:
    				return "Twelve";
    			case 13:
    				return "Thirteen";
    			case 14:
    				return "Fourteen";
    			case 15:
    				return "Fifteen";
    			case 16:
    				return "Sixteen";
    			case 17:
    				return "Seventeen";
    			case 18:
    				return "Eighteen";
    			case 19:
    				return "Nineteen";
    			case 20:
    				return "Twenty";
    			case 30:
    				return "Thirty";
			case 40:
				return "Forty";
			case 50:
				return "Fifty";
    			case 60:
    				return "Sixty";
    			case 70:
    				return "Seventy";
    			case 80:
    				return "Eighty";
    			case 90:
    				return "Ninety";
    			case 100:
    				return "Hundred";
    			case 1000:
    				return "Thousand";
    			case 100000:
    				return "Lakh";
    			case 10000000:
   	 			return "Crore";
		}
		return "";
	}
}

… from College notes (BCA/MCA assignments):


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: