Check Prime Optimized Function Code Java Programming

Optimized Function: Check if a number is Prime or not Java code: 🧊

public class CheckPrimeOptimized {

    public static boolean isPrime(int n) {
        if (n == 2) {
            return true;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPrime(6833));
        System.out.println(isPrime(6834));

    }
}

java .\CheckPrimeOptimized.java

true

false

Call by value Example using java programming code

Call by value Example using java programming code 

public class CallByValue {

    public static void swap(int a, int b) {
        // Swap
        int temp = b;
        b = a;
        a = 35;

    }

    public static void main(String[] args) {
        // swap - value exchange
        int a = 45;
        int b = 25;
        swap(a, b);
        System.out.println("a is : " + a);
        System.out.println("b is : " + b);

    }
}


Product of a and b simple java programming code

Product of a and b 

public class FindProductAB {
    public static int multiply(int a, int b) {
        int product = a * b;
        return product;
    }

    public static void main(String[] args) {
        int Nov = 25;
        int Dec = 4;
        // int prod = multiply(3, 5);
        int prod = multiply(Nov, Dec);
        System.out.println(Nov + " * " + Dec + " = " + prod);
        prod = multiply(3, 5);
        System.out.println(prod);
    }
}


Simple Factorial Function using JAVA programming code

Factorial Function

public class FacrorialOfNumberN {

    public static int factorial(int n) {
        int f = 1;
        for (int i = 1; i <= n; i++) {
            f = f * i;
        }
        return f; // factorial of n.
    }

    public static void main(String[] args) {
        // int number = 9;
        // int fac = factorial(number);
        // System.out.println(fac);
        System.out.println(factorial(4));

    }
}

I love u bby 🤭

V.1 Find Binomial Coefficient using Factorial Function Java Programming Code

easy method Find Binomial Coefficient using Factorial Function Java Programming Code

public class BinomialCoefficient {
    public static int factorial(int n) {
        int f = 1;
        for (int i = 1; i <= n; i++) {
            f = f * i;
        }
        return f; // factorial of n.
    }

    public static void main(String[] args) {
        int n = 5, r = 2;
        int nmr = n - r;
        int factN = factorial(n);
        int factR = factorial(r);
        int factNMR = factorial(nmr);
        int BinCo = factN / (factR * factNMR);
        System.out.println(BinCo);

    }
}

fghd

V.2 Find Binomial Coefficient using Factorial Function Java Programming Code

Find Binomial Coefficient using Factorial Function Java Programming Code

public class BinCoeff {

    public static int factorial(int n) {
        int f = 1;
        for (int i = 1; i <= n; i++) {
            f = f * i;
        }
        return f; // factorial of n.
    }

    public static int BinCoeff(int n, int r) {
        int fact_n = factorial(n);
        int fact_r = factorial(r);
        int fact_nmr = factorial(n - r);
        int BinCoeff = fact_n / (fact_r * fact_nmr);
        return BinCoeff;
    }

    public static void main(String[] args) {
        System.out.println(BinCoeff(5, 2));

    }
}

🤭

Function Overloading using Parameters, Java Programming Code

Function Overloading using Parameters, Java Programming Code

public class functionOverLoading {
    // func to calc sum of 2 nums
    public static int sum(int a, int b) {
        return a + b;
    }

    // func to calc sum of 3 nums
    public static int sum(int a, int b, int c) {
        return a + b + c;
    }

    public static void main(String[] args) {
        System.out.println(sum(5, 6));
        System.out.println(sum(5, 7, 12));
    }
}


Function Overloading using Data Types, Java programming Code

Data Types: Function Overloading using Data Types

public class functionOverLoadingDataTypes {

    // func to cal int sum
    public static int sum(int a, int b) {
        return a + b;
    }

    // func to cal float sum
    public static float sum(Float a, Float b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(sum(10, 15));
        System.out.println(sum(10.5f, 15.57f));

    }
}


Function: Check if a number is Prime or not Java programming code

Check if a number is Prime or not Function Code:

public class PrimeNumF1 {
    public static boolean isPrime(int n) {

        // corner cases
        // 2
        if (n == 2) {
            return true;
        }

        // boolean isPrime = true;
        for (int i = 2; i <= n - 1; i++) {
            if (n % i == 0) { // Completely dividing
                // isPrime = false;
                // We any use any technic in comment options.
                // break;
                // return isPrime;
                return false;
            }
        }
        // return isPrime;
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isPrime(7907));
    }
}


