-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

About me

Hello

I'mITEngineer8995

Spatial Data Specialist

Do you want all of your codes in one place?? Then you are landed on the right blog... Here you will find varieties of codes of various languages starting from noob to advance with their samples. Do learn, execute and share your knowledge with others. Do visit my YouTube Channel for execution of these codes .

Experience

Video Clone Detector using Hadoop

2017-2018

A detector which differentiates copied videos from the original ones using Hadoop.

Portfolio

Java Program To Calculate Power of a Number using pow() function


Java Program To Calculate Power of a Number using pow() function

In this Java Program, we calculate the power of the number using while loop.

The Power (exponent) of the numbers says how many times to use the number in a multiplication. It is written in a small number right and above the base number. 

E.g. the little 3 says to use 10 two times in a multiplication.

                     103 = 10 * 10 * 10 = 1000 

Logic :  result = Math.pow(number, p);

Step 1 : Create the variables named as number and p and store 10 in number and 3 in p.

Step 2 : Create variable result, Use the Math.pow() function to calculate the power of the number and store the result in the variable named result.

Step 3 : Display the result by using the System.out.print.

Program :

public class Power2 
    {
        public static void main(String[] args)
        {
            int number = 10, p =3;
            double result = Math.pow(number, p);
            System.out.println(number+"^"+p+"="+result);
        }
}

Output:

10^3 = 1000

For execution of the same do visit my YouTube Channel:


Java Program To Calculate Power of a Number Using While Loop


Java Program To Calculate Power of a Number Using While Loop

In this Java Program, we calculate the power of the number using while loop.

The Power (exponent) of the numbers says how many times to use the number in a multiplication. It is written in a small number right and above the base number.

E.g. the little 2 says to use 5 two times in a multiplication.

                       52  = 5 * 5 = 25 

 Logic :    while(i!=0)
            {
                result *= number;
                --i;
            }

Step 1 : Create variable number and store the number 5 in it, similarly create variable p and store 2 in it.

Step 2 : Create variable result and store 1 in it.

Step 3 : Initialize i to p.

Step 4 : Apply the while loop

Step 5 : Do the multiplication and store the number into the variable result.

Step 6 : Decrement the i

Step 7 : Display the result by using System.out.print.
 
Program :

public class Power 
    {
        public static void main(String[] args)
        {
            int number = 5, p = 2;
            long result = 1;
            
            int i = p;
            while(i!=0)
            {
                result *= number;
                --i;
            }
            System.out.println(number+"^"+p+ "="+result);
                   
        }
    
}

Output:

2^5 = 32

For the execution of the same do visit my YouTube Channel:

Java Program To Find Quotient and Remainder


Java Program To Find Quotient and Remainder

In this Java Program, we find the Quotient and Remainder of the given two numbers.

The Quotient is the quantity produced by the division of the two numbers while the Remainder is the integer left over after dividing one integer by another.
             
E.g.                  7       👈 Quotient 
                     ___
                  2 | 15
                   -  14
                    ____
                         1       👈 Remainder
                               

In the above example, we take two numbers num 1 = 15 and num 2 = 2, where num 1 is the dividend and num 2 is the divisor.  When num 1 is divided by the num 2 we get the quotient as 7 and remainder as 1.

Logic : quotient = num 1 / num 2
          : remainder = num 1 % num 2

Step 1 : Create variables num1 and num2 and store the numbers 15 in num1 and 2 in num2.

Step 2 : Create variable quotient and apply the logic and store the result into the variable quotient.

Step 3 : Create variable remainder and apply the logic and store the result into the variable remainder.

Step 4 : Display the result by using the System.out.print.

Program :

public class QuotientRemainder 
    {
        public static void main(String[] args)
        {
            int num1 = 15, num2 = 2;
            int quotient = num1 / num2;
            int remainder = num1 % num2;
            System.out.println("Quotient is:" +quotient);
            System.out.println("Remainder is:" +remainder);
        }
    
}

Output:

Quotient is : 7
Remainder is : 1

For execution of the same do visit my YouTube Channel:

Java Program To Calculate Simple Interest


