

ITEngineer8995
May 26, 2020
ITEngineer8995
May 23, 2020
ITEngineer8995
May 20, 2020

ITEngineer8995
May 15, 2020

ITEngineer8995
May 11, 2020
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:
ITEngineer8995
May 09, 2020
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:
ITEngineer8995
May 09, 2020
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:
ITEngineer8995
May 09, 2020
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:
ITEngineer8995
May 09, 2020
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:
ITEngineer8995
May 09, 2020
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:
ITEngineer8995
May 09, 2020
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
May 09, 2020
