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:
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:
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:
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");
}
}
}
Comments
Post a Comment