Java Program To Calculate Simple Interest

In this Java Program, we calculate Simple Interest.

The interest calculated on the principal amount of the loan is called as simple interest. It is determined by calculating the daily interest rate by the principal by the number of days that elapse between payments.

                                         Simple Interest = (P * R * T) / 100

P = Principal amount
R = Rate per annum
T = Time in years

Let's say a man deposit 2000 INR in a bank account at an interest rate of 6% per annum for 3 years, calculate the simple interest at the end of 3 years.

Simple Interest = 2000 * 6 * 3 / 100 = 360 INR

Logic : sinterest = (p * r * t) / 100

Step 1 : Import java.util.Scanner

Step 2 : Create variables p, r, t, sinterest.

Step 3 : Ask the user to enter the principal by using the Scanner 

Step 4 : Store the user entered principal in the variable named p

Step 5 : Ask the user to enter the rate of interest by using the Scanner 

Step 6 : Store the user entered rate of interest in the variable named r

Step 7 : Ask the user to enter the time period by using the Scanner 

Step 8 : Store the user entered time period in the variable named t

Step 9 : Close the Scanner to avoid memory leak

Step 10 : Calculate the simple interest by using the logic and store it in the variable named sinterest.

Step 11 : Display the result using System.out.print.

Program :

import java.util.Scanner;

public class SimpleInterest
    {
        public static void main(String[] args)
        {
            float p, r, t, sinterest;
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter the Principal:");
            p = sc.nextFloat();
            System.out.print("Enter the Rate of interest:");
            r = sc.nextFloat();
            System.out.print("Enter the Time period:");
            t = sc.nextFloat();
            sc.close();
            sinterest = (p * r * t) / 100;
            System.out.print("Simple Interest is:"+sinterest);
                
        }
    
}

Output:

Enter the Principal : 2000
Enter the Rate of interest : 6
Enter the Time period : 3
Simple Interest is : 360.0

For execution of the same do visit my YouTube Channel:

Java Program To Calculate Compound Interest


Java Program To Calculate Compound Interest

In this Java Program, we calculate the Compound Interest.

The addition of interest to the principal sum of loan or deposit or in other words interest on interest is called as Compound Interest.

                                                P (1 + R/n) (nt) - P

Here, P = Principal Amount
      R = annual interest rate
      t = time the money is invested or borrowed for.
      n = number of times the interest is compounded per unit t


Let's say an amount of  $2,000 is deposited into a bank account as a fixed deposit at an annual interest rate of 8 %, compound monthly, the compound interest after 5 years would be:

P  = 2000
R = 8/100 = 0.08 (decimal)
n = 12
t =  5

Let's put these values into the formula

Compound Interest : 2000 (1 + 0.08 / 12)(12 * 5) - 2000 = $979.69

So, the compound interest after 5 years is $979.69

Logic : double amount = p * Math.pow(1 + (r / n), n * t);
             double cinterest = amount - p;

Step 1 : Create class CompoundInterest

Step 2 : Create method calculate and initialize variables p, t, r and n.

Step 3 : Apply the logic 

Step 4 : Call the object of the CompoundInterest Class in the main method. 

Program :

public class CompoundInterest 
    {
        public void calculate(int p, int t, double r, int n)
        {
            double amount = p * Math.pow(1 + (r / n), n * t);
            double cinterest = amount - p;
            System.out.println("Compound Interest after " + t + "years:" + cinterest);
            System.out.println("Amount after " + t + "years:" + amount);
        }
        
        public static void main (String[] args)
        {
            CompoundInterest obj = new CompoundInterest();
            obj.calculate(2000, 5, .08, 12);
        }
    
}

Output:

Compound Interest after 5 years : 979.6914
Amount after 5 years : 2972.69141660321

For execution of the same do visit my YouTube Channel:



Java Program To Check Leap Year


Java Program To Check Leap Year

In this Java Program, we check whether the year entered by the user is a leap year or not.

The year which is exactly divisible by 4 is a Leap Year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.

For e.g. the years 1700, 1800 and 1900 are not leap years, but the years 1600 and 2000 are leap years.