java .\PrimeNumF1.java

true

NUMBER PYRAMID Pattern simple Java Programming code

 Output of Code:

java .\NumberPyramid.java
     1
    2 2
   3 3 3
  4 4 4 4
 5 5 5 5 5
6 6 6 6 6 6






Java Code:

public class NumberPyramid {
    public static void main(String[] args) {
        int n = 6;
        // char ch = 'A';
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " ");
            }

            System.out.println(" ");

        }
    }
}

Print Half Pyramid Character Pattern Java Programming Code

  Output:

Simple Java Code:

public class printCharacterPatternHalfPyramid {
    public static void main(String[] args) {
        int n = 7;
        char ch = 'A';
        for (int i = 1; i <= n; i++) {
            // number print
            for (int num = 1; num <= i; num++) {
                System.out.print(ch);
                ch++;
            }
            System.out.println();
        }
    }
}

...



java .\printCharacterPatternHalfPyramid.java

A

BC

DEF

GHIJ

KLMNO

PQRSTU

Half Pyramid Pattern Simple Java Programming Code

 This is Half Pyramid image, but in programming: this code is not fixed name, it's used to just define this code.




Half Pyramid Pattern Java Code:

public class HalfPyramid {
    public static void main(String[] args) {
        int n = 11;
        for (int i = 1; i <= n; i++) {
            // number print
            for (int num = 1; num <= i; num++) {
                System.out.print(num);
            }
            System.out.println();
        }
    }
}


Output: 

java .\HalfPyramid.java

1

12

123

1234

12345

123456

1234567

12345678

123456789

12345678910

1234567891011

Inverted Stars Pattern Java Programming Code (Using Nested Loop)

Using Nested Loop Inverted Star Pattern

import java.util.Scanner;

public class InvertedStarPattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int line = 1; line <= n; line++) {
            // one line
            for (int star = 1; star <= n - line + 1; star++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}

Output: 

7

*******

******

*****

****

***

**

*

Nested Loops Star Pattern Java Programming java code

Simple Java Code for Star Pattern.

import java.util.Scanner;

public class StarPatternUsingNastedLoops {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        for (int line = 1; line <= i; line++) {
            // one line
            for (int star = 1; star <= line; star++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}

Output: 

java .\StarPatternUsingNastedLoops.java

7

*

**

***

****

*****

******

*******


Java program to print the multiplication table of a number N, entered by the user.

filename = MultiplicationTable.java


//Write a program to print the multiplication table of a number N, entered by the user.
import java.util.*;

class MultiplicationTable {
    public static void printMultiplicationTable(int number) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number : ");
        int n = sc.nextInt();
        for (int i = 1; i <= 10; i++) {
            System.out.println(n + " * " + i + " = " + n * i);
        }
    }

    public static void main(String s[]) {
        printMultiplicationTable(5);
    }
}





filename: MultiTest.java

import java.util.*; //Write a program to print the multiplication table of a number N, entered by the user.

public class MultiTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number : ");
        int n = sc.nextInt();
        for (int i = 1; i <= 10; i++) {
            System.out.println(n + " * " + i + " = " + n * i);
        }
    }
}




filename: printMultiplicationTable.java

import java.util.*; //Write a program to print the multiplication table of a number N, entered by the user.

class MultiplicationTable {
    public static void printMultiplicationTable(int number) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number : ");
        int n = sc.nextInt();
        for (int i = 1; i <= 10; i++) {
            System.out.println(n + " * " + i + " = " + n * i);
        }
    }

    public static void main(String s[]) {
        printMultiplicationTable(5);
    }
}



Output of program: 

Loops> java .\MultiplicationTable.java

Enter number: 5

5 * 1 = 5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50

Write a JAVA program that reads a set of integers, and then prints the sum of the even and odd integers.

 import java.util.Scanner;


public class ReadSetofIntegersPrintsSumofEvenAndOddIntegers {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number, chioce, evenSum = 0, OddSum = 0;

