Hack windows pc using kali linux

Basic calculation in java


                                                 

           Program To Add Two Numbers      

     The basic of programming is to learn simple math operations. However you  need not to be a great mathematician in order to do so.There is a Math class in java which enable you to do so.However here we are not covering it here i am showing you use of different operators in java ,and here we begin with our addition program.

    class Addition
    {

    public static void main(String args[])
    {
    int a,b,sum;  // declaring variables use to store two numbers and sum of it
    a=5;
    b=4;
    sum=a+b;  // "+" operator is used to add the values stored in variables
    System.out.print("The sum of  "+a+" and "+b+" is "+sum);
    }//closing of main()
    }// closing of class


    Output:-


    The sum of  5 and 4 is 9






                  Program To Subtract Two Numbers



    Now here is the program to subtract two numbers.

    class Subtraction
    {
    public static void main(String args[])
    {
    int a,b,sub;  // declaring variables use to store two numbers and difference of it
    a=5;
    b=4;
    sub=a-b;  // "+" operator is used to add the values stored in variables
    System.out.print("The difference of  "+a+" and "+b+" is "+sub);
    }//closing of main()
    }// closing of class


    Output:-


    The difference of  5 and 4 is 1



    TIPS  :-Do you know the difference between subtraction and difference? If i say subtract A from B ,then it means (B - A),notice it can be negative as well as positive depending upon values but if i say find the difference between A and B then you have to decide which one is greater  and then you will subtract ,it will give you the difference which can't be negative. 

                        

                       Program To Multiply Two Numbers

    Now below is the program to multiply two numbers.

    class Multiplication
    {
    public static void main(String args[])
    {
    int a,b,pro;  // declaring variables use to store two numbers and product of it
    a=5;
    b=4;
    pro=a*b;  // "+" operator is used to add the values stored in variables
    System.out.print("The product of  "+a+" and "+b+" is "+pro);
    }//closing of main()
    }// closing of class


    Output:-


    The product of  5 and 4 is 20





                              Program To Divide Two Numbers

    Now here is the program to divide two numbers.

    class Division
    {
    public static void main(String args[])
    {
    int a,b,div;  // declaring variables use to store two numbers and quotient of it
    a=5;
    b=4;
    div=a/b;  // "/" operator is used to divide the values stored in variables
    System.out.print("The quotient of  "+a+" / "+b+" is "+div);
    }//closing of main()
    }// closing of class


    Output:-


    The quotient of  5 / 4 is 1



      If i am not wrong,my dear readers would be thinking that i am poor in maths as i have written that 5 / 4=1
    but let me tell you and explain you this stuff.In java and i think in almost every programming language there is a specific rule to solve division and multiplication.If you have notice 5 is of integer type and so is  4 ,and in java when an integer is either divided or multiplied by an integer the result is always an integer.
    E.g :-
            4 / 3 = 1
            45 / 7= 6
    Fractional part is   ignored only the quotient part is take.Now what to do to take fraction also in account it's simple just convert one to double or floating datatype and the work is done.
    E.g :-
        4 / 3.0=1.33333........
        45.0 / 7=6.428.......

    To make the above program take fractional part into consideration we need to do following changes.(Shown by Blue text)

                               Program To Divide Two Numbers

    Now here is the program to divide two numbers.

    class Division
    {
    public static void main(String args[])
    {
    double a,b,div;  // declaring variables use to store two numbers and quotient of it
    a=5.0;// not a compulsion to write 5.0
    b=4.0;
    div=a/b;  // "/" operator is used to divide the values stored in variables
    System.out.print("The quotient of  "+a+" / "+b+" is "+div);
    }//closing of main()
    }// closing of class


    Output:-


    The quotient of  5 / 4 is 1.25




    The whole stuff can be categorized  as following.


    int     / int    = int          23    / 4   =5
    float /  int    = float      23.0 / 4 =5.75
    int    /  float = float      23    / 4.0=5.75
    float /  float = float      23.0 / 4.0=5.75

    Now if i am not wrong you might have guessed a similar problem in all the above programs,Yes the problem is the above programs do calculation on only two constants i.e. 4 and 5 .So to upgrade these programs  to programs which might give user the feasibility  to enter their own numbers we have a tutorial on Taking User Input. In which i will tell you how to take input from the user through keyboard.



                                                  



    Comments