Logic :    if(year % 4 == 0)
                {
                    if(year % 100 == 0)
                    {
                        if(year % 400 == 0)
                            isLeap = false;
                    }
                    else
                        isLeap = true;
                }
                else {
                    isLeap = false;
                }

Step 1 : Import java.util.Scanner

Step 2 : Create a variable named year.

Step 3 : Ask the user to enter the year by using System.out.println method.

Step 4 : Store the user entered year in the variable year.

Step 5 : Close the Scanner to avoid memory leak.

Step 6 : Apply the boolean isLeap = false

Step 7 : Apply the if loop

Step 8 : Display the result.

Program :

import java.util.Scanner;

public class LeapYear 
        {
            public static void main(String[] args)
            {
                int year;
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter any Year:");
                year = sc.nextInt();
                sc.close();
                boolean isLeap = false;
                
                if(year % 4 == 0)
                {
                    if(year % 100 == 0)
                    {
                        if(year % 400 == 0)
                            isLeap = false;
                    }
                    else
                        isLeap = true;
                }
                else {
                    isLeap = false;
                }
                if(isLeap == true)
                    System.out.println(year +"is a Leap Year");
                else 
                    System.out.println(year + "is not a Leap Year");
            }
    
}

Output:

Enter any Year:
2001
2001 is not a Leap Year.

For execution of the same do visit my YouTube Channel:


Java Program To Multiply Two Floating Point Numbers


Java Program To Multiply Two Floating Point Numbers


In this Java Program, we perform multiplication of two floating point number.


Logic : double c = double a * double b


Step 1 : Import java.util.Scanner.


Step 2 : Ask the user to enter the first number by using System.out.print method.


Step 3 : Store the user entered number in the variable num1.


Step 4 : Ask the user to enter the second number by using System.out.print method.


Step 5 : Store the user entered number in the variable num2.


Step 6 : Close the Scanner to avoid memory leak.


Step 7 : Calculate the product of the two numbers by using * operator and store the product in the variable product.


Step 8 : Display the result by using System.out.print.


Program :


import java.util.Scanner;


public class Multiply2

    {

        public static void main(String[] args)

        {

            //This reads the input provided by user using keyboard

           

            Scanner sc = new Scanner(System.in);

            System.out.print("Enter first number:");

           

            //This method reads the number provided using keyboard

            double num1 = sc.nextDouble();

           

            System.out.print("Enter second number:");

            double num2 = sc.nextDouble();

           

            //Closing Scanner after the use

            sc.close();

           

            //Calculating product of two numbers

            double product = num1*num2;

           

            //Displaying the multiplication result

            System.out.println("Output:"+product);

           

        }

   


}


Output:


Enter First Number : 1.5

Enter Second Number : 2.5

Output: 3.75


For execution of the same do visit my YouTube Channel:


https://youtu.be/4pYXmpHd88k

Java Program to Multiply Two Integer Numbers


Java Program to Multiply Two Integer Numbers


In this Java Program, we perform multiplication of the two integer numbers.


Logic : int c = int a * int b


Step 1 : Import java.util.Scanner.


Step 2 : Ask the user to enter the first number by using System.out.print method.


Step 3 : Store the user entered number in the variable num1.


Step 4 : Ask the user to enter the second number by using System.out.print method.


Step 5 : Store the user entered number in the variable num2.


Step 6 : Close the Scanner to avoid memory leak.


Step 7 : Calculate the product of the two numbers by using * operator and store the product in the variable product.


Step 8 : Display the result by using System.out.print.


Program :


import java.util.Scanner;


public class Multiply

        {

            public static void main(String[] args)

            {

               

                //This reads the input provided by user using keyboard

               

                Scanner sc = new Scanner(System.in);

                System.out.print("Enter first number:");

               

                //This method reads the number provided using keyboard

                int num1 = sc.nextInt();

               

                System.out.print("Enter second number:");

                int num2 = sc.nextInt();

               

                //Closing Scanner after the use

                sc.close();

               

                //Calculating product of two numbers

                int product = num1*num2;

               

                //Displaying the multiplication result

                System.out.println("Output:"+product);

               

            }


}



