Hello friends! What happens to you java, how to install java and much more has been explained in the java menu. Today in this post all the Java Programming Examples of java programming are shown with the output, so let’s start.
Contents
- java programming Example
- 1.Write a program to print factorial by given number..
- 2.Write a program to cheak amongstrong number..
- 3.Write a program to cheak number is parlidrom or not..
- 4.Write a program to check number is magic or not…
- 5.Write a program for print table given by user input…
- 6.Write a program to print sum of number(234=2+3+4=9)..
- 8.Write a program to print Fibonacci series …
- 9.Write a program to print first natural number…
- 10.Write a program to find greatest number between two number..
- 11.Write a program to convert currency in java..
- 12.Write a program to convert days into month and year..
- 13.Write a program in java to find smallest and greatest number in array…
- 14.Write a program to copy one array element to another array..
- 15.Write a program to print sum of element in array….
- 16.Write a program print all even and odd number in array…
- 17.Write a program to print pattern….
- 18.Write a program to print pattern…
- 19.Write a program to print pattern….
- 20.Write a program to print pattern…
- 21.Write a program to print counting 1 to 100..
- 22.Write a program among a class account using the inherit and static that show all bank functions like widrall , deposit.
- 23.write a program to find parameter of rectangle using this keyword.
- 24.Write a program to create menu using frame.
- 25.Write a to implement flow layout and border layout.
- 26.write a program to exception handling using try, catch, finally.
- 27.write a program to different type of heritance.
- 28.write a program for overloading and overwriting in java.
- 29.write a program to create thread that implement the RUNABLE INTERFACE.
java programming Example
1.Write a program to print factorial by given number..
import java.util.Scanner; class Factorial { public static void main(String[] javahindi) { int n, c, fact = 1; System.out.println("Enter an integer to calculate it's factorial"); Scanner in = new Scanner(System.in); n = in.nextInt(); if (n < 0){ System.out.println("Number should be non-negative."); } else{ for (c = 1; c <= n; c++){ fact = fact*c; System.out.println("Factorial of "+n+" is = "+fact); } } } }
OUTPUT
2.Write a program to cheak amongstrong number..
import java.util.Scanner; public class amongstrong { public static void main(String[] javahindi){ int num=0, start , rem, temp ; Scanner scanner = new Scanner(System.in); System.out.print("Enter the number: "); start= scanner.nextInt(); temp = start; while(temp != 0) { rem = temp%10; num = num + rem*rem*rem; temp = temp/10; } if(start == num) { System.out.println("Number is amongstrong:" +num); } else { System.out.println("Number is not amongstrong:" +start); } } }
OUTPUT
3.Write a program to cheak number is parlidrom or not..
import java.util.Scanner; public class Palidrom { public static void main(String[] javahindi){ int num=0, start , rem, temp ; Scanner scanner = new Scanner(System.in); System.out.print("Enter the number: "); start= scanner.nextInt(); temp=start; while(temp!=0){ rem=temp%10; num=(num*10)+rem; temp=temp/10; } if(start==num){ System.out.println(num +"is palidrom number."); } else{ System.out.println(start +"is not palidrom number"); } } }
OUTPUT
4.Write a program to check number is magic or not…
import java.util.Scanner; public class Maggic { public static void main(String[] javahindi){ int num,temp,sum=0,maggic=1,rem,d; System.out.print("Enter table:"); Scanner in=new Scanner(System.in); num=in.nextInt(); temp=num; while(temp!=0){ rem=temp%10; sum=sum+rem; d=rem; maggic=d*maggic; temp=temp/10 } if(sum==maggic){ System.out.println(num+"is maggic number"); } else{ System.out.println(num+"is not maggic number"); } } }
OUTPUT
5.Write a program for print table given by user input…
import java.util.Scanner; public class Table { public static void main(String[] javahindi){ int num; System.out.print("Enter table:"); Scanner in=new Scanner(System.in); num=in.nextInt(); for(int i=1;i<=10;i++){ System.out.println(num*i); } } }
OUTPUT
6.Write a program to print sum of number(234=2+3+4=9)..
import java.util.Scanner; public class sum { public static void main(String[] javahindi){ int num=0, start , rem, temp ; Scanner scanner = new Scanner(System.in); System.out.print("Enter the number: "); start= scanner.nextInt(); while(start!=0){ rem=start%10; num=num+rem; start=start/10; } System.out.println("Sum of number:"+ num); } }
OUTPUT
7.Write a program to check number is prime or not..import java.util.Scanner;
public class PrimeNumber { public static void main(String[] javahindi) { int num,b,c; Scanner s=new Scanner(System.in); System.out.println("Enter A Number"); num =s.nextInt(); b=1; c=0; while(b<= num) { if((num%b)==0) c=c+1; b++; } if(c==2) System.out.println(num +" is a prime number"); else System.out.println(num +" is not a prime number"); } }
OUTPUT
8.Write a program to print Fibonacci series …
import java.util.Scanner; public class FibonacciSeries { public static void main(String[] javahindi) { Scanner s = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = s.nextInt(); fibonacci(n); } public static void fibonacci(int n) { if (n == 0) { System.out.println("0"); } else if (n == 1) { System.out.println("0 1"); } else { System.out.print("0 1 "); int a = 0; int b = 1; for (int i = 1; i < n; i++) { int nextNumber = a + b; System.out.print(nextNumber + " "); a = b; b = nextNumber; } } } }
OUTPUT
9.Write a program to print first natural number…
import java.util.Scanner; class Star{ public static void main(String[] javahindi){ int num; System.out.println("Enter the natural number of "); Scanner in=new Scanner(System.in); num=in.nextInt(); for(int i=1;i<=num;i++){ System.out.println("natural number is:"+i); } } }
OUTOUT
10.Write a program to find greatest number between two number..
import java.util.Scanner; public class JavaProgram { public static void main(String[] javahindi) { int a, b, big; Scanner scan = new Scanner(System.in); System.out.print("Enter Two Number : "); a = scan.nextInt(); b = scan.nextInt(); if(a>b) { big = a; } else { big = b; } System.out.print("Largest of Two Number is " +big); } }
OUTPUT
11.Write a program to convert currency in java..
import java.util.*; import java.text.DecimalFormat; public class CurrencyConverter { public static void main(String[] javahindi) { double amount, dollar, pound, code, euro, yen, ringgit, rupee; DecimalFormat f = new DecimalFormat("##.##"); Scanner sc = new Scanner(System.in); System.out.println("hi, Welcome to the Currency Converter!"); System.out.println("which currency You want to Convert ? "); System.out.println("1:Ruppe \t2:Dollar \t3:Pound \n4:Euro \t5:Yen \t6:Ringgit "); code = sc.nextInt(); System.out.println("How much Money you want to convert ?"); amount = sc.nextFloat(); // For amounts Conversion if (code == 1) { dollar = amount / 70; System.out.println("Your " + amount + " Rupee is : " + f.format(dollar) + " Dollar"); pound = amount / 88; System.out.println("Your " + amount + " Rupee is : " + f.format(pound) + " Pound"); euro = amount / 80; System.out.println("Your " + amount + " Rupee is : " + f.format(euro) + " Euro"); yen = amount / 0.63; System.out.println("Your " + amount + " Rupee is : " + f.format(yen) + " Yen"); ringgit = amount / 16; System.out.println("Your " + amount + " Rupee is : " + f.format(ringgit) + " ringgit"); } else if (code == 2) { // For Dollar Conversion rupee = amount * 70; System.out.println("Your " + amount + " Dollar is : " + f.format(rupee) + " Ruppes"); pound = amount * 0.78; System.out.println("Your " + amount + " Dollar is : " + f.format(pound) + " Pound"); euro = amount * 0.87; System.out.println("Your " + amount + " Dollar is : " + f.format(euro) + " Euro"); yen = amount * 111.087; System.out.println("Your " + amount + " Dollar is : " + f.format(yen) + " Yen"); ringgit = amount * 4.17; System.out.println("Your " + amount + " Dollar is : " + f.format(ringgit) + " ringgit"); } else if (code == 3) { // For Pound Conversion rupee = amount * 88; System.out.println("Your " + amount + " pound is : " + f.format(rupee) + " Ruppes"); dollar = amount * 1.26; System.out.println("Your " + amount + " pound is : " + f.format(dollar) + " Dollar"); euro = amount * 1.10; System.out.println("Your " + amount + " pound is : " + f.format(euro) + " Euro"); yen = amount * 140.93; System.out.println("Your " + amount + " pound is : " + f.format(yen) + " Yen"); ringgit = amount * 5.29; System.out.println("Your " + amount + " pound is : " + f.format(ringgit) + " ringgit"); } else if (code == 4) { // For Euro Conversion rupee = amount * 80; System.out.println("Your " + amount + " euro is : " + f.format(rupee) + " Ruppes"); dollar = amount * 1.14; System.out.println("Your " + amount + " euro is : " + f.format(dollar) + " Dollar"); pound = amount * 0.90; System.out.println("Your " + amount + " euro is : " + f.format(pound) + " Pound"); yen = amount * 127.32; System.out.println("Your " + amount + " euro is : " + f.format(yen) + " Yen"); ringgit = amount * 4.78; System.out.println("Your " + amount + " euro is : " + f.format(ringgit) + " ringgit"); } else if (code == 5) { // For Yen Conversion rupee = amount * 0.63; System.out.println("Your " + amount + " yen is : " + f.format(rupee) + " Ruppes"); dollar = amount * 0.008; System.out.println("Your " + amount + " yen is : " + f.format(dollar) + " Dollar"); pound = amount * 0.007; System.out.println("Your " + amount + " yen is : " + f.format(pound) + " Pound"); euro = amount * 0.007; System.out.println("Your " + amount + " yen is : " + f.format(euro) + " Euro"); ringgit = amount * 0.037; System.out.println("Your " + amount + " yen is : " + f.format(ringgit) + " ringgit"); } else if (code == 6) { // For Ringgit Conversion rupee = amount * 16.8; System.out.println("Your " + amount + " ringgit is : " + f.format(rupee) + " Ruppes"); dollar = amount * 0.239; System.out.println("Your " + amount + " ringgit is : " + f.format(dollar) + " dollar"); pound = amount * 0.188; System.out.println("Your " + amount + " ringgit is : " + f.format(pound) + " pound"); euro = amount * 0.209; System.out.println("Your " + amount + " ringgit is : " + f.format(euro) + " euro"); yen = amount * 26.63; System.out.println("Your " + amount + " ringgit is : " + f.format(yen) + " yen"); } else { System.out.println("Invalid input"); } System.out.println("Thank you for choosing our Example Programs"); } }
OUTPUT
12.Write a program to convert days into month and year..
import java.util.Scanner; class Star{ public static void main(String[] javahindi){ int days; int month,year,rem,rem1; System.out.println("Enter days:"); Scanner in=new Scanner(System.in); days=in.nextInt(); month=days/30; rem=days%30; year=days/365; rem1=days%365; System.out.println("Total month is:"+month+"and"+rem+"days"); System.out.println("Total year is:"+year+"and"+rem1+"days"); } }
OUTPUT
13.Write a program in java to find smallest and greatest number in array…
import java.util.Arrays import java.util.Collections; import java.util.Scanner; public class MinMax{ public static void main(String[] javahindi) { System.out.println("Enter Up To 10 Numbers"); Scanner sc=new Scanner(System.in); Integer[] numbers =new Integer[10]; for (int i=0;i<numbers.length;i++) { System.out.print("enter numbers["+i+"]:"); numbers[i]=sc.nextInt(); } int min = (int) Collections.min(Arrays.asList(numbers)); int max = (int) Collections.max(Arrays.asList(numbers)); System.out.println("Min number: " + min); System.out.println("Max number: " + max); } }
OUTPUT
14.Write a program to copy one array element to another array..
public class Table { public static void main(String[] javahindi) { int [] arr1 = new int [] {1, 2, 3, 4, 5}; int arr2[] = new int[arr1.length]; for (int i = 0; i < arr1.length; i++) { arr2[i] = arr1[i]; } System.out.println("Elements of original array: "); for (int i = 0; i < arr1.length; i++) { System.out.print(arr1[i] + " "); } System.out.println(); System.out.println("Elements of new array: "); for (int i = 0; i < arr2.length; i++) { System.out.print(arr2[i] + " "); } } }
OUTPUT
15.Write a program to print sum of element in array….
import java.util.Scanner; class Star{ public static void main(String[] javahindi){ int n,sum=0; Scanner s=new Scanner(System.in); System.out.println("Enter array value"); n=s.nextInt(); int add[]=new int[n]; System.out.println("Enter element"); for(int i=0;i<n;i++){ add[i]=s.nextInt(); sum=sum+add[i]; } System.out.println("sum of array="+sum); } }
OUTPUT
16.Write a program print all even and odd number in array…
import java.util.Scanner; public class Table { public static void main(String[] javahindi) { int n; Scanner s = new Scanner(System.in); System.out.print("Enter no. of elements you want in array:"); n = s.nextInt(); int a[] = new int[n]; System.out.println("Enter all the elements:"); for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } System.out.print("Odd numbers:"); for(int i = 0 ; i < n ; i++) { if(a[i] % 2 != 0) { System.out.print(a[i]+" "); } } System.out.println(""); System.out.print("Even numbers:"); for(int i = 0 ; i < n ; i++) { if(a[i] % 2 == 0) { System.out.print(a[i]+" "); }
OUTPUT
17.Write a program to print pattern….
public class Star { public static void main(String[] javahindi) { for(int i=1;i<=5;i++){ for(int j=5;j>=i;j--){ System.out.print(" "); } for(int k=1;k<=i;k++){ System.out.print(" *"); } System.out.println(); } } }
OUTPUT
18.Write a program to print pattern…
public class Star { public static void main(String[] javahindi) { int p=0; for(int i=1;i<=5;i++){ for(int j=1;j<=i-1;j++){ System.out.print(" "); } for(int j=1;j<=5-p;j++){ System.out.print("*"); } p=p+1; System.out.println(); } } }
OUTPUT
19.Write a program to print pattern….
public class Star { public static void main(String[] javahindi) { for(int i=1;i<=5;i++){ for(int j=5;j>=i+1;j--){ System.out.print(j+"\t"); } for(int k=i;k==i;k++){ System.out.print("_\t"); } for(int z=i-1;z>=1;z--){ System.out.print(z+"\t"); } System.out.println(); } } }
OUTPUT
20.Write a program to print pattern…
public class Star { public static void main(String[] javahindi) { int p=2; for(int i=1;i<=5;i++){ for(int j=1;j<=i;j++){ System.out.print("*"); } for(int j=1;j<=10-p;j++){ System.out.print(" "); } for(int j=1;j<=i;j++){ System.out.print("*"); } p=p+2; System.out.println(); } } }
OUTPUT
21.Write a program to print counting 1 to 100..
public class Star { public static void main(String[] javahindi){ for(int i=1;i<=10;i++){ int j=i; while(j<=100){ System.out.print(j+"\t "); j=j+10; } System.out.println(); } }
OUTPUT
22.Write a program among a class account using the inherit and static that show all bank functions like widrall , deposit.
import java.util.Scanner; public class project { public static void main (String[] javahindi){ Scanner in=new Scanner(System.in); while(true){ System.out.println("1.Open new account"); System.out.println("2.widrall"); System.out.println("3.deposit"); System.out.println("4.Exit"); System.out.print("Enter your choice"); int ch=in.nextInt(); switch(ch){ case 1: account(); break; case 2: widrall(); break; case 3: deposit(); break; case 4: System.out.println("byee"); System.exit(0); } } } static void account(){ Scanner in=new Scanner(System.in); System.out.println("Enter name:"); String n=in.nextLine(); System.out.println("Enter age"); int age=in.nextInt(); System.out.println("Enter aadhar number"); long an=in.nextLong(); System.out.println("you account is created"); } static void widrall(){ int balance=10000; Scanner in=new Scanner(System.in); System.out.println("Enter balance you want to widrall"); int w=in.nextInt(); if (w<=balance) System.out.println("Your transaction succesfully"); else System.out.println("please enter ammount bellow 10000"); } static void deposit(){ int d=49000; Scanner in=new Scanner(System.in); System.out.println("Enter deposit ammount"); long dm=in.nextLong(); if (dm<=d) System.out.println("your ammount succesfully deposit"); if (dm>d) System.out.print("please enter pancard number"); int pan=in.nextInt(); System.out.println("your ammount succesfully deposit"); } }
OUTPUT
23.write a program to find parameter of rectangle using this keyword.
class Rectangle{ private int length; private int breadth; Rectangle(int length){ this.length = length; } Rectangle(int length, int breadth){ this(length); this.breadth = breadth; } public int area(){ return (length*breadth); } } public class project { public static void main(String [] args){ Rectangle rect = new Rectangle(5,5); System.out.println("The Area of rectangle is : "+ + rect.area()); } }
import java.awt.*; class project { project(){ Frame f= new Frame("Menu and MenuItem Example"); MenuBar mb=new MenuBar(); Menu menu=new Menu("Menu"); Menu submenu=new Menu("Sub Menu"); MenuItem i1=new MenuItem("Item 1"); MenuItem i2=new MenuItem("Item 2"); MenuItem i3=new MenuItem("Item 3"); MenuItem i4=new MenuItem("Item 4"); MenuItem i5=new MenuItem("Item 5"); menu.add(i1); menu.add(i2); menu.add(i3); submenu.add(i4); submenu.add(i5); menu.add(submenu); mb.add(menu); f.setMenuBar(mb); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public static void main(String args[]) { new project(); } } import java.awt.*; import java.awt.event.*; public class project { private static Dialog d; project() { Frame f= new Frame(); d = new Dialog(f , "Dialog Example", true); d.setLayout( new FlowLayout() ); Button b = new Button ("OK"); b.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent e ) { project.d.setVisible(false); } }); d.add( new Label ("Click button to continue.")); d.add(b); d.setSize(300,300); d.setVisible(true); } public static void main(String args[]) { new project(); } }
25.Write a to implement flow layout and border layout.
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; public class project { public static void main(String[] args) { JButton btnA = new JButton("Button1 (Left)"); JButton btnB = new JButton("Button2 (Right)"); JButton btnC = new JButton("Button3 (Left)"); JButton btnD = new JButton("Button4 (Right)"); btnA.setPreferredSize(new Dimension(150, 20)); btnB.setPreferredSize(new Dimension(150, 20)); btnC.setPreferredSize(new Dimension(150, 20)); btnD.setPreferredSize(new Dimension(150, 20)); JPanel btnAPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JPanel btnCPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JPanel btnDPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); btnAPanel.add(btnA); btnBPanel.add(btnB); btnCPanel.add(btnC); btnDPanel.add(btnD); JPanel panelGrid = new JPanel(new GridLayout(10, 5, 10, 10)); panelGrid.add(new JCheckBox("Demo CheckBox1")); panelGrid.add(new JCheckBox("Demo CheckBox2")); panelGrid.add(btnAPanel); panelGrid.add(btnBPanel); panelGrid.add(btnCPanel); panelGrid.add(btnDPanel); JPanel panelBrdLayout = new JPanel(new BorderLayout()); panelBrdLayout.add(panelGrid, BorderLayout.SOUTH); panelBrdLayout.setPreferredSize(new Dimension(550, 300)); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panelBrdLayout); frame.setSize(550, 400); frame.setVisible(true); } }
26.write a program to exception handling using try, catch, finally.
class project { public static void main (String[] args) { int[] arr = new int[4]; try { int i = arr[4]; System.out.println("Inside try block"); } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("Exception caught in catch block"); } finally { System.out.println("finally block executed"); } System.out.println("Outside try-catch-finally clause"); } }
27.write a program to different type of heritance.
#Singal Inheritance:
class Shape{ void draw() { System.out.println(“Draw shape”); } } class Circle extends Shape { void drwaCircle(){ System.out.println(“Draw Circle”); } public static void main(String args[]) { Circle c = new Circle(); c.draw(); c.drawCircle(); } }
Multi-leval Inheritance:
class Teacher { void teach() { System.out.println("Teaching subject"); } } class Student extends Teacher { void listen() { System.out.println("Listening"); } } class homeTution extends Student { void explains() { System.out.println("Does homework"); } } class project { public static void main(String argu[]) { homeTution h = new homeTution(); h.explains(); h.teach(); h.listen(); } }
Hirerchical Inheritence:
class Teacher { void teach() { System.out.println("Teaching subject"); } } class Student extends Teacher { void listen() { System.out.println("Listening"); } } class Principal extends Teacher { void evaluate() { System.out.println("Evaluating"); } } class project { public static void main(String argu[]) { Principal p = new Principal(); p.evaluate(); p.teach(); } }
28.write a program for overloading and overwriting in java.
Overloading:
class DisplayOverloading { public void disp(char c) { System.out.println(c); } public void disp(char c, int num) { System.out.println(c + " "+num); } } class project { public static void main(String args[]) { DisplayOverloading obj = new DisplayOverloading(); obj.disp('a'); obj.disp('a',10); } }
Overwriding:
class Parent { void show() { System.out.println("Parent's show()"); } } // Inherited class class Child extends Parent { // This method overrides show() of Parent @Override void show() { System.out.println("Child's show()"); } } // Driver class class project { public static void main(String[] args) { // If a Parent type reference refers // to a Parent object, then Parent's // show is called Parent obj1 = new Parent(); obj1.show(); // If a Parent type reference refers // to a Child object Child's show() // is called. This is called RUN TIME // POLYMORPHISM. Parent obj2 = new Child(); obj2.show(); } }
29.write a program to create thread that implement the RUNABLE INTERFACE.
class MyRunnableThread implements Runnable{ public static int myCount = 0; public MyRunnableThread(){ } public void run() { while(MyRunnableThread.myCount <= 10){ try{ System.out.println("Expl Thread: "+(++MyRunnableThread.myCount)); Thread.sleep(100); } catch (InterruptedException iex) { System.out.println("Exception in thread: "+iex.getMessage()); } } } } public class project { public static void main(String a[]){ System.out.println("Starting Main Thread..."); MyRunnableThread mrt = new MyRunnableThread(); Thread t = new Thread(mrt); t.start(); while(MyRunnableThread.myCount <= 10){ try{ System.out.println("Main Thread: "+(++MyRunnableThread.myCount)); Thread.sleep(100); } catch (InterruptedException iex){ System.out.println("Exception in main thread: "+iex.getMessage()); } } System.out.println("End of Main Thread..."); } }
Request – If you like this article(java examples), then you must share this post with your friends and comment for any topic related questions Thank you