Java Basic Programs for Interviews
Java Basic Programs for Interviews
Basic Programs
NIYAZ UL HASAN 1
IHUB TALENT MANAGEMENT
import [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
float f= cel * 9/5 +32;
Input:
Output:
Input:
NIYAZ UL HASAN 16
IHUB TALENT MANAGEMENT
Output:
import [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
if(age>=18)
[Link](“You are eligible to vote”);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
if(a>b)
[Link](a+“ is greatest”);
else
[Link](b+“ is greatest”);
}
}
Input:
NIYAZ UL HASAN 20
IHUB TALENT MANAGEMENT
Output:
20 is greatest
Input:
Output:
35 is greatest
import [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
if(n==0)
{
[Link](“It is not a +ve or -ve number”);
[Link](0);
}
//logic
if(n>0)
[Link](“It is positive number ”);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
[Link](“Enter the Number :”);
int n=[Link]();
//logic
if(n%2==0)
[Link](“It is even number ”);
else
[Link](“It is odd number ”);
}
}
Input:
NIYAZ UL HASAN 22
IHUB TALENT MANAGEMENT
Output:
It is even number
Input:
Output:
It is odd number
import [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
if(n%2==1 || n%2!=0)
[Link](“It is odd number ”);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
if(year%4==0)
[Link](“It is a Leap Year ”);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
if(ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’|| ch==’u’)
[Link](“It is a Vowel ”);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
if(ch>=’A’ && ch<=’Z’)
[Link](“It is upper case letter ”);
else if(ch>=’A’ && ch<=’Z’)
[Link](“It is lower case letter ”);
else if(ch>=’0’ && ch<=’9’)
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
for(int i=1;i<=n;i++)
{
[Link](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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
int fact=1;
//asking inputs
Scanner sc=new Scanner([Link]);
//logic
for(int i=n;i>=1;i--)
{
NIYAZ UL HASAN 30
IHUB TALENT MANAGEMENT
fact=fact*I;
}
Input:
Output:
Input:
Output:
import [Link].*;
class Test
{
public static void main(String[] args)
{
int rem,sum=0;
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
int rem,rev=0;
//asking inputs
Scanner sc=new Scanner([Link]);
//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 [Link].*;
class Test
{
public static void main(String[] args)
{
int rem,rev=0,temp;
//asking inputs
Scanner sc=new Scanner([Link]);
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)
[Link](“It is palindrome number);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
int rem,sum=0,temp;
//asking inputs
Scanner sc=new Scanner([Link]);
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)
[Link](“It is Armstrong number);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
int a=0,b=1,c;
//asking inputs
Scanner sc=new Scanner([Link]);
NIYAZ UL HASAN 35
IHUB TALENT MANAGEMENT
int n=[Link]();
[Link](a+” “+b);
//logic
for(int i=1;i<=n;i++)
{
c=a+b;
[Link](“ “+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 [Link].*;
class Test
{
public static void main(String[] args)
{
int flag=0;
//asking inputs
Scanner sc=new Scanner([Link]);
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)
[Link](“It is a prime number ”);
else
[Link](“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 [Link].*;
class Test
{
public static void main(String[] args)
{
int sum=0;
//asking inputs
Scanner sc=new Scanner([Link]);
NIYAZ UL HASAN 37
IHUB TALENT MANAGEMENT
//logic
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
{
sum=sum+i;
// [Link](sum);
}
}
if(sum==n)
[Link](“It is a perfect number ”);
else
[Link](“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 [Link].*;
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)[Link](2,i++);
}
[Link](“Binary to Decimal value is =”+sum);
}
}
Input:
Output:
Input:
Output:
import [Link].*;
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([Link]);
//logic
while(n>0)
{
rem=n%10;
n=n/10;
sum=rem+sum;
}
[Link](“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 [Link] 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<[Link] 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 [Link].*;
class Test
{
public static void main(String[] args)
{
//asking inputs
Scanner sc=new Scanner([Link]);
//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)
[Link]("It is Evil Number ");
else
[Link]("It is not Evil Number");
}//end if
else
{
[Link](“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 [Link];
class Test
{
public static void main(String[] args)
{
NIYAZ UL HASAN 42
IHUB TALENT MANAGEMENT
//logic
for(int i=1;i<=10;i++)
{
[Link](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 [Link].*;
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;
}
[Link]("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 [Link].*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Number :");
int n=[Link]();
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 [Link].*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
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)
[Link](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 [Link].*;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
while(y!=0)
{
int carry=x&y;
x=x^y;
y=carry<<1;
}
[Link]("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 [Link];
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++)
{
[Link](i+" ");
}
[Link](" ");
}
}
}
NIYAZ UL HASAN 49
IHUB TALENT MANAGEMENT
1234
1234
1234
1234
import [Link];
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++)
{
[Link](j+" ");
}
[Link](" ");
}
}
}
NIYAZ UL HASAN 50
IHUB TALENT MANAGEMENT
* ***
* ***
* ***
* ***
import [Link];
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++)
{
[Link]("*"+" ");
}
[Link](" ");
}
}
}
NIYAZ UL HASAN 51
IHUB TALENT MANAGEMENT
4444
3333
2222
1111
import [Link];
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++)
{
[Link](i+" ");
}
[Link](" ");
}
}
}
NIYAZ UL HASAN 52
IHUB TALENT MANAGEMENT
AAAA
BBBB
CCCC
DDDD
import [Link];
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++)
{
[Link](i+" ");
}
[Link](" ");
}
}
}
NIYAZ UL HASAN 53
IHUB TALENT MANAGEMENT
* * * *
* *
* *
* * * *
import [Link];
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)
[Link]("*"+" ");
else
[Link]("-");
}
[Link](" ");
}
}
}
NIYAZ UL HASAN 54
IHUB TALENT MANAGEMENT
* - - -
- * - -
- - * -
- - - *
import [Link];
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)
[Link]("*"+" ");
else
[Link]("-");
}
[Link](" ");
NIYAZ UL HASAN 55
IHUB TALENT MANAGEMENT
}
}
* - - - *
- * - * -
- - * - -
- * - * -
* - - - *
import [Link];
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)
[Link]("*"+" ");
else
[Link]("-");
NIYAZ UL HASAN 56
IHUB TALENT MANAGEMENT
}
[Link](" ");
}
}
}
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;
}
[Link](" ");
}
}
}
Q)
1
3 5
7 9 11
13 15 17 19
NIYAZ UL HASAN 63
IHUB TALENT MANAGEMENT
[Link](k+" ");
k=k+2;
}
[Link](" ");
}
}
}
Q)
1
22
333
4444
NIYAZ UL HASAN 64
IHUB TALENT MANAGEMENT
for(j=1;j<=i;j++)
{
[Link](i+" ");
}
[Link](" ");
}
}
}
Q)
*
**
***
****
int i,j;
//rows
for(i=1;i<=4;i++)
{
//space
for(j=4;j>i;j--)
{
[Link](" ");
NIYAZ UL HASAN 65
IHUB TALENT MANAGEMENT
Q)
4444
333
22
1
NIYAZ UL HASAN 66
IHUB TALENT MANAGEMENT
[Link](" ");
}
Q)
1
22
333
4444
333
22
1
}
for(j=1;j<=i;j++)
NIYAZ UL HASAN 67
IHUB TALENT MANAGEMENT
{
[Link](i+" ");
}
[Link](" ");
}
for(i=3;i>=1;i--)
{
for(j=4;j>i;j--)
{
[Link](" ");
}
for(j=1;j<=i;j++)
{
[Link](i+" ");
}
[Link](" ");
}
}
}
Q)
1
222
33333
4444444
for(j=4;j>i;j--)
{
[Link](" ");
NIYAZ UL HASAN 68
IHUB TALENT MANAGEMENT
for(j=1;j<=i;j++)
{
[Link](i+"");
}
[Link](" ");
}
}
}
Q)
1
121
12321
1234321
for(j=4;j>i;j--)
{
[Link](" ");
NIYAZ UL HASAN 69
IHUB TALENT MANAGEMENT
[Link](" ");
}
}
}
Q)
1234321
12321
121
1
for(j=4;j>i;j--)
{
NIYAZ UL HASAN 70
IHUB TALENT MANAGEMENT
[Link](" ");
[Link](" ");
}
}
}
Q)
A
ABA
ABCBA
ABCDCBA
NIYAZ UL HASAN 71
IHUB TALENT MANAGEMENT
for(j='D';j>i;j--)
{
[Link](" ");
[Link](" ");
}
}
}
import [Link].*;
public class Test
{
public static void main(String args[])
{
int counter = 2;
NIYAZ UL HASAN 72
IHUB TALENT MANAGEMENT
while(!isPrimeNumber(counter))
{
counter++;
}
[Link](counter+" ");
counter++;
}
[Link]();
}
}
NIYAZ UL HASAN 73
IHUB TALENT MANAGEMENT
Arrays
import [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
int size=[Link]();
NIYAZ UL HASAN 74
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
//displaying elements
for(int i=0;i<size;i++)
{
[Link](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 [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
int size=[Link]();
NIYAZ UL HASAN 75
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
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 [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
int size=[Link]();
NIYAZ UL HASAN 76
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
[Link]("Reverse of a given number is :");
//displaying elements
for(int i=size-1;i>=0;i--)
{
[Link](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 [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
int size=[Link]();
NIYAZ UL HASAN 77
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
int small=arr[0];
//logic
for(int i=0;i<size;i++)
{
if(arr[i]<small)
{
small=arr[i];
}
}
[Link]("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 [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
int size=[Link]();
NIYAZ UL HASAN 78
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
int big=arr[0];
//logic
for(int i=0;i<size;i++)
{
if(arr[i]<big)
{
small=arr[i];
}
}
[Link]("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 [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
NIYAZ UL HASAN 79
IHUB TALENT MANAGEMENT
int size=[Link]();
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
//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++)
{
[Link](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 [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
NIYAZ UL HASAN 80
IHUB TALENT MANAGEMENT
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
//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++)
{
[Link](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 [Link].*;
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++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
int even=0,odd=0;
//Logic
for(int i=0;i<size;i++)
{
if(arr[i]%2==0)
even++;
else
odd++;
}
[Link]("No of Even element is ="+even);
[Link]("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 [Link].*;
public class Test
{
public static void main(String args[])
NIYAZ UL HASAN 82
IHUB TALENT MANAGEMENT
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
int size=[Link]();
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
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];
}
[Link]("Sum of Even element is ="+even);
[Link]("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 [Link].*;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the Array Size: ");
int size=[Link]();
//inserting elements
for(int i=0;i<size;i++)
{
[Link]("Enter the element of arr["+i+"] :");
arr[i]=[Link]();
}
//Asking element
[Link]("Enter the element : ");
int ele=[Link]();
int cnt=0;
//Logic
for(int i=0;i<size;i++)
{
if(arr[i]==ele)
{
cnt++;
}
}
[Link]("No of occurance of a given element is ="+cnt);
}
}
import [Link];
public class Test
NIYAZ UL HASAN 84
IHUB TALENT MANAGEMENT
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
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 [Link];
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
[Link](arr);
import [Link];
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
[Link](arr);
NIYAZ UL HASAN 87
IHUB TALENT MANAGEMENT
import [Link];
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
[Link](arr);
import [Link];
public class Test
{
public static void main(String[] args)
{
int[] arr={6,1,2,8,9,4,5};
[Link](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 [Link];
import [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
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=[Link]+1;
NIYAZ UL HASAN 91
IHUB TALENT MANAGEMENT
String Programs
NIYAZ UL HASAN 92
IHUB TALENT MANAGEMENT
import [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
//asking inputs
[Link]("Enter the String :");
String str=[Link]();
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 [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
//asking inputs
[Link]("Enter the First String :");
String str1=[Link]();
String concat_str=[Link](str2);
[Link]("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 [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
//asking inputs
[Link]("Enter the First String :");
String str1=[Link]();
//asking inputs
[Link]("Enter the Second String :");
String str2=[Link]();
boolean compare_str=[Link](str2);
if(compare_str)
[Link]("Both are equal");
else
[Link]("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 [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
//asking inputs
[Link]("Enter the String :");
String str=[Link]();
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 [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
//asking inputs
[Link]("Enter the String :");
String str=[Link]();
NIYAZ UL HASAN 97
IHUB TALENT MANAGEMENT
[Link](sarr[i]+" ");
}
}
}
Example
Input:
Enter the String:
This Is Java Class
Output:
sihT sI avaJ ssalC
import [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
//asking inputs
[Link]("Enter the String :");
String str=[Link]();
NIYAZ UL HASAN 98
IHUB TALENT MANAGEMENT
for(int i=[Link]-1;i>=0;i--)
{
[Link](carr[i]);
}
//space after each word
[Link](" ");
}
}
}
import [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
Input:
Output:
Input:
NIYAZ UL HASAN 99
IHUB TALENT MANAGEMENT
Output:
import [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
[Link]().distinct().forEach(c->[Link]((char)c));
[Link](sb);
}
}
Input:
Output:
Gogle
Input:
Hello
Output:
Helo
import [Link];
public class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
String characters="";
String duplicates="";
}
characters+=current;
}
[Link](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 [Link];
public class Test
{
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner([Link]);
//asking inputes
[Link]("Please enter original String");
String input = [Link]();
if (checkRotatation(input, rotation))
{
[Link](input + " and " + rotation + " are rotation of each
other");
}
else
{
[Link]("Sorry, they are not rotation of another");
}
[Link]();
}
public static boolean checkRotatation(String original, String rotation)
{
if ([Link]() != [Link]())
{
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 [Link].*;
class Test
{
public static void main(String[] args)
{
findDuplicatesWords("I am am Learning java java");
}
for(String tempString: s)
{
if([Link](tempString)!=null)
{
[Link](tempString,[Link](tempString)+1);
}
else
{
[Link](tempString,1);
}
}
[Link](lhm);
}
}
Example
Input:
java
Output:
J=1, a=2, v=1
import [Link].*;
class Test
{
public static void main(String[] args)
{
findDuplicatesCharacters("java");
}
for(int i=0;i<[Link]();i++)
{
char c=[Link](i);
if([Link](c)!=null)
{
[Link](c,[Link](c)+1);
}
else
{
[Link](c,1);
}
}
[Link](lhm);
}
}
Example:
Input:
([{}])
Output:
Balanced
import [Link].*;
public class Test
{
public static void main(String[] args)
{
String expr = "([{}])";
// Function call
if (areBracketsBalanced(expr))
[Link]("Balanced ");
else
[Link]("Not Balanced ");
}
// If stack is empty
if ([Link]())
{
return false;
}
char check;
switch (x)
{
case ')':
check = [Link]();
if (check == '{' || check == '[')
return false;
break;
case '}':
check = [Link]();
if (check == '(' || check == '[')
return false;
break;
case ']':
check = [Link]();
Example:
Input:
([{}}])
Output:
Not Balanced