Saturday, June 8, 2013

Modulus of a Number


SOURCE CODE

class Modulus {     // start of class Modulus.
public static void main(String args[]) {     // start of main() method.
int a=15, b=4, c;     // Initializing and declaring variables.
c=a%b;     // finding mod of a number a and storing it in variable c.
System.out.println("Result is "+c);     // printing result.
}     // end of main() method.
}     // end class modulus .


OUTPUT

Result is 3




NOTE:-
 
Mod (%) of a number shows the remainder obtained by dividing another number to that number. You can check by assigning different value to the variable b. 
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 : "+(a%b));
If you want to use above single line then you don't have to declare the third variable i.e. c.
In another case you can also use
                c = a%5; or c = a % any_number;
so that you don't have to initialize and assign variable b.

0 comments:

Post a Comment

    Translate

    Protected by Copyscape