OUTPUT:


Enter first number : 15

Enter second number : 6

Output : 90


For execution of the same do visit my YouTube Channel:


Java Program to add two complex numbers


Java Program to add two complex numbers


In this Java Program, we perform addition of two complex numbers.


Complex numbers generally consists of two parts, real number and imaginary number. In this program, we add real number together and imaginary numbers together. The imaginary number has a letter i written after the number. Eg. 2i, 5i etc.


                                    2 + 5i  + 4 + 3i = 6 + 8i


Step 1 : Create two variables named real and img to store both the numbers.


Step 2 : Create a Constructor to initialize the complex number. For e. g. when we create an instance of this class like this ComplexNumber temp = new ComplexNumber(0, 0); it actually creates a complex number 0 + 0i.


Step 3 : Declare a method sum() to add the two numbers by adding their real and imaginary parts together.


Step 4 : Create a temporary complex number to hold the sum of the two numbers.


Step 5 : Return the output complex number.


Step 6 : Display the result by using System.out.printf.


Program :


public class AddTwoComplexNumbers

    {

        //for real and imaginary parts of complex numbers

        double real, img;

     

        //constructor to initialize the complex number

        AddTwoComplexNumbers(double r, double i)

        {

            this.real = r;

            this.img = i;

        }

     

        public static AddTwoComplexNumbers sum(AddTwoComplexNumbers a1, AddTwoComplexNumbers a2)

        {

            //creating a temporary complex number to hold the sum of two numbers

            AddTwoComplexNumbers temp = new AddTwoComplexNumbers(0,0);

         

            temp.real = a1.real + a2.real;

            temp.img = a1.img + a2.img;

         

            //returning the output complex number

            return temp;

        }

     

        public static void main(String [] args)

        {

            AddTwoComplexNumbers a1 = new AddTwoComplexNumbers(5.5,4);

            AddTwoComplexNumbers a2 = new AddTwoComplexNumbers(1.2,3.5);

            AddTwoComplexNumbers temp = sum(a1,a2);

            System.out.printf("Sum is:"+temp.real+" + "+temp.img+"i");

        }

}


Output: 


Sum is : 6.7 + 7.5i


For execution of the same do visit my YouTube Channel:


https://youtu.be/E7c50kFqlFE

Java program to add two binary numbers


Java program to add two binary numbers


In this Java Program, we perform addition of two binary numbers.


Binary number  system consists of 0's and 1's hence a binary number is made up of only 0 and 1.


                                             Adding Numbers 11100 & 10101

                                                                       

                                                                       1                               11                                 1

    11100                        11100                    11100                         11100                           11100       

+  10101         👉       +  10101       👉      +  10101          👉       +  10101           👉        +  10101              

------------                    -------------                -------------                    -------------                   -------------

            1                              01                          001                          0001                          110001


Step 1 : Import java.util.Scanner Class


Step 2 : Create two variables b1 and b2 to hold the two binary numbers entered by the user.


Step 3 : Initialize int i = 0 and carry = 0.


Step 4 : Create an array int[] sum to hold the output binary number


Step 5 : By using the Scanner, take the two binary numbers from the user and store it in b1 and b2.


Step 6 : Close the Scanner to avoid the memory leak.


Step 7 : Add the two binary numbers bit by bit storing the result into the array.


Step 8 : Display the result by using System.out.println method.


Program :


import java.util.Scanner;


public class AddTwoBinaryNumbers

    {

        public static void main (String[] args)

        {

            //Two variables to hold two input binary numbers

            long b1, b2;

            int i = 0, carry = 0;

           

            //This is to hold the output binary number

            int[] sum = new int[10];

           

            //To read the input binary numbers entered by the user

            Scanner scanner = new Scanner(System.in);

           

            //getting first binary number from user

            System.out.print("Enter first binary number:");

            b1 = scanner.nextLong();

           

            //getting second binary number from the user

            System.out.print("Enter second binary number:");

            b2 = scanner.nextLong();

           

            //closing scanner after use to avoid memory leak

            scanner.close();

           

            while (b1 != 0 || b2!=0)

               {

                sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);

                carry = (int)((b1 % 10 + b2 % 10 + carry)/2);

                b1 = b1 / 10;

                b2 = b2 / 10;

               

                }

           

            if (carry !=0)

                {

                    sum[i++] = carry;

                }

            --i;

            System.out.print("Output:");

            while (i >=0)

                {

                    System.out.print(sum[i--]);

                }

            System.out.print("\n");

                }

               

            }



