Tuesday, 26 September 2017

Top 10 Basic Java Programs : Asked In Java Interview

                  

Top 10 Basic Java Programs

      Here is the Top 10 Basic Java Programs to tackle any Java  technical interview ,it will also help to understand basic Java Concepts ,this Programs  are ask in Various Technical Interview Over the Year Whether you are Fresher or experience one, in interview or any Programming test this kind of questions you may face. So please take a look and if you have any Question Please comment below.
Happy Learnings!!!



Program:
class SwapWithoutThirdVariable
{
public static void main( String args[ ] )
{
int   a, b;
a = 10;
b = 20;
System.out.print("\nBefore Swapping : " );
System.out.print( a + "   " + b );
a = a + b;
b = a - b;
a = a - b;
System.out.print( "\nAfter Swapping  : " );
System.out.print( a + "   " + b );
}
}
Output:
Before Swapping :  10 20
After Swapping  : 20 10
Program  :
class SwapWithoutThirdVariable
{
public static void main( String args[ ] )
{
int   a, b, t ;
a = 10;
b = 20;
System.out.print("\nBefore Swapping : " );
System.out.print( a + "   " + b );
a=t;
a=b;
b=t;
System.out.print( "\nAfter Swapping  : " );
System.out.print( a + "   " + b );
}
}
Output:
Before Swapping :  10 20
After Swapping  : 20 10
Program :
class  FibonacciTest
{
public static void main( String args[ ] )
{
int n, i , a = 0, b = 1, c = 0;
 
n = 6;
System.out.print( " " + a + " " + b );
for( i=1 ; i<=(n-2) ; i++ )
   {
c = a + b;
System.out.print( " " + c );
a = b;
b = c;
   }
   }
}
Output :
0  1  1  2  3  5
Program:
class  PrimeTest
{
public static void main( String args[ ] )
{
int i, n;
boolean  flag = true;
n = 29;
for( i=2 ; i<=n/2 ; i++ )
{
if( n % i == 0 )
{
flag = false;
break;
}
}
if( flag == true )
System.out.print( "\n Number " + n + " is a PRIME Number." );
else
System.out.print( "\n Number " + n + " is NOT a PRIME Number." );
}
}
Output:
Number 29 is a PRIME Number.
Program :
class  ArguSum
{
   public static void main( String args[ ] )
   {
int  s = 0, len;
len = args.length;
 
for( int i=0 ; i<len ; i++ )
{
int  x = Integer.parseInt( args[ i ] );
s = s + x;
}
System.out.println( " Sum of Command-Line Arguments ::: " + s );
  }
}
Output:
D:\Java Examples\java ArguSum.java 1 2 3 4 5 6 7 8 9 10
Sum of Command-Line Arguments ::: 55
Program :
class  FactorialTest
{
public static void main( String args[ ] )
{
int i, n;
n = 5;
int f = 1;
for( i=n ; i>=1 ; i-- )
{
f = f * i ;
}
System.out.println( "\n The Factorial of " + n + " is = " + f );
}
}
Output:
The Factorial of 5 is = 120
Program :
class  ArmstrongTest
{
public static void main( String args[ ] )
{
int r, n, x, sum;
n = 153;
x = n;
sum = 0;
while( n != 0 )
{
r = n % 10;
sum = sum + ( r * r * r );
n = n / 10;
}
if( sum == x )
System.out.println( " Number " + x + " is a Amstrong Number." );
else
System.out.println( " Number " + x + " is NOT a Amstrong Number." );
}
}
Output: 153 is Armstrong Number
Program:
public class PalindromeNumber {
    public static void main(String[] args) {
        int n = 1215;
        int sum = 0;
        int m = n;
        while (n > 0) {
            //int sum=0;
            int r = n % 10;
            sum = sum * 10 + r;
            n = n / 10;
        }
        if (m == sum) {
            System.out.println("palindrome");
        } else {
            System.out.println("not palindrome");
        }
    }
}
Output:
not palindrome
Program :
class OddEvenSum {
    public static void main(String[] args) {
        int i;
        int sum = 0;
        int sum1 = 0;
        for (i = 0; i <= 10; i++) {
            if (i % 2 == 0) {
                sum = sum + i;
            } else {
                sum1 = sum1 + i;
            }
        }
        System.out.println("sum of even nos is : " + sum);
        System.out.println("sum of odd nos is : " + sum1);
    }
}
Output:
sum of even nos is : 30
sum of odd nos is : 25
Program :
class  NumberOfDigits
{
public static void main( String args[ ] )
{
int n, c;
n = 1234;
c = 0;
while( n != 0 )
{
n = n / 10;
c++;
}
System.out.println( " Number of Digits : " + c );
}
}
Output:
Number of Digits : 4

No comments:

Post a Comment

Abstract Class & Interface in Java

      Abstract Class & Interface in Java         Here is the most important topic on Java Abstract class and interface, Abstraction ...