examples of java programs,these programs are very useful to Beginners,and students to learn java
Wednesday, 26 July 2017
Login page using swings
/* Program for login page when it is excuted it need another welcome ,that program given below*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Login extends JFrame implements ActionListener
{
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
{
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
}
public void actionPerformed(ActionEvent ae)
{
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("java") && value2.equals("java")) {
WelcomePage page=new WelcomePage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
}
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"wrong login or password",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class LoginDemo
{
public static void main(String arg[])
{
try
{
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
}
}
OUTPUT:
/*welcome page when excute that above program this below dailouge applet will appear*/
import javax.swing.*;
import java.awt.*;
class WelcomePage extends JFrame
{
NextPage()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}
TIC-TAC-TIE game
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Tictactie extends JFrame implements ItemListener, ActionListener{
int i,j,ii,jj,x,y,yesnull;
int a[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},
{10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},
{10,7,8,9,11} };
int a1[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},
{10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},{10,7,8,9,11} };
boolean state,type,set;
Icon ic1,ic2,icon,ic11,ic22;
Checkbox c1,c2;
JLabel l1,l2;
JButton b[]=new JButton[9];
JButton reset;
public void showButton(){
x=10; y=10;j=0;
for(i=0;i<=8;i++,x+=100,j++){
b[i]=new JButton();
if(j==3)
{j=0; y+=100; x=10;}
b[i].setBounds(x,y,100,100);
add(b[i]);
b[i].addActionListener(this);
}
reset=new JButton("RESET");
reset.setBounds(100,350,100,50);
add(reset);
reset.addActionListener(this);
}
public void check(int num1){
for(ii=0;ii<=7;ii++){
for(jj=1;jj<=3;jj++){
if(a[ii][jj]==num1){ a[ii][4]=11; }
}
}
}
public void complogic(int num){
for(i=0;i<=7;i++){
for(j=1;j<=3;j++){
if(a[i][j]==num){ a[i][0]=11; a[i][4]=10; }
}
}
for(i=0;i<=7;i++){
set=true;
if(a[i][4]==10){ //if 1
int count=0;
for(j=1;j<=3;j++){
if(b[(a[i][j]-1)].getIcon()!=null){
count++;
}
else{ yesnull=a[i][j]; }
}
if(count==2){
b[yesnull-1].setIcon(ic2);
this.check(yesnull); set=false;break;
}
}
else
if(a[i][0]==10){
for(j=1;j<=3;j++){
if(b[(a[i][j]-1)].getIcon()==null){ //if 1
b[(a[i][j]-1)].setIcon(ic2);
this.check(a[i][j]);
set=false;
break;
}
}
if(set==false)
break;
}
if(set==false)
break;
}
}
Tictactie(){
super("Tic-Tac-tie");
CheckboxGroup cbg=new CheckboxGroup();
c1=new Checkbox(" system",cbg,false);
c2=new Checkbox(" friend",cbg,false);
c1.setBounds(120,80,100,40);
c2.setBounds(120,150,100,40);
add(c1); add(c2);
c1.addItemListener(this);
c2.addItemListener(this);
state=true;type=true;set=true;
ic1=new ImageIcon("cross.jpeg"); /*change the image from your local disk*/
ic2=new ImageIcon("zero.jpeg"); /*change the image from your local disk*/
ic11=new ImageIcon("cross.jpeg"); /*change the image from your local disk*/
ic22=new ImageIcon("zero.jpeg"); /*change the image from your local disk*/
setLayout(null);
setSize(330,450);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e){
if(c1.getState())
{
type=false;
}
else if(c2.getState())
{ type=true;
}
remove(c1);remove(c2);
repaint(0,0,330,450);
showButton();
}
public void actionPerformed(ActionEvent e){
if(type==true)
{
if(e.getSource()==reset){
for(i=0;i<=8;i++){
b[i].setIcon(null);
}
}
else{
for(i=0;i<=8;i++){
if(e.getSource()==b[i]){
if(b[i].getIcon()==null){
if(state==true){ icon=ic2;
state=false;} else{ icon=ic1; state=true; }
b[i].setIcon(icon);
}
}
}
}
}
else if(type==false){
if(e.getSource()==reset){
for(i=0;i<=8;i++){
b[i].setIcon(null);
}//eof for
for(i=0;i<=7;i++)
for(j=0;j<=4;j++)
a[i][j]=a1[i][j];
}
else{
for(i=0;i<=8;i++){
if(e.getSource()==b[i]){
if(b[i].getIcon()==null){
b[i].setIcon(ic1);
if(b[4].getIcon()==null){
b[4].setIcon(ic2);
this.check(5);
} else{
this.complogic(i);
}
}
}
}
}
}
for(i=0;i<=7;i++){
Icon icon1=b[(a[i][1]-1)].getIcon();
Icon icon2=b[(a[i][2]-1)].getIcon();
Icon icon3=b[(a[i][3]-1)].getIcon();
if((icon1==icon2)&&(icon2==icon3)&&(icon1!=null)){
if(icon1==ic1){
b[(a[i][1]-1)].setIcon(ic11);
b[(a[i][2]-1)].setIcon(ic11);
b[(a[i][3]-1)].setIcon(ic11);
JOptionPane.showMessageDialog(Tictactie.this,"!!!YOU won!!! click reset");
break;
}
else if(icon1==ic2){
b[(a[i][1]-1)].setIcon(ic22);
b[(a[i][2]-1)].setIcon(ic22);
b[(a[i][3]-1)].setIcon(ic22);
JOptionPane.showMessageDialog(Tictactie.this,"won! click reset");
break;
}
}
}
}
public static void main(String []args){
new Tictactie();
}
}
Tuesday, 25 July 2017
to print Current DATE and TIME
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Time {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date dateobj = new Date();
System.out.println(df.format(dateobj));
Calendar calobj = Calendar.getInstance();
System.out.println(df.format(calobj.getTime()));
}
}
OUTPUT:
25/07/17 12:01:29
25/07/17 12:01:29
checklist in applet
import java.applet.Applet;
import java.awt.Checkbox;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Checklist
extends Applet implements ItemListener{
Checkbox java = null;
Checkbox oracle = null;
Checkbox DOTNET = null;
public void init(){
java = new Checkbox("Java");
oracle = new Checkbox("oracle");
DOTNET = new Checkbox("DOTNET");
add(java);
add(oracle);
add(DOTNET);
java.addItemListener(this);
oracle.addItemListener(this);
DOTNET.addItemListener(this);
}
public void paint(Graphics g){
g.drawString("Java: " + java.getState(),10,80);
g.drawString("oracle: " + oracle.getState(), 10, 100);
g.drawString("DOTNET: " + DOTNET.getState(), 10, 120);
}
public void itemStateChanged(ItemEvent ie) {
repaint();
}
}
import java.awt.Checkbox;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Checklist
extends Applet implements ItemListener{
Checkbox java = null;
Checkbox oracle = null;
Checkbox DOTNET = null;
public void init(){
java = new Checkbox("Java");
oracle = new Checkbox("oracle");
DOTNET = new Checkbox("DOTNET");
add(java);
add(oracle);
add(DOTNET);
java.addItemListener(this);
oracle.addItemListener(this);
DOTNET.addItemListener(this);
}
public void paint(Graphics g){
g.drawString("Java: " + java.getState(),10,80);
g.drawString("oracle: " + oracle.getState(), 10, 100);
g.drawString("DOTNET: " + DOTNET.getState(), 10, 120);
}
public void itemStateChanged(ItemEvent ie) {
repaint();
}
}
APPLET example
import java.applet.Applet;
import java.awt.*;
public class Applet extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
import java.awt.*;
public class Applet extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);
}
}
Main method inside the class
class Book{
int id=10;
String name="java";
public static void main(String args[]){
Student s1=new Student();//creating an object of Book
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
int id=10;
String name="java";
public static void main(String args[]){
Student s1=new Student();//creating an object of Book
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
OUTPUT:
10
java
Monday, 24 July 2017
multification Table program using java
import java.util.Scanner;
public class Table {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("enter the number");
int n=scan.nextInt();
System.out.println("here your "+n+" table");
int i;
int k=0;
for ( i=1;i<=10;i++)
{
k=n*i;
System.out.println(n+"*"+i+"="+k);
}
}
}
OUTPUT:
enter the number
12
here your 12 table
12*1=12
12*2=24
12*3=36
12*4=48
12*5=60
12*6=72
12*7=84
12*8=96
12*9=108
12*10=120
public class Table {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("enter the number");
int n=scan.nextInt();
System.out.println("here your "+n+" table");
int i;
int k=0;
for ( i=1;i<=10;i++)
{
k=n*i;
System.out.println(n+"*"+i+"="+k);
}
}
}
OUTPUT:
enter the number
12
here your 12 table
12*1=12
12*2=24
12*3=36
12*4=48
12*5=60
12*6=72
12*7=84
12*8=96
12*9=108
12*10=120
Break Statement
public class Break{
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int j=1;j<=6;j++)
{
if(i==2&&j==3)
{
break;
}
System.out.println(i+" "+j);
}
}
}
}
OUTPUT:
0 1
0 2
0 3
0 4
0 5
0 6
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 2
3 1
3 2
3 3
3 4
3 5
3 6
4 1
4 2
4 3
4 4
4 5
4 6
5 1
5 2
5 3
5 4
5 5
5 6
public static void main(String[] args)
{
for(int i=0;i<=5;i++)
{
for(int j=1;j<=6;j++)
{
if(i==2&&j==3)
{
break;
}
System.out.println(i+" "+j);
}
}
}
}
OUTPUT:
0 1
0 2
0 3
0 4
0 5
0 6
1 1
1 2
1 3
1 4
1 5
1 6
2 1
2 2
3 1
3 2
3 3
3 4
3 5
3 6
4 1
4 2
4 3
4 4
4 5
4 6
5 1
5 2
5 3
5 4
5 5
5 6
Print infinite stars * using DO-While Loop
public class DoWhile {
public static void main(String[] args) {
do{
System.out.println("*");
}while(true);
}
}
OUTPUT:
*
*
*
*
*
and so on.....
public static void main(String[] args) {
do{
System.out.println("*");
}while(true);
}
}
OUTPUT:
*
*
*
*
*
and so on.....
DO-WHILE example
public class DoWhile {
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=5);
}
}
OUTPUT:
1
2
3
4
5
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=5);
}
}
OUTPUT:
1
2
3
4
5
Print infinite numbers using WHILE loop
public class WhileExample {
public static void main(String[] args) {
while(true){
for(int i=0;;i++)
System.out.println(i);
}
}
}
OUTPUT:
1
2
3
4
5
6
7
8
9
and so on....
public static void main(String[] args) {
while(true){
for(int i=0;;i++)
System.out.println(i);
}
}
}
OUTPUT:
1
2
3
4
5
6
7
8
9
and so on....
while example in java
public class Whileexample {
public static void main(String[] args) {
int i=0;
while(i<=5){
System.out.println(i);
i++;
}
}
}
OUTPUT:
0
1
2
3
4
5
public static void main(String[] args) {
int i=0;
while(i<=5){
System.out.println(i);
i++;
}
}
}
OUTPUT:
0
1
2
3
4
5
Sunday, 23 July 2017
Selection Sort
import java.util.*;
public class SelectionSort {
Scanner scan=new Scanner(System.in);
public static void selectionSort(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String a[]){
int[] arr1 = {11,32,3,18,98,43,9,65};
System.out.println("Before Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr1);//sorting array using selection sort
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
OUTPUT:
Before Selection Sort
11 32 3 18 98 43 9 65
After Selection Sort
3 9 11 18 32 43 65 98
public class SelectionSort {
Scanner scan=new Scanner(System.in);
public static void selectionSort(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String a[]){
int[] arr1 = {11,32,3,18,98,43,9,65};
System.out.println("Before Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr1);//sorting array using selection sort
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
OUTPUT:
Before Selection Sort
11 32 3 18 98 43 9 65
After Selection Sort
3 9 11 18 32 43 65 98
Thursday, 20 July 2017
number pattern program
import java.util.Scanner;
public class Number{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println(" number pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows-i; j++)
{
System.out.print(1);
}
for (int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
sc.close();
}
}
OUTPUT:
How many rows you want in this pattern?
9
number pattern
111111111
111111122
111111333
111114444
111155555
111666666
117777777
188888888
999999999
public class Number{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println(" number pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows-i; j++)
{
System.out.print(1);
}
for (int j = 1; j <= i; j++)
{
System.out.print(i);
}
System.out.println();
}
sc.close();
}
}
OUTPUT:
How many rows you want in this pattern?
9
number pattern
111111111
111111122
111111333
111114444
111155555
111666666
117777777
188888888
999999999
number pattern
import java.util.Scanner;
public class Number
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = scan.nextInt();
System.out.println(" Numeber pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
for (int i = rows-1; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
scan.close();
}
}
OUTPUT:
How many rows you want in this pattern?
5
Numeber pattern
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
public class Number
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("How many rows you want in this pattern?");
int rows = scan.nextInt();
System.out.println(" Numeber pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
for (int i = rows-1; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int j = i; j <= rows; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
scan.close();
}
}
OUTPUT:
How many rows you want in this pattern?
5
Numeber pattern
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
Program for number pattern
class Number
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
OUTPUT:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
star pattern program for daimond shape
class Daimond
{
public static void main(String[] args)
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
{
public static void main(String[] args)
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
print traingle in reverse
class Star
{
public static void main(String[] args)
{
int i, j, k;
for(i=10;i>=1;i--)
{
for(j=10;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
OUTPUT:
*********
*******
*****
***
*
{
public static void main(String[] args)
{
int i, j, k;
for(i=10;i>=1;i--)
{
for(j=10;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
OUTPUT:
*********
*******
*****
***
*
To print traingle with stars
class Traingle
{
public static void main(String[] args)
{
int a,b,c;
for(a=1; a<=5; a++)
{
for(b=4; b>=a; b--)
{
System.out.print(" ");
}
for(c=1; c<=(2*a-1); c++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
OUTPUT:
*
***
*****
*******
*********
{
public static void main(String[] args)
{
int a,b,c;
for(a=1; a<=5; a++)
{
for(b=4; b>=a; b--)
{
System.out.print(" ");
}
for(c=1; c<=(2*a-1); c++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
OUTPUT:
*
***
*****
*******
*********
to print rightangle traingle in reverse
class Star
{
public static void main(String[] args)
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print("*");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("");
}
System.out.println("");
}
}
}
OUTPUT:
*****
****
***
**
*
{
public static void main(String[] args)
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print("*");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("");
}
System.out.println("");
}
}
}
OUTPUT:
*****
****
***
**
*
To print stars in right angle traingle
class star
{
public static void main(String[] args)
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print("");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
OUTPUT:
*
***
*****
*******
*********
{
public static void main(String[] args)
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print("");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
OUTPUT:
*
***
*****
*******
*********
Enhanced for loop example
public class Forloop {
public static void main(String[] args) {
int array[]={21,34,65,76,95};
for(int a:array){
System.out.println(a);
}
}
}
public static void main(String[] args) {
int array[]={21,34,65,76,95};
for(int a:array){
System.out.println(a);
}
}
}
output:
21
34
65
76
95
To sort given numbers even and odd
public class Sorting {
public static void main(String[] args) {
int a[]={11,23,32,46,51,65,77,89,98};
System.out.println("even numbers");
for(int i=0;i<=8;i++) //array size
{
if(a[i]%2==0)
System.out.println(a[i]);
}
System.out.println();
System.out.println("Odd numbers");
for(int j=0;j<=8;j++)
{
if(a[j]%2==1)
System.out.println(a[j]);
}
}}
OUTPUT:
even numbers
32
46
98
Odd numbers
11
23
51
65
77
89
public static void main(String[] args) {
int a[]={11,23,32,46,51,65,77,89,98};
System.out.println("even numbers");
for(int i=0;i<=8;i++) //array size
{
if(a[i]%2==0)
System.out.println(a[i]);
}
System.out.println();
System.out.println("Odd numbers");
for(int j=0;j<=8;j++)
{
if(a[j]%2==1)
System.out.println(a[j]);
}
}}
OUTPUT:
even numbers
32
46
98
Odd numbers
11
23
51
65
77
89
Sorting numbers using Iterator
import java.util.*;
class Sorting{
public static void main(String args[]){
Set<Integer> list=new TreeSet<Integer>();
list.add(14);
list.add(31);
list.add(29);
list.add(23);
list.add(34);
list.add(45);
list.add(67);
list.add(115);
list.add(98);
list.add(23);
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
OUTPUT:
14
23
29
31
34
45
67
98
115
class Sorting{
public static void main(String args[]){
Set<Integer> list=new TreeSet<Integer>();
list.add(14);
list.add(31);
list.add(29);
list.add(23);
list.add(34);
list.add(45);
list.add(67);
list.add(115);
list.add(98);
list.add(23);
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
OUTPUT:
14
23
29
31
34
45
67
98
115
java program for print stars in X shape
public class Cross {
public static void main(String[] args) {
int n=9;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
{
if(i==j||i+j==n+1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
OUTPUT:
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
public static void main(String[] args) {
int n=9;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
{
if(i==j||i+j==n+1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
OUTPUT:
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
Print Stars in plus(+) pattern
public class For {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
System.out.println(" *");
}
for(int i=1;i<=10;i++){
System.out.print("*"+" ");
}System.out.println();
for(int i=6;i<=10;i++){
System.out.println(" *");
}
}
}
OUTPUT:
*
*
*
*
*
* * * * * * * * * *
*
*
*
*
*
public static void main(String[] args) {
for(int i=1;i<=5;i++){
System.out.println(" *");
}
for(int i=1;i<=10;i++){
System.out.print("*"+" ");
}System.out.println();
for(int i=6;i<=10;i++){
System.out.println(" *");
}
}
}
OUTPUT:
*
*
*
*
*
* * * * * * * * * *
*
*
*
*
*
To print numbers in plus(+) pattern
public class Pattern {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
System.out.println(" "+i);
}
for(int i=1;i<=10;i++){
System.out.print(i+" ");
}System.out.println();
for(int i=6;i<=10;i++){
System.out.println(" "+i);
}
}
}
OUTPUT:
1
2
3
4
1 2 3 4 5 6 7 8 9 10
6
7
8
9
10
public static void main(String[] args) {
for(int i=1;i<=5;i++){
System.out.println(" "+i);
}
for(int i=1;i<=10;i++){
System.out.print(i+" ");
}System.out.println();
for(int i=6;i<=10;i++){
System.out.println(" "+i);
}
}
}
OUTPUT:
1
2
3
4
1 2 3 4 5 6 7 8 9 10
6
7
8
9
10
Wednesday, 19 July 2017
To print infinite stars using for loop
public class InfiniteStars {
public static void main(String[] args) {
for(;;)
{
System.out.println("*");
}
}
}
To print infinite numbers using for loop
public class Infinite {
public static void main(String[] args) {
for(;;){
for(int i=0;;i++)
{
System.out.println(i);
} }
}
}
Simple for loop for print 1-5 numbers
public class Forloop {
public static void main(String[] args)
{
for(int i=1;i<=5;i++){
System.out.println(i);
}
}
}
OUTPUT:
1
2
3
4
5
public static void main(String[] args)
{
for(int i=1;i<=5;i++){
System.out.println(i);
}
}
}
OUTPUT:
1
2
3
4
5
Switch case
import java.util.*;
public class Switch {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter number");
int number=scan.nextInt();
switch(number){
case 1: System.out.println("SUNDAY");break;
case 2: System.out.println("MONDAY");break;
case 3: System.out.println("TUESDAY");break;
case 4:System.out.println("WEDNESDAY");break;
case 5:System.out.println("THRUSDAY");break;
case 6:System.out.println("FRIDAY");break;
case 7:System.out.println("SATURDAY");break;
default:System.out.println("invalid number");
}
}
}
public class Switch {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter number");
int number=scan.nextInt();
switch(number){
case 1: System.out.println("SUNDAY");break;
case 2: System.out.println("MONDAY");break;
case 3: System.out.println("TUESDAY");break;
case 4:System.out.println("WEDNESDAY");break;
case 5:System.out.println("THRUSDAY");break;
case 6:System.out.println("FRIDAY");break;
case 7:System.out.println("SATURDAY");break;
default:System.out.println("invalid number");
}
}
}
IF-ELSE-IF Statement
import java.util.*;
public class Ifelseif
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter marks");
int marks=scan.nextInt();
if(marks>=90 && marks<100){
System.out.println("A+ grade");
}
else if(marks>=80 && marks<90)
{
System.out.println("A grade");
}
else if(marks>=70 && marks<80)
{
System.out.println("B grade");
}
else if(marks>=50 && marks<70)
{
System.out.println("C grade");
}
else if(marks>=35 && marks<50)
{
System.out.println("D grade");
}else if( marks<35){
System.out.println("FAIL");
}else
{
System.out.println("Invalid!");
}
}
}
public class Ifelseif
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter marks");
int marks=scan.nextInt();
if(marks>=90 && marks<100){
System.out.println("A+ grade");
}
else if(marks>=80 && marks<90)
{
System.out.println("A grade");
}
else if(marks>=70 && marks<80)
{
System.out.println("B grade");
}
else if(marks>=50 && marks<70)
{
System.out.println("C grade");
}
else if(marks>=35 && marks<50)
{
System.out.println("D grade");
}else if( marks<35){
System.out.println("FAIL");
}else
{
System.out.println("Invalid!");
}
}
}
If statement
public class Ifstatement example
{
public static void main(String[] args)
{
int salary=10000;
if(salary>2300){
System.out.print("salary is greater than 2300");
}
}
}
OUTPUT:salary is greater than 2300
{
public static void main(String[] args)
{
int salary=10000;
if(salary>2300){
System.out.print("salary is greater than 2300");
}
}
}
OUTPUT:salary is greater than 2300
If-else Satatement
import java.util.*;
public class Ifelse
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("enter number to find even or odd");
int number=scan.nextInt();
if(number%2==0)
{
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
}
}
public class Ifelse
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("enter number to find even or odd");
int number=scan.nextInt();
if(number%2==0)
{
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
}
}
Factorial program
import java.util.*;
class Factorial{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
int i,fact=1;
System.out.println("enter number to find factorial");
int number=scan.nextInt();
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
class Factorial{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
int i,fact=1;
System.out.println("enter number to find factorial");
int number=scan.nextInt();
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Bubble sort
public class Bubblesort {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] ={32,89,54,43,23,86};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
Array Before Bubble Sort
32 89 54 43 23 86
Array After Bubble Sort
23 32 43 54 86 89
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] ={32,89,54,43,23,86};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
OUTPUT:
Array Before Bubble Sort
32 89 54 43 23 86
Array After Bubble Sort
23 32 43 54 86 89
To check Armstrong number
import java.util.*;
class Armstrong{
private static Scanner s;
public static void main(String[] args) {
s = new Scanner (System.in);
int c=0,a,temp;
System.out.println("enter some number");
int n=s.nextInt();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
}
}
class Armstrong{
private static Scanner s;
public static void main(String[] args) {
s = new Scanner (System.in);
int c=0,a,temp;
System.out.println("enter some number");
int n=s.nextInt();
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
}
}
Polindrome number or not
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int r,sum=0,temp;
System.out.println("Enter number to check polindrome or not");
int n=s.nextInt();
temp=n;
while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
class Palindrome
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int r,sum=0,temp;
System.out.println("Enter number to check polindrome or not");
int n=s.nextInt();
temp=n;
while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
PRIME NUMBER
import java.util.*;
class Prime
{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int i,m=0,flag=0;
System.out.println("enter number");
int n=s.nextInt();
m=n/2;
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Number is prime");
}
}
class Prime
{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int i,m=0,flag=0;
System.out.println("enter number");
int n=s.nextInt();
m=n/2;
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Number is prime");
}
}
Fibonacci series
Example using without recursion
#1
class Fibonacci{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}}
OUTPUT:0 1 1 2 3 5 8 13 21 34
#2
#1
class Fibonacci{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}}
OUTPUT:0 1 1 2 3 5 8 13 21 34
#2
import java.util.*;
class Fibonacci{
public static void main(String args[])
{
int n1=0,n2=1,n3,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter number");
int count=sc.nextInt();
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}}
INPUT:10
OUTPUT::0 1 1 2 3 5 8 13 21 34
# Using recursion
class FibonacciExample{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);
printFibonacci(count-2);
}
}
OUTPUT:0 1 1 2 3 5 8 13 21 34
# Using recursion
class FibonacciExample{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);
printFibonacci(count-2);
}
}
OUTPUT:0 1 1 2 3 5 8 13 21 34
subtraction program in java
Method #1
class subtraction{
public static void main(String[] args){
int a=10;
int b=10;
int c=a-b;
System.out.println(c);
}}
OUTPUT: 20
Method #2
Import java.util.*;
class subtraction{
public static void main(String[] args){
Scanner scan= new Scanner (Syetem.in);
System.out.println("Enter 1st number:");
int a=scan.nextInt() ;
System.out.println("Enter 1st number:");
int b=scan.nextInt();
int c=a-b;
System.out.println(c);
}}
Method #3
Import java.util.*;
class subtraction{
public static void main(String[] args){
int a=10;
int b=10;
System.out.println(a-b);
}}
Method #4 (Using methods)
class Subtraction
public static void main(String[] args) {
int m=12;
int n=10;
int p= Sub(m,n);
System.out.println(p);
}
private static int Sub(int a, int b) {
int Sub=a-b;
return Sub;
}
}
class subtraction{
public static void main(String[] args){
int a=10;
int b=10;
int c=a-b;
System.out.println(c);
}}
OUTPUT: 20
Method #2
Import java.util.*;
class subtraction{
public static void main(String[] args){
Scanner scan= new Scanner (Syetem.in);
System.out.println("Enter 1st number:");
int a=scan.nextInt() ;
System.out.println("Enter 1st number:");
int b=scan.nextInt();
int c=a-b;
System.out.println(c);
}}
Method #3
class subtraction{
public static void main(String[] args){
int a=10;
int b=10;
System.out.println(a-b);
}}
Method #4 (Using methods)
class Subtraction
public static void main(String[] args) {
int m=12;
int n=10;
int p= Sub(m,n);
System.out.println(p);
}
private static int Sub(int a, int b) {
int Sub=a-b;
return Sub;
}
}
Subscribe to:
Posts (Atom)