22:38

Prime Number

Posted by Mitesh Patel |


class PrimeNumberExample {

    void prime(int num) {

        int i;
        int flag = 0;

        for (i = 2; i < num; i++) {
            if (num % i == 0) {
                flag = 1; //flag= 1 not prime
            }
        }


        if (flag == 0) {
            System.out.println("prime = " + num);

        }
    }

    public static void main(String args[]) {

        int i;
        PrimeNumberExample x = new PrimeNumberExample();
        for (i = 1; i <= 100; i++) {
            x.prime(i);
        }

    }
}

22:37

Double Array

Posted by Mitesh Patel |


import java.util.Scanner;

class TwoDArray {

    public static void main(String args[]) {

        int Row, Col;

        int a[][] = new int[3][3];

        for (Row = 0; Row < 3; Row++) {
            for (Col = 0; Col < 3; Col++) {

                a[Row][Col] = new Scanner(System.in).nextInt();

            }
        }



        //print data

        for (Row = 0; Row < 3; Row++) {
            for (Col = 0; Col < 3; Col++) {

                System.out.print("\t" + a[Col][Row]);

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


    }
}

22:36

Palindrome

Posted by Mitesh Patel |


class Palindrome {

    public static void main(String args[]) {

        int i;
        Palindrome x = new Palindrome();
        for (i = 1; i <= 100; i++) {
            x.pali(i);
        }
    }

    void pali(int number) {

        int i;
        int copy = number;
        int rem, rev = 0;

        for (i = 0; number != 0; i++) {
            rem = number % 10;
            rev = rev * 10 + rem;
            number = number / 10;
        }


        if (copy == rev) {
            System.out.println("Pali = " + copy);
        }

    }
}

22:35

Method Overloading

Posted by Mitesh Patel |

/*
method overloading means
 *
 *
 * u can declare multiple functions/methods with the same name
 *
 * that functions must have different number/type of arguments
 */

class OverloadExample {

    public void show() // First function
    {
        System.out.println("function with no argument");
    }

    public void show(int x) // second function
    {
        System.out.println("function with one argument");
    }

    public void show(int x, int y) // second function
    {
        System.out.println("function with two argument");
    }
}

class MethodOverloading {

    public static void main(String args[]) {
        OverloadExample part= new OverloadExample();
        part.show();
        part.show(25);
        part.show(25, 75);

        OverloadExample mitesh = new OverloadExample();
        mitesh.show();
    }
}

22:33

Inheritance

Posted by Mitesh Patel |

/*
 inheritance example java
 */

class First
{
    void show()
    {
        System.out.println("This is parent class method");
    }
}

class Second extends First
{

}

class JavaInheritanceExample {

    public static void main(String args[])
    {
        Second mitesh = new Second();
        mitesh.show();
    }

}

22:31

Nesting Method

Posted by Mitesh Patel |


class MyClass {

    void show() {
        System.out.println("Just Free Knowledge");
    }

    void call() {

        show(); // call show method

    }
}

class FunctionNesting {

    public static void main(String args[]) {

        MyClass x = new MyClass();
        x.call();

    }
}

22:29

Array

Posted by Mitesh Patel |


import java.util.*;

class ArrayExample{

    public static void main(String args[]) {
        int a[] = new int[5];
        int i;
        int sum = 0;

        for(i=0;i<a.length;i++)
        {
            a[i]  = new Scanner(System.in).nextInt();
            sum = sum + a[i];
        }

       
        System.out.println("Avg = " + (sum/5));




    }
}

Subscribe