Simple Java Programs You Should Know

Simple Java Programs You Should Know

Hi friends, today I'm going to provide you some of the simple java programs that you may learn to write, which may help to improve your coding skills if you are a beginner.

So here I am going to show you 5 java programs, you may also write it if you want.

1) Write a program to the square root of the number to be entered by the user.


import java.util.*;

class SquareRoot

{

    void Sqroot(double a)

    {

        System.out.println("The Square Root of the number "+ Math.sqrt(a));

        

    }

    public static void main(String args[])

    {

        Scanner in = new Scanner(System.in);

        SquareRoot ob= new SquareRoot();

        double a;

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

        a=in.nextDouble();

        ob.Sqroot(a);

    }

}

Output:

Write a program to the square root of the number to be entered by the user.



2) Write a program to print a random number between 0 to 9.

class Random

{

    public static void main(String args[])

    {

        double a;

        a=Math.random()*10;

        System.out.println((int)a);

    }

}

Output:

Write a program to print a random number between 0 to 9.



Now, friends, I will show you a program that will help you to make a frame through java.

3)Write a  program in java To make a frame.

import javax.swing.*;


class FrameMaking

{

    public static void main(String args[])

    {

        JFrame frame = new JFrame("My First Frame");

        frame.setVisible(true);

        frame.setSize(450,450);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

Output:


Write a  program in java To make a frame.


4) Write a program to check whether a word is palindrome or not.

Palindrome words are the words which are same when read backwards or forward like the word "dad".

import java.util.*;

class Palindrome

{

    public static void main(String args[])

    {

        Scanner in = new Scanner(System.in);

        String wrd , wrd1="";char b;

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

        wrd=in.next();

        for(int i=wrd.length()-1;i>=0;i--)

        {

             b=wrd.charAt(i);

            wrd1=wrd1+b;

        }

        if(wrd1.equalsIgnoreCase(wrd))

        {

            System.out.println("It is a palindrome word");

        }

        else

        {

            System.out.println("It is not a palindrome word");

        }

        

    }

}


Output:

Write a program to check whether a word is palindrome or not.



5) Write a program to make a simple console calculator which can do:-

a) Addition
b) Subtraction
c)Division
d)Multiplication

import java.util.*;
class Calculator
{
    void calculate(int a , int b,String word)
    {
        if(word.equalsIgnoreCase("add"))
        {
            System.out.println(a+b);
        }
         else if(word.equalsIgnoreCase("sub"))
        {
            System.out.println(a-b);
        } else if(word.equalsIgnoreCase("multi"))
        {
            System.out.println(a*b);
        } else if(word.equalsIgnoreCase("div"))
        {
            System.out.println((double)a/b);
        }
        else
        {
            System.out.println("Sorry, you have entered wrong data");
        }
    }
    
    public static void main(String[] args)
    {
        int a ,  b;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the two numbers");
        a=in.nextInt();
        b=in.nextInt();
        System.out.println("Type add for addition");
        System.out.println("Type sub for subtraction");
        System.out.println("Type multi for multiplication");
        System.out.println("Type div for division");
        String word= in.next();
        Calculator ob= new Calculator();
        ob.calculate(a, b , word);
        
        
    }
}

Output:

Write a program to make a simple console calculator

Hope you enjoyed reading these programs. You may also try these programs on your own.



Comments

Popular Posts