Output:


Enter first binary number: 11100

Enter second binary number: 10101

Output: 110001


For the execution of the same do visit my YouTube channel:


https://youtu.be/RNfVTsCfNrU

Java Program to check even odd numbers


Java Program to check even odd numbers


In this Java Program, We check whether the number entered by the user is an even number or an odd number.


Logic : If the number is divisible by 2 then it is an even number or its an odd number.


Code : if (num %  2) == 0


Step 1 : Import java.util.Scanner class.


Step 2 : Create an variable named as num.


Step 3 : Use the System.out.println method for the user to enter the number.


Step 4 : Store the user entered number in the variable num.


Step 5 : Apply the logic of the even and odd number by using the if loop. 


Step 6 : Display the result by using the System.out.println method.


Program :

 

import java.util.Scanner;


public class CheckEvenOdd

    {

        public static void main(String[] args)

        {

            int num;

            System.out.println("Enter an Integer number:");

         

            //The input provided by user is stored in num

            Scanner input = new Scanner(System.in);

            num = input.nextInt();

         

            //If number is divisible by 2 then it's an even number else odd number

         

            if(num % 2 ==0)

             

                System.out.println("Entered number is even");

            else

                System.out.println("Entered number is odd");

         

        }

}


Output:


Enter an Integer Number:

78

Entered Number is even


Enter an Integer Number:

77

Entered Number is odd


For execution of the same do visit my YouTube channel:


https://youtu.be/qnNzhL_iZ7E






Java Program to add two numbers using Scanner


Java Program to add two numbers using Scanner


Logic : int c = int a + int b


In this Java Program, we use the Scanner class to get the numbers from the user.  


Step 1 : Create 3 variables named as num1, num2 and sum. 


Step 2 : Insert Scanner Class for taking the input from the user.  


Step 3 : Use System.out.println method to take input of first number from the user.


Step 4 : Store the user's input in variable num1


Step 5 : Use System.out.println method to take input of second number from the user.


Step 6 : Store the user's input in variable num2


Step 7 : Close the Scanner.


Step 8 : Perform the addition of the two numbers entered by the user with the help of  "+" operator and store the result in the variable named as sum.


Step 9 : Display the result by using the System.out.println method. 


Program :

 

import java.util.Scanner;


public class AddTwoNumbers2

    {

        public static void main(String[] args)

        {

            int num1, num2, sum;

            Scanner sc = new Scanner(System.in);

            System.out.println("Enter First Number:");

            num1 = sc.nextInt();

         

            System.out.println("Enter Second Number:");

            num2 = sc.nextInt();

         

            sc.close();

            sum = num1 + num2;

            System.out.println("Sum of these numbers:"+sum);

         

        }

}


Output:


Enter First Number :

121

Enter Second Number:

19

Sum of these numbers: 140


For execution of the code do visit my YouTube channel:


https://youtu.be/OY7-fUy9bjo

Java Program to add two numbers


Java Program to add two numbers

In this Java Program, we perform addition of two numbers.

Logic : int c = int a + int b

Step 1 : Take two numbers 5 and 15 and stored it in the variables named as num1 and num2 and create another variable named as sum.

Step 2 : Perform the addition of the two numbers by using the + operator and store the result of the addition in the variable named as sum.

Step 3 : Use the System.out.println method to display the result to the user.

Program:

public class AddTwoNumbers
    {
        public static void main(String[] args)
        {
            int num1 = 5, num2 = 15, sum;
            sum = num1 + num2;
       
            System.out.println("Sum of these numbers:"+sum);
        }
}

Output :

Sum of these numbers: 20

For execution of the code to visit my YouTube channel.

https://youtu.be/MUjD5D2ZWmk

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages