Saturday, June 1, 2013

Divide One Number By Another



SOURCE CODE

class Division {
public static void main(String args[]) {        // start of main() method.
int a=10, b=5;  // initializing a & b of type integer. 
double c; // initializing c of type double. 
c = a / b; //dividing a by b using "/" operator and storing in c.
System.out.println("Division is : "+c);   // printing result
} // end of main() method.
} // end of class.



OUTPUT

Division is : 2.0





NOTE:-


Instead of using
                c = a / b;

              System.out.println("Multiplication is : "+c);
you can use following single line of code

                System.out.println("Multiplication is : "+(float)(a/b));
If you want to use above single line then you don't have to declare the third variable i.e. c.
variable at denominator should be greater than 0. if it is not then it will show the error.

0 comments:

Post a Comment

    Translate

    Protected by Copyscape