Thursday, 3 October 2013

Gauss Matrix Elimination : Java : BlueJ

" I would like to show my Acknowledgement and special Thanks to the Original Author Of Many of the Programs in this Blog : Sir A.K. Seal who has shown great teaching skills to make Programming Practical and Simple. "
Objective :


In linear algebra, Gaussian elimination is an algorithm for solving systems of linear equations. It can also be used to find the rank of a matrix, to calculate the determinant of a matrix, and to calculate the inverse of an invertible square matrix. The method is named after Carl Friedrich Gauss, but it was not invented by him.
Elementary row operations are used to reduce a matrix to what is called triangular form (in numerical analysis) or row echelon form (in abstract algebra). Gauss–Jordan elimination, an extension of this algorithm, reduces the matrix further to diagonal form, which is also known as reduced row echelon form. Gaussian elimination alone is sufficient for many applications, and requires fewer calculations than the Gauss–Jordan version.
The process of Gaussian elimination has two parts. The first part (Forward Elimination) reduces a given system to either triangular or echelon form, or results in a degenerate equation, indicating the system has no unique solution but may have multiple solutions. This is accomplished through the use of elementary row operations. The second step uses back substitution to find the solution of the system above.
Stated equivalently for matrices, the first part reduces a matrix to row echelon form using elementary row operations while the second reduces it to reduced row echelon form, or row canonical form.
Another point of view, which turns out to be very useful to analyze the algorithm, is that Gaussian elimination computes a matrix decomposition. The three elementary row operations used in the Gaussian elimination (multiplying rows, switching rows, and adding multiples of rows to other rows) amount to multiplying the original matrix with invertible matrices from the left. The first part of the algorithm computes an LU decomposition, while the second part writes the original matrix as the product of a uniquely determined invertible matrix and a uniquely determined reduced row-echelon matrix.

Example : 

Suppose the goal is to find and describe the solution(s), if any, of the following system of linear equations:
\begin{alignat}{7}
2x &&\; + \;&& y             &&\; - \;&& z  &&\; = \;&& 8 & \qquad (L_1) \\
-3x &&\; - \;&& y             &&\; + \;&& 2z &&\; = \;&& -11 & \qquad (L_2) \\
-2x &&\; + \;&& y &&\; +\;&& 2z  &&\; = \;&& -3 &  \qquad (L_3)
\end{alignat}
The algorithm is as follows: eliminate x from all equations below L_1, and then eliminate y from all equations below L_2. This will put the system into triangular form. Then, using back-substitution, each unknown can be solved for.
In the example, x is eliminated from L_2 by adding \begin{matrix}\frac{3}{2}\end{matrix} L_1 to L_2. x is then eliminated from L_3 by adding L_1 to L_3. Formally:
L_2 + \frac{3}{2}L_1 \rightarrow L_2
L_3 + L_1 \rightarrow L_3
The result is:
\begin{alignat}{7}
2x &&\; + && y &&\; - &&\; z &&\; = \;&& 8 &  \\
&& && \frac{1}{2}y &&\; + &&\; \frac{1}{2}z &&\; = \;&& 1 & \\
&& && 2y &&\; + &&\; z &&\; = \;&& 5 &  
\end{alignat}
Now y is eliminated from L_3 by adding -4L_2 to L_3:
L_3 + -4L_2 \rightarrow L_3
The result is:
\begin{alignat}{7}
2x &&\; + && y \;&& - &&\; z \;&& = \;&& 8 &  \\
&& && \frac{1}{2}y \;&& + &&\; \frac{1}{2}z \;&& = \;&& 1 & \\
&& && && &&\; -z \;&&\; = \;&& 1 &  
\end{alignat}
This result is a system of linear equations in triangular form, and so the first part of the algorithm is complete.
The last part, back-substitution, consists of solving for the knowns in reverse order. It can thus be seen that
z = -1 \quad (L_3)
Then, z can be substituted into L_2, which can then be solved to obtain
y = 3 \quad (L_2)
Next, z and y can be substituted into L_1, which can be solved to obtain
x = 2 \quad (L_1)
The system is solved.
Some systems cannot be reduced to triangular form, yet still have at least one valid solution: for example, if y had not occurred in L_2 and L_3 after the first step above, the algorithm would have been unable to reduce the system to triangular form. However, it would still have reduced the system to echelon form. In this case, the system does not have a unique solution, as it contains at least one free variable. The solution set can then be expressed parametrically (that is, in terms of the free variables, so that if values for the free variables are chosen, a solution will be generated).
In practice, one does not usually deal with the systems in terms of equations but instead makes use of the augmented matrix (which is also suitable for computer manipulations). For example:
\begin{alignat}{7}
2x &&\; + \;&& y             &&\; - \;&& z  &&\; = \;&& 8 & \qquad (L_1) \\
-3x &&\; - \;&& y             &&\; + \;&& 2z &&\; = \;&& -11 & \qquad (L_2) \\
-2x &&\; + \;&& y &&\; +\;&& 2z  &&\; = \;&& -3 &  \qquad (L_3)
\end{alignat}
Therefore, the Gaussian Elimination algorithm applied to the augmented matrix begins with:

