Archive

Posts Tagged ‘Java Basic Programs’

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

January 5, 2009 Leave a comment

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):


Java Program to multiply two matrices – Q9

January 5, 2009 Leave a comment

Q9: Java Program to multiply two matrices

class matrix_mul{
	public static void main(String args[]){
		int mat1[][] = { {1,2,3},
						 {4,5,6},
						 {7,8,9} };
		int mat2[][] = { {9,8,7},
						 {6,5,4},
						 {3,2,1} };
							
		int tot[][] = new int[3][3];
		int i, j, k;

		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				tot[i][j] = 0;
				for(k=0; k<3; k++){
					tot[i][j] = tot[i][j] + (mat1[i][k] * mat2[k][j]);
				}
			}
		}

		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				System.out.print(" " + tot[i][j]);
			}
			System.out.println();
		}
	}
}

… from College notes (BCA/MCA assignments):


Java Program to add two matrices – Q8

January 4, 2009 Leave a comment

Q8: Java Program to add two matrices

… from College notes (BCA/MCA assignments):

class matrix_add{
	public static void main(String args[]){
		int mat1[][] = { {1,2,3},
						 {4,5,6},
						 {7,8,9} };
		int mat2[][] = { {9,8,7},
						 {6,5,4},
						 {3,2,1} };
		
		int tot[][] = new int[3][3];
		int i, j;
		
		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				tot[i][j] = mat1[i][j] + mat2[i][j];
			}
		}
		for(i=0; i<3; i++){
			for(j=0; j<3; j++){
				System.out.print(" " + tot[i][j]);
			}
			System.out.println();
		}
	}
}

Java Program to find n prime number – Q7

January 4, 2009 Leave a comment

Q7: Java Program to find n prime number

Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
For example: 2, 3, 5, 7, 11, 13, 19, 23, 29, etc.

class primeupto{
	public static void main(String args[]){
		boolean pr = false;
		int a = Integer.parseInt(args[0]);
		System.out.print("Prime Numbers upto " + a + " are: " + 2);
		for(int i=3; i<a; i++){
			for(int j=2; j<i; j++){
				if(i % j == 0){
					pr = false;
					break;
				}
				else
					pr = true;
			} // end of for 2
			if(pr==true)
				System.out.print(" " + i);
		} // end of for 1
	} // end of main
} // end of class primeupto

… from College notes (BCA/MCA assignments):


Java Program to find prime numbers up to a given number n – Q6

January 3, 2009 Leave a comment

Q6: Java Program to find prime numbers up to a given number n

Prime Number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
For example: 2, 3, 5, 7, 11, 13, 19, 23, 29, etc.

class primetill{
	public static void main(String args[]){
		boolean pr = false;
		int a = Integer.parseInt(args[0]);
		System.out.print("Prime Numbers upto " + a + " are: " + 2);
		for(int i=3, x=0; x<a; i++){
			for(int j=2; j<i; j++){
				if(i % j == 0){
					pr = false;
					break;
				}
				else
					pr = true;
			} // end of for 2
			if(pr==true){
				System.out.print(" " + i);
				x++;
			}
		} // end of for 1
	} // end of main
} // end of class primetill

… from College notes (BCA/MCA assignments):