Author: saqibkhan

  • Multiply Two Numbers in Java:

    public class MultiplyTwoNumbers {
    
     public static void main(String[] args) {
    
     double first = 2.5, second = 4.5;
    
     double product = first * second;
    
     System.out.println(“The product is: ” + product);
    
     }

    Output:

    The product is: 11.25
  • Add Two Complex Numbers in Java:

    class Complex {
    
     double real, imaginary;
    
     Complex(double r, double i) {
    
     this.real = r;
    
     this.imaginary = i;
    
     }
    
     public static Complex add(Complex c1, Complex c2) {
    
     Complex temp = new Complex(0, 0);
    
     temp.real = c1.real + c2.real;
    
     temp.imaginary = c1.imaginary + c2.imaginary;
    
     return temp;
    
     }
    
    }
    
    public class AddComplexNumbers {
    
     public static void main(String[] args) {
    
     Complex c1 = new Complex(4.5, 5);
    
     Complex c2 = new Complex(2.5, 3.5);
    
     Complex temp = Complex.add(c1, c2);
    
     System.out.println(“Sum = ” + temp.real + ” + ” + temp.imaginary + “i”);
    
     }
    
    }

    Output:

    Sum = 7.0 + 8.5i
  • Add Two Binary Numbers in Java:

    public class AddBinaryNumbers {
    
     public static void main(String[] args) {
    
     long binary1 = 1010, binary2 = 1101;
    
     int i = 0, remainder = 0;
    
     long[] sum = new long[20];
    
     while (binary1 != 0 || binary2 != 0) {
    
     sum[i++] = (binary1 % 10 + binary2 % 10 + remainder) % 2;
    
     remainder = (int) (binary1 % 10 + binary2 % 10 + remainder) / 2;
    
     binary1 = binary1 / 10;
    
     binary2 = binary2 / 10;
    
     }
    
     if (remainder != 0) {
    
     sum[i++] = remainder;
    
     }
    
     –i;
    
     System.out.print(“Sum of two binary numbers: “);
    
     while (i >= 0) {
    
     System.out.print(sum[i–]);
    
     }
    
     }

    Output:

    Sum of two binary numbers: 11011
  • Print Pattern in Java:

    public class PrintPattern {
    
     public static void main(String[] args) {
    
     int rows = 5;
    
     for (int i = 1; i <= rows; ++i) {
    
     for (int j = 1; j <= i; ++j) {
    
     System.out.print(“* “);
    
     }
    
     System.out.println();
    
     }
    
     }
    
    }

    Output:

    * 
    
    * * 
    
    * * * 
    
    * * * * 
    
    * * * * *
  • Factorial of a Number:

    public class Factorial {
    
     public static void main(String[] args) {
    
     int num = 5, factorial = 1;
    
     for(int i = 1; i <= num; ++i) {
    
     factorial *= i;
    
     }
    
     System.out.println(“Factorial of ” + num + ” is: ” + factorial);
    
     }

    Output:

    Factorial of 5 is: 120
  • Check Even or Odd Number:

    public class EvenOdd {
    
     public static void main(String[] args) {
    
     int num = 5;
    
     if(num % 2 == 0)
    
     System.out.println(num + ” is even.”);
    
     else
    
     System.out.println(num + ” is odd.”);
    
     }

    Output:

    5 is odd.
  • Find Maximum of Three Numbers:

    public class MaxOfThreeNumbers {
    
     public static void main(String[] args) {
    
     int num1 = 10, num2 = 20, num3 = 15, max;
    
     max = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
    
     System.out.println(“Maximum of ” + num1 + “, ” + num2 + “, and ” + num3 + ” is: ” + max);
    
     }

    Output:

    Maximum of 10, 20, and 15 is: 20
  • Addition of Two Numbers:

    public class AddNumbers {
    
     public static void main(String[] args) {
    
     int num1 = 5, num2 = 10, sum;
    
     sum = num1 + num2;
    
     System.out.println(“Sum of ” + num1 + ” and ” + num2 + ” is: ” + sum);
    
     }

    Output:

    Sum of 5 and 10 is: 15
  • Hello World Program:

    public class HelloWorld {
    
     public static void main(String[] args) {
    
     System.out.println(“Hello, World!”);
    
     }
    
    }

    Output:

    Hello, World!
  • Kotlin Comment

    Comments are the statements that are used for documentation purpose. Comments are ignored by compiler so that don’t execute. We can also used it for providing information about the line of code. There are two types of comments in Kotlin.

    1. Single line comment.
    2. Multi line comment.

    Single line comment

    Single line comment is used for commenting single line of statement. It is done by using ‘//’ (double slash). For example:

    fun main(args: Array<String>) {  
    
    // this statement used for print   
    
        println("Hello World!")  
    
    } 

      Output

      Hello World!
      

      Multi line comment

      Multi line comment is used for commenting multiple line of statement. It is done by using /* */ (start with slash strict and end with star slash). For example:

      fun main(args: Array<String>) {  
      
      /* this statement 
      
         is used 
      
         for print */  
      
          println("Hello World!")  
      
      } 

        Output:

        Hello World!