\left[ \begin{array}{ccc|c}
2 & 1 & -1 & 8 \\
-3 & -1 & 2 & -11 \\
-2 & 1 & 2 & -3
\end{array} \right]
which, at the end of the first part (Gaussian elimination, zeros only under the leading 1) of the algorithm, looks like this:

\left[ \begin{array}{ccc|c}
1 & \frac{1}{3} & \frac{-2}{3} & \frac{11}{3} \\
0 & 1 & \frac{2}{5} & \frac{13}{5} \\
0 & 0 & 1 & -1
\end{array} \right]
That is, it is in row echelon form.
At the end of the algorithm, if the Gauss–Jordan elimination(zeros under and above the leading 1) is applied:

\left[ \begin{array}{ccc|c}
1 & 0 & 0 & 2 \\
0 & 1 & 0 & 3 \\
0 & 0 & 1 & -1
\end{array} \right]
That is, it is in reduced row echelon form, or row canonical form.
BlueJ Program Screenshot :



Java Program Source Code :

/**
 * The Program Takes Number of Eqautions and Then Creates An
 * Square Matrix of Order x Order and Formulates a Top Triangle
 * My Continuous Elimination and Then Solves the Set of Equations
 * to Find the Values of the Order number of Variables.
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
 */
import java.util.*;
public class GaussMatrix
{
    // INSTANCE VARIABLES
    private double[][] m;    // MATRIX OF CO-EFFICIENTS
    private double[] constants; // VECTOR OF CONSTANT TERMS
    private double[] solution; // SOLUTION SET
    private int numEq;      // NUMBER OF EQUATIONS
    static Scanner sc=new Scanner(System.in);
    
    public GaussMatrix(int equations)   // CONSTRUCTOR
    {
        numEq=equations;
        m=new double[numEq][numEq];
        constants=new double[numEq];
        solution=new double[numEq];
    }
    
    public void fillMatrix()
    {
        for(int i=0;i<numEq;i++){
            System.out.println("Enter the co-efficients of unknowns and constant term for Equation "+(i+1)+" :");
            for(int j=0;j<numEq;j++){
                System.out.print("Enter Co-efficient "+(j+1)+" : ");
                m[i][j]=sc.nextDouble();
            }
            System.out.print("Enter Constant Term : ");
            constants[i]=sc.nextDouble();
        }
    }
    
    public void printSolution()
    {
        System.out.println("\nSolution Set is : ");
        for(int i=0;i<numEq;i++)
            System.out.println((char)('A'+i)+" = "+solution[i]);
    }
    