        do {

            System.out.print("Enter the number ");

            number = sc.nextInt();


            if (number % 2 == 0) {

                evenSum += number;

            } else {

                OddSum += number;

            }

            System.out.println("Do you want to continue? Press 1 for yes or 0 for no");

            chioce = sc.nextInt();

        } while (chioce == 1);

        System.out.println("Sum of even numbers: " + evenSum);

        System.out.println("Sum of odd numbers: " + OddSum);

    }

}

import java.util.Scanner;

public class ReadSetofIntegersPrintsSumofEvenAndOddIntegers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number, chioce, evenSum = 0, OddSum = 0;
        do {
            System.out.print("Enter the number ");
            number = sc.nextInt();

            if (number % 2 == 0) {
                evenSum += number;
            } else {
                OddSum += number;
            }
            System.out.println("Do you want to continue? Press 1 for yes or 0 for no");
            chioce = sc.nextInt();
        } while (chioce == 1);
        System.out.println("Sum of even numbers: " + evenSum);
        System.out.println("Sum of odd numbers: " + OddSum);

    }
}



Output:
Enter the number 4 Do you want to continue? Press 1 for yes or 0 for no 1 Enter the number 6 Do you want to continue? Press 1 for yes or 0 for no 1 Enter the number 8 Do you want to continue? Press 1 for yes or 0 for no 1 Enter the number 9 Do you want to continue? Press 1 for yes or 0 for no 0 Sum of even numbers: 18 Sum of odd numbers: 9

Check if a number is Prime or not simple Java programming Code

 import java.util.Scanner;

public class PrimeNumberCheck {

    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();


        boolean isPrime = true;

        for (int i = 2; i <= n - 1; i++) {

            if (n % i == 0) { // n is multiple of i (i not equal to 1 or n)

                isPrime = false;

            }

        }

        if (isPrime == true) {

            System.out.print(n + " is prime number");

        } else {

            System.out.print(n + " is not prime number");

        }

    }

}

import java.util.Scanner;

public class PrimeNumberCheck {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        boolean isPrime = true;
        for (int i = 2; i <= n - 1; i++) {
            if (n % i == 0) { // n is multiple of i (i not equal to 1 or n)
                isPrime = false;
            }
        }
        if (isPrime == true) {
            System.out.print(n + " is prime number");
        } else {
            System.out.print(n + " is not prime number");
        }
    }
}

Keep entering numbers till user enters a multiple of 10, simple Java programming Code by Naymul Islam PCIU CSE 18

 import java.util.Scanner;


public class KeepEnteringNumbersTillUserEntersMultipleOf {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        do {

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

            int num = sc.nextInt();


            if (num % 10 == 0) {

                break;

            }

            System.out.println(num);

        } while (true);


    }

}

import java.util.Scanner;

public class KeepEnteringNumbersTillUserEntersMultipleOf {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("Enter your Number : ");
            int num = sc.nextInt();

            if (num % 10 == 0) {
                break;
            }
            System.out.println(num);
        } while (true);

    }
}

Reverse the given number simple Java programming Code by Naymul Islam PCIU CSE 18

 import java.util.Scanner;


public class ReverseTheGivennNumber {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int num = sc.nextInt();


        int rev = 0;

        while (num > 0) {

            int lastDigit = num % 10;

            rev = (rev * 10) + lastDigit;

            num /= 10;

        }

        System.out.println(rev);

    }

}


import java.util.Scanner;

public class ReverseTheGivennNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        int rev = 0;
        while (num > 0) {
            int lastDigit = num % 10;
            rev = (rev * 10) + lastDigit;
            num /= 10;
        }
        System.out.println(rev);
    }
}

Print reverse of a number simple Java programming Code by Naymul Islam PCIU CSE 18

 import java.util.Scanner;


public class ReverseOfNumber {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in); // use for input from user

        int num = sc.nextInt();

        // int num = 10899;


        while (num > 0) {

            int lastDigit = num % 10;

            System.out.print(lastDigit);

            num = num / 10; // n/10;

        }

        System.out.println();

    }

}

import java.util.Scanner;

public class ReverseOfNumber {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in); // use for input from user
        int num = sc.nextInt();
        // int num = 10899;

        while (num > 0) {
            int lastDigit = num % 10;
            System.out.print(lastDigit);
            num = num / 10; // n/10;
        }
        System.out.println();
    }
}


