Java-Interview Programs
Java-Interview Programs
Basic Programs
NIYAZ UL HASAN 1
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
int c=a+b;
Input:
Enter the First Number:
10
Enter the Second Number:
20
Output:
Sum of two numbers is =30
Input:
Enter the First Number:
5
Enter the Second Number:
10
Output:
Sum of two numbers is =15
NIYAZ UL HASAN 2
IHUB TALENT MANAGEMENT
Q) Write a java program to perform sum of two numbers without using third variable?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
Input:
Enter the First Number:
10
Enter the Second Number:
20
Output:
Sum of two numbers is =30
Input:
Enter the First Number:
5
Enter the Second Number:
10
Output:
Sum of two numbers is =15
NIYAZ UL HASAN 3
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
int square= n*n;
Input:
Enter the Number:
5
Output:
Square of a given number is =25
Input:
Enter the Number:
6
Output:
Square of a given number is =36
NIYAZ UL HASAN 4
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
int square= n*n*n;
Input:
Enter the Number:
5
Output:
Cube of a given number is =125
Input:
Enter the Number:
3
Output:
Cube of a given number is =27
NIYAZ UL HASAN 5
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
double area=3.14*r*r;
Input:
Enter the Radius:
5
Output:
Area of a circle is = 78.5
Input:
Enter the Radius:
3
Output:
Area of a circle is = 28.26
NIYAZ UL HASAN 6
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
double perimeter=2*3.14*r;
Input:
Enter the Radius:
5
Output:
Perimeter of circle is= 31.4
Input:
Enter the Radius:
9
Output:
Perimeter of circle is= 56.52
NIYAZ UL HASAN 7
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
double area=0.5*base*height;
Input:
Enter the Base:
5
Enter the Height:
3
Output:
Area of a triangle is = 7.5
Input:
Enter the Base:
6
Enter the Height:
4
NIYAZ UL HASAN 8
IHUB TALENT MANAGEMENT
Output:
Area of a triangle is = 12.0
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
double area= l*b;
Input:
Enter the Length
5
Enter the Breadth:
8
Output:
Area of a rectangle is = 40
Input:
Enter the Length
4
Enter the Breadth:
3
NIYAZ UL HASAN 9
IHUB TALENT MANAGEMENT
Output:
Area of a rectangle is = 12
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
double area= s*s;
Input:
Enter the Side:
5
Output:
Area of a square is =25
Input:
Enter the Side:
5
Output:
Area of a square is =25
NIYAZ UL HASAN 10
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
int temp=a;
a=b;
b=temp;
Input:
Enter the First Number:
10
Enter the Second Number:
20
Output:
NIYAZ UL HASAN 11
IHUB TALENT MANAGEMENT
Q) Write a java program to perform swapping of two numbers without using third variable?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
a=a+b;
b=a-b;
a=a-b;
Input:
Enter the First Number:
10
Enter the Second Number:
20
Output:
NIYAZ UL HASAN 12
IHUB TALENT MANAGEMENT
Q) Write a java program to find out greatest of two numbers using ternary operator?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
int max=(a>b) ? a: b;
Input:
Enter the First Number:
10
Enter the Second Number:
20
Output:
Greatest of two numbers is = 20
Input:
NIYAZ UL HASAN 13
IHUB TALENT MANAGEMENT
Output:
Greatest of two numbers is = 100
Q) Write a java program to find out greatest of three numbers using ternary operator?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
int max=(a>b) ? (a>c ? a : c) : (b>c ? b : c);
Input:
NIYAZ UL HASAN 14
IHUB TALENT MANAGEMENT
Output:
Greatest of three numbers is = 30
Q) Write a java program to accept one salary then find out 10% of tax?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
double tax=(double)tax*10/100;
Input:
Enter the Salary: 1000
Output:
Tax deduction is =100.0
Input:
Enter the Salary: 1004
Output:
Tax deduction is =100.4
NIYAZ UL HASAN 15
IHUB TALENT MANAGEMENT
Input:
Enter the Salary: 5000
Output:
Tax deduction is =500.0
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
float f= cel * 9/5 +32;
Input:
Output:
Input:
NIYAZ UL HASAN 16
IHUB TALENT MANAGEMENT
Output:
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
float cel=((temp-32)*5)/9;
Input:
Output:
NIYAZ UL HASAN 17
IHUB TALENT MANAGEMENT
Input:
Output:
Control Statements
Programs
NIYAZ UL HASAN 18
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
if(age>=18)
System.out.println(“You are eligible to vote”);
else
System.out.println(“You are not eligible to vote”);
}
}
Input:
Output:
NIYAZ UL HASAN 19
IHUB TALENT MANAGEMENT
Input:
Output:
Q) Write a java program to find out greatest of two numbers using if and else statement?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
if(a>b)
System.out.println(a+“ is greatest”);
else
System.out.println(b+“ is greatest”);
}
}
Input:
NIYAZ UL HASAN 20
IHUB TALENT MANAGEMENT
Output:
20 is greatest
Input:
Output:
35 is greatest
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
if(n==0)
{
System.out.prinltn(“It is not a +ve or -ve number”);
System.exit(0);
}
//logic
if(n>0)
System.out.println(“It is positive number ”);
else
System.out.println(“It is negative number ”);
}
NIYAZ UL HASAN 21
IHUB TALENT MANAGEMENT
Input:
Enter the Number:
5
Output:
It is positive number
Input:
Enter the Number:
-5
Output:
It is negative number
Input:
Enter the Number:
0
Output:
It is not a +ve or -ve number
Q) Write a java program to find out given number is even or odd?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the Number :”);
int n=sc.nextInt();
//logic
if(n%2==0)
System.out.println(“It is even number ”);
else
System.out.println(“It is odd number ”);
}
}
Input:
NIYAZ UL HASAN 22
IHUB TALENT MANAGEMENT
Output:
It is even number
Input:
Output:
It is odd number
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
if(n%2==1 || n%2!=0)
System.out.println(“It is odd number ”);
else
System.out.println(“It is not odd number ”);
}
}
Input:
Enter the Number:
3
NIYAZ UL HASAN 23
IHUB TALENT MANAGEMENT
Output:
It is odd number
Input:
Enter the Number:
4
Output:
It is not odd number
Input:
Enter the Number:
7
Output:
It is odd number
Q) Write a java program to find out given year is Leap year or not?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
if(year%4==0)
System.out.println(“It is a Leap Year ”);
else
System.out.println(“It is not a Leap Year ”);
}
}
Input:
NIYAZ UL HASAN 24
IHUB TALENT MANAGEMENT
Output:
It is not a Leap year
Input:
Output:
It is a Leap year
Q) Write a java program to accept one alphabet then find out given alphabet is a vowel or not?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
if(ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’|| ch==’u’)
System.out.println(“It is a Vowel ”);
else
System.out.println(“It is not a Vowel ”);
}
}
Input:
NIYAZ UL HASAN 25
IHUB TALENT MANAGEMENT
Output:
It is a vowel
Input:
Enter the Alphabet:
e
Output:
It is a vowel
Input:
Enter the Alphabet:
z
Output:
It is not a vowel
Q) Write a java program to accept one alphabet then find out given alphabet is a upper case
letter,lower case letter , digit or special symbol?
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
if(ch>=’A’ && ch<=’Z’)
System.out.println(“It is upper case letter ”);
else if(ch>=’A’ && ch<=’Z’)
System.out.println(“It is lower case letter ”);
else if(ch>=’0’ && ch<=’9’)
System.out.println(“It is digit”);
else
NIYAZ UL HASAN 26
IHUB TALENT MANAGEMENT
Input:
Enter the Alphabet:
A
Output:
It is upper case letter
Input:
Enter the Alphabet:
$
Output:
It is special symbol
Input:
Enter the Alphabet:
7
Output:
It is digit
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
NIYAZ UL HASAN 27
IHUB TALENT MANAGEMENT
Input:
Enter the Alphabet
a
Output:
It is vowel
Input:
Enter the Alphabet
p
Output:
It is Consonant
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
for(int i=1;i<=n;i++)
{
System.out.print(i+“ ”);
}
}
}
NIYAZ UL HASAN 28
IHUB TALENT MANAGEMENT
Input:
Output:
1 2 3 4 5 6 7 8 9 10
Input:
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
int sum=0
for(int i=1;i<=n;i++)
{
sum=sum+I;
NIYAZ UL HASAN 29
IHUB TALENT MANAGEMENT
Input:
Output:
Input:
Output:
import java.util.*;
class Test
{
public static void main(String[] args)
{
int fact=1;
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
for(int i=n;i>=1;i--)
{
NIYAZ UL HASAN 30
IHUB TALENT MANAGEMENT
fact=fact*I;
}
Input:
Output:
Input:
Output:
import java.util.*;
class Test
{
public static void main(String[] args)
{
int rem,sum=0;
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
while(n>0)
NIYAZ UL HASAN 31
IHUB TALENT MANAGEMENT
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
Input:
Output:
Input:
Output:
import java.util.*;
class Test
{
public static void main(String[] args)
{
int rem,rev=0;
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
while(n>0)
NIYAZ UL HASAN 32
IHUB TALENT MANAGEMENT
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
Input:
Output:
Input:
Output:
import java.util.*;
class Test
{
public static void main(String[] args)
{
int rem,rev=0,temp;
//asking inputs
Scanner sc=new Scanner(System.in);
NIYAZ UL HASAN 33
IHUB TALENT MANAGEMENT
temp=n;
//logic
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(temp==rev)
System.out.println(“It is palindrome number);
else
System.out.println(“It is not a palindrome number”);
}
}
Input:
Enter the number:
121
Output:
It is palindrome number
Input:
Enter the number:
121
Output:
It is not a palindrome number
import java.util.*;
class Test
{
public static void main(String[] args)
{
int rem,sum=0,temp;
//asking inputs
Scanner sc=new Scanner(System.in);
NIYAZ UL HASAN 34
IHUB TALENT MANAGEMENT
temp=n;
//logic
while(n>0)
{
rem=n%10;
rev=sum+rem*rem*rem;
n=n/10;
}
if(temp==sum)
System.out.println(“It is Armstrong number);
else
System.out.println(“It is not Armstrong number”);
}
}
Input:
Enter the Number:
121
Output:
It is not Armstrong number
Input:
Enter the Number:
153
Output:
It is Armstrong number
import java.util.*;
class Test
{
public static void main(String[] args)
{
int a=0,b=1,c;
//asking inputs
Scanner sc=new Scanner(System.in);
NIYAZ UL HASAN 35
IHUB TALENT MANAGEMENT
int n=sc.nextInt();
System.out.print(a+” “+b);
//logic
for(int i=1;i<=n;i++)
{
c=a+b;
System.out.print(“ “+c);
a=b;
b=c;
}
}
}
Input:
Enter the number:
5
Output:
0112358
Input:
Enter the number:
7
Output:
0 1 1 2 3 5 8 13 21
import java.util.*;
class Test
{
public static void main(String[] args)
{
int flag=0;
//asking inputs
Scanner sc=new Scanner(System.in);
NIYAZ UL HASAN 36
IHUB TALENT MANAGEMENT
//logic
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
System.out.println(“It is a prime number ”);
else
System.out.println(“It is not a prime number ”);
}
}
Input:
Output:
It is not a prime number
Input:
Output:
It is a prime number
Q) Write a java program to check given number is perfect or not?
import java.util.*;
class Test
{
public static void main(String[] args)
{
int sum=0;
//asking inputs
Scanner sc=new Scanner(System.in);
NIYAZ UL HASAN 37
IHUB TALENT MANAGEMENT
//logic
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
{
sum=sum+i;
// System.out.println(sum);
}
}
if(sum==n)
System.out.println(“It is a perfect number ”);
else
System.out.println(“It is not a perfect number ”);
}
}
Input:
Output:
It is a perfect number
Input:
Output:
It is not a perfect number
Q) Write a java program to convert Binary to Decimal number?
import java.util.*;
class Test
{
public static void main(String[] args)
{
int rem,sum=0,i=0;
//asking inputs
NIYAZ UL HASAN 38
IHUB TALENT MANAGEMENT
//logic
while(n>0)
{
rem=n%10;
n=n/10;
sum=sum+rem*(int)Math.pow(2,i++);
}
System.out.println(“Binary to Decimal value is =”+sum);
}
}
Input:
Output:
Input:
Output:
import java.util.*;
class Test
{
public static void main(String[] args)
{
int rem,i=0;
String sum=””;
NIYAZ UL HASAN 39
IHUB TALENT MANAGEMENT
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
while(n>0)
{
rem=n%10;
n=n/10;
sum=rem+sum;
}
System.out.println(“Decimal to Binary value is =”+sum);
}
}
Input:
Output:
Input:
Output:
Question: 1
An Evil number is a positive whole number which has even number of 1’s in is binary equivalent.
Example: Binary equivalent of 9’s is 1001.Which contains even number of 1’s.
NIYAZ UL HASAN 40
IHUB TALENT MANAGEMENT
Design a program to accept a positive whole number ‘N’ where N>2 and N<100.Find the binary
equivalent of the number and count the number of 1’s in it and display whether it is an Evil number or
not with an appropriate message.
Example:
INPUT:
N=15
OUTPUT:
Binary Equivalent: 1111
No of 1’s is: 4
It is Evil Number
Example:
INPUT:
N=3
OUTPUT:
Binary Equivalent: 1010
No of 1’s is: 1
It is not Evil Number
import java.util.*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner(System.in);
//logic
if(n>2 && n<100)
{
//get the converted binary number
String bin=getBinaryNumber(n);
int cnt=0;
NIYAZ UL HASAN 41
IHUB TALENT MANAGEMENT
if(cnt%2==0)
System.out.println("It is Evil Number ");
else
System.out.println("It is not Evil Number");
}//end if
else
{
System.out.println(“Number out of Range ”);
}
}
//method to convert a number to binary
public static String getBinaryNumber(int n)
{
String sum="";
while(n>0)
{
int rem=n%2;
sum=rem+sum;
n=n/2;
}
return sum;
}
}
Import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
NIYAZ UL HASAN 42
IHUB TALENT MANAGEMENT
//logic
for(int i=1;i<=10;i++)
{
System.out.println(n+” * “+i+” = “+n*i);
}
}
}
Input:
Output:
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
import java.util.*;
public class Test
{
public static void main(String[] args)
{
int result=1;
NIYAZ UL HASAN 43
IHUB TALENT MANAGEMENT
for(int i=1;i<=power;i++)
{
result=base*result;
}
System.out.println("Power of a Number is ="+result);
}
}
Input:
Enter the Base Number:
5
Enter the Power Number:
3
Output:
NIYAZ UL HASAN 44
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number :");
int n=sc.nextInt();
int sum,rem;
while(n>=0)
{
sum=0;
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}//end
if(sum>=10)
n=sum;
else
break;
}//end
}
}
Input:
Output:
19
10
1
NIYAZ UL HASAN 45
IHUB TALENT MANAGEMENT
import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
for(int i=2;i<=n;i++)
{
boolean flag=true;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
flag=false;
break;
}
}
//display all prime numbers
if(flag==true)
System.err.print(i+" ");
}
}
}
Input:
Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
NIYAZ UL HASAN 46
IHUB TALENT MANAGEMENT
Q) Write a java program to perform addition of two numbers without using Addition (+)
operator?
import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
while(y!=0)
{
int carry=x&y;
x=x^y;
y=carry<<1;
}
System.out.println("Sum of two numbers is ="+x);
}
}
Input:
Output:
NIYAZ UL HASAN 47
IHUB TALENT MANAGEMENT
LOOP PATTERNS
NIYAZ UL HASAN 48
IHUB TALENT MANAGEMENT
1111
2222
3333
4444
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int i,j;
//rows
for(i=1;i<=4;i++)
{
//cols
for(j=1;j<=4;j++)
{
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
NIYAZ UL HASAN 49
IHUB TALENT MANAGEMENT
1234
1234
1234
1234
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int i,j;
//rows
for(i=1;i<=4;i++)
{
//cols
for(j=1;j<=4;j++)
{
System.out.print(j+" ");
}
System.out.println(" ");
}
}
}
NIYAZ UL HASAN 50
IHUB TALENT MANAGEMENT
* ***
* ***
* ***
* ***
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int i,j;
//rows
for(i=1;i<=4;i++)
{
//cols
for(j=1;j<=4;j++)
{
System.out.print("*"+" ");
}
System.out.println(" ");
}
}
}
NIYAZ UL HASAN 51
IHUB TALENT MANAGEMENT
4444
3333
2222
1111
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int i,j;
//rows
for(i=4;i>=1;i--)
{
//cols
for(j=1;j<=4;j++)
{
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
NIYAZ UL HASAN 52
IHUB TALENT MANAGEMENT
AAAA
BBBB
CCCC
DDDD
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
char i,j;
//rows
for(i=’A’;i<=’D’;i++)
{
//cols
for(j=’A’;j<=’D’;j++)
{
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
NIYAZ UL HASAN 53
IHUB TALENT MANAGEMENT
* * * *
* *
* *
* * * *
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int i,j;
//rows
for(i=1;i<=4;i++)
{
//cols
for(j=1;j<=4;j++)
{
If(i==1 || i==4|| j==1 ||j==4)
System.out.print("*"+" ");
else
System.out.println("-");
}
System.out.println(" ");
}
}
}
NIYAZ UL HASAN 54
IHUB TALENT MANAGEMENT
* - - -
- * - -
- - * -
- - - *
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int i,j;
//rows
for(i=1;i<=4;i++)
{
//cols
for(j=1;j<=4;j++)
{
If(i==j)
System.out.print("*"+" ");
else
System.out.println("-");
}
System.out.println(" ");
NIYAZ UL HASAN 55
IHUB TALENT MANAGEMENT
}
}
* - - - *
- * - * -
- - * - -
- * - * -
* - - - *
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
int i,j;
//rows
for(i=1;i<=5;i++)
{
//cols
for(j=1;j<=5;j++)
{
If(i==j || i+j==6)
System.out.print("*"+" ");
else
System.out.println("-");
NIYAZ UL HASAN 56
IHUB TALENT MANAGEMENT
}
System.out.println(" ");
}
}
}
Q)
1
22
333
4444
NIYAZ UL HASAN 57
IHUB TALENT MANAGEMENT
Q)
4444
333
22
1
NIYAZ UL HASAN 58
IHUB TALENT MANAGEMENT
Q)
****
***
**
*
NIYAZ UL HASAN 59
IHUB TALENT MANAGEMENT
Q)
1
24
369
4 8 12 16
NIYAZ UL HASAN 60
IHUB TALENT MANAGEMENT
Q)
1
23
456
7 8 9 10
NIYAZ UL HASAN 61
IHUB TALENT MANAGEMENT
Q)
2
4 6
8 10 12
14 16 18 20
NIYAZ UL HASAN 62
IHUB TALENT MANAGEMENT
k=k+2;
}
System.out.println(" ");
}
}
}
Q)
1
3 5
7 9 11
13 15 17 19
NIYAZ UL HASAN 63
IHUB TALENT MANAGEMENT
System.out.print(k+" ");
k=k+2;
}
System.out.println(" ");
}
}
}
Q)
1
22
333
4444
NIYAZ UL HASAN 64
IHUB TALENT MANAGEMENT
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
Q)
*
**
***
****
int i,j;
//rows
for(i=1;i<=4;i++)
{
//space
for(j=4;j>i;j--)
{
System.out.print(" ");
NIYAZ UL HASAN 65
IHUB TALENT MANAGEMENT
Q)
4444
333
22
1
NIYAZ UL HASAN 66
IHUB TALENT MANAGEMENT
System.out.print(" ");
}
Q)
1
22
333
4444
333
22
1
}
for(j=1;j<=i;j++)
NIYAZ UL HASAN 67
IHUB TALENT MANAGEMENT
{
System.out.print(i+" ");
}
System.out.println(" ");
}
for(i=3;i>=1;i--)
{
for(j=4;j>i;j--)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print(i+" ");
}
System.out.println(" ");
}
}
}
Q)
1
222
33333
4444444
for(j=4;j>i;j--)
{
System.out.print(" ");
NIYAZ UL HASAN 68
IHUB TALENT MANAGEMENT
for(j=1;j<=i;j++)
{
System.out.print(i+"");
}
System.out.println(" ");
}
}
}
Q)
1
121
12321
1234321
for(j=4;j>i;j--)
{
System.out.print(" ");
NIYAZ UL HASAN 69
IHUB TALENT MANAGEMENT
System.out.println(" ");
}
}
}
Q)
1234321
12321
121
1
for(j=4;j>i;j--)
{
NIYAZ UL HASAN 70
IHUB TALENT MANAGEMENT
System.out.print(" ");
System.out.println(" ");
}
}
}
Q)
A
ABA
ABCBA
ABCDCBA
NIYAZ UL HASAN 71
IHUB TALENT MANAGEMENT
for(j='D';j>i;j--)
{
System.out.print(" ");
System.out.println(" ");
}
}
}
import java.util.*;
public class Test
{
public static void main(String args[])
{
int counter = 2;
NIYAZ UL HASAN 72
IHUB TALENT MANAGEMENT
while(!isPrimeNumber(counter))
{
counter++;
}
System.out.print(counter+" ");
counter++;
}
System.out.println();
}
}
NIYAZ UL HASAN 73
IHUB TALENT MANAGEMENT
Arrays
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
int size=sc.nextInt();
NIYAZ UL HASAN 74
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
//displaying elements
for(int i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Input:
Enter the Array Size:
2
Enter the element of arr[0]:
6
Enter the element of arr[1]:
10
Output:
Given Elements are :
6 10
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
int size=sc.nextInt();
NIYAZ UL HASAN 75
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
int sum=0;
//logic
for(int i=0;i<size;i++)
{
sum=sum+arr[i];
}
Input:
Enter the Array Size:
2
Enter the element of arr[0]:
6
Enter the element of arr[1]:
10
Output:
Sum of array elements is = 16
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
int size=sc.nextInt();
NIYAZ UL HASAN 76
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
System.out.println("Reverse of a given number is :");
//displaying elements
for(int i=size-1;i>=0;i--)
{
System.out.print(arr[i]+" ");
}
}
}
Input:
Enter the Array Size:
3
Enter the element of arr[0]:
6
Enter the element of arr[1]:
10
Enter the element of arr[2]:
1
Output:
Reverse of a given number is = 1 10 6
Q) Write a java program to find out least or smallest element in a given array?
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
int size=sc.nextInt();
NIYAZ UL HASAN 77
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
int small=arr[0];
//logic
for(int i=0;i<size;i++)
{
if(arr[i]<small)
{
small=arr[i];
}
}
System.out.println("Least Element in a given array is ="+small);
}
}
Input:
Enter the Array Size:
3
Enter the element of arr[0]:
6
Enter the element of arr[1]:
10
Enter the element of arr[2]:
1
Output:
Least element in a given array is = 1
Q) Write a java program to find out highest or largest element in a given array?
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
int size=sc.nextInt();
NIYAZ UL HASAN 78
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
int big=arr[0];
//logic
for(int i=0;i<size;i++)
{
if(arr[i]<big)
{
small=arr[i];
}
}
System.out.println("Largest Element in a given array is ="+big);
}
}
Input:
Enter the Array Size:
3
Enter the element of arr[0]:
6
Enter the element of arr[1]:
10
Enter the element of arr[2]:
1
Output:
Largest element in a given array is = 10
Q) Write a java program to display array elements in sorting order i.e ascending order?
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
NIYAZ UL HASAN 79
IHUB TALENT MANAGEMENT
int size=sc.nextInt();
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
//ascending logic
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i]<arr[j])
{
int temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//displaying elements
for(int i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Input : 2 5 4 6 1
Output : 1 2 4 5 6
Q) Write a java program to display array elements in sorting order i.e descending order?
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
NIYAZ UL HASAN 80
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
//descending logic
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//displaying elements
for(int i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
}
}
Input : 2 5 4 6 1
Output : 6 5 4 2 1
Q) Write a java program to find out number of even and odd elements in a given array?
import java.util.*;
public class Test
{
public static void main(String args[])
{
NIYAZ UL HASAN 81
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
int even=0,odd=0;
//Logic
for(int i=0;i<size;i++)
{
if(arr[i]%2==0)
even++;
else
odd++;
}
System.out.println("No of Even element is ="+even);
System.out.println("No of Odd element is ="+odd);
}
}
Input: 1 5 6 4 7 3
Output:
Q) Write a java program to find out sum of even and odd elements in a given array?
import java.util.*;
public class Test
{
public static void main(String args[])
NIYAZ UL HASAN 82
IHUB TALENT MANAGEMENT
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
int size=sc.nextInt();
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
int even=0,odd=0;
//Logic
for(int i=0;i<size;i++)
{
if(arr[i]%2==0)
even=even+arr[i];
else
odd=odd+arr[i];
}
System.out.println("Sum of Even element is ="+even);
System.out.println("Sum of Odd element is ="+odd);
}
}
Input: 1 5 6 4 7 3
Output:
Q) Write a java program to find out number of occurrence of a given number in array?
Input : 2 1 3 5 1 4 1 3 5 9
Enter the element: 1
NIYAZ UL HASAN 83
IHUB TALENT MANAGEMENT
Output:
No of Occurrence is : 3
import java.util.*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Array Size: ");
int size=sc.nextInt();
//inserting elements
for(int i=0;i<size;i++)
{
System.out.println("Enter the element of arr["+i+"] :");
arr[i]=sc.nextInt();
}
//Asking element
System.out.println("Enter the element : ");
int ele=sc.nextInt();
int cnt=0;
//Logic
for(int i=0;i<size;i++)
{
if(arr[i]==ele)
{
cnt++;
}
}
System.out.println("No of occurance of a given element is ="+cnt);
}
}
import java.util.Scanner;
public class Test
NIYAZ UL HASAN 84
IHUB TALENT MANAGEMENT
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] arr={3,4,1,1,7,8,4};
Output:
Input:
int[] arr={5,7,8,1,3,3,5,7};
Output:
NIYAZ UL HASAN 85
IHUB TALENT MANAGEMENT
{
public static void main(String[] args)
{
int[] arr={3,2,2,5,6,1,1};
Output:
Input:
Int[] arr={4,5,6,3,2,1,1,5,9};
Output:
NIYAZ UL HASAN 86
IHUB TALENT MANAGEMENT
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
Arrays.sort(arr);
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
Arrays.sort(arr);
NIYAZ UL HASAN 87
IHUB TALENT MANAGEMENT
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
Arrays.sort(arr);
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
Arrays.sort(arr);
NIYAZ UL HASAN 88
IHUB TALENT MANAGEMENT
Q) Write a java program to find out all the pairs of Integer elements in array whose sum is
equals to given number?
import java.util.Arrays;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] arr={6,1,2,8,9,4,5};
Input:
Enter the number: 8
Output:
The array created is: 6,1,2,8,9,4,5
Indicates of the elements whose sum is:
6+2=8
NIYAZ UL HASAN 89
IHUB TALENT MANAGEMENT
Q) Write a program to print all the LEADERS in the array. An element is leader if it is greater
than all the elements to its right side. And the rightmost element is always a leader.
For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2?
Output:
18 9 5
NIYAZ UL HASAN 90
IHUB TALENT MANAGEMENT
Q) You are given a list of n-1 integers and these integers are in the range of 1 to n. There are
no duplicates in the list. One of the integers is missing in the list. Write an efficient code to
find the missing integer.
Example:
int expected_elements=arr.length+1;
NIYAZ UL HASAN 91
IHUB TALENT MANAGEMENT
String Programs
NIYAZ UL HASAN 92
IHUB TALENT MANAGEMENT
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//asking inputs
System.out.println("Enter the String :");
String str=sc.nextLine();
Input:
Enter the String:
Ihub
Output:
Length of the String is = 4
Input:
Enter the String:
Training
Output:
Length of the String is = 8
NIYAZ UL HASAN 93
IHUB TALENT MANAGEMENT
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//asking inputs
System.out.println("Enter the First String :");
String str1=sc.nextLine();
String concat_str=str1.concat(str2);
System.out.println("Concatenate String is : "+concat_str);
}
}
Input:
Enter the First String:
Ihub
Enter the Second String:
Training
Output:
Concatenate String is : IhubTraining
Input:
Enter the First String:
Java
Enter the Second String:
Training
NIYAZ UL HASAN 94
IHUB TALENT MANAGEMENT
Output:
Concatenate String is : JavaTraining
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//asking inputs
System.out.println("Enter the First String :");
String str1=sc.nextLine();
//asking inputs
System.out.println("Enter the Second String :");
String str2=sc.nextLine();
boolean compare_str=str1.equals(str2);
if(compare_str)
System.out.println("Both are equal");
else
System.out.println("Both are not equal");
}
}
Input:
Enter the First String:
hi
Enter the Second String:
hi
Output:
Both are equal
Input:
NIYAZ UL HASAN 95
IHUB TALENT MANAGEMENT
Output:
Both are not equal
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//asking inputs
System.out.println("Enter the String :");
String str=sc.nextLine();
Input:
Enter the String:
This Is Java
Ouptut:
avaJ sI sihT
Input:
NIYAZ UL HASAN 96
IHUB TALENT MANAGEMENT
Ouptut:
gniniarT buhI
Example:
Input:
This Is Java Class
Output:
Class Java Is This
Example:
Input:
Java Class
Output:
Class Java
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//asking inputs
System.out.println("Enter the String :");
String str=sc.nextLine();
NIYAZ UL HASAN 97
IHUB TALENT MANAGEMENT
System.out.print(sarr[i]+" ");
}
}
}
Example
Input:
Enter the String:
This Is Java Class
Output:
sihT sI avaJ ssalC
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//asking inputs
System.out.println("Enter the String :");
String str=sc.nextLine();
NIYAZ UL HASAN 98
IHUB TALENT MANAGEMENT
for(int i=carr.length-1;i>=0;i--)
{
System.out.print(carr[i]);
}
//space after each word
System.out.print(" ");
}
}
}
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Input:
Output:
Input:
NIYAZ UL HASAN 99
IHUB TALENT MANAGEMENT
Output:
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
str.chars().distinct().forEach(c->sb.append((char)c));
System.out.println(sb);
}
}
Input:
Output:
Gogle
Input:
Hello
Output:
Helo
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String characters="";
String duplicates="";
}
characters+=current;
}
System.out.println(duplicates);
}
}
Input:
Enter the String:
google
Output:
og
Q) Write an efficient program to test if two given String is a rotation of each other or not,
Ex:
If the given String is "XYZ" and "ZXY" then your function should return true.
But if the input is "XYZ" and "YXZ" then return false.
Example:
Input:
Please enter original string: XYZ
Please enter rotation string: ZXY
Output:
XYZ and ZXY are rotation to each other.
Example:
Input:
Please enter original string: XYZ
Please enter rotation string: ABC
Output:
Sorry, they are not rotation of another.
Example:
Input:
Output:
Sorry, they are not rotation of another.
import java.util.Scanner;
public class Test
{
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
//asking inputes
System.out.println("Please enter original String");
String input = sc.nextLine();
if (checkRotatation(input, rotation))
{
System.out.println(input + " and " + rotation + " are rotation of each
other");
}
else
{
System.out.println("Sorry, they are not rotation of another");
}
sc.close();
}
public static boolean checkRotatation(String original, String rotation)
{
if (original.length() != rotation.length())
{
return false;
}
String concatenated = original + original;
For example
If given input is "123" then your program should print all 6 permutations
e.g. "123", "132", "213", "231", "312" and "321".
Example
Input:
I am am Learning java java
Output:
I=1 , am=2, Learning=1, java=2
import java.util.*;
class Test
{
public static void main(String[] args)
{
findDuplicatesWords("I am am Learning java java");
}
for(String tempString: s)
{
if(lhm.get(tempString)!=null)
{
lhm.put(tempString,lhm.get(tempString)+1);
}
else
{
lhm.put(tempString,1);
}
}
System.out.println(lhm);
}
}
Example
Input:
java
Output:
J=1, a=2, v=1
import java.util.*;
class Test
{
public static void main(String[] args)
{
findDuplicatesCharacters("java");
}
for(int i=0;i<str.length();i++)
{
char c=str.charAt(i);
if(lhm.get(c)!=null)
{
lhm.put(c,lhm.get(c)+1);
}
else
{
lhm.put(c,1);
}
}
System.out.println(lhm);
}
}
Example:
Input:
([{}])
Output:
Balanced
import java.util.*;
public class Test
{
public static void main(String[] args)
{
String expr = "([{}])";
// Function call
if (areBracketsBalanced(expr))
System.out.println("Balanced ");
else
System.out.println("Not Balanced ");
}
// If stack is empty
if (stack.isEmpty())
{
return false;
}
char check;
switch (x)
{
case ')':
check = stack.pop();
if (check == '{' || check == '[')
return false;
break;
case '}':
check = stack.pop();
if (check == '(' || check == '[')
return false;
break;
case ']':
check = stack.pop();
Example:
Input:
([{}}])
Output:
Not Balanced