Watch & Subscribe my SQL videos on YouTube | Join me on Facebook

Java Program to create star triangle pattern – Q12

January 6, 2009 Leave a comment

Q12: Java Program to create star triangle pattern

class star_triangle{
	public static void main(String args[]){
		int i, j, k;
		for(i=0; i<10; i++){
			for(j=i; j<10; j++){
				System.out.print(" ");
			}
			for(k=0; k<i;  k++){
				System.out.print(" " + "*");
			}
			System.out.println();
		}
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to find Armstrong number (sum of cubes of individual digits is equal to the given number) – Q11

January 6, 2009 Leave a comment

Q11: Java Program to find Armstrong number (sum of cubes of individual digits is equal to the given number)

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.

For example: 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

class armg{
	public static void main(String args[]){
		int num;
		num = Integer.parseInt(args[0]);
		int res=0, x, n;
		while(num != 0){
			x = num % 10;
			res += x*x*x;
			num /= 10;
			}
		n = Integer.parseInt(args[0]);
		if(n == res)
			System.out.println(args[0] + " is Armgstrong Number");
		else
			System.out.println(args[0] + " is not Armgstrong Number");
	}
}

… from College notes (BCA/MCA assignments):


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();
		}
	}
}