Print Square pattern Java programming Code Basic

 import java.util.*;

//Print Square pattern

public class squarePattern {

    public static void main(String args[]) {

        // for (int lines = 1; lines <= 4; lines++)

        // System.out.println("****");


        int lines = 1;

        while (lines <= 4) {

            System.out.println("****");

            lines++;


        }


    }


}


import java.util.*;

//Print Square pattern
public class squarePattern {
    public static void main(String args[]) {
        // for (int lines = 1; lines <= 4; lines++)
        // System.out.println("****");

        int lines = 1;
        while (lines <= 4) {
            System.out.println("****");
            lines++;

        }

    }

}

Sum of first N natural Numbers Java programming code

 import java.util.Scanner;

// Sum of first N natural Numbers , Naymul Islam CSE 18

public class sumofN {

    public static void main(String args[]) {

        System.out.print("Enter Natural Number: ");

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int sum = 0;

        int i = 1;

        while (i <= n) {

            sum += i;

            i++;

        }

        // System.out.println(sum);

        System.out.println("Sum of first " + n + " natural Numbers: " + sum);

    }

}


import java.util.Scanner;

// Sum of first N natural Numbers
public class sumofN {
    public static void main(String args[]) {
        System.out.print("Enter Natural Number: ");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        int i = 1;
        while (i <= n) {
            sum += i;
            i++;
        }
        // System.out.println(sum);
        System.out.println("Sum of first " + n + " natural Numbers: " + sum);
    }
}

I Love You Print in 100 times JAVA code

 import java.util.Scanner;

public class WhileLoop {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int counter = 1;

        while (counter <= n) {

            System.out.println("I Love You " + counter);

            counter++;

        }

        System.out.println(n + " times I love you print.");

    }

}


Simple Calculator JAVA code

 import java.util.Scanner;

public class calculator {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a :");

        int a = sc.nextInt();

        System.out.println("Enter b :");

        int b = sc.nextInt();

        System.out.println("Enter Operator");

        char operator = sc.next().charAt(0);


        switch (operator) {

            case '+':

                System.out.println(a + b);

                break;

            case '-':

                System.out.println(a - b);

                break;

            case '*':

                System.out.println(a * b);

                break;

            case '/':

                System.out.println(a / b);

                break;

            case '%':

                System.out.println(a % b);

                break;

            default:

                System.out.println("Wrong Operator");

                break;

        }


    }

}



import java.util.Scanner;

public class calculator {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a :");
        int a = sc.nextInt();
        System.out.println("Enter b :");
        int b = sc.nextInt();
        System.out.println("Enter Operator");
        char operator = sc.next().charAt(0);

        switch (operator) {
            case '+':
                System.out.println(a + b);
                break;
            case '-':
                System.out.println(a - b);
                break;
            case '*':
                System.out.println(a * b);
                break;
            case '/':
                System.out.println(a / b);
                break;
            case '%':
                System.out.println(a % b);
                break;
            default:
                System.out.println("Wrong Operator");
                break;
        }

    }
}

"Year is a leap year or not" JAVA code by Naymul Islam CSE 18 PCIU


Code: 

import java.util.Scanner;

public class LeapYear {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Input the year: ");

        int year = sc.nextInt();

        boolean x = (year % 4) == 0;

        boolean y = (year % 100) != 0;

        boolean z = ((year % 100 == 0) && (year % 400 == 0));


        if (x && (y || z)) {

            System.out.println(year + " is a leap year");

        } else {

            System.out.println(year + " is not a leap year");

        }

    }

}


 import java.util.Scanner;


public class LeapYear {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Input the year: ");
        int year = sc.nextInt();
        boolean x = (year % 4) == 0;
        boolean y = (year % 100) != 0;
        boolean z = ((year % 100 == 0) && (year % 400 == 0));

        if (x && (y || z)) {
            System.out.println(year + " is a leap year");
        } else {
            System.out.println(year + " is not a leap year");
        }
    }
}

                                                                                                                                        

Max Sub Array Sum: Kadane's Algorithm

Code of: Max Sub Array Sum: Kadane's Algorithm. public class MaxSubArraySumKadanesALgorithm {     public static void Kadane ( int ...