    public void printMatrix()   // FOR DEBUGGING PURPOSE
    {
        for(int i=0;i<numEq;i++){
            for(int j=0;j<numEq;j++){
                if(m[i][j]>=0)
                    System.out.print(" +"+m[i][j]+((char)('A'+j))+" ");
                else if(m[i][j]<0)
                    System.out.print(" "+m[i][j]+((char)('A'+j))+" ");
            }
            System.out.println(" = "+constants[i]);
        }
    }
    
    public void swapRows(int row1,int row2)
    {
        double temp;
        for(int j=0;j<numEq;j++){   // SWAPPING CO-EFFICIENT ROWS
            temp=m[row1][j];
            m[row1][j]=m[row2][j];
            m[row2][j]=temp;
        }
        temp=constants[row1];   // SWAPPING CONSTANTS VECTOR
        constants[row1]=constants[row2];
        constants[row2]=temp;
    }
    
    public void eliminate()
    {
        int i,j,k,l;
        for(i=0;i<numEq;i++){   // i -> ROW ; MATRIX ORDER DECREASES DURING ELIMINATION
            // FIND LARGEST CO-EFFICIENTSOF THE CURRENT COLUMN MOVING ROW-WISE
            double largest=Math.abs(m[i][i]);
            int index=i;
            for(j=i+1;j<numEq;j++){
                if(Math.abs(m[j][i])>largest){
                    largest=m[j][i];
                    index=j;
                }
            }
            swapRows(i,index);  // SWAPPING i-th ROW to index-th ROW
            for(k=i+1;k<numEq;k++){
                double factor=m[k][i]/m[i][i];
                // PROCESSING COLUMN WISE
                for(l=i;l<numEq;l++){
                    m[k][l]-=factor*m[i][l];
                }
                constants[k]-=factor*constants[i];  // PROCESSING CONSTANTS
            }
        }
    }
    
    public void solve()
    {
        for(int i=numEq-1;i>=0;i--){
            solution[i]=constants[i];   // COPY
            for(int j=numEq-1;j>i;j--){
                solution[i]-=m[i][j]*solution[j];
            }
            solution[i]/=m[i][i];
        }
    }
    
    public static void main(String args[])
    {
        System.out.print("Enter the Number of Terms : ");
        GaussMatrix obj=new GaussMatrix(sc.nextInt());
        obj.fillMatrix();
        System.out.println("\fYou Have Entered The Following Equations :");
        obj.printMatrix();
        obj.eliminate();
        obj.solve();
        obj.printSolution();

Full Adder Half Adder Truth Table : Java : BlueJ

OBJECTIVE :


In electronics, an adder or summer is a digital circuit that performs addition of numbers. In many computers and other kinds of processors, adders are used not only in the arithmetic logic unit(s), but also in other parts of the processor, where they are used to calculate addresses, table indices, and similar.
Although adders can be constructed for many numerical representations, such as binary-coded decimal or excess-3, the most common adders operate on binary numbers. In cases where two's complement or ones' complement is being used to represent negative numbers, it is trivial to modify an adder into an adder–subtractor. Other signed number representations require a more complex adder.

Half Adder :

Half Adder Logic Diagram
The half adder adds two one-bit binary numbers A and B. It has two outputs, S and C (the value theoretically carried on to the next addition); the final sum is 2C + S. The simplest half-adder design, pictured on the right, incorporates an XOR gate for S and an AND gate for C. With the addition of an OR gate to combine their carry outputs, two half adders can be combined to make a full adder.


With the help of half adder, we can design circuits that are capable of performing simple addition with the help of logic gates.
Let us first take a look at the addition of single bits.
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 1 0
These are the least possible single-bit combinations. But the result for 1+1 is 10. Though this problem can be solved with the help of an EXOR Gate, if you do care about the output, the sum result must be re-written as a 2-bit output. Thus the above equations can be written as
0 + 0 = 0 0
0 + 1 = 0 1
1 + 0 = 0 1
1 + 1 = 1 0
Here the output ‘1’of ‘10’ becomes the carry-out. The result is shown in a truth-table below. ‘SUM’ is the normal output and ‘CARRY’ is the carry-out.
INPUTS                 OUTPUTS
A             B             SUM      CARRY
0              0              0              0
0              1              1              0
1              0              1              0
1              1              0              1

Full Adder :

A full adder adds binary numbers and accounts for values carried in as well as out. A one-bit full adder adds three one-bit numbers, often written as A, B, and Cin; A and B are the operands, and Cin is a bit carried in from the next less significant stage.[2] The full-adder is usually a component in a cascade of adders, which add 8, 16, 32, etc. binary numbers. The circuit produces a two-bit output sum typically represented by the signals Cout and S, where . The one-bit full adder's truth table is:

Full Adder Logic Diagram
A full adder can be implemented in many different ways such as with a custom transistor-level circuit or composed of other gates. One example implementation is with  and .
In this implementation, the final OR gate before the carry-out output may be replaced by an XOR gate without altering the resulting logic. Using only two types of gates is convenient if the circuit is being implemented using simple IC chips which contain only one gate type per chip. In this light, Cout can be implemented as .
A full adder can be constructed from two half adders by connecting A and B to the input of one half adder, connecting the sum from that to an input to the second adder, connecting Ci to the other input and OR the two carry outputs. Equivalently, S could be made the three-bit XOR of A, B, and Ci, and Cout could be made the three-bit majority function of A, B, and Cin.

This type of adder is a little more difficult to implement than a half-adder. The main difference between a half-adder and a full-adder is that the full-adder has three inputs and two outputs. The first two inputs are A and B and the third input is an input carry designated as CIN. When a full adder logic is designed we will be able to string eight of them together to create a byte-wide adder and cascade the carry bit from one adder to the next.
The output carry is designated as COUT and the normal output is designated as S. Take a look at the truth-table.
INPUTS                 OUTPUTS
A             B             CIN         COUT    S
0              0              0              0              0
0              0              1              0              1
0              1              0              0              1
0              1              1              1              0
1              0              0              0              1
1              0              1              1              0
1              1              0              1              0
1              1              1              1              1
From the above truth-table, the full adder logic can be implemented. We can see that the output S is an EXOR between the input A and the half-adder SUM output with B and CIN inputs. We must also note that the COUT will only be true if any of the two inputs out of the three are HIGH.
Thus, we can implement a full adder circuit with the help of two half adder circuits. The first will half adder will be used to add A and B to produce a partial Sum. The second half adder logic can be used to add CIN to the Sum produced by the first half adder to get the final S output. If any of the half adder logic produces a carry, there will be an output carry.

BLUEJ PROGRAM SCREENSHOT :



JAVA PROGRAM SOURCE CODE :

/**
 * The Program Prints the TruthTable for Full Adder and Half Adder. 
 * For Full Adder, Sum = x'y'z + xy'z' + x'yz' + xyz  Carry = xy + yz + xz
 * For Half Adder, Sum = x'y + xy'  Carry = xy
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
 */
public class TruthTable
{
    public void fullAdderTable()
    {
        boolean a,b,c,s,cr;
        int x,y,z,sum,carry;
        System.out.println(" x | y | z | c | s ");  System.out.println("---|---|---|---|---"); // TRUTH TABLE HEADER
        for(x=0;x<2;x++){
            for(y=0;y<2;y++){
                for(z=0;z<2;z++){
                    a=(x==0)?false:true;    b=(y==0)?false:true;    c=(z==0)?false:true; // INITIALIZING BOOLEAN VALUES IN BINARY FORM
                    s=!a&&!b&&c||a&&!b&&!c||!a&&b&&!c||a&&b&&c;     cr=a&&b||b&&c||a&&c; // FULL ADDER SUM AND CARRY CHECK OPERATION
                    sum=(s==false)?0:1;     carry=(cr==false)?0:1; // CONVERTING BOOLEAN SUM AND CARRY TO INTEGERS
                    System.out.println(" "+x+" | "+y+" | "+z+" | "+carry+" | "+sum); // PRINTING EACH LINE OF TRUTH TABLE
                }
            }
        }
    }
    public void halfAdderTable()
    {
        boolean a,b,s,cr;
        int x,y,sum,carry;
        System.out.println(" x | y | c | s ");  System.out.println("---|---|---|---"); // TRUTH TABLE HEADER
        for(x=0;x<2;x++){
            for(y=0;y<2;y++){
                a=(x==0)?false:true;    b=(y==0)?false:true; // INITIALIZING BOOLEAN VALUES IN BINARY FORM
                s=!a&&b||a&&!b;     cr=a&&b; // FULL ADDER SUM AND CARRY CHECK OPERATION
                sum=(s==false)?0:1;     carry=(cr==false)?0:1; // CONVERTING BOOLEAN SUM AND CARRY TO INTEGERS
                System.out.println(" "+x+" | "+y+" | "+carry+" | "+sum); // PRINTING EACH LINE OF TRUTH TABLE
            }
        }
    }
    public static void main(String args[])
    {
        TruthTable obj=new TruthTable();
        System.out.println("\nFull Adder Truth Table\n");  obj.fullAdderTable();
        System.out.println("\nHalf Adder Truth Table\n");  obj.halfAdderTable();

Full Adder Half Adder Truth Table : Java : BlueJ

OBJECTIVE :


In electronics, an adder or summer is a digital circuit that performs addition of numbers. In many computers and other kinds of processors, adders are used not only in the arithmetic logic unit(s), but also in other parts of the processor, where they are used to calculate addresses, table indices, and similar.
Although adders can be constructed for many numerical representations, such as binary-coded decimal or excess-3, the most common adders operate on binary numbers. In cases where two's complement or ones' complement is being used to represent negative numbers, it is trivial to modify an adder into an adder–subtractor. Other signed number representations require a more complex adder.

Half Adder :

Half Adder Logic Diagram
The half adder adds two one-bit binary numbers A and B. It has two outputs, S and C (the value theoretically carried on to the next addition); the final sum is 2C + S. The simplest half-adder design, pictured on the right, incorporates an XOR gate for S and an AND gate for C. With the addition of an OR gate to combine their carry outputs, two half adders can be combined to make a full adder.


With the help of half adder, we can design circuits that are capable of performing simple addition with the help of logic gates.
Let us first take a look at the addition of single bits.
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 1 0
These are the least possible single-bit combinations. But the result for 1+1 is 10. Though this problem can be solved with the help of an EXOR Gate, if you do care about the output, the sum result must be re-written as a 2-bit output. Thus the above equations can be written as
0 + 0 = 0 0
0 + 1 = 0 1
1 + 0 = 0 1
1 + 1 = 1 0
Here the output ‘1’of ‘10’ becomes the carry-out. The result is shown in a truth-table below. ‘SUM’ is the normal output and ‘CARRY’ is the carry-out.
INPUTS                 OUTPUTS
A             B             SUM      CARRY
0              0              0              0
0              1              1              0
1              0              1              0
1              1              0              1

Full Adder :

A full adder adds binary numbers and accounts for values carried in as well as out. A one-bit full adder adds three one-bit numbers, often written as A, B, and Cin; A and B are the operands, and Cin is a bit carried in from the next less significant stage.[2] The full-adder is usually a component in a cascade of adders, which add 8, 16, 32, etc. binary numbers. The circuit produces a two-bit output sum typically represented by the signals Cout and S, where . The one-bit full adder's truth table is:

Full Adder Logic Diagram
A full adder can be implemented in many different ways such as with a custom transistor-level circuit or composed of other gates. One example implementation is with  and .
In this implementation, the final OR gate before the carry-out output may be replaced by an XOR gate without altering the resulting logic. Using only two types of gates is convenient if the circuit is being implemented using simple IC chips which contain only one gate type per chip. In this light, Cout can be implemented as .
A full adder can be constructed from two half adders by connecting A and B to the input of one half adder, connecting the sum from that to an input to the second adder, connecting Ci to the other input and OR the two carry outputs. Equivalently, S could be made the three-bit XOR of A, B, and Ci, and Cout could be made the three-bit majority function of A, B, and Cin.

This type of adder is a little more difficult to implement than a half-adder. The main difference between a half-adder and a full-adder is that the full-adder has three inputs and two outputs. The first two inputs are A and B and the third input is an input carry designated as CIN. When a full adder logic is designed we will be able to string eight of them together to create a byte-wide adder and cascade the carry bit from one adder to the next.
The output carry is designated as COUT and the normal output is designated as S. Take a look at the truth-table.
INPUTS                 OUTPUTS
A             B             CIN         COUT    S
0              0              0              0              0
0              0              1              0              1
0              1              0              0              1
0              1              1              1              0
1              0              0              0              1
1              0              1              1              0
1              1              0              1              0
1              1              1              1              1
From the above truth-table, the full adder logic can be implemented. We can see that the output S is an EXOR between the input A and the half-adder SUM output with B and CIN inputs. We must also note that the COUT will only be true if any of the two inputs out of the three are HIGH.
Thus, we can implement a full adder circuit with the help of two half adder circuits. The first will half adder will be used to add A and B to produce a partial Sum. The second half adder logic can be used to add CIN to the Sum produced by the first half adder to get the final S output. If any of the half adder logic produces a carry, there will be an output carry.

BLUEJ PROGRAM SCREENSHOT :



JAVA PROGRAM SOURCE CODE :

/**
 * The Program Prints the TruthTable for Full Adder and Half Adder. 
 * For Full Adder, Sum = x'y'z + xy'z' + x'yz' + xyz  Carry = xy + yz + xz
 * For Half Adder, Sum = x'y + xy'  Carry = xy
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
 */
public class TruthTable
{
    public void fullAdderTable()
    {
        boolean a,b,c,s,cr;
        int x,y,z,sum,carry;
        System.out.println(" x | y | z | c | s ");  System.out.println("---|---|---|---|---"); // TRUTH TABLE HEADER
        for(x=0;x<2;x++){
            for(y=0;y<2;y++){
                for(z=0;z<2;z++){
                    a=(x==0)?false:true;    b=(y==0)?false:true;    c=(z==0)?false:true; // INITIALIZING BOOLEAN VALUES IN BINARY FORM
                    s=!a&&!b&&c||a&&!b&&!c||!a&&b&&!c||a&&b&&c;     cr=a&&b||b&&c||a&&c; // FULL ADDER SUM AND CARRY CHECK OPERATION
                    sum=(s==false)?0:1;     carry=(cr==false)?0:1; // CONVERTING BOOLEAN SUM AND CARRY TO INTEGERS
                    System.out.println(" "+x+" | "+y+" | "+z+" | "+carry+" | "+sum); // PRINTING EACH LINE OF TRUTH TABLE
                }
            }
        }
    }
    public void halfAdderTable()
    {
        boolean a,b,s,cr;
        int x,y,sum,carry;
        System.out.println(" x | y | c | s ");  System.out.println("---|---|---|---"); // TRUTH TABLE HEADER
        for(x=0;x<2;x++){
            for(y=0;y<2;y++){
                a=(x==0)?false:true;    b=(y==0)?false:true; // INITIALIZING BOOLEAN VALUES IN BINARY FORM
                s=!a&&b||a&&!b;     cr=a&&b; // FULL ADDER SUM AND CARRY CHECK OPERATION
                sum=(s==false)?0:1;     carry=(cr==false)?0:1; // CONVERTING BOOLEAN SUM AND CARRY TO INTEGERS
                System.out.println(" "+x+" | "+y+" | "+carry+" | "+sum); // PRINTING EACH LINE OF TRUTH TABLE
            }
        }
    }
    public static void main(String args[])
    {
        TruthTable obj=new TruthTable();
        System.out.println("\nFull Adder Truth Table\n");  obj.fullAdderTable();
        System.out.println("\nHalf Adder Truth Table\n");  obj.halfAdderTable();

Convert Amount In Number to Words : Java : BlueJ

Objective :

In this tutorial, I will explain how a number can be converted to its value as a word using Java. If you are wondering how and where this can be used, take the example of Money values. If the Amount is in numbers and you would like to convert the amount in words then this program will be useful for you. The Program Checks and Converts the Number to String and Using the SubString function changes the position to the converted String by Referencing to the Array Index with the Deduced Character Index.

BlueJ Program Screenshot :



Java Program Source Code :

/**
 * The class AmountInWords converts the Amount in Numbers into Words.
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
 */
import java.util.*;
public class AmountInWords
{
    private static String amount; private static int num;
    private static String[] units={""," One"," Two"," Three"," Four"," Five"," Six"," Seven"," Eight"," Nine"};
    private static String[] teen={" Ten"," Eleven"," Twelve"," Thirteen"," Fourteen"," Fifteen"," Sixteen"," Seventeen"," Eighteen"," Nineteen"};
    private static String[] tens={" Twenty"," Thirty"," Fourty"," Fifty"," Sixty"," Seventy"," Eighty"," Ninety"};
    private static String[] maxs={"",""," Hundred"," Thousand"," Lakh"," Crore"};
    public AmountInWords()
    {
        amount="";
    }
    public String convertToWords(int n)
    {
        amount=numToString(n); String converted=""; int pos=1; boolean hun=false;
        while(amount.length()>0)
        {
            if(pos==1) // TENS AND UNIT POSITION
            {   if(amount.length()>=2) // 2DIGIT NUMBERS
                {   String C=amount.substring(amount.length()-2,amount.length()); amount=amount.substring(0,amount.length()-2);   converted+=digits(C);    }
                else if(amount.length()==1) // 1 DIGIT NUMBER
                {   converted+=digits(amount); amount="";   }    pos++; // INCREASING POSITION COUNTER
            }
            else if(pos==2) // HUNDRED POSITION
            {   String C=amount.substring(amount.length()-1,amount.length()); amount=amount.substring(0,amount.length()-1);
                if(converted.length()>0&&digits(C)!=""){   converted=(digits(C)+maxs[pos]+" and")+converted;hun=true;  }   else{   if(digits(C)=="")  ; else  converted=(digits(C)+maxs[pos])+converted;hun=true;}   pos++; // INCREASING POSITION COUNTER
            }
            else if(pos>2) // REMAINING NUMBERS PAIRED BY TWO
            {
                if(amount.length()>=2) // EXTRACT 2 DIGITS
                {   String C=amount.substring(amount.length()-2,amount.length()); amount=amount.substring(0,amount.length()-2);
                    if(!hun&&converted.length()>0)converted=digits(C)+maxs[pos]+" and"+converted;    else{ if(digits(C)=="")  ; else converted=digits(C)+maxs[pos]+converted; }   }
                else if(amount.length()==1) // EXTRACT 1 DIGIT
                {   if(!hun&&converted.length()>0)converted=digits(amount)+maxs[pos]+" and"+converted;    else{ if(digits(amount)=="")  ; else converted=digits(amount)+maxs[pos]+converted;   amount="";  } }    pos++; // INCREASING POSITION COUNTER
            }
        }
        return converted;
    }
    private String digits(String C) // TO RETURN SELECTED NUMBERS IN WORDS
    {
        String converted="";
        for(int i=C.length()-1;i>=0;i--)
        {   int ch=C.charAt(i)-48;
            if(i==0&&ch>1&&C.length()>1)    converted=tens[ch-2]+converted; // IF TENS DIGIT STARTS WITH 2 OR MORE IT FALLS UNDER TENS
            else if(i==0&&ch==1&&C.length()==2) // IF TENS DIGIT STARTS WITH 1 IT FALLS UNDER TEENS
            {   int sum=0;      for(int j=0;j<2;j++)    sum=(sum*10)+(C.charAt(j)-48);      return teen[sum-10];    }
            else{   if(ch>0)converted=units[ch]+converted;  } // IF SINGLE DIGIT PROVIDED    
        }   return converted;
    }
    private String numToString(int n) // CONVERT THE NUMBER TO STRING
    {
        String num="";  while(n!=0) {   num=((char)((n%10)+48))+num;  n/=10;  }      return num;
    }
    private void input()
    {
        Scanner in=new Scanner(System.in);
        try{System.out.print("Enter Amount to Convert in Words : ");
        num=in.nextInt();}catch(Exception e){System.out.println("Number Less than 1 Arab(1000000000) Only Possible.");System.exit(1);}
    }
    public static void main(String[] args)
    {
        AmountInWords obj=new AmountInWords();
        obj.input();
        System.out.println("Amount in Words : "+obj.convertToWords(num));

Using GNU lightning on an Intel Mac

GNU lightning is a library that generates assembly language code at run-time. This is an useful tool for writing a Just-In-Time compiler.


While your jit compiled code will work on most systems, you will probably experience crash under Mac OS X with an EXC_BAD_INSTRUCTION exception on a movdqa %xmm0,32(%esp) instruction. The reason if this crash may seem obscure but is in fact simple: on Mac OS X, the stack must be 16-byte aligned at the point of function calls. This is documented in the Mac OS X ABI Function Call Guide.

So, how to fix this problem ? Align the stack manually by jit compiling special instructions before every function call depending on the number of parameters pushed ? This is quite tedious. A better solution is to use gcc's -mstackrealignswitch.

Here is the documentation of this option:

-mstackrealign
Realign the stack at entry. On the Intel x86, the -mstackrealign option will generate an alternate prologue/epilogue that realigns
the runtime stack. This supports mixing legacy codes that keep a 4-byte aligned stack with modern codes that keep a 16-byte stack for SSE compatibility. The alternate prologue and epilogue are slower and bigger than the regular ones, and they require one dedicated register for the entire function. This also lowers the number of registers available if used in conjunction with the "regparm" attribute. Nested functions encountered while -mstackrealign is on will generate warnings, and they will not realign the stack when called.

Enjoy, your program is not crashing anymore. :-)
keywords: GNU lightning, Intel Mac, crash, EXC_BAD_INSTRUCTION, movdqa x

Suitable lens for Wedding Photography? - 1000% Free Software Downloads

Lenses are suitable for wedding in my opinion is a great zoom lens berbukaan zoom lens provides flexibility for when shooting. In addition, a large zoom lens berbukaan be practically used when shooting with enough light dark.

Specifically, my advice is lens Canon EF-S 17-55mm f/2.8 IS USM. Quality of this lens over the 18-55mm and has a large constant aperture (f/2.8), making it easier for me to make the background blur. I was able to photograph the entire wedding just with this lens.

Additional lenses are quite useful telephoto zoom lens like the Canon EF 70-200mm f/2.8 (ideally with Image Stabilization / IS). Such lenses are perfect for taking photos from a distance like a bridal portrait or expression, or when swapping ring.

If funds are insufficient, you should use the same brand with the camera lens so that compatibility and autofokusnya goes well. If funds are limited, alternative to other brands such as Tamron and Sigma should be considered.

In recent years, many quality lenses from Tamron and Sigma are of high quality with a more affordable price. Examples Tamron 70-200mm VC USD f/2.8 or Sigma 70-200mm f/2.8 OS HSM. Do not forget to flash very useful to help the dark lighting in the room.

Nokia EOS 41 Megapixel - 1000% Free Software Downloads

It is no secret that Nokia will release a smartphone with a 41 megapixel camera capability, which is dubbed as the EOS. Well, here's roughly shaped appearance.

Image is Nokia EOS allegedly leaked in cyberspace universe. Of shape, revealed that mobile phone based on Windows Phone is quite interesting.