Top 10 Java Programs
Advertisements
Top 10 Program in Java
Here we discuss about Top 10 Programs in Java. Generally in java from control statements, array, string, oops etc related programs asked in any interview.
1. Armstrong Program in Java
Armstrong Program in Java
import java.util.Scanner; class Armstrong { public static void main(String[] args) { int arm=0,a,b,c,d,no; Scanner s=new Scanner(System.in); System.out.println("Enter any num :"); no=s.nextInt(); d=no; while(no>0) { a=no%10; no=no/10; arm=arm+a*a*a; } if(arm==d) { System.out.println("Armstrong :"); } else { System.out.println("not Armstrong"); } } }
Output
Enter any num : 153 Armstrong
2. Prime Number Program in Java
Prime number is a number which is divisible by 1 and itself. Prime number cannot be divisible by other numbers except 1 or itself. Prime number start from 2, 3, 5, 7, 11, 13, 17, 19.....etc. 0 and 1 are not a prime number.
Prime Number Program in Java
import java.util.Scanner; class Primenumber { public static void main(String[] args) { int no, i, fect=1; Scanner s=new Scanner(System.in); System.out.println("Enter any number :"); no=s.nextInt(); if(no==1) { System.out.println("Smallest Prime number is 2"); } for(i=2;i<no;i++) { if(no%i==0) { System.out.println("Not Prime"); break; } } if(no==i) { System.out.println("Prime"); } } }
Output
Enter any number : 7 Prime
3. Factorial Program in Java
Factorial Program in Java
import java.util.Scanner; class Factorial { public static void main(String[] args) { int no, fect=1; Scanner s=new Scanner(System.in); System.out.println("Enter any number :"); no=s.nextInt(); for(int i=1; i<=no; i++) { fect=fect*i; } System.out.println("Factorial is :" +fect); } }
Output
Enter any number : 6 Factorial is : 720
4. Reverse Number Program in Java
Reverse Number Program in Java
import java.util.Scanner; class Reverse { public static void main(String[] args) { int no,rev=0,r,a; Scanner s=new Scanner(System.in); System.out.println("Enter any no.: "); no=s.nextInt(); a=no; while(no>0) { r=no%10; rev=rev*10+r; no=no/10; } System.out.println("Reverse: "+rev); } }
Output
Enter any no. : 153 Reverse: 351
5. Palindrome Number Program in Java
Palindrome number is a number that remains the same after reverse. for example : 151, 242, 585, 3333, etc.
Palindrome Number Program in Java
import java.util.Scanner; class Palindrome { public static void main(String[] args) { int a,no,b,temp=0; Scanner s=new Scanner(System.in); System.out.println("Enter any num: "); no=s.nextInt(); b=no; while(no>0) { a=no%10; no=no/10; temp=temp*10+a; } if(temp==b) { System.out.println("Palindrome"); } else { System.out.println("not Palindrome"); } } }
Output
Enter any num : 151 Palindrome
6. Sort an Array Elements in Ascending Order in Java
Sort an Array Elements in Ascending Order in Java
import java.util.Scanner; public class SortArray { public static void main(String[] args) { int n, temp; 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(); } for (int i =0; i<n; i++) { for (int j=i+1; j<n; j++) { if (a[i]>a[j]) // compare numbers { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } System.out.print("Elements in Ascending Order:"); for (int i=0; i<n-1; i++) { System.out.print(a[i]+ ", "); // print in same line and separate with comma } System.out.print(a[n-1]); } }
Output
Enter no. of elements you want in array: 5 3 4 1 7 6 Elements in Ascending Order: 1, 3, 4, 6, 7
7. Java Program to Find Odd or Even
Java Program to Find Odd or Even
import java.util.Scanner; class Oddeven { public static void main(String[] args) { int no; Scanner s=new Scanner(System.in); System.out.println("Enter any number :"); no=s.nextInt(); if(no%2==0) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }
Output
Enter any number : 10 Even number
8. Print Table of Number in Java
Print Table of Number in Java
import java.util.Scanner; class Table { public static void main(String[] args) { int i,no,table=1; Scanner s=new Scanner(System.in); System.out.println("Enter any number: "); no=s.nextInt(); for(i=1; i<=10; i++) { table=no*i; System.out.println(table); } } }
Output
Enter any number : 5 5 10 15 20 25 30 35 40 45 50
9. Fibonacci Series Program in Java
Fibonacci Series Program in Java
import java.util.Scanner; class Fibonacci { public static void main(String[] args) { int i,no, first=0, second=1, next; Scanner s=new Scanner(System.in); System.out.println("Enter number of terms for Series: "); no=s.nextInt(); first=0; second=1; System.out.println("Fibonacci series are: "); for(i=0; i<no; i++) { System.out.println(first); next = first + second; first = second; second = next; } } }
Output
Enter nubmer of terms for Series: 5 Fibonacci series are: 0 1 1 2 3
10. Java Program to Find sum of Digits
Java Program to Find sum of Digits
import java.util.Scanner; class Sumofdigit { public static void main(String[] args) { int a,no,sum=0; Scanner s=new Scanner(System.in); System.out.println("Enter any number: "); no=s.nextInt(); while(no>0) { a=no%10; no=no/10; sum=sum+a; } System.out.println("Sum of Digits :"+sum); } }
Output
Enter Value in a : 10 Enter Value in b : 20 Value in a : 20 Value in b : 10
Google Advertisment