Hai friends,these are the top 25 java programs that are frequently asked in interviews.All are executed and tested.Keep it for reference before attending interviews.
1.Armstrong Number
import java.*;
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
int n,sum=0,temp,r;
Scanner input=new Scanner(System.in);
System.out.println("Enter number to find whether it is Armstrong or not");
n=input.nextInt();
temp=n;
System.out.println("temp value");
System.out.println(temp);
while(temp!=0)
{
r=temp%10;
System.out.println("r value");
System.out.println(r);
sum=sum+r*r*r;
System.out.println("sum is:");
System.out.println(sum);
temp=temp/10;
System.out.println("rest of no.");
System.out.println(temp);
}
if(n==sum)
{
System.out.println("num is armstrong");
}
else{
System.out.println("num is not an Armstrong");
}
}
}
-----------------------------------------------------------------------------------------------------------
2.Ascending Order:
import java.util.Scanner;
public class AscendingOrder {
public static void main(String[] args) {
int a[]= new int[5];
int t;
Scanner inp = new Scanner(System.in);
System.out.println("Enter 5 numbers to print the ascending order of them");
for(int i=0;i<5 br="" i="">
a[i]= inp.nextInt();
for(int i=0;i<5 br="" i="">
{
for(int j=i+1;j<5 br="" j=""> {
5>5>5>
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int i=0;i<=5;i++)
{
try
{
System.out.println(a[i]);
}
catch(Exception e)
{
}
}
}
}
------------------------------------------------------------------------------------------------------------------
3.CommonData:
import java.util.ArrayList;
public class CommonUncommonData {
public static void main(String[] args) {
ArrayList v = new ArrayList ();
v.add(1);
v.add(2);
v.add(3);
v.add(4);
v.add(5);
System.out.println(v);
ArrayList v1 = new ArrayList ();
v1.add(2);
v1.add(3);
v1.add(10);
v1.add(12);
v1.add(13);
System.out.println(v1);
System.out.println("The common Data is");
v.retainAll(v1);
System.out.println(v);
}
}
----------------------------------------------------------------------------------------------------------------------
4.Counting:how many letters Upper,Lower,symbols,etc.......
import java.util.*;
import java.lang.String;
public class Counting {
public static void main(String[] args) {
int Upper=0,lower=0,blank=0,special=0,num=0;
int l;
Scanner input = new Scanner(System.in);
String n=input.nextLine();
for(int i=0;i
{
char ch=n.charAt(i);
l=ch;
if(l>=65 && l<=90)
Upper++;
else if(l>=97 && l<=122)
lower++;
else if(l==' ')
blank++;
else if(l>=48 && l<=57)
num++;
else
special++;
}
System.out.println("upper case alphabets "+Upper);
System.out.println("lower case alphabets "+lower);
System.out.println("blank spaces "+blank);
System.out.println("numbers "+num);
System.out.println("special characters"+special);
}
}
--------------------------------------------------------------------------------------------------------------
5.Count of Character:
import java.util.Scanner;
public class CountOfCharacters {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
char l;
int count=0;
System.out.println("Enter a String");
String val = inp.nextLine();
System.out.println("Enter a character to find the count of it in the string");
char search= inp.next().charAt(0);
int n=search;
for(int i=0;i
{
l=val.charAt(i);
int g=l;
if(g==search)
{
count++;
}
}
System.out.println("The letter repeated "+count + " times");
}
}
------------------------------------------------------------------------------------------------
6.Count of Common Integers:
import java.awt.List;
import java.util.*;
public class CountOfCommonIntegers {
public static void main(String[] args) {
int name[]={32,22,43,54,32,78,23,3} ;
ArrayList v = new ArrayList ();
for(int i=0;i<8 br="" i="">
{
v.add(name[i]);
}
HashMap emp = new HashMap ();
for(Integer s1 : v)
{
if(emp.containsKey(s1))
{
int c = emp.get(s1);
c++;
emp.put(s1, c);
}
else
{
emp.put(s1, 1);
}
}
System.out.println(emp);
}
}
-----------------------------------------------------------------------------------------------------
DOWNLOAD TOP 25 BASIC JAVA PROGRAMS FOR FRESHERS IN WORD FORMAT (CLICK HERE)
DOWNLOAD TOP 25 BASIC JAVA PROGRAMS FOR FRESHERS IN PDF FORMAT (CLICK HERE)
8>
1.Armstrong Number
import java.*;
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
int n,sum=0,temp,r;
Scanner input=new Scanner(System.in);
System.out.println("Enter number to find whether it is Armstrong or not");
n=input.nextInt();
temp=n;
System.out.println("temp value");
System.out.println(temp);
while(temp!=0)
{
r=temp%10;
System.out.println("r value");
System.out.println(r);
sum=sum+r*r*r;
System.out.println("sum is:");
System.out.println(sum);
temp=temp/10;
System.out.println("rest of no.");
System.out.println(temp);
}
if(n==sum)
{
System.out.println("num is armstrong");
}
else{
System.out.println("num is not an Armstrong");
}
}
}
-----------------------------------------------------------------------------------------------------------
2.Ascending Order:
import java.util.Scanner;
public class AscendingOrder {
public static void main(String[] args) {
int a[]= new int[5];
int t;
Scanner inp = new Scanner(System.in);
System.out.println("Enter 5 numbers to print the ascending order of them");
for(int i=0;i<5 br="" i="">
a[i]= inp.nextInt();
for(int i=0;i<5 br="" i="">
{
for(int j=i+1;j<5 br="" j=""> {
5>5>5>
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int i=0;i<=5;i++)
{
try
{
System.out.println(a[i]);
}
catch(Exception e)
{
}
}
}
}
------------------------------------------------------------------------------------------------------------------
3.CommonData:
import java.util.ArrayList;
public class CommonUncommonData {
public static void main(String[] args) {
ArrayList
v.add(1);
v.add(2);
v.add(3);
v.add(4);
v.add(5);
System.out.println(v);
ArrayList
v1.add(2);
v1.add(3);
v1.add(10);
v1.add(12);
v1.add(13);
System.out.println(v1);
System.out.println("The common Data is");
v.retainAll(v1);
System.out.println(v);
}
}
----------------------------------------------------------------------------------------------------------------------
4.Counting:how many letters Upper,Lower,symbols,etc.......
import java.util.*;
import java.lang.String;
public class Counting {
public static void main(String[] args) {
int Upper=0,lower=0,blank=0,special=0,num=0;
int l;
Scanner input = new Scanner(System.in);
String n=input.nextLine();
for(int i=0;i
{
char ch=n.charAt(i);
l=ch;
if(l>=65 && l<=90)
Upper++;
else if(l>=97 && l<=122)
lower++;
else if(l==' ')
blank++;
else if(l>=48 && l<=57)
num++;
else
special++;
}
System.out.println("upper case alphabets "+Upper);
System.out.println("lower case alphabets "+lower);
System.out.println("blank spaces "+blank);
System.out.println("numbers "+num);
System.out.println("special characters"+special);
}
}
--------------------------------------------------------------------------------------------------------------
5.Count of Character:
import java.util.Scanner;
public class CountOfCharacters {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
char l;
int count=0;
System.out.println("Enter a String");
String val = inp.nextLine();
System.out.println("Enter a character to find the count of it in the string");
char search= inp.next().charAt(0);
int n=search;
for(int i=0;i
{
l=val.charAt(i);
int g=l;
if(g==search)
{
count++;
}
}
System.out.println("The letter repeated "+count + " times");
}
}
------------------------------------------------------------------------------------------------
6.Count of Common Integers:
import java.awt.List;
import java.util.*;
public class CountOfCommonIntegers {
public static void main(String[] args) {
int name[]={32,22,43,54,32,78,23,3} ;
ArrayList
for(int i=0;i<8 br="" i="">
{
v.add(name[i]);
}
HashMap
for(Integer s1 : v)
{
if(emp.containsKey(s1))
{
int c = emp.get(s1);
c++;
emp.put(s1, c);
}
else
{
emp.put(s1, 1);
}
}
System.out.println(emp);
}
}
-----------------------------------------------------------------------------------------------------
DOWNLOAD TOP 25 BASIC JAVA PROGRAMS FOR FRESHERS IN WORD FORMAT (CLICK HERE)
DOWNLOAD TOP 25 BASIC JAVA PROGRAMS FOR FRESHERS IN PDF FORMAT (CLICK HERE)