0% found this document useful (0 votes)
2 views187 pages

Java Coding Book

The document is a coding book focused on Java programming, authored by Srinivas Garapati, which includes various topics such as operators, variables, loops, and object-oriented programming. It provides detailed explanations, example programs, and exercises to help learners understand Java concepts. The book also includes a structured table of contents and contact information for further inquiries.

Uploaded by

f20231133
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views187 pages

Java Coding Book

The document is a coding book focused on Java programming, authored by Srinivas Garapati, which includes various topics such as operators, variables, loops, and object-oriented programming. It provides detailed explanations, example programs, and exercises to help learners understand Java concepts. The book also includes a structured table of contents and contact information for further inquiries.

Uploaded by

f20231133
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding Book

{Java}

SRINIVAS GARAPATI
Address : Near Satyam theatre, Opposite HDFC Bank, 3rd Floor, Ameerpet, Hyderabad-500016
Contact : 9119556789 , 7306021113
[Link]

Contents
S No Topic Page Num
01 Understanding Operators Briefly 02
02 Program to understand variables behavior 09
03 Formatting Output 11
04 Programs on arithmetic operators 17
05 Scanner class – Reading input 20
06 If , If-else, If-else-if, Nested if 23
07 Loops Introduction 36
08 For Loop Programs 37
09 While Loop Programs 44
10 Break & Continue 60
11 Switch Case 61
12 Do While Loop 63
13 Nested For Loop 64
14 Range Based Programs 65
15 Pattern Programs 68
16 Object oriented programming 97
17 Static and Instance methods 99
18 Recursion 111
19 Menu Driven Programs 113
20 Setters and Getters 117
21 Object Initialization 123
22 Arryas 126
23 Two dimensional arrays 156
24 Strings 165

Contact : 911 955 6789 1


[Link]

1. Complete the following variable definitions?


Account Patient
___________ accnum = __________________ ___________ name = __________________
___________ name = __________________ ___________ ward = __________________
___________ age = __________________ ___________ age = __________________
___________ balance = __________________ ___________ disease = __________________
___________ pin = __________________ ___________ vaccinated = __________________
___________ married = __________________ ___________ married = __________________
___________ gender = __________________ ___________ hospital = __________________
___________ IFSC = __________________ ___________ billDue = __________________
___________ mail = __________________ ___________ doctor = __________________ A
___________ address = __________________ ___________ address = __________________ M
E
E
Text Book Mobile
R
___________ mobile = __________________ P
___________ name = __________________ ___________ model = __________________ E
___________ price = __________________ ___________ color = __________________ T
___________ pages = __________________ ___________ price = __________________ T
___________ author = __________________ ___________ ram = __________________ E
___________ ISBN = __________________ ___________ earphones = __________________ C
___________ publisher = __________________ ___________ screen = __________________ H
___________ dimension = __________________ N
___________ storage = __________________
O
L
O
Arithmetic Operators G
int a=10; a int a=5; a I
a=20; a=a+1; E
S
a=30; a=a+1;
a=40; a=a+1;
a=50; a=a+1;
print(a); print(a);
int a=5; a int a=15; a
a=a+1; a=a+5;
a=a+2; a=a+4;
a=a+3; a=a+3;
a=a+4; a=a+4;
print(a); print(a);

Contact : 911 955 6789 2


[Link]

int a=5, x=1; a int a=5, b=5; a


a=a+x; a=a+b;
x = x+1; b = b-1;
a=a+x; a=a+b;
x = x+1; x b= b-1; b
a=a+x; a=a+b;
x = x+1; b = b-1;
a=a+x; a=a+b;
print(a, x); print(a, b);

int n=2; n s int n=2; n c


int s=n*n; int c=n*n*n;
print(s); print(c);

int n=2; s c int bal=5000; bal amt


int s=n*n; int amt=3500;
int c=n*n*n; bal = bal + amt;
print(s+c); print(bal);

int a=5, b=3; a b c int a=5, b=3; a b


int c=a+b; a=b;
print(c); b=a;
print(a,b);

int a=5, b=3, c; a b c int a=2, b=3; a b


c=a; a=a+b;
a=b; b=a+a;
b=c; print(a,b);
print(a,b);

int a=2, b=3; a b int a=2, b=3; a b


a=a+b; a=a*b;
b=a-b; b=a/b;
a=a-b; a=a/b;
print(a,b); print(a,b);

Contact : 911 955 6789 3


[Link]

int n=234; n d int n=234; n d


int d=n%10; int d=n/10;
print(d); print(d);

int sum=0, i=1; i sum int fact=1, i=1; i fact


sum=sum+i; fact=fact*i;
i=i+1; i=i+1;
sum=sum+i; fact=fact*i;
i=i+1; i=i+1;
sum=sum+i; fact=fact*i;
i=i+1; i=i+1;
sum=sum+i; fact=fact*i;
print(sum); print(fact);

int n=2345, sum=0; int n=2345, rev=0;


sum=sum+n%10; rev=rev*10+n%10;
n=n/10; n=n/10;

sum=sum+n%10; rev=rev*10+n%10;
n=n/10; n=n/10;

sum=sum+n%10; rev=rev*10+n%10;
n=n/10; n=n/10;

sum=sum+n%10; rev=rev*10+n%10;
n=n/10; n=n/10;

print(sum, n); Print(rev, n);

Contact : 911 955 6789 4


[Link]

Writing Conditions using Arithmetic & Relational Operations


Condition to check the number is Positive or Negative:
Variables Condition Testing
Valid case Invalid case
n=13 n=-5
int n = ? n >= 0 13>=0 -5>=0
true false

Condition to check the number is equal to zero

Condition to check the 2 numbers equal A


M
E
E
Condition to check First Num greater than Second Num R
P
E
T
Condition to check Square of the First Number equals to Second Number
T
E
C
Condition to check sum of 2 numbers equal to 10 H
N
O
L
Condition to check multiplication of first 2 numbers equal to 3rd O
G
I
E
Condition to check the number divisible by 7 S

Condition to check the number is even

Condition to check the last digit of given Number is 0

Condition to check the last digit of Number divisible by 3

Contact : 911 955 6789 5


[Link]

Condition to check the multiplication of first 2 numbers equal to square of 3rd number

Condition to check the last digits of both numbers equal A


M
E
E
R
Condition to check the sum of last digits of given two numbers is equals to 10 P
E
T

T
Condition to check average of 4 subjects marks greater than 60 E
C
H
N
Condition to check the sum of First 2 numbers equals to last digit of 3rd num
O
L
O
G
Condition to check average of 3 numbers equals to first number I
E
S

Condition to check the given quantity of fruits exactly in dozens

Condition to check the given quantity of fruits exactly in dozens

Condition to check the square of last digit is greater than 10

Condition to check square of last digit of given number divisible by 3

Contact : 911 955 6789 6


[Link]

Writing Conditions using Arithmetic, Relational and Logical Operators


1. Condition to check the number is Positive or Negative:

2. Condition to check the number is equal to zero or not:

3. Condition to check the 2 numbers equal or not:

4. Condition to check First Num greater than Second Num or Not: A


M
E
5. Square of First Number equals to Second Number or Not :
E
R
P
6. Condition to check sum of 2 numbers equal to 10 or not:
E
T

7. Condition to check the number divisible by 3 or not: T


E
C
8. Condition to check the number is Even or not: H
N
O
L
9. Condition to check the last digit of Number is 0 or not:
O
G
I
10. Condition to check the last digit of Number is Even or not: E
S

11. Condition to check the multiplication of 2 numbers not equals to 3 rd number or not:

12. Condition to check average of 4 subjects marks greater than 60 or not:

13. Condition to check the sum of First 2 numbers equals to last digit of 3 rd num or not:

14. Condition to check average of 3 numbers equals to first number or not:

Contact : 911 955 6789 7


[Link]

15. Condition to check the given quantity of fruits exactly in dozens or not:

16. Condition to person is eligible for Vote or Not:

17. Condition to check First Num is greater than both Second & Third Nums or Not:

18. Condition to check the number divisible by both 3 and 5 or not:

19. Condition to check the number is in between 30 and 50 or not:

A
20. Condition to check the number is 3 digits number or not: M
E
E
21. Condition to check the student passed in all 5 subjects or Not: R
P
E
22. Condition to check the character is Vowel or Not: T

T
E
23. Condition to check the character is Upper case alphabet or not: C
H
N
24. Condition to check the character is digit or not: O
L
O
25. Condition to check the character is Alphabet or not: G
I
E
26. Condition to check the character is Symbol or not: S

27. Condition to check the 3 numbers equal or not:

28. Condition to check any 2 numbers equal or not among the 3 numbers:

29. Condition to check the 3 numbers are unique or not:

Contact : 911 955 6789 8


[Link]

Basic Programs to Understand Variables Behavior

Display Message on Console:


public class Code
{
public static void main(String[] args)
{
[Link]("Welcome to Java");
}
}

Variables must be without quotes while using them in programming:


public class Code
{
public static void main(String[] args)
{
int a=10;
[Link]("a");
[Link]('a');
[Link](a);
}
}

We need to specify the datatype only once in variable creation(not everytime in use):
public class Code
{
public static void main(String[] args)
{
int a=10;
int a=20;
[Link](a);
}
}

Local variables cannot be accessed without initialization(assigning any value):


public class Code
{
public static void main(String[] args)
{
int a;
[Link](a);
}
}

Contact : 911 955 6789 9


[Link]

Variable value can be modified with any operation:


public class Code
{
public static void main(String[] args) {
int a=10;
a=20;
a=30;
[Link](a);
}
}

public class Code


{
public static void main(String[] args) {
int a=5;
a=a+2;
a=a+3;
a=a+4;
[Link](a);
}
}

Printing Quotient and Remainder:


public class Code
{
public static void main(String[] args) {
int a=5, b=2;
[Link](a);
[Link](b);
}
}

Declaring different types of variables:


public class Code
{
public static void main(String[] args) {
String name = "Amar";
int age = 21;
[Link](name);
[Link](age);
}
}

Contact : 911 955 6789 10


[Link]

Formatting Output
• An output without format doesn’t make sense to the end user.
• We always display results in string format.
• We need to create formatted string with output values as follows

Syntax Example
int + int = int 10 + 20 => 30
String + String = String "Java" + "Book" => JavaBook
"10" + "20" => 1020
String + int = String "Book" + 5 => Book5
"123" + 456 => 123456
String + int + int = String "Sum = " + 10 + 20 => Sum = 1020
String + (int + int) = String "Sum = " + (10 + 20) => Sum = 30
String + double = String "Value = " + 23.45 => Value = 23.45
String – int = Error

public class Main


{
public static void main(String[] args) {
int a=10;
[Link](a);
}
}
Output: 10

public class Main


{
public static void main(String[] args) {
int a=10;
[Link]("a");
}
}
Output: a

public class Main


{
public static void main(String[] args) {
int a=10;
[Link]("A value = " + a);
}
}
Output: A value = 10

Contact : 911 955 6789 11


[Link]

public class Main


{
public static void main(String[] args)
{
int a=10, b=20;
[Link](a);
[Link](b);
}
}
Output: 10
20

public class Main


{
public static void main(String[] args) {
int a=10, b=20;
[Link]("a val : " + a);
[Link]("b val : " + b);
}
}
Output: a val : 10
b val : 20

public class Main


{
public static void main(String[] args)
{
int a=10, b=20;
[Link](a + " , " + b);
}
}
Output : 10, 20

public class Main


{
public static void main(String[] args)
{
int a=10, b=20;
[Link](a + " \t " + b);
}
}
Output: 10 20

Contact : 911 955 6789 12


[Link]

public class Main


{
public static void main(String[] args) {
int a=10, b=20;
[Link](a + " \n " + b);
}
}
Output: 10
20

public class Main


{
public static void main(String[] args)
{
int a=10, b=20;
[Link]("a = " + a + ", b = " + b);
}
}
Output: a=10, b=20

public class Main


{
public static void main(String[] args)
{
int a=10, b=20;
[Link]("a = " + a + "\n" + "b = " + b);
}
}
Output: a=10
b=20

public class Main


{
public static void main(String[] args)
{
int num=101;
String name="Amar";
double salary=35000;
[Link](num + " , " + name + " , " + salary);
}
}
Output: 101, Amar, 35000.0

Contact : 911 955 6789 13


[Link]

public class Main


{
public static void main(String[] args) {
int num=101;
String name="Amar";
double salary=35000;
[Link]("Details : " + num + " , " + name + " , " + salary);
}
}
Output: Details : 101, Amar, 35000

public class Main


{
public static void main(String[] args) {
int a=10, b=20;
int c=a+b;
[Link]("Sum = " + c);
}
}
Output: Sum = 30

public class Main


{
public static void main(String[] args)
{
int a=10, b=20;
int c=a+b;
[Link]("Sum of " + a + " and " + b + " is " + c);
}
}
Output: Sum of 10 and 20 is 30

public class Main


{
public static void main(String[] args)
{
int a=10, b=20;
int c=a+b;
[Link](a + " + " + b + " = " + c);
}
}
Output: 10 + 20 = 30

Contact : 911 955 6789 14


[Link]

public class Main


{
public static void main(String[] args) {
int a=10, b=20;
[Link](a + " + " + b + " = " + (a+b));
}
}
Output: 10 + 20 = 30

public class Main


{
public static void main(String[] args)
{
int a=5, b=2;
[Link](a + " + " + b + " = " + (a+b));
[Link](a + " - " + b + " = " + (a-b));
[Link](a + " * " + b + " = " + (a*b));
[Link](a + " / " + b + " = " + (a/b));
[Link](a + " % " + b + " = " + (a%b));
}
}
Output: 5+2=7
5–2=3
5 * 2 = 10
5/2=2
5%2=1

public class Main


{
public static void main(String[] args) {
[Link]("This is 'Java' Class");
}
}
Output: This is 'Java' class

public class Main


{
public static void main(String[] args) {
[Link]("This is \"Java\" Class");
}
}
Output: This is "Java" class

Contact : 911 955 6789 15


[Link]

public class Main


{
public static void main(String[] args) {
String course = "Java";
[Link]("I am Learning '" + course +"'");
}
}
Output: I am learning 'Java'

public class Main


{
public static void main(String[] args)
{
String course = "Java";
[Link]("I am Learning \"" + course +"\"");
}
}
Output: I am learning "Java"

public class Main


{
public static void main(String[] args)
{
int id=101;
String name="Amar";
double salary=23000;
[Link]("Details = (" + id + " , " + name + " , " + salary + ")");
}
}
Output: Details = (101, Amar, 23000)

public class Main


{
public static void main(String[] args)
{
int id=101;
String name="Amar";
double salary=23000;
[Link]("Details = (" + id + " , '" + name + "' , " + salary + ")");
}
}
Output: Details = (101, 'Amar', 23000)

Contact : 911 955 6789 16


[Link]

Programs on Arithmetic Operators

Adding two numbers:


public class Addition
{
public static void main(String[] args)
{
int a = 5;
int b = 10;
int sum = a + b;
[Link]("Sum: " + sum);
}
}

Perform all arithmetic operations:


public class ArithmeticOperations
{
public static void main(String[] args)
{
int a = 10, b = 5;
int sum = a + b;
[Link]("Sum: " + sum);
int difference = a - b;
[Link]("Difference: " + difference);
int product = a * b;
[Link]("Product: " + product);
int quotient = a / b;
[Link]("Quotient: " + quotient);
int remainder = a % b;
[Link]("Remainder: " + remainder);
}
}

Display Last digit of given number:


public class LastDigit
{
public static void main(String[] args)
{
int n = 12345;
int last = n % 10;
[Link]("Last Digit: " + last);
}
}

Contact : 911 955 6789 17


[Link]

Remove Last digit of given number:


public class RemoveLastDigit
{
public static void main(String[] args) {
int n = 12345;
int removed = n / 10;
[Link]("N with last digit removed: " + removed);
}
}

Find Total Salary for given basic salary:


public class SalaryCalculation
{
public static void main(String[] args) {
double basic = 50000.0;
double hra = basic * 0.3;
double ta = basic * 0.25;
double da = basic * 0.2;
double total = basic + hra + ta + da;
[Link]("Basic Salary: " + basic);
[Link]("HRA: " + hra);
[Link]("TA: " + ta);
[Link]("DA: " + da);
[Link]("Total Salary: " + total);
}
}

Swapping 2 numbers in java:


public class Swap
{
public static void main(String[] args) {
int a = 10, b= 20;
[Link]("Before swapping:");
[Link]("a = " + a);
[Link]("b = " + b);
int temp = a;
a = b;
b = temp;
[Link]("After swapping:");
[Link]("a = " + a);
[Link]("b = " + b);
}
}

Contact : 911 955 6789 18


[Link]

Swapping without third variable:


public class Swap
{
public static void main(String[] args)
{
int a = 10, b = 20;
[Link]("Before swapping:");
[Link]("a = " + a);
[Link]("b = " + b);
a = a + b;
b = a - b;
a = a - b;
[Link]("After swapping:");
[Link]("a = " + a);
[Link]("b = " + b);
}
}

Calculate the total amount for the given quantity of fruits:


public class TotalAmount
{
public static void main(String[] args)
{
int quantity = 50;
double pricePerDozen = 46.5;
double totalAmount = (quantity/12) * pricePerDozen;
[Link]("Total Amount: " + totalAmount);
}
}

Find the sum of first N Numbers:


public class SumOfFirstNNumbers
{
public static void main(String[] args)
{
int n = 5;
int sum = (n * (n + 1)) / 2;
[Link]("Sum : " + sum);
}
}

Contact : 911 955 6789 19


[Link]

Scanner Class – Reading Input from User

Scanner Class:
• Using java library class Scanner, we can read input like integers, characters, strings,
double values from the user.
• Different methods to read different values such as nextInt(), next(), nextDouble()…
• We need to specify the Keyboard([Link]) while creating Scanner class object.
Scanner scan = new Scanner([Link]);
• We access all the methods using object reference name.

Reading integer value: nextInt() method read and returns an integer value
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter integer : ");
int n = [Link]();
[Link]("Input value is : " + n);
}
}
Output: Enter integer : 10
Input value is : 10

Reading Boolean value: nextBoolean() method returns Boolean value that we entered
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter boolean value : ");
boolean b = [Link]();
[Link]("Input value is : " + b);
}
}

Output: Enter Boolean value : true


Input value is : true

Contact : 911 955 6789 20


[Link]

Read 2 Nums & Perform Arithmetic Operations:


import [Link];
public class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter two integers : ");
int a = [Link]();
int b = [Link]();
[Link](a + " + " + b + " = " + (a+b));
[Link](a + " - " + b + " = " + (a-b));
[Link](a + " * " + b + " = " + (a*b));
[Link](a + " / " + b + " = " + (a/b));
[Link](a + " % " + b + " = " + (a%b));
}
}

Reading String: using next() method we can read single word string from the user
import [Link];
public class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter your name : ");
String name = [Link]();
[Link]("Hello : " + name);
}
}
Output: Enter your name: Amar
Hello : Amar

Reading character:
import [Link];
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter character : ");
char ch = [Link]().charAt(0);
[Link]("Input character is : " + ch);
}
}
Output: Enter character : A
Input character is : A

Contact : 911 955 6789 21


[Link]

Read Employee details and print:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Emp details : ");
int id = [Link]();
String name = [Link]();
double salary = [Link]();
[Link]("Details : " + id + ", " + name + ", " + salary);
}
}
Output : Enter Emp details :
101
Amar
53000
Details : 101, Amar, 53000.0

Print Last Digit of given Number:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter a number : ");
int n = [Link]();
int d = n%10;
[Link]("Last digit of " + n + " is " + d);
}
}
Output:
Enter a number :
234
Last digit of 234 is 4

Contact : 911 955 6789 22


[Link]

If Block Programs

If Block: Execute a block only if the specified condition is valid.


Syntax Flow Chart

if(Condition)
{
If-block-logic;
}

Display bill amount of customer:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter bill : ");
double bill = [Link]();
[Link]("Pay : " + bill);
}
}

Give 15% bill amount to every customer:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter bill : ");
double bill = [Link]();
double discount = 0.15 * bill;
bill = bill-discount;
[Link]("Pay : " + bill);
}
}

Contact : 911 955 6789 23


[Link]

Program to give 15% discount on bill if the bill amount is greater than 5000
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter bill : ");
double bill = [Link]();
if(bill>=5000)
{
double discount = 0.15 * bill;
bill = bill-discount;
}
[Link]("Pay : " + bill);
}
}

Output: Enter bill amount : 6000


Final bill to pay : 5400.0

Program to give 20% bonus on salary if the employee has more than 5 years of
experience:
import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter salary : ");
double salary = [Link]();
[Link]("Enter experience : ");
int exp = [Link]();
if(exp>5){
double bonus = 0.2*salary;
salary = salary + bonus ;
}
[Link]("Salary to pay : " + salary);
}
}
Output: Enter salary : 30000
Enter experience : 7
Salary to pay : 36000

Contact : 911 955 6789 24


[Link]

If Else Block Programs

Else Block: It executes when the given condition of if block fails. Else block is optional for if
block. Else block cannot be defined without IF block.
Syntax Flow Chart

if(condition)
{
If-stats;
}
else
{
Else-stats;
}

Program to check the input number is Positive or Negative:


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter an integer : ");
int n = [Link]();
if(n>=0)
[Link]("Positive number");
else
[Link]("Negative number");
}
}
Output: Enter an integer: 5
Positive Number

Check the input number is Even or Odd:


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter number : ");
int n = [Link]();
if(n%2==0)
[Link]("Even number");

Contact : 911 955 6789 25


[Link]

else
[Link]("Not even number");
}
}
Output: Enter number: 7
Not even number

Program to Check Person can Vote or Not:


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter age : ");
int age = [Link]();
if(age>=18)
[Link]("Eligible for Vote");
else
[Link]("Wait " + (18-age) + " more years to vote");
}
}
Output: Enter age: 13
Wait 5 more years to vote

Program to find the biggest of two numbers:


import [Link];
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter 2 numbers : ");
int a = [Link]();
int b = [Link]();
if(a>b)
[Link]("a is big");
else
[Link]("b is big");
}
}
Output: Enter 2 numbers:
10
20
b is big

Contact : 911 955 6789 26


[Link]

Program to check the character is Vowel or Not


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter character : ");
char ch = [Link]().charAt(0);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
[Link]("Vowel");
else
[Link]("Not vowel");
}
}
Output: Enter character: g
Not vowel

Program to check the character is Alphabet or Not:


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter character : ");
char ch = [Link]().charAt(0);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
[Link]("Alphabet");
else
[Link]("Not an Alphabet");
}
}
Output: Enter character : 3
Not an Alphabet

Program to Check Login details are Correct or Not:


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter user name : ");
String user = [Link]();
[Link]("Enter password : ");
int pwd = [Link]();

Contact : 911 955 6789 27


[Link]

if([Link]("logic") && pwd==1234)


[Link]("Login Success");
else
[Link]("Error : Invalid login");
}
}
Output: Enter user name : logic
Enter password : 1111
Error : Invalid login

Program to Check the Number between 30 and 50 or not:


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter number between 30 and 50 : ");
int num = [Link]();
if(num>=30 && num<=50)
[Link]("Valid number entered");
else
[Link]("Invalid number entered");
}
}
Output: Enter number between 30 and 50 : 54
Invalid number entered

Check the input year is Leap year or Not:


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter year : ");
int n = [Link]();
if((n%400==0) || (n%4==0 && n%100!=0))
[Link]("Leap year");
else
[Link]("Not a leap year");
}
}
Output: Enter year : 1996
Leap year

Contact : 911 955 6789 28


[Link]

If Else If block

If else if: The if-else-if ladder statement executes only block among multiple we defined based
on valid condition.
Syntax Flow Chart
if(condition1){
Statement1;
}
else if(condition2){
Statement2;
}
else if(condition3){
Statement3;
}
else{
Statement4;
}

Check the Number is Positive or Negative or Zero


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter n value : ");
int n = [Link]();
if(n>0)
[Link]("Positive num");
else if(n<0)
[Link]("Negative num");
else
[Link]("Zero");
}
}
Output: Enter n value : -13
Negative num

Check the Character is Alphabet or Digit or Symbol


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);

Contact : 911 955 6789 29


[Link]

[Link]("Enter character : ");


char ch = [Link]().charAt(0);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
[Link]("Alphabet");
else if(ch>='0' && ch<='9')
[Link]("Digit");
else
[Link]("Special Symbol");
}
}
Output: Enter character: 0
Digit

Program to check the input year is Leap or Not:


Leap Year rules:
• 400 multiples are leap years : 400, 800, 1200, 1600….
• 4 multiples are leap years: 4, 8, 12, … 92, 96…
• 100 multiples are not leap years: 100, 200, 300, 500, 600…
import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter year : ");
int n = [Link]();
if((n%400==0))
[Link]("Leap year");
else if(n%4==0 && n%100!=0)
[Link]("Leap Year");
else
[Link]("Not leap year");
}
}
Output: Enter year: 1996
Leap year

Program to find Biggest of Three Numbers:


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter a, b, c values : ");
int a = [Link]();
int b = [Link]();

Contact : 911 955 6789 30


[Link]

int c = [Link]();
if(a>b && a>c)
[Link]("a is big");
else if(b>c)
[Link]("b is big");
else
[Link]("c is big");
}
}
Output: Enter a, b, c values :
10
30
20
b is big

Program to Calculate current bill based on units consumed:


• 0-100 -> 0.80 per unit
• 101-200 -> 1.2 per unit
• 201-300 -> 1.5 per unit
• above 300 -> 1.8 per unit

import [Link];
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of units : ");
int units = [Link]();
double bill=0.0;
if(units>=0 && units<=100)
bill = units*0.8;
else if(units>100 && units<=200)
bill = 80 + (units-100)*1.2;
else if(units>200 && units<=300)
bill = 200 + (units-200)*1.5;
else
bill = 350 + (units-300)*1.8;
[Link]("Total bill amount : " + bill);
}
}
Output: Enter number of units: 150
Total bill amount : 140.0

Contact : 911 955 6789 31


[Link]

Nested If Block
Nested If: Defining if block inside another if block.
Syntax Flow Chart
if(condition){
if(condition){
if-if-Stat;
}
else{
if-else-stat;
}
}
else{
if(condition){
else-if-stat;
}
else{
else-else-
stat;
}
}

Nested If Programs
Check Even Number or Not only if it is Positive
import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter number : ");
int n = [Link]();
if(n>0){
if(n%2==0)
[Link]("Even number");
else
[Link]("Not even number");
}
else{
[Link]("Negative number given");
}
}
}
Output: Enter number: 5
Not even number

Contact : 911 955 6789 32


[Link]

Biggest of two only if the two numbers or not equal


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter a, b values : ");
int a = [Link]();
int b = [Link]();
if(a!=b){
if(a>b)
[Link]("a is big");
else
[Link]("b is big");
}
else{
[Link]("a and b are equal");
}
}
}
Output: Enter a, b values : 10
20
b is big

Program to check the person can donate blood or not


Requirements: Age between 18 to 60 and Weight must be greater than 50
import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter age : ");
int age = [Link]();
[Link]("Enter weight : ");
int weight = [Link]();
if(age>=18 && age<=60){
if(weight>=50)
[Link]("Can donate blood");
else
[Link]("Under weight");
}
else
[Link]("Not suitable age");
}
}

Contact : 911 955 6789 33


[Link]

Output: Enter age : 25


Enter weight : 49
Under weight

Display Student Grade only if the student passed in all Subjects


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter marks of 3 subjects : ");
int m1 = [Link](); A
int m2 = [Link](); M
int m3 = [Link](); E
if(m1>=40 && m2>=40 && m3>=40){ E
int avg = (m1+m2+m3)/3; R
if(avg>=60) P
[Link]("Grade-A"); E
else if(avg>=50) T
[Link]("Grade-B");
T
else E
[Link]("Grade-C"); C
} H
else{ N
[Link]("Student failed"); O
} L
} O
} G
Output: Enter marks of 3 subjects: I
45 E
56 S
67
Grade-B

Check the Triangle is Equilateral or Isosceles or Scalene


• Equilateral: All sides are equal a==b==c
• Isosceles: Any two sides are equal a==b or a==c or b==c
• Scalene: No sides are equal a != b != c
import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter 3 sides of triangle : ");
int a = [Link]();

Contact : 911 955 6789 34


[Link]

int b = [Link]();
int c = [Link]();
if(a==b && b==c)
[Link]("Equilateral");
else if(a==b ||a==c||b==c)
[Link]("Isosceles");
else
[Link]("Scalene");
}
}
Output: Enter 3 sides of triangle :
70
70
40
Isosceles

Display days for the specified month


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter Month number : ");
int n = [Link]();
if(n>=1 && n<=12){
if(n==2)
[Link]("28 days / 29 days");
else if(n==4 || n==6 || n==9 || n==11)
[Link]("30 days");
else
[Link]("31 days");
}
else{
[Link]("Invalid month number");
}
}
}
Output: Enter Month number : 2
28 days / 29 days

Contact : 911 955 6789 35


[Link]

Introduction to Loops

Loop: A Block of instructions execute repeatedly as long the condition is valid.

Note: Block executes only once whereas Loop executes until condition become False

Java Supports 3 types of Loops:


1. For Loop
2. While Loop
3. Do-While Loop

For Loop: We use for loop only when we know the number of repetitions. For example,
• Print 1 to 10 numbers
• Print Array elements
• Print Multiplication table
• Print String character by character in reverse order

While loop: We use while loop when we don’t know the number of repetitions.
• Display contents of File
• Display records of Database table

Do while Loop: Execute a block at least once and repeat based on the condition.
• ATM transaction: When we swipe the ATM card, it starts the first transaction. Once the
first transaction has been completed, it asks the customer to continue with another
transaction and quit.

Contact : 911 955 6789 36


[Link]

For Loop
for loop: Execute a block of instructions repeatedly as long as the condition is valid. We use for
loop only when we know the number of iterations to do.

Syntax Flow Chart

for(init ; condition ; modify)


{
statements;
}

for(start ; stop ; step)


{
statements;
}

Program to Print 1 to 10 Numbers


class Code
{
public static void main(String[] args)
{
for (int i=1 ; i<=10 ; i++)
{
[Link]("i val : " + i);
}
}
}

Program to Print 10 to 1 numbers


class Code
{
public static void main(String[] args)
{
for (int i=10 ; i>=1 ; i--)
{
[Link]("i val : " + i);
}
}
}

Contact : 911 955 6789 37


[Link]

Program to display A-Z alphabets


class Code
{
public static void main(String[] args)
{
for (char ch='A' ; ch<='Z' ; ch++)
{
[Link](ch + " ");
}
}
}

Program to Print 1 to N numbers


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter n val : ");
int n = [Link]();
for (int i=1 ; i<=n ; i++)
{
[Link]("i val : " + i);
}
}
}

Program to display A-Z alphabets with ASCII values


class Code
{
public static void main(String[] args)
{
for (char ch='A' ; ch<='Z' ; ch++)
{
[Link](ch + " : " + (int)ch);
}
}
}

Contact : 911 955 6789 38


[Link]

Program to display ASCII character set


class Code
{
public static void main(String[] args)
{
for (int i=0 ; i<256 ; i++)
[Link](i + " : " + (char)i);
}
}

Find Sum of First N numbers


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter n value : ");
int n = [Link]();
int sum=0;
for (int i=1 ; i<=n ; ++i)
sum = sum+i;
[Link]("Sum of first " + n + " numbers is : " + sum);
}
}
Program to Find Factorial of Number
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter n value : ");
int n = [Link]();
int fact=1;
for (int i=1 ; i<=n ; ++i)
{
fact = fact*i;
}
[Link]("Factorial of " + n + " is : " + fact);
}
}

Contact : 911 955 6789 39


[Link]

Program to display Multiplication table


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter table number : ");
int n = [Link]();
for (int i=1 ; i<=10 ; ++i)
{
[Link](n + " x " + i + " = " + (n*i));
}
}
}

Print Even Numbers from 1 to 10


class Code {
public static void main(String[] args) {
for (int i=1 ; i<=10 ; ++i){
if(i%2==0){
[Link](i);
}
}
}
}

Find Sum of Even Numbers in Java


import [Link];
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter range : ");
int n = [Link]();
int sum=0;
for (int i=1 ; i<=n ; i++){
if(i%2==0)
sum=sum+i;
}
[Link]("Even numbers sum : " + sum);
}
}

Contact : 911 955 6789 40


[Link]

Program to print factors of given number


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter n value : ");
int n = [Link]();
for (int i=1 ; i<=n ; i++)
{
if(n%i==0)
[Link](i + " is a factor");
}
}
}

Program to count factors of given number


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter n value : ");
int n = [Link]();
int count=0;
for (int i=1 ; i<=n ; i++)
{
if(n%i==0)
count++;
}
[Link]("Factors count is : " + count);
}
}

Program to check the input number is Prime or Not


import [Link];
class Code
{
public static void main(String[] args)
{

Contact : 911 955 6789 41


[Link]

Scanner sc = new Scanner([Link]);


[Link]("Enter n value : ");
int n = [Link]();
int count=0;
for (int i=1 ; i<=n ; i++)
{
if(n%i==0)
count++;
}
if(count==2)
[Link]("Prime Number");
else
[Link]("Not a Prime Number");
}
}

Program to find the sum of factors of given number


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter n value : ");
int n = [Link]();
int sum=0;
for (int i=1 ; i<=n ; i++)
{
if(n%i==0)
sum=sum+i;
}
[Link]("Sum of Factors : " + sum);
}
}

Program to check the input number is Perfect or Not


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter n value : ");

Contact : 911 955 6789 42


[Link]

int n = [Link]();
int sum=0;
for (int i=1 ; i<n ; i++)
{
if(n%i==0)
sum=sum+i;
}
if(n==sum)
[Link]("Perfect Number");
else
[Link]("Not a Perfect Number");
}
}

Program to print Fibonacci series


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number of factors in series : ");
int n = [Link]();
int a=0, b=1, c;
for (int i=1 ; i<=n ; i++)
{
[Link](a + " ");
c=a+b;
a=b;
b=c;
}
}
}

Contact : 911 955 6789 43


[Link]

While Loop
While loop: Execute a block of instructions repeatedly until the condition is false. We use while
loop only when don’t know the number of iterations to do.

Syntax Flow Chart

while(condition)
{
statements;
}

Check even or Not until user exits:


import [Link];
public class Check
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
while(true)
{
[Link]("Enter a number : ");
int num = [Link]();

if(num%2==0)
[Link](num + " is even.");
else
[Link](num + " is odd.");

[Link]("Do you want to check another number? (y/n): ");


char ch = [Link]().charAt(0);
if(ch=='n')
break;
}
[Link]("Exiting program.");
}
}

Contact : 911 955 6789 44


[Link]

Print Multiplication table until user exits:


import [Link];
public class Check
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
while(true)
{
[Link]("Enter table number : ");
int n = [Link]();

for(int i=1 ; i<=10 ; i++)


{
[Link](n + " * " + i + " = " + (n*i));
}

[Link]("Do you want print another table ? (y/n): ");


char ch = [Link]().charAt(0);
if(ch=='n')
{
break;
}
}
[Link]("Exiting program.");
}
}

Check the Number is Prime or Not until user exits:


import [Link];
public class Check
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
while(true)
{
[Link]("Enter table number : ");
int n = [Link]();

int count=0;
for(int i=1 ; i<=n ; i++)
{

Contact : 911 955 6789 45


[Link]

if(n%i==0)
{
count++;
}
}
if(count==2)
[Link](n + " is Prime");
else
[Link](n + " is not Prime");

[Link]("Do you want print another table ? (y/n): ");


char ch = [Link]().charAt(0);
if(ch=='n')
break;
}
[Link]("Exiting program.");
}
}

Menu driven program to perform arithmetic operations:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
while(true)
{
[Link]("1. add");
[Link]("2. subtract");
[Link]("3. multiply");
[Link]("4. exit");

[Link]("Enter your choice : ");


int ch = [Link]();
if(ch==1)
{
[Link]("Enter 2 nums : ");
int a = [Link]();
int b = [Link]();
[Link]("Result = " + (a+b));
}
else if(ch==2)

Contact : 911 955 6789 46


[Link]

{
[Link]("Enter 2 nums : ");
int a = [Link]();
int b = [Link]();
[Link]("Result = " + (a-b));
}
else if(ch==3)
{
[Link]("Enter 2 nums : ");
int a = [Link]();
int b = [Link]();
[Link]("Result = " + (a*b));
}
else if(ch==4)
{
[Link]("End of program");
break;
}
else
[Link]("Error : Invalid choice");
}
}
}

Digit Based Programs


Program to Display Last Digit of given number
• When we divide any number with 10 then last digit will be the remainder.
• 1234%10 -> 4
• 1005%10 -> 5
• 200%10 -> 0
import [Link];
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int d = n%10;
[Link]("Last digit is : " + d);
}
}
Output: Enter Num : 1234
Last digit is : 4

Contact : 911 955 6789 47


[Link]

Program to remove the last digit of number:


• When we divide any number with 10 then it gives quotient by removing last digit
• 1234/10 -> 123
• 1000/10 -> 100

import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
n = n/10;
[Link]("After removing last digit : " + n);
}
}

Output: Enter Num : 1234


After removing last digit : 123

Display digits of a number in reverse order:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
while(n!=0)
{
[Link](n%10);
n=n/10;
}
}
}

Output: Enter Num : 234


4
3
2

Contact : 911 955 6789 48


[Link]

Program to Count the digits in given number


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int count=0;
while(n!=0)
{
n=n/10;
count++;
}
[Link]("Digits count : " + count);
}
}
Output: Enter Num : 1234
Digits count : 4

Display sum of the digits in the given number


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int sum=0;
while(n!=0)
{
sum = sum + n%10;
n=n/10;
}
[Link]("Sum of digits : " + sum);
}
}

Output: Enter Num : 1234


Sum of digits : 10

Contact : 911 955 6789 49


[Link]

Program to display the sum of even digits:


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int sum=0, r;
while(n>0){
r = n%10;
if(r%2==0)
sum = sum+r;
n = n/10;
}
[Link]("Sum of Even digits are : " + sum);
}
}
Output: Enter Num : 12345
Sum of Even digits are : 6

Program to count the number of 0 digits in the given number:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int count=0, r;
while(n>0){
r = n%10;
if(r==0)
count++;
n = n/10;
}
[Link]("Zero's count : " + count);
}
}
Output: Enter Num : 10005
Zero’s count : 3

Contact : 911 955 6789 50


[Link]

Program to display Multiplication table to each digit in the given number:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int r;
while(n>0)
{
r = n%10;
for(int i=1 ; i<=10 ; i++){
[Link](r + " * " + i + " = " + (r*i));
}
n = n/10;
}
}
}

Program to display factoral of each digit in the given number:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int r;
while(n>0)
{
r = n%10;
int fact=1;
for(int i=1 ; i<=r ; i++){
fact=fact*i;
}
[Link]("Factorial of " + r + " is : " + fact);
n = n/10;
}
}
}

Contact : 911 955 6789 51


[Link]

Program to display prime number digits in the given number:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
while(n>0)
{
int r = n%10;
int count=0;
for(int i=1 ; i<=r ; i++)
{
if(r%i==0)
count++;
}
if(count==2)
[Link](r + " is prime");
n = n/10;
}
}
}

Display Smallest Digit in the given number:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int small=9;
int r;
while(n>0)
{
r = n%10;
if(r < small)
{
small = r;
}

Contact : 911 955 6789 52


[Link]

n = n/10;
}
[Link]("Smallest digit : " + small);
}
}

Program to display biggest digit in the given number:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int large=0;
int r;
while(n>0)
{
r = n%10;
if(r>large)
{
large = r;
}
n = n/10;
}
[Link]("Large digit : " + large);
}
}

Program to find the sum of Largest and Smallest Digit in the given number:
import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int large=0, small=9;;
int r;
while(n>0)
{

Contact : 911 955 6789 53


[Link]

r = n%10;
if(r>large)
large = r;
if(r<small)
small = r;
n = n/10;
}
[Link]("Sum of large and small : " + (large+small));
}
}

Program to display the first digit of given number:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
while(n>=10)
{
n = n/10;
}
[Link]("First Digit : " + n%10);
}
}
Output: Enter Num : 2345
First Digit : 2

Program to Find the Sum of First and Last digits of given number
import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int first = n%10;
n=n/10;
while(n>=10){
n = n/10;

Contact : 911 955 6789 54


[Link]

}
int last = n%10;
[Link]("Sum of First & Last Digits : " + (first+last));
}
}
Output: Enter Num : 1234
Sum of First & Last Digits : 5

Program to reverse the given number


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int rev=0, r;
while(n>0){
r = n%10;
rev = rev*10 + r;
n = n/10;
}
[Link]("Reverse Number is : " + rev);
}
}
Output: Enter Num: 5342
Reverse Number is : 2435

Palindrome Number: The number become same when we reverse is called Palindrome number
Examples: 121, 1001, 123321
import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int rev=0, r, temp=n;
while(n>0)
{
r = n%10;
rev = rev*10 + r;

Contact : 911 955 6789 55


[Link]

n = n/10;
}
if(temp==rev)
[Link]("Palindrome Number");
else
[Link]("Not Palindrome Number");
}
}
Output: Enter Num : 1221
Palindrome Number

Check the given 3 digit number is Armstrong Number or not :


Sum of cubes of individual digits equals to the same number
Example: 153 -> 1^3 + 5^3 + 3^3 = 153
import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
int n = [Link]();
int temp, sum=0, r;
temp=n;
while(n>0)
{
r = n%10;
sum = sum + r*r*r;
n = n/10;
}
if(temp==sum)
[Link]("ArmStrong Number");
else
[Link]("Not an ArmStrong Number");
}
}
Output: Enter 3 digit num : 145
Not an Armstrong Number

Contact : 911 955 6789 56


[Link]

Program to check the given number is Armstrong number or not:


Armstrong Number: Sum of its own digits raised to power number of digits
Examples : 153 = 1^3 + 5^3 + 3^3 = 153
1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1634
import [Link];
class Code
{
public static void main(String[] args)
{
int n, r, sum=0, temp, c=0, s, i;
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
n = [Link]();
temp=n;
while(n>0)
{
n=n/10;
c++;
}
n=temp;
while(n>0)
{
r = n%10;
s=1;
for(i=1 ; i<=c ; i++)
{
s = s*r;
}
sum = sum + s;
n = n/10;
}
if(temp==sum)
[Link]("ArmStrong Number");
else
[Link]("Not an ArmStrong Number");
}
}
Output: Enter Number : 153
Armstrong Number

Contact : 911 955 6789 57


[Link]

Program to find Sum of Digits till Single Digit:


9657 -> 9+6+5+7 -> 27 -> 2+7 -> 9

import [Link];
class Code
{
public static void main(String[] args)
{
int num, sum, dig;
Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
num = [Link]();
[Link](num + "->");
while(num/10!=0)
{
sum = 0;
while(num!=0)
{
dig=num%10;
sum+=dig;
num/=10;
}
[Link](sum + "->");
num=sum;
}
}
}
Output: Enter Num: 9657
9657 -> 27 -> 9 ->

ADAM Number: Take a number then square it then reverse it then find its square root then
reverse. If the given number equals to the final number then it is called ADAM.
• Take the number (12)
• Square the number (144)
• Reverse the number(441)
• Square root of number (21)
• Reverse the number(12)

import [Link];
class Code
{
public static void main(String[] args)
{

Contact : 911 955 6789 58


[Link]

int num, temp, r1, r2, sq, rev1 = 0, rev2 = 0;


Scanner scan = new Scanner([Link]);
[Link]("Enter Num : ");
num = [Link]();
temp = num * num;
[Link]("Square of the num : " + temp);
while (temp != 0)
{
r1 = temp % 10;
rev1 = rev1 * 10 + r1;
temp = temp / 10;
}
[Link]("Reverse Num : " + rev1);
sq = (int)[Link](rev1);
[Link]("Sqrt num : " + sq);
while (sq != 0)
{
r2 = sq % 10;
rev2 = rev2 * 10 + r2;
sq = sq / 10;
}
[Link]("Reverse Num : " + rev2);
if (rev2 == num)
[Link](num + " is an Adam number");
else
[Link](num + " is not an Adam number");
}
}

Output: Enter Num: 12


12 is an Adam number

Contact : 911 955 6789 59


[Link]

Break and Continue

break: A branching statement that terminates the execution flow of a Loop or Switch case.
class Code
{
public static void main(String[] args) {
for (int i=1 ; i<=10 ; i++){
if(i==5){
break;
}
[Link](i + " ");
}
}
}
Output: 1 2 3 4

Break statement terminates the flow of infinite loop also:


class Code
{
public static void main(String[] args) {
while(true){
[Link]("Loop");
break;
}
}
}
Output: Loop prints infinite times

Continue: A branching statement that terminates the current iteration of loop execution.
class Code
{
public static void main(String[] args) {
for (int i=1 ; i<=10 ; i++){
if(i==5){
continue;
}
[Link](i + " ");
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10

Contact : 911 955 6789 60


[Link]

Switch case
Switch: is a Conditional Statement executes based on given choice (case). Default case executes
if the user entered invalid choice. Case should terminate with break statement.
Syntax FlowChart

switch(choice)
{
case 1 : Statements ;
break
case 2 : Statements ;
break
......

case n : Statements ;
break
default: Statements ;
}

import [Link];
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter character(r, g, b) : ");
char ch = [Link]().charAt(0);
switch(ch)
{
case 'r' : [Link]("Red");
break;
case 'g' : [Link]("Green");
break;
case 'b' : [Link]("Blue");
break;
default : [Link]("Weird");
}
}
}
Output: Enter character(r, g, b): g
Green

Contact : 911 955 6789 61


[Link]

Arithmetic Operations using Menu options:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("[Link]");
[Link]("[Link]");
[Link]("[Link]");

[Link]("Enter your choice : ");


int ch = [Link]();

int a=0, b=0;


if(ch>=1 && ch<=3)
{
[Link]("Enter 2 nums : ");
a = [Link]();
b = [Link]();
}
switch(ch)
{
case 1 : [Link]("Sum : " + (a+b));
break;
case 2 : [Link]("Difference : " + (a-b));
break;
case 3 : [Link]("Product : " + (a*b));
break;
default: [Link]("Invalid choice");
}
}
}
Output : 1. Add
2. Subtract
3. Multiply
Enter your choice : 2
30
10
Difference : 20

Contact : 911 955 6789 62


[Link]

Do-While Loop
do-while: Executes a block at least once and continue iteration until condition is false.

Syntax Flow Chart

do
{
statements;
} while(condition);

Check Even numbers until user exits:


import [Link];
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
char res='n';
do{
[Link]("Enter num : ");
int n = [Link]();
if(n%2==0)
[Link](n + " is even");
else
[Link](n + " is not even");

[Link]("Do you want to check another num (y/n) : ");


res = [Link]().charAt(0);
}while (res == 'y');
}
}
Output: Enter num : 5
5 is not even
Do you want to check another num (y/n) : y
Enter num : 6
6 is even
Do you want to check another num (y/n) : n

Contact : 911 955 6789 63


[Link]

Nested For Loop


Nested for loop: Defining for loop inside another for loop. We used nested loops to process
two-dimensional data such as patterns, matrix type arrays, range based programs, sorting,
remove duplicates in arrays, display duplicate count of each element in array and so on.

Syntax Flow chart

for(init1 ; cond1 ; modify1)


{
for(init2 ; cond2 ; modify2)
{
Statements;
}
}

Number of iterations in Nested loop is equals to “outer loop iterations multiplied by inner
loop iterations”.

class Code
{
public static void main(String[] args)
{
int count=0;
for (int i=1 ; i<=5 ; i++)
{
for (int j=1 ; j<=5 ; j++){
count++;
}
}
[Link]("Number of Iterations : " + count);
}
}
Output : Number of Iterations : 25

Contact : 911 955 6789 64


[Link]

Range Based Programs

Program to display even numbers in the given range


import [Link].*;
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter range : ");
int range = [Link]();
[Link]("Even Numbers in the given range :");
for(int n=1 ; n<=range ; n++){
if(n%2==0)
[Link](n + " ");
}
}
}
Output: Enter range : 10
Even Numbers in the given range : 2 4 6 8 10

Program to print factorial for numbers 2 to 7


class Code
{
public static void main(String[] args) {
for(int n=2 ; n<=7 ; n++){
int fact=1;
for (int i=1 ; i<=n ; i++){
fact=fact*i;
}
[Link]("Factorial of " + n + " is : " + fact);
}
}
}
Output: Factorial of 2 is : 2
Factorial of 3 is : 6
…..

Program to display multiplication tables in the given range


import [Link].*;
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

Contact : 911 955 6789 65


[Link]

[Link]("Enter Lower Limit : ");


int lower = [Link]();
[Link]("Enter Upper Limit : ");
int upper = [Link]();
for(int n=lower ; n<=upper ; n++){
[Link]("Table : " + n);
for (int i=1 ; i<=10 ; i++){
[Link](n + " x " + i + " = " + (n*i));
}
[Link]();
}
}
}

Program to display Prime numbers in the given range


import [Link].*;
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Lower Limit : ");
int lower = [Link]();
[Link]("Enter Upper Limit : ");
int upper = [Link]();
[Link]("Prime Numbers from " + lower + " to " + upper);
for(int n=lower ; n<=upper ; n++){
int count=0;
for (int i=1 ; i<=n ; i++){
if(n%i==0)
count++;
}
if(count==2)
[Link](n);
}
}
}

Program to display Perfect numbers in the given range


import [Link].*;
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

Contact : 911 955 6789 66


[Link]

[Link]("Enter Lower Limit : ");


int lower = [Link]();
[Link]("Enter Upper Limit : ");
int upper = [Link]();
[Link]("Perfect Numbers from " + lower + " to " + upper);
for(int n=lower ; n<=upper ; n++){
int sum=0;
for (int i=1 ; i<n ; i++){
if(n%i==0)
sum=sum+i;
}
if(n==sum)
[Link](n);
}
}
}

Program to display Palindrome numbers in the given range


import [Link].*;
class Code
{
public static void main(String[] args) { A
Scanner sc = new Scanner([Link]); M
[Link]("Enter Lower Limit : "); E
int lower = [Link](); E
[Link]("Enter Upper Limit : "); R
int upper = [Link](); P
E
[Link]("Palindrome Numbers from " + lower + " to " + upper); T
for(int n=lower ; n<=upper ; n++){
T
int temp=n;
E
int rev=0, r=0;
C
while(temp!=0){
H
r=temp%10; N
rev=rev*10+r; O
temp=temp/10; L
} O
if(rev==n) G
[Link](n); I
} E
} S
}

Contact : 911 955 6789 67


[Link]

Pattern Programs
Pattern:
• Representation of data in two-dimensional format (rows and columns)
• We use nested loops to print patterns.
• We can print patterns with numbers, characters, starts, symbols
• Patterns can be in different shapes like triangle, rectangle, half triangle, pyramid and so
on.

Basic Number Patterns


Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
12345
for (int j=1 ; j<=5 ; j++)
12345
12345 {
12345 [Link](j);
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
11111
for (int j=1 ; j<=5 ; j++)
22222
33333 {
44444 [Link](i);
55555 }
[Link]();
}

Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
*****
for (int j=1 ; j<=5 ; j++)
*****
***** {
***** [Link]("*");
***** }
[Link]();
}

Contact : 911 955 6789 68


[Link]

Pattern Logic
for (int i=5 ; i>=1 ; i--)
54321 {
54321
for (int j=5 ; j>=1 ; j--){
54321
54321 [Link](j);
54321 }
[Link]();
}
}
Pattern Logic
for (int i=5 ; i>=1 ; i--)
55555 {
44444
for (int j=5 ; j>=1 ; j--)
33333
22222 {
11111 [Link](i);
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=5 ; i++)
10101 {
10101
for (int j=1 ; j<=5 ; j++)
10101
10101 {
10101 [Link](j%2);
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=5 ; i++)
11111 {
00000
for (int j=1 ; j<=5 ; j++)
11111
00000 {
11111 [Link](i%2);
}
[Link]();
}

Contact : 911 955 6789 69


[Link]

Basic Character Patterns


Pattern Logic
for(char x='A' ; x<='E'; x++){
AAAAA for(char y='A' ; y<='E' ; y++){
BBBBB
[Link](x);
CCCCC
DDDDD }
EEEEE [Link]();
}
Pattern Logic
ABCDE for(char x='A' ; x<='E'; x++){
ABCDE for(char y='A' ; y<='E' ; y++){
ABCDE
[Link](y);
ABCDE
ABCDE }
[Link]();
}
Pattern Logic
for(char x='E' ; x>='A'; x--){
EEEEE for(char y='E' ; y>='A' ; y--){
DDDDD
[Link](x);
CCCCC
BBBBB }
AAAAA [Link]();
}
Pattern Logic
for(char x='E' ; x>='A'; x--){
EDCBA for(char y='E' ; y>='A' ; y--){
EDCBA
[Link](y);
EDCBA
EDCBA }
EDCBA [Link]();
}

Basic Patterns with Conditions


Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<=5 ; j++){
$$$$$
if(i%2==0)
#####
$$$$$ [Link]("#");
##### else
$$$$$ [Link]("$");
}
[Link]();
}

Contact : 911 955 6789 70


[Link]

Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<=5 ; j++){
$#$#$
if(j%2==0)
$#$#$
$#$#$ [Link]("#");
$#$#$ else
$#$#$ [Link]("$");
}
[Link]();
}

Pattern Logic
int k=1;
12345 for (int i=1 ; i<=5 ; i++){
67891
for (int j=1 ; j<=5 ; j++){
23456
78912 [Link](k++%10);
34567 }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
11111
for (int j=1 ; j<=5 ; j++){
12345
33333 if(i%2==0)
12345 [Link](j);
55555 else
[Link](i);
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
55555
for (int j=1 ; j<=5 ; j++){
54321
33333 if(i%2==0)
54321 [Link](j);
11111 else
[Link](i);
}
[Link]();
}

Contact : 911 955 6789 71


[Link]

Patterns Plus Cross Swastika


Pattern Logic
+++++++ for (int i=1 ; i<=7 ; i++)
+++++++ {
+++++++
for (int j=1 ; j<=7 ; j++)
+++++++
+++++++ {
+++++++ [Link]("+");
+++++++ }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++)
+++++++ {
+ +
for (int j=1 ; j<=7 ; j++){
+ +
+ + if(i==1||i==7||j==1||j==7)
+ + [Link]("+");
+ + else
+++++++ [Link](" ");
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==4||j==4)
+
+++++++ [Link]("+");
+ else
+ [Link](" ");
+ }
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+++++++ for (int j=1 ; j<=7 ; j++){
+ + +
if(i==1||i==4||i==7||j==1||j==4||j==7)
+ + +
+++++++ [Link]("+");
+ + + else
+ + + [Link](" ");
+++++++ }
[Link]();
}

Contact : 911 955 6789 72


[Link]

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==j)
+
+ [Link]("+");
+ else
+ [Link](" ");
+ }
[Link]();
}

Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==8-i)
+
+ [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(i==j || j==8-i)
+ +
+ [Link]("+");
+ + else
+ + [Link](" ");
+ + }
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + + + for (int j=1 ; j<=7 ; j++){
+ + + +
if(i==1||i==7||j==1||j==7||i==j||j==8-i)
+ + + +
+ + + [Link]("+");
+ + + + else
+ + + + [Link](" ");
+ + + + + + + }
[Link]();
}

Contact : 911 955 6789 73


[Link]

Pattern Logic
for (int i=1 ; i<=7 ; i++){
++++ for (int j=1 ; j<=7 ; j++){
+
if((i==1 && j<=4) || j==4 || (i==7 && j>=4))
+
+ [Link]("+");
+ else
+ [Link](" ");
++++ }
[Link]();
}
Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==4||(j==1 && i>=4) || (j==7 && i<=4))
+++++++
+ [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}
Pattern Logic
++++ + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(i==4||(j==1 && i>=4) || (j==7 && i<=4) ||
+++++++
+ + (i==1 && j<=4) || j==4 || (i==7 && j>=4))
+ + [Link]("+");
+ ++++ else
[Link](" ");
}
[Link]();
}

Patterns – All Digits


Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=5 ; j++){
+ +
if(i==1||i==7||j==1||j==5)
+ +
+ + [Link]("+");
+ + else
+ + + + + [Link](" ");
}
[Link]();
}

Contact : 911 955 6789 74


[Link]

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=5 ; j++){
+ +
if(j==4-i || j==3 || i==7)
+ +
+ [Link]("+");
+ else
+ [Link](" ");
+ + + + + + + }
[Link]();

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=5 ; j++){
+
if(i==1||i==4||i==7||(j==5&&i<=4)||(j==1&&i>=4))
+
+ + + + + [Link]("+");
+ else
+ [Link](" ");
+ + + + + }
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=5 ; j++){
+
if(i==1||i==4||i==7||j==5)
+
+ + + + + [Link]("+");
+ else
+ [Link](" ");
+ + + + + }
[Link]();
}

Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==5 || i==5 || j==6-i)
+ +
+ + + + + + + + [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}

Contact : 911 955 6789 75


[Link]

Pattern Logic
for (int i=1 ; i<=7 ; i++)
+ + + + + {
+
for (int j=1 ; j<=5 ; j++){
+
+ + + + + if(i==1||i==4||i==7||(j==1&&i<=4)||(j==5&&i>=4))
+ [Link]("*");
+ else
+ + + + + [Link](" ");
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=5 ; j++){
+
if(i==1||j==1||i==4||i==7||(j==5&&i>=4))
+
+ + + + + [Link]("*");
+ + else
+ + [Link](" ");
+ + + + + }
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++){
for (int j=1 ; j<=5 ; j++){
+ + + + + +
if(i==1||j==7-i)
+
+ [Link]("*");
+ else
+ [Link](" ");
+ }
[Link]();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=5 ; j++){
+ +
if(i==1||i==4||i==7||j==1||j==5)
+ + + + +
+ + [Link]("*");
+ + else
+ + + + + [Link](" ");
}
[Link]();
}

Contact : 911 955 6789 76


[Link]

Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=5 ; j++){
+ +
if(i==1||i==4||i==7||(j==1&&i<=4)||j==5)
+ + + + +
+ [Link]("*");
+ else
+ + + + + [Link](" ");
}
[Link]();
}

Patterns – All Alphabets


Pattern Logic
+ for (int i=1 ; i<=5 ; i++){
+ + for (int j=1 ; j<=9 ; j++){
+ +
if((j==6-i)||(i==3&&j>=3&&j<=7)||(j==4+i))
+ + + +
+ + [Link]("+"); A
+ + else M
[Link](" "); E
} E
[Link](); R
} P
E
Pattern Logic
T
+ + + + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){ T
+ +
if((j==1)||(i==1&&j<=6)||(i==4&&j<=6)||(i==7&&j<=6 E
+ + + +
+ + )||(j==7&&i>1&&i<4)||(j==7&&i>4&&i<7)) C
+ + [Link]("+"); H
+ + + + else N
[Link](" "); O
} L
[Link](); O
} G
Pattern Logic I
+ + + + for (int i=1 ; i<=7 ; i++){ E
+ for (int j=1 ; j<=7 ; j++){ S
+
if((i==1&&j>1)||(j==1&&i>1&&i<7)||(i==7&&j>1))
+
+ [Link]("+");
+ else
+ + + + [Link](" ");
}
[Link]();
}

Contact : 911 955 6789 77


[Link]

Pattern Logic
+ + + + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==1||(i==1&&j<7)||(i==7&&j<7)||(j==7&&i>1&&i<
+ +
+ + 7))
+ + + + [Link]("+");
else
[Link](" ");
}
[Link]();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==1||i==4||i==7)
+ + + + +
+ [Link]("+");
+ else
+ + + + + [Link](" ");
}
[Link]();
}

Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==1||i==4)
+ + + + +
+ [Link]("+");
+ else
+ [Link](" ");
}
[Link]();
}

Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==1||i==7||(j==7&&i>=4)||(i==4&&j>=4))
+ + +
+ + [Link]("+");
+ + else
+ + + + + [Link](" ");
}
[Link]();
}

Contact : 911 955 6789 78


[Link]

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==1||i==4||j==7)
+ +
+ + + + + [Link]("+");
+ + else
+ + [Link](" ");
+ + }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=7 ; j++){
+
if(i==1||j==4||i==7)
+
+ [Link]("+");
+ else
+ [Link](" ");
+ + + + + }
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + for (int j=1 ; j<=7 ; j++){
+
if(i==1||j==4||i==j+3)
+
+ + [Link]("+");
+ + else
+ [Link](" ");
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==1||(j==6-i)||(i==2+j))
+ +
+ [Link]("+");
+ + else
+ + [Link](" ");
+ + }
[Link]();
}

Contact : 911 955 6789 79


[Link]

Pattern Logic
for (int i=1 ; i<=7 ; i++)
{
+
for (int j=1 ; j<=7 ; j++)
+
+ {
+ if(j==1||i==7)
+ [Link]("+");
+ + + + + else
[Link](" ");
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++)
{
+ +
for (int j=1 ; j<=7 ; j++)
+ + + +
+ + + + {
+ + + if(j==1||j==7||(i==j&&j<=4)||(j==8-i&&j>4))
+ + [Link]("+");
+ + else
+ + [Link](" ");
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=7 ; i++)
{
+ +
for (int j=1 ; j<=7 ; j++)
+ + +
+ + + {
+ + + if(j==1||j==7||(i==j))
+ + + [Link]("+");
+ + + else
+ + [Link](" ");
}
[Link]();
}

Contact : 911 955 6789 80


[Link]

Numbers Triangle Patterns

Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
12 {
123
for (int j=1 ; j<=i ; j++){
1234
12345 [Link](j);
}
[Link]();
}
Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
21 {
321
for (int j=i ; j>=1 ; j--){
4321
54321 [Link](j);
}
[Link]();
}
Pattern Logic
12345 for (int i=5 ; i>=1 ; i--)
1234 {
123
for (int j=1 ; j<=i ; j++){
12
1 [Link](j);
}
[Link]();
}

Pattern Logic
12345 for (int i=1 ; i<=5 ; i++)
2345 {
345
for (int j=i ; j<=5 ; j++){
45
5 [Link](j);
}
[Link]();
}

Pattern Logic
5 for (int i=5 ; i>=1 ; i--){
54 for (int j=5 ; j>=i ; j--){
543
[Link](j);
5432
54321 }
[Link]();
}

Contact : 911 955 6789 81


[Link]

Pattern Logic
5 for (int i=5 ; i>=1 ; i--){
45 for (int j=i ; j<=5 ; j++){
345
[Link](j);
2345
12345 }
[Link]();
}

Pattern Logic
54321 for (int i=1 ; i<=5 ; i++)
5432 {
543
for (int j=5 ; j>=i ; j--){
54
5 [Link](j);
}
[Link]();
}
Pattern Logic
54321 for (int i=5 ; i>=1 ; i--)
4321 {
321
for (int j=i ; j>=1 ; j--){
21
1 [Link](j);
}
[Link]();
}

Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
22 {
333
for (int j=1 ; j<=i ; j++){
4444
55555 [Link](i);
}
[Link]();
}

Pattern Logic
11111 for (int i=1 ; i<=5 ; i++)
2222 {
333
for (int j=i ; j<=5 ; j++){
44
5 [Link](i);
}
[Link]();
}

Contact : 911 955 6789 82


[Link]

Pattern Logic
5 for (int i=5 ; i>=1 ; i--)
44 {
333
for (int j=i ; j<=5 ; j++){
2222
11111 [Link](i);
}
[Link]();
}

Pattern Logic
for (int i=5 ; i>=1 ; i--)
55555 {
4444
for (int j=1 ; j<=i ; j++)
333
22 {
1 [Link](i);
}
[Link]();
}
Pattern Logic
int k=1;
for (int i=1 ; i<=5 ; i++)
1
{
23
456 for (int j=1; j<=i ; j++)
7891 {
23456 [Link](k++);
if(k>9)
k=1;
}
[Link]();
}
Pattern Logic
int k=1;
for (int i=5 ; i>=1 ; i--)
12345
{
6789
123 for (int j=1; j<=i ; j++)
45 {
6 [Link](k++);
if(k>9)
k=1;
}
[Link]();
}

Contact : 911 955 6789 83


[Link]

Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
1
for (int j=i ; j<5 ; j++){
21
321 [Link](" ");
4321 }
54321 for (int k=i ; k>=1 ; k--){
[Link](k);
}
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=i ; j<5 ; j++){
12345
[Link](" ");
1234
123 }
12 for (int k=1 ; k<=i ; k++){
1 [Link](k);
}
[Link]();
}

Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<i ; j++){
12345
[Link](" ");
2345
345 }
45 for (int k=i ; k<=5 ; k++){
5 [Link](k);
}
[Link]();
}

Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=1 ; j<i ; j++){
5
[Link](" ");
45
345 }
2345 for (int k=i ; k<=5 ; k++){
12345 [Link](k);
}
[Link]();
}

Contact : 911 955 6789 84


[Link]

Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=1 ; j<i ; j++){
5
[Link](" ");
54
543 }
5432 for (int k=5 ; k>=i ; k--){
54321 [Link](k);
}
[Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=i ; j<5 ; j++){
54321
[Link](" ");
4321
321 }
21 for (int k=i ; k>=1 ; k--){
1 [Link](k);
}
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
54321
for (int j=1 ; j<i ; j++){
5432
543 [Link](" ");
54 }
5 for (int k=5 ; k>=i ; k--){
[Link](k);
}
[Link]();
}

Characters Triangle Patterns


Pattern Logic
for (char i='A' ; i<='E' ; i++)
A {
AB
for (char j='A' ; j<=i ; j++){
ABC
ABCD [Link](j);
ABCDE }
[Link]();
}

Contact : 911 955 6789 85


[Link]

Pattern Logic
for (char i='A' ; i<='E' ; i++)
A {
BA
for (char j=i ; j>='A' ; j--){
CBA
DCBA [Link](j);
EDCBA }
[Link]();
}

Pattern Logic
for (char i='E' ; i>='A' ; i--)
ABCDE {
ABCD
for (char j='A' ; j<=i ; j++){
ABC
AB [Link](j);
A }
[Link]();
}

Pattern Logic
for (char i='A' ; i<='E' ; i++){
ABCDE for (char j=i ; j<='E' ; j++){
BCDE
[Link](j);
CDE
DE }
E [Link]();
}
Pattern Logic
E for (char i='E' ; i>='A' ; i--)
ED {
EDC
for (char j='E' ; j>=i ; j--){
EDCB
EDCBA [Link](j);
}
[Link]();
}

Pattern Logic
for (char i='E' ; i>='A' ; i--){
E for (char j=i ; j<='E' ; j++){
DE [Link](j);
CDE
}
BCDE
ABCDE [Link]();
}

Contact : 911 955 6789 86


[Link]

attern Logic
EDCBA for (char i='A' ; i<='E' ; i++){
EDCB for (char j='E' ; j>=i ; j--){
EDC
[Link](j);
ED
E }
[Link]();
}

Pattern Logic
EDCBA for (char i='E' ; i>='A' ; i--){
DCBA for (char j=i ; j>='A' ; j--){
CBA
[Link](j);
BA
A }
[Link]();
}

Star Triangle Patterns


Pattern Logic
for (int i=1 ; i<=5 ; i++){
* for (int j=1 ; j<=i ; j++){
**
[Link]("*");
***
**** }
***** [Link]();
}

Pattern Logic
for (int i=5 ; i>=1 ; i--){
***** for (int j=1 ; j<=i ; j++){
****
[Link]("*");
***
** }
* [Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=i ; j<5 ; j++){
*
[Link](" ");
**
*** }
**** for (int k=1 ; k<=i ; k++){
***** [Link]("*");
}
[Link]();
}

Contact : 911 955 6789 87


[Link]

Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<i ; j++){
*****
[Link](" ");
****
*** }
** for (int k=i ; k<=5 ; k++){
* [Link]("*");
}
[Link]();
}
Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
*
for(int j=1 ; j<=i ; j++){
**
*** [Link]("*");
**** }
***** [Link]();
**** }
*** else{
**
for(int k=i ; k<10 ; k++)
*
[Link]("*");
}
[Link]();
}
}
Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
for(int x=i ; x<=5 ; x++){
[Link](" ");
* }
** for(int j=1 ; j<=i ; j++)
*** [Link]("*");
**** [Link]();
*****
}
****
*** else{
** for(int x=i ; x>=5 ; x--)
* [Link](" ");
for(int k=i ; k<10 ; k++)
[Link]("*");
[Link]();
}
}

Contact : 911 955 6789 88


[Link]

Pattern Logic
for(int i=1 ; i<10 ; i++)
{
if(i<=5){
*****
**** for(int j=i ; j<=5 ; j++){
*** [Link]("*");
** }
* [Link]();
** }
***
else{
****
***** for(int k=5 ; k<=i ; k++){
[Link]("*");
}
[Link]();
}
}

Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
for(int x=1 ; x<=i ; x++){
[Link](" ");
}
***** for(int j=i ; j<=5 ; j++){
**** [Link]("*");
*** }
**
[Link]();
*
** }
*** else{
**** for(int x=i ; x<10 ; x++){
***** [Link](" ");
}
for(int k=5 ; k<=i ; k++){
[Link]("*");
}
[Link]();
}
}

Contact : 911 955 6789 89


[Link]

Binary Number Patterns


Pattern Logic
int k=1;
for (int i=1 ; i<=5 ; i++)
1
{
01
010 for (int j=1; j<=i ; j++){
1010 [Link](k++%2);
10101 }
[Link]();
}

Pattern Logic
int k=1;
for (int i=5 ; i>=1 ; i--)
10101
{
0101
010 for (int j=1; j<=i ; j++){
10 [Link](k++%2);
1 }
[Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
1 for (int j=1; j<=i ; j++){
10
[Link](j%2);
101
1010 }
10101 [Link]();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
1 for (int j=1; j<=i ; j++){
00
[Link](i%2);
111
0000 }
11111 [Link]();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
11111 for (int j=1; j<=i ; j++){
0000
[Link](i%2);
111
00 }
1 [Link]();
}

Contact : 911 955 6789 90


[Link]

Border Only Patterns

Pattern Logic
int n=7;
* for(int i=1 ; i<=n ; i++)
**
{
* *
* * for(int j=1 ; j<=i ; j++)
* * {
* * if(i==1 || i==n || j==1 || j==i)
* * [Link]("*");
******** else
[Link](" ");
}
[Link]();
}

Pattern Logic
int n=7;
******** for(int i=n ; i>=1 ; i--)
* *
{
* *
* * for(int j=1 ; j<=i ; j++){
* * if(i==1 || i==n || j==1 || j==i)
* * [Link]("*");
** else
* [Link](" ");
}
[Link]();
}

Pattern Logic
for(int i=1 ; i<=7 ; i++)
{
********
for(int j=1 ; j<i ; j++){
* *
* * [Link](" ");
* * }
* * for(int k=i ; k<=7 ; k++){
* * if(i==1 || i==7 || k==i || k==7)
** [Link]("*");
*
else
[Link](" ");
}
[Link]();
}

Contact : 911 955 6789 91


[Link]

Pattern Logic
for(int i=1 ; i<=7 ; i++)
{
*
for(int j=i ; j<7 ; j++){
**
* * [Link](" ");
* * }
* * for(int k=1 ; k<=i ; k++){
* * if(i==1 || i==7 || k==1 || k==i)
* * [Link]("*");
********
else
[Link](" ");
}
[Link]();
}

Pyramid Patterns
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++)
*
{
***
***** for(int j=i ; j<n ; j++){
******* [Link](" ");
********* }
for(int k=1 ; k<=2*i-1 ; k++){
[Link]("*");
}
[Link]();
}

Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++){
for(int j=i ; j<n ; j++){
*
* * [Link](" ");
* * }
* * for(int k=1 ; k<=2*i-1 ; k++){
********* if(i==1 || i==n || k==1 || k==2*i-1)
[Link]("*");
else
[Link](" ");
}
[Link]();
}

Contact : 911 955 6789 92


[Link]

Pattern Logic
1 int n=7;
222 for(int i=1 ; i<=n ; i++)
33333
{
4444444
555555555 for(int j=i ; j<n ; j++){
[Link](" ");
}
for(int k=1 ; k<=2*i-1 ; k++){
[Link](i);
}
[Link]();
}
A
M
Reverse Pyramid Patterns E
Pattern Logic E
int n=7; R
for(int i=n ; i>=1 ; i--) P
********* { E
*******
for(int j=i ; j<n ; j++){ T
*****
*** [Link](" ");
T
* }
E
for(int k=1 ; k<=2*i-1 ; k++){
C
[Link]("*");
H
}
N
[Link]();
O
}
L
O
Pattern Logic
G
int n=7; I
for(int i=n ; i>=1 ; i--) E
{ S
*********
* * for(int j=i ; j<n ; j++){
* * [Link](" ");
* * }
* for(int k=1 ; k<=2*i-1 ; k++){
if(i==1 || i==n || k==1 || k==2*i-1)
[Link]("*");
else
[Link](" ");
}
[Link]();
}

Contact : 911 955 6789 93


[Link]

Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--)
{
555555555
4444444 for(int j=i ; j<n ; j++){
33333 [Link](" ");
222 }
1 for(int k=1 ; k<=2*i-1 ; k++){
[Link](i);
}
[Link]();
}

Pattern Logic
int r=6, c=1;
for(int i=0; i<r ; i++)
{
1
1 1 for(int s=1; s<r-i; s++) {
1 2 1 [Link](" ");
1 3 3 1 }
1 4 6 4 1 for(int j=0; j<=i; j++) {
1 5 10 10 5 1 if (j==0 || i==0)
c=1;
else
c=c*(i-j + 1)/j;
[Link]("%4d", c);
}
[Link]();
}
for (int i = 5; i >= 1; i--)
{
123454321
for (int j = 5 - i; j >= 1; j--){
1234321
12321 [Link](" ");
121 }
1 for (int j = 1; j <= i; j++){
[Link](j);
}
for (int j = i - 1; j >= 1; j--){
[Link](j);
}
[Link]();
}

Contact : 911 955 6789 94


[Link]

Complex Pattern programs


Pattern Logic
1 int n=7;
121 for(int i=1 ; i<=n ; i++){
12321
for(int j=i ; j<n ; j++){
1234321
123454321 [Link](" ");
}
for(int k=1 ; k<=i ; k++){
[Link](k);
}
for(int l=i-1 ; i>=1 ; i--){
[Link](l);
}
[Link]();
}
Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--){
for(int j=i ; j<n ; j++){
123454321
1234321 [Link](" ");
12321 }
121 for(int k=1 ; k<=i ; k++){
1 [Link](k);
}
for(int l=i-1 ; l>=1 ; l--){
[Link](l);
}
[Link]();
}
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++){
for(int j=i ; j<n ; j++){
[Link](" ");
1 }
212 for(int k=i ; k>=1 ; k--){
32123 [Link](k);
4321234 }
543212345
for(int l=1+1 ; l<=i ; l++){
[Link](l);
}
[Link]();
}

Contact : 911 955 6789 95


[Link]

Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--){
for(int j=i ; j<n ; j++){
543212345
4321234 [Link](" ");
32123 }
212 for(int k=i ; k>=1 ; k--){
1 [Link](k);
}
for(int l=1+1 ; l<=i ; l++){
[Link](l);
}
[Link]();
}

Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++){
for(int j=i ; j<n ; j++){
* [Link](" ");
*** }
***** for(int k=1 ; k<=2*i-1 ; k++){
******* [Link]("* ");
********* }
***********
[Link]();
*********
******* }
***** for(int i=n-1 ; i>=1 ; i--){
*** for(int j=i ; j<n ; j++){
* [Link](" ");
}
for(int k=1 ; k<=2*i-1 ; k++){
[Link]("* ");
}
[Link]();
}
}

Contact : 911 955 6789 96


[Link]

Object Oriented Programming

Object Oriented Programming: is a concept of defining objects and establish the relation
between the objects.

Object oriented application:


• Application is a collection of Objects.
• Objects represented by Classes.
• Class consists of variables and methods.
• Variables are used to store the information.
• Methods are used to perform opertions on information.

Defining a class:
• In Java application, every statement belongs to class.
• Class has identity – to access.

Types of variables:
1. Static variables:
• A variable with static keyword inside the class outside to methods.
• Static variable stores common information of all objects.
• Static variable gets memory only once.
• Static variables can be accessed using class name.

2. Instance variables:
• A variable inside the class and outside to methods.
• Instance variables store specific information of object.
• Instance variables get separate memory inside every object.
• Instance variables can be accessed using object address.

Contact : 911 955 6789 97


[Link]

3. Method parameters:
• Define variables inside method parenthesis.
• Parameters used to take input of method.
• Parameters get memory when method invokes.
• Parameters can be accessed directly and from the same method.

4. Local variables:
• A variable inside method.
• Local variables store processed information inside the object.
• Local variables get memory when we call the method.
• Local variables can be accessed directly.

A
M
E
E
R
P
E
T

T
E
C
H
N
O
L
O
G
I
E
Method: S
• Method is a block of instructions that performs a task.
• Method is called a sub program.
• Method takes input, process the input and returns the output.

Syntax Example
returntype identity(arguments) int add(int a, int b)
{ {
statements; int c=a+b;
} return c;
}

Contact : 911 955 6789 98


[Link]

Static and Instane Methods

Static Method:
• Defining a method using static keyword.
• Static methods process static variables data.
• We can access static methods using class-name.

public class Code


{
static void fun()
{
// logic
}
}

Accessing static method:


[Link]();

Instance Method:
• Defining a method without static keyword.
• Instance methods represents specific information of object.
• We can access instance methods using object-reference.

public class Code


{
void fun()
{
// logic
}
}

Accessing Instance methods:


Code obj = new Code();
[Link]();

Classification of Methods: Based on taking input and returning output, methods are classified
into 4 types.
1. No arguments and No return values
2. With arguments and No return values
3. With arguments and with return values
4. No arguments and with return values

Contact : 911 955 6789 99


[Link]

• Java program execution starts from main method.


• main() method automatically invokes by JVM.
• Other methods execute only if we invoke.

public class Code


{
public static void main(String[] args)
{
[Link]("main...");
}
static void fun()
{
[Link]("fun...");
}
}

• Method executes only when we invoke.


• We can invoke the methods any number of times once we defined.

public class Code


{
public static void main(String[] args)
{
[Link]("main...");
[Link]();
[Link]();
}
static void fun()
{
[Link]("fun...");
}
}

Contact : 911 955 6789 100


[Link]

Type: No arguments and No return values


Invoking the method of same class:
public class Code
{
public static void main(String[] args)
{
[Link]();
}
static void fun(){
[Link]("fun...");
}
}

Invoke other class methods:


public class Code
{
public static void main(String[] args)
{
[Link]();
}
}
class Say
{
static void hiToAll(){
[Link]("Hi to all");
}
}

We can define any number of classes in java application:


class Clear
public class Code {
{ static void all(){
public static void main(String[] args) [Link]("Clear all");
{ }
[Link](); }
[Link](); class Hello
} {
} static void everyOne(){
[Link]("Welcome all");
}
}

Contact : 911 955 6789 101


[Link]

Type : with arguments and without return values


Adding 2 numbers:
public class Code
{
public static void main(String[] args)
{
[Link](10, 20);
}
}
class Calculate
{
static void sum(int a, int b)
{
int c = a+b;
[Link]("Sum = " + c);
}
}

Check the number is Even or Not:


public class Code
{
public static void main(String[] args)
{
[Link](13);
}
}
class Check
{
static void isEven(int num)
{
if(num%2==0)
[Link]("Even num");
else
[Link]("Odd num");
}
}

Read the input values using Scanner class:


import [Link];
public class Code
{
public static void main(String[] args)
{

Contact : 911 955 6789 102


[Link]

Scanner scan = new Scanner([Link]);


[Link]("Enter number : ");
int num = [Link]();
[Link](num);
}
}
class Check
{
static void isEven(int num)
{
if(num%2==0)
[Link]("Even num");
else
[Link]("Odd num");
}
}

Reading 2 numbers and pass to method to perform sum operation:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter 2 numbers : ");
int a = [Link]();
int b = [Link]();
[Link](a, b);
}
}
class Calculate
{
static void sum(int a, int b)
{
int c = a+b;
[Link]("Sum = " + c);
}
}

Check the character is vowel or not:


import [Link];
public class Code
{

Contact : 911 955 6789 103


[Link]

public static void main(String[] args)


{
Scanner scan = new Scanner([Link]);
[Link]("Enter a character : ");
char ch = [Link]().charAt(0);
[Link](ch);
} A
} M
class Check E
{ E
static void isVowel(char ch) R
{ P
if(ch=='a' || ch=='e' || ch=='i' || ch=='0' || ch=='u') E
[Link]("It is vowel"); T
else
T
[Link]("it is not vowel"); E
} C
} H
N
Reading all Employee details and pass to method: O
import [Link]; L
public class Code O
{ G
public static void main(String[] args) I
{ E
Scanner scan = new Scanner([Link]); S
[Link]("Enter emp details : ");
int id = [Link]();
String name = [Link]();
Double salary = [Link]();
char gender = [Link]().charAt(0);
[Link](id, name, salary, gender);
}
}
class Employee
{
static void details(int id, String name, double salary, char gender)
{
[Link]("Details : " + id + " , " + name + " , " + salary + " , " + gender);
}
}

Contact : 911 955 6789 104


[Link]

Method with arguments and with return values:


public class Code
{
public static void main(String[] args)
{
int res = [Link](10, 20);
[Link]("Sum = " + res);
}
}
class Calculate
{
static int sum(int a, int b)
{
int c = a+b;
return c;
}
}

Check the number is even or not:


public class Code
{
public static void main(String[] args)
{
String res = [Link](13);
[Link](res);
}
}
class Check
{
static String isEven(int num)
{
if(num%2==0)
return "even";
else
return "odd";
}
}

Display biggest of 3 numbers:


public class Code
{
public static void main(String[] args)
{

Contact : 911 955 6789 105


[Link]

int b = [Link](10, 30, 20);


[Link]("Big : " + b);
}
}
class Display
{
static int big(int a, int b, int c){
if(a>b && a>c)
return a;
else if(b>c)
return b;
else
return c;
}
}

Method returns the message as biggest number:


import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter 3 numbers : ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
String res = [Link](a, b, c);
[Link](res);
}
}
class Display
{
static String big(int a, int b, int c){
if(a>b && a>c)
return "a is big";
else if(b>c)
return "b is big";
else
return "c is big";
}
}

Contact : 911 955 6789 106


[Link]

Reading number and display multiplication table:


import [Link];
public class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter table number : ");
int n = [Link]();
[Link](n);
}
}
class Print{
static void table(int n){
for(int i=1 ; i<=10 ; i++)
{
[Link](n + " * " + i + " = " + (n*i));
}
}
}

Check the number is Prime or Not:


import [Link];
public class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter number : ");
int n = [Link]();
[Link](n);
}
}
class Check{
static void isPrime(int n){
int count=0;
for(int i=1 ; i<=n/2 ; i++){
if(n%2==0)
count++;
}
if(count==2)
[Link]("Prime number");
else
[Link]("Not prime number");
}
}

Contact : 911 955 6789 107


[Link]

Instance Method Examples


Sum of 2 numbers:
public class Code {
public static void main(String[] args){
Calculate obj = new Calculate();
int res = [Link](10, 20);
[Link]("Sum = " + res);
}
}
class Calculate{
int sum(int a, int b){
int c = a+b;
return c;
}
}

Biggest of 3 numbers:
import [Link];
public class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter 3 numbers : ");
int a = [Link]();
int b = [Link]();
int c = [Link]();
Display obj = new Display();
String res = [Link](a, b, c);
[Link](res);
}
}
class Display{
String big(int a, int b, int c)
{
if(a>b && a>c)
return "a is big";
else if(b>c)
return "b is big";
else
return "c is big";
}
}

Contact : 911 955 6789 108


[Link]

Consider Class-name as “Test” & Invoke the below methods


static void m1(); void m2();

static void m3(int x); void m4(int x);

static int m5(); int m6();

static void m7(int x, int y); void m8(int x, float y) A


M
E
E
static int m9(int x); String m10(char x); R
P
E
T
int m11(String x, String y); boolean m12(int x, int y, int z); T
E
C
H
N
char charAt(int index) public static void big(int a, int b, int c); O
L
O
G
static int getAge(); static void setString(String s) I
E
S

double add(int x, double y) void check(int x, double y)

String getVal(String x, int y); long millis(int x, int y);

Contact : 911 955 6789 109


[Link]

Write codes for each problem:


Problem1:
1. Define class with name Code.
2. Define main() method and static abc() method in Code class.
3. Invoke abc() method from main() method.

Problem2:
1. Define class with name Code.
2. Define main() method in Code class.
3. Define class with name Wish and a static method toAll() inside the Wish class.
4. Invoke toAll() method from main() method.

Problem3:
1. Define class with name Code.
2. Define main() method in Code class.
3. Define a class with name Arithmetic and static method operations() inside the class.
4. Read 2 integers in main() method and pass to operations() method.
5. Perform all arithmetic operations and print results inside the method.

Problem4:
1. Define class with name Code.
2. Define main() method in Code class. A
3. Define class with name Person and a static method canVote() inside the class. M
4. canVote() method takes age as input and print message Can Vote or Not. E
5. Invoke canVote() method by passing input from main() method. E
R
Problem5: P
1. Define class with name Code. E
2. Define main() method and instance method abc() in Code class. T
3. Invoke abc() method from main() method.
T
E
Problem6:
C
1. Define class with name Code.
H
2. Define main() method and instance method isPerfect() method in Code class.
N
3. isPerfect() method takes integer input and display message Perfect or Not.
O
4. Read input using Scanner class and invoke the method isPerfect().
L
O
Problem7:
G
1. Define class with name Code and main() method..
I
2. Define a class with name Check and instance method isSymbol() inside that class. E
3. Read character from main() and pass to isSymbol() method. S
4. Display the character is Symbol or not inside the method.

Contact : 911 955 6789 110


[Link]

Recursion
Recursion:
• Calling method itself is called Recursion.
• Invoking the method from the body of same method.

Program to display 1 to 10 numbers using recursion:


class Code {
public static void main(String[] args) {
[Link](1);
}
static void print(int n){
[Link](n);
if(n<10)
print(n+1);
}
}

Program to display 10 to 1 number using recursion:


class Code {
public static void main(String[] args) {
[Link](1);
}
static void print(int n){
if(n<10)
print(n+1);
[Link](n);
}
}

Program to display Sum of First N numbers using recursion:


class Code {
public static void main(String[] args) {
int n=5;
int sum = [Link](n);
[Link](sum);
}
static int calc(int n){
if(n>0)
return n + calc(n-1);
else
return n;
}
}

Contact : 911 955 6789 111


[Link]

Program to display Factorial of given number using recursion:


class Code
{
public static void main(String[] args)
{
int n=5;
int res = [Link](n);
[Link](res);
}
static int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
}

Program to display Fibonacci series using recursion:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("Enter limit : ");
int n = [Link]();
for(int i=1; i<=n; i++){
[Link](fib(i)+" ");
}
}
static int fib(int n)
{
if(n==1)
return 0;
else if(n == 2)
return 1;
else
return fib(n-2)+fib(n-1);
}
}

Contact : 911 955 6789 112


[Link]

Menu Driven Programs

Arithmetic Operations Program using Menu Driven Approach:


• Menu driven approach is the concept of continuous execution of program by performing
required operations until user quits.
• We use while-loop in Menu driven approach as we don’t know the number of times we
need to repeat the loop.
• We can implement menu driven programs using if-else-if or switch statements.

import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
while(true)
{
[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");
[Link]("5. Quit");
[Link]("Enter your choice : ");
int ch = [Link]();
if(ch==1)
{
[Link]("Enter 2 numbers : ");
int a=[Link]();
int b=[Link]();
int c=a+b;
[Link](a + " + " + b + " = " + c);
}
else if(ch==2)
{
[Link]("Enter 2 numbers : ");
int a=[Link]();
int b=[Link]();
int c=a-b;
[Link](a + " - " + b + " = " + c);
}
else if(ch==3)
{
[Link]("Enter 2 numbers : ");

Contact : 911 955 6789 113


[Link]

int a=[Link]();
int b=[Link]();
int c=a*b;
[Link](a + " * " + b + " = " + c);
}
else if(ch==4)
{
[Link]("Enter 2 numbers : ");
int a=[Link]();
int b=[Link]();
int c=a/b;
[Link](a + " / " + b + " = " + c);
}
else if(ch==5)
{
[Link]("End of Program");
[Link](1);
}
else
{
[Link]("Invalid Choice");
}
}
}
}

Arithmetic Operations Menu Driven Approach – Switch Case:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
while(true)
{
[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");
[Link]("5. Quit");

[Link]("Enter your choice : ");


int ch = [Link]();

Contact : 911 955 6789 114


[Link]

int a=0, b=0;


if(ch>=1 && ch<=4)
{
[Link]("Enter 2 numbers : ");
a=[Link]();
b=[Link]();
}
switch(ch)
{
case 1 : [Link](a+" + "+b+" = "+(a+b));
break;
case 2 : [Link](a+" - "+b+" = "+(a-b));
break;
A
case 3 : [Link](a+" * "+b+" = "+(a*b));
M
break;
E
case 4 : [Link](a+" / "+b+" = "+(a/b));
E
break; R
case 5 : [Link]("End of Program"); P
[Link](1); E
default: [Link]("Invalid choice"); T
}
} T
} E
} C
H
Arithmetic Operations Menu Driver Approach – using Methods: N
O
import [Link];
L
public class Code
O
{
G
public static void main(String[] args)
I
{
E
Scanner sc = new Scanner([Link]);
S
while(true)
{
[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");
[Link]("5. Quit");

[Link]("Enter your choice : ");


int ch = [Link]();

Contact : 911 955 6789 115


[Link]

int a=0, b=0;


if(ch>=1 && ch<=4)
{
[Link]("Enter 2 numbers : ");
a=[Link]();
b=[Link]();
}

switch(ch){
case 1 : add(a,b);
break;
case 2 : subtract(a,b);
break;
case 3 : multiply(a,b);
break;
case 4 : divide(a,b);
break;
case 5 : [Link]("End of Program");
[Link](1);
default: [Link]("Invalid choice");
}
}
}
static void add(int a, int b)
{
[Link](a+" + "+b+" = "+(a+b));
}
static void subtract(int a, int b)
{
[Link](a+" - "+b+" = "+(a-b));
}
static void multiply(int a, int b)
{
[Link](a+" * "+b+" = "+(a*b));
}
static void divide(int a, int b)
{
[Link](a+" / "+b+" = "+(a/b));
}
}

Contact : 911 955 6789 116


[Link]

Setters and Getters

Encapsulation(POJO) rules:
• We can protect the information of object in java by defining variables as private.
• One object cannot access the data of another object directly if it is private.
• Data sharing is possible using functionality in communication using setters and getters.
• Set method is used to send the values of object to another object.
• Get method is used to receive the values of object from another object.

Trying to access the private static variables of same class – No error:


public class Code
{
private static int x=10;
public static void main(String[] args)
{
[Link]("x value : " + Code.x);
}
}

Accessing private instance variable of same class:


public class Code
{
private int x;
public static void main(String[] args)
{
Code obj = new Code();
[Link]("x value : " + obj.x);
}
}

Accessing private variables of another class results Error:


public class Code
{
public static void main(String[] args)
{
[Link]("x value : " + Test.x); // Error :
}
}
class Test
{
private static int x=10;
}

Contact : 911 955 6789 117


[Link]

Accessing private static variable of another class using get() method:


• Static variables can be processed using static methods.
• Instance variables can be processed using instance methods.
• Getter method must have return type as it is returning the value.
• Getter method identity starts with word ‘get’ followed by variable name.
o getX();
o getId();
o getEmpName();
o getAccHolderAddress();
public class Code
{
public static void main(String[] args)
{
[Link]("x value : " + [Link]());
}
}
class Test
{
private static int x=10;
public static int getX()
{
return Test.x;
}
}

Accessing private instance variable of another class using get() method:


public class Code
{
public static void main(String[] args)
{
Test obj = new Test();
[Link]("x value : " + [Link]());
}
}
class Test
{
private int x;
public int getX()
{
return this.x;
}
}

Contact : 911 955 6789 118


[Link]

Setting values to private static variable using set() method:


public class Code
{
public static void main(String[] args)
{
[Link]("x value : " + [Link]());
[Link](20);
[Link]("Modified x value : " + [Link]());
}
}
class Test
{
private static int x=10;
public static int getX(){
return Test.x;
}
public static void setX(int x){
Test.x = x;
}
}

Setting values to private instance variables of another class:


public class Code
{
public static void main(String[] args)
{
Test obj = new Test();
[Link]("x value : " + [Link]());
[Link](10);
[Link]("Modified x value : " + [Link]());
}
}
class Test
{
private int x;
public int getX(){
return this.x;
}
public void setX(int x){
this.x = x;
}
}

Contact : 911 955 6789 119


[Link]

Setting values by reading input from the User:


• Scanner class is used to read the input from the User.
• We need to define set() and get() methods for each variable separately.

import [Link];
public class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter x value : ");
int x = [Link]();
[Link]("Enter y value : ");
int y = [Link]();
[Link](x);
[Link](y);
[Link]("x value : " + [Link]());
[Link]("y value : " + [Link]());
}
}

class Test
{
private static int x, y;
public static int getX()
{
return Test.x;
}
public static void setX(int x)
{
Test.x = x;
}
public static int getY()
{
return Test.y;
}
public static void setY(int y)
{
Test.y = y;
}
}

Contact : 911 955 6789 120


[Link]

Accessing Employee object with private variables:


class Employee
{
private int id;
private String name;
private double salary;
A
public void setId(int id){ M
[Link]=id; E
} E
public void setName(String name){ R
[Link]=name; P
} E
public void setSalary(double salary){ T
[Link]=salary;
T
}
E
C
public int getId(){
H
return [Link];
N
}
O
public String getName(){
L
return [Link]; O
} G
public double getSalary(){ I
return [Link]; E
} S
}
public class Code
{
public static void main(String[] args)
{
Employee e = new Employee();
[Link](101);
[Link]("Amar");
[Link](35000);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}

Contact : 911 955 6789 121


[Link]

Accessing student object with private variables:


class Student
{
private int id;
private String name;
private String course;
public void setId(int id)
{
[Link]=id;
}
public void setName(String name)
{
[Link]=name;
}
public void setCourse(String course)
{
[Link]=course;
}
public int getId()
{
return [Link];
}
public String getName()
{
return [Link];
}
public String getCourse()
{
return [Link];
}
}
public class Code{
public static void main(String[] args) {
Student s = new Student();
[Link](101);
[Link]("Annie");
[Link]("Java");
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}

Contact : 911 955 6789 122


[Link]

Object Initialization using Constructor

Constructor:
• Define a method with the same name of class.
• Return types not allowed for constructor.
• We invoke constructor every time in object creation process

public class Code


{
public static void main(String[] args) {
Test obj = new Test();
}
}
class Test
{
Test(){
[Link]("Constructor called");
}
}

Parameterized constructor:
• A constructor with parameters.
• Parameterized constructors are used to set values to instance variables at the time of
Objet creation only.

public class Code


{
public static void main(String[] args) {
Test obj = new Test(10);
[Link]("a value : " + [Link]());
}
}
class Test
{
private int a ;
Test(int a){
this.a = a;
}
public int getA(){
return this.a;
}
}

Contact : 911 955 6789 123


[Link]

We can initialize any number variables with single constructor:


public class Code
{
public static void main(String[] args) {
Test obj = new Test(10, 20);
[Link]();
}
}
class Test
{
private int a, b ;
Test(int a, int b){
this.a = a;
this.b = b;
}
public void details(){
[Link]("a val : " + this.a);
[Link]("b val : " + this.b);
}
}

It is recommended to override toString() method to display the values easily:


public class Code
{
public static void main(String[] args) {
Test obj = new Test(10, 20);
[Link](obj);
}
}
class Test{
private int a, b ;
Test(int a, int b){
this.a = a;
this.b = b;
}
@Override
public String toString()
{
return "Details : " + this.a + " , " + this.b;
}
}

Contact : 911 955 6789 124


[Link]

Initializing Employee using Parameterized constructor:


class Employee
{
private int id;
private String name;
private double salary;

public Employee(int id, String name, double salary)


{
[Link] = id;
[Link] = name;
[Link] = salary;
}

@Override
public String toString()
{
return "Details : " + [Link] + " , " + [Link] + " , " + [Link];
}
}

public class Code


{
public static void main(String[] args)
{
Employee e1 = new Employee(101, "Amar", 25000);
Employee e2 = new Employee(102, "Annie", 35000);
[Link](e1);
[Link](e2);
}
}

Try to implement following programs:


Program1:
• Initialize Employee object using parameterized constructor by reading input values from
the user using Scanner class.

Program2:
• Create Student class with private instance variables id, name, course and fee.
• Define parameterized constructor to initialize the object.
• Read input values from the user and create object of Student class.
• Display details by overriding toString() method

Contact : 911 955 6789 125


[Link]

Java - Arrays

Array:
• Primitive variable can store only one value at a time.
• Array variable can store more than one element but of same type.
For example marks[ ] = {56, 45, 90, 67, 56};
• Array elements get side by side memory locations in a block.
• Array elements can be accessed using their index starts from 0 to length-1;

Array creation: int[ ] arr = {3, 7, 1, 9, 5, 2, 7, 6};

Memory representation:

Java – Program to Find the length of given array


• ‘length’ is a pre-defined instance variable that returns length of array.

public class Code


{
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};
[Link]("Length is : " + [Link]);
}
}

Note: If we just declare the array variable instead of allocating the memory, we cannot find the
length. The following program raises error.

public class Code


{
public static void main(String[] args)
{
int[ ] arr;
[Link]("Lengt is : " + [Link]);
}
}

Contact : 911 955 6789 126


[Link]

Java - Display first element of array


class Code
{
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};
int first = arr[0];
[Link]("First element is : " + first);
}
}

Display first and last element of array:


class Code
{
public static void main(String[] args) {
int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};
int n = [Link];
int first = arr[0];
int last = arr[n-1];
[Link]("First element is : " + first);
[Link]("Last element is : " + last);
}
}

It is recommended to check the array contains elements or not before accessing elements:
class Code
{
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};
if([Link]==0)
{
[Link]("Empty array");
}
else
{
int first = arr[0];
[Link]("First element is : " + first);
}
}
}

Contact : 911 955 6789 127


[Link]

Handle ArrayIndexOutOfBoundsException instead of checking before accessing elements:


class Code
{
public static void main(String[] args) {
int[] arr = {4, 2, 8, 9, 1, 6, 7, 4};
try{
int first = arr[0];
[Link]("First element is : " + first);
}
catch(ArrayIndexOutOfBoundsException e){
[Link]("Exception : No index to access elements");
}
}
}
A
Check the first element of Array is Even or not M
class Code{ E
public static void main(String[] args) E
{ R
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5}; P
int first = arr[0]; E
if(first%2==0) T
[Link]("First element is Even");
T
else
E
[Link]("First element is not Even");
C
}
H
} N
O
Check the sum of first and last elements in Array equals to 10 or not L
class Code O
{ G
public static void main(String[] args) I
{ E
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5}; S
int first = arr[0];
int last = arr[[Link]-1];
if(first+last==10)
[Link]("Equals");
else
[Link]("Not Equals");
}
}

Contact : 911 955 6789 128


[Link]

Java – Program to check first and last elements of Array are same or not
class Code {
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
if(arr[0]==arr[[Link]-1])
[Link]("Same");
else
[Link]("Not Same");
}
}

Java – Program to Check the length of array is even or not


class Code {
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
if([Link]%2==0)
[Link]("Even length array");
else
[Link]("Not an Even length array");
}
}

Display the median value of array:


• If the length is ODD, Print middle element
• If the length is EVEN, Print average of middle 2 elements

class Code
{
public static void main(String[] args) {
int[] arr = {6, 2, 8, 9, 1, 6, 7, 5};
int n = [Link];
if(n%2!=0)
[Link]("Mean : " + arr[n/2]);
else{
int x = arr[n/2-1];
int y = arr[n/2];
[Link]("Mean : " + ((x+y)/2));
}
}
}

Contact : 911 955 6789 129


[Link]

Java - Program to display array elements


• Array consists more than one element.
• It is recommended to process multiple elements using loops.
• We use for() loop as we know the number of repetitions.

class Code
{
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
[Link]("Array elements are : ");
for (int i=0 ; i<=[Link]-1 ; i++)
{
[Link](arr[i]);
}
}
}

Program to display array elements in reverse order


class Code
{
public static void main(String[] args)
{
int[] arr = {5, 9, 2, 1, 8, 4, 6};
[Link]("Reverse Array : ");
for (int i=[Link]-1 ; i>=0 ; i--)
{
[Link](arr[i]);
}
}
}

Display default values: As soon as memory allocated to array, all the locations initialized with
default values based on type of array

Data type Default value


int 0
double 0.0
char Blank
boolean False
String null

Contact : 911 955 6789 130


[Link]

Java - Program to display default values of array


class Code {
public static void main(String[] args)
{
int[] arr = new int[6];
[Link]("Default values of Array : ");
for (int i=0 ; i<=[Link]-1 ; i++){
[Link](arr[i]);
}
}
}

Java - Program to display array elements using for each loop


• For-each loop is used to process Array or Collection elements easily.
• For-each loop process elements from start to end without using index.
• For-each loop process elements only in forward direction.

class Code {
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
[Link]("Array elements using for-each loop : ");
for (int x : arr){
[Link](x);
}
}
}

Program to find the average of array elements


class Code {
public static void main(String[] args) {
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
double sum=0;
for (int x : arr)
{
sum = sum + x;
}
double avg = sum/[Link];
[Link]("Average of array elements : " + avg);
}
}

Contact : 911 955 6789 131


[Link]

Program to find the sum of array elements:


class Code{
public static void main(String[] args){
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
int sum=0;
for (int i=0 ; i<[Link] ; i++){
sum = sum + arr[i];
}
[Link]("Sum of elements : " + sum);
}
}

Display Array element which are greater than average of all elements:
class Code {
public static void main(String[] args)
{
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
double sum=0;
for (int x : arr){
sum = sum + x;
}
double avg = sum/[Link];
for(int x : arr){
if(x>avg)
[Link](x);
}
}
}

Program to increase each value by one in array


class Code{
public static void main(String[] args) {
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
for (int i=0 ; i<[Link] ; i++){
arr[i]++;
}
[Link]("Array elements : ");
for (int x : arr){
[Link](x + " ");
}
}
}

Contact : 911 955 6789 132


[Link]

Java - Swap First and Last location elements in array


class Code {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int n = [Link];
int temp = arr[0];
arr[0] = arr[n-1];
arr[n-1] = temp;
[Link]("Array elements : ");
for (int x : arr)
A
{
M
[Link](x);
E
}
E
}
R
}
P
E
Program to print even numbers of array using for each loop: T
class Code
{ T
public static void main(String[] args){ E
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5}; C
[Link]("Even numbers of array : "); H
for (int x : arr){ N
if(x%2==0) O
[Link](x); L
O
}
G
}
I
}
E
S
Program to display odd numbers in the array using for loop
class Code
{
public static void main(String[] args) {
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
[Link]("Odd numbers of array using for loop : ");
for (int i=0 ; i<[Link] ; i++){
if(arr[i]%2!=0)
[Link](arr[i]);
}
}
}

Contact : 911 955 6789 133


[Link]

Program to find the sum of even numbers in the array:


class Code{
public static void main(String[] args) {
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
int sum=0;
for (int i=0 ; i<[Link] ; i++){
if(arr[i]%2==0)
sum=sum+arr[i];
}
[Link]("Sum of even numbers : " + sum);
}
}

Java – Read Array Size and create object:


import [Link];
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Array created with specified length");
}
}

Java – Read elements into array


import [Link];
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter " + n + " elements : ");
for (int i=0 ; i<n ; i++){
arr[i] = [Link]();
}
[Link]("Array elements are : ");
for (int x : arr)
[Link](x + " ");
}
}

Contact : 911 955 6789 134


[Link]

Program to find the smallest element in the array:


class Code
{
public static void main(String[] args)
{
int[] arr = {4, 2, 8, 1, 9, 6, 3, 7};
int small = arr[0];
for (int i=1 ; i<[Link] ; i++)
{
if(arr[i] < small)
small = arr[i];
}
[Link]("Smallest element is : " + small);
}
}

Java - Program to increase each value by 1 in array


class Code
{
public static void main(String[] args) {
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
for (int i=0 ; i<[Link] ; i++){
arr[i]++;
}
[Link]("Array elements : ");
for (int x : arr){
[Link](x + " ");
}
}
}

Java - Program to print square value of each number in the array


class Code
{
public static void main(String[] args)
{
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
[Link]("Square values : ");
for (int x : arr){
[Link](x + " square is : " + (x*x));
}
}
}

Contact : 911 955 6789 135


[Link]

Java - Print multiplication tables of each number in the array


class Code
{
public static void main(String[] args) {
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
for (int i=0 ; i<[Link] ; i++){
int n = arr[i];
[Link]("Table - " + n);
for (int j=1 ; j<=10 ; j++){
[Link](n + " x " + j + " = " + (n*j));
}
}
}
}

Java - Print only prime numbers in the given array


class Code
{
public static void main(String[] args)
{
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
for (int i=0 ; i<[Link] ; i++){
int n = arr[i];
int count=0;
for (int j=1 ; j<=n ; j++){
if(n%j==0)
count++;
}
if(count==2)
[Link](n + " is prime");
}
}
}

Program to print only perfect numbers in the given array


class Code
{
public static void main(String[] args)
{
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
for (int i=0 ; i<[Link] ; i++)
{

Contact : 911 955 6789 136


[Link]

int n = arr[i];
int sum=0;
for (int j=1 ; j<n ; j++)
{
if(n%j==0)
sum=sum+j;
}
if(n==sum)
[Link](n + " is perfect");
}
}
}

Check specified element is duplicated or not in Array:


import [Link];
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
int[] arr = {4, 2, 8, 1, 9, 6, 2, 7};
[Link]("Enter element : ");
int ele = [Link]();
int count=0;
for (int i=0 ; i<[Link] ; i++){
if(arr[i] == ele)
count++;
}
if(count==0)
[Link]("Element Not found");
else if(count==1)
[Link]("Element not duplicated");
else
[Link]("Duplicated element");
}
}

Java – swap array elements of specified locations:


import [Link];
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
int n = [Link];

Contact : 911 955 6789 137


[Link]

[Link]("Enter two locations from 0 to " + (n-1));


int a = [Link]();
int b = [Link]();
[Link]("Before swap : ");
for (int x : arr){
[Link](x + " ");
}
[Link]();

int temp = arr[a];


arr[a] = arr[b];
arr[b] = temp;
A
[Link]("After swap : "); M
for (int x : arr){ E
[Link](x + " "); E
} R
P
[Link]();
E
}
T
}
T
Java – Reverse the elements in array: E
class Code{ C
public static void main(String[] args) H
{ N
int[] arr = {10, 20, 30, 40, 50}; O
int i=0; L
int j=[Link]-1; O
while(i<j) G
{ I
E
int temp = arr[i];
S
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
[Link]("Reverse array is : ");
for (int x : arr){
[Link](x);
}
}
}

Contact : 911 955 6789 138


[Link]

Program to Swap the First even number and Last odd number in the array:
class Code{
public static void main(String[] args) {
int[] arr = {5, 2, 3, 8, 1, 4, 6};
int i, j;
for (i=0 ; i<[Link] ; i++){
if(arr[i]%2==0)
break;
}
for (j=[Link]-1 ; j>=0 ; j--){
if(arr[j]%2!=0)
break;
}
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
[Link]("Array after swap : ");
for (i=0 ; i<[Link] ; i++){
[Link](arr[i]);
}
}
}

Program to Merge 2 arrays:


import [Link];
class Code {
public static void main(String[] args)
{
int[] a1 = {10,20,30,40};
int[] a2 = {50,60,70};
int[] merge = new int[[Link]+[Link]];
[Link]("a1 array : " + [Link](a1));
[Link]("a2 array : " + [Link](a2));
for (int i=0 ; i<[Link] ; i++){
merge[i] = a1[i];
}
for (int i=0 ; i<[Link] ; i++){
merge[[Link]+i] = a2[i];
}
[Link]("Merged array : " + [Link](merge));

}
}

Contact : 911 955 6789 139


[Link]

Program to search for an element in the array:


Linear Search:
• Linear search algorithm is used to search an element in the array.
• It is index based searching.
• It starts from index 0 and continues searching upto last index.
• If element found, it display “Element found @ location” else display error message that
“Element not found”

import [Link] ;
class Demo
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int arr[ ] = new int[n];

[Link]("Enter " + n + " values : ");


for (int i=0 ; i<n ; i++)
{
arr[i] = [Link]();
}

[Link]("Enter element to search : ");


int ele = [Link]();
boolean found=false;
for (int i=0 ; i<n ; i++)
{
if(arr[i] == ele)
{
[Link]("Element found at location : " + i);
found = true;
break;
}
}
if(!found)
[Link]("Element not found");
}
}

Contact : 911 955 6789 140


[Link]

Program to pass array as parameter to method.


class Code
{
public static void main(String[] args)
{
int[] arr = {10, 20, 30, 40, 50};
[Link](arr);
}

static void display(int[] x)


{
[Link]("Array elements : ");
for (int i=0 ; i<[Link] ; i++)
{
[Link](x[i]);
}
}
}

Program to return an array from the method


class Code
{
public static void main(String[] args)
{
int[] x = [Link]();
[Link]("Array elements : ");
for (int i=0 ; i<[Link] ; i++)
{
[Link](x[i]);
}
}

static int[] getArray()


{
int[] arr = {10, 20, 30, 40, 50};
return arr;
}
}

Contact : 911 955 6789 141


[Link]

Arrays Class in Java

Arrays class:
• Java library class belongs to [Link] package.
• Arrays class providing set of method to process array elements.
• Some of the methods as follows.

static void sort(int[] a) Sorts the specified array into ascending numerical order.
static String toString(int[] a) Returns a string representation of the contents of the
specified array.
static <T> List<T> asList(T... a) Returns a fixed-size list backed by the specified array.
A
M
static int binarySearch(int[] a, int Searches the specified array of ints for the specified
E
key) value using the binary search algorithm
E
static int[] copyOfRange(int[] Copies the specified range of the specified array into a
R
original, int from, int to) new array.
P
static boolean equals(int[] a, int[] Returns true if the two specified arrays of ints are equal
E
a2) to one another.
T
static void fill(int[] a, int val) Assigns the specified int value to each element of the
specified array of ints. T
E
Sort array elements and display as String: C
import [Link]; H
import [Link]; N
class SortArray O
{ L
public static void main(String[] args) O
G
{
I
Random rand = new Random();
E
int arr[] = new int[5];
S
for(int i=0 ; i<5 ; i++)
{
arr[i] = [Link](100);
}
[Link]("Before sort : " + [Link](arr));

[Link](arr);
[Link]("After sort : " + [Link](arr));
}
}

Contact : 911 955 6789 142


[Link]

Search element using [Link]():


Binary Search:
• Another searching algorithm to search an element in the array.
• In binary search, we find the mid element of array and compare with the element to be
searched. If element matches, we return mid location as element location.
• If the element is less than mid location element, search continues in the left side array of
mid location.
• If the element is greater than mid location element, search continues in right side
[Link] process continues with iterator until element found.

import [Link] ;
import [Link] ;
class Search
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int arr[ ] = new int[n];

[Link]("Enter " + n + " values : ");


for (int i=0 ; i<n ; i++)
{
arr[i] = [Link]();
}

[Link]("Enter element to be searched : ");


int key = [Link]();

int result = [Link](arr,key);


if (result < 0)
[Link]("Element is not found!");
else
[Link]("Element is found at index : " + result);
}
}

Contact : 911 955 6789 143


[Link]

Binary Search without library method:


import [Link] ;
class Demo
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int arr[ ] = new int[n];

[Link]("Enter " + n + " values : ");


for (int i=0 ; i<n ; i++)
{
arr[i] = [Link]();
}
[Link]("Enter element to search : ");
int ele = [Link]();

boolean found=false;
int low=0;
int high=n-1;
while(low <= high)
{
int mid = (low+high)/2;
if(ele < arr[mid])
high = mid-1;
else if(ele > arr[mid])
low = mid+1;
else if(ele == arr[mid]){
[Link]("Element found at location : " + mid);
found = true;
break;
}
}
if(!found)
[Link]("Element not found");
}
}

Contact : 911 955 6789 144


[Link]

Bubble sort:
• Sorting is the concept of arranging elements in the array either in ascending order or in
descending order.
• To sort the array, n number of algorithms is given such as Bubble Sort, Insertion Sort,
and Selection Sort and so on.
• Bubble sort is a simple sorting technique where elements can be sorted using their
index.

import [Link] ;
class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter array size : ");
int n = [Link]();
int arr[ ] = new int[n];

[Link]("Enter " + n + " values : ");


for (int i=0 ; i<n ; i++)
arr[i] = [Link]();

[Link]("Before sort : ");


for(int ele : arr){
[Link](ele + "\t");
}
[Link]();

for (int i=0 ; i<n-1 ; i++){


for (int j=0 ; j<n-1-i ; j++ ){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
[Link]("After sort : ");
for(int ele : arr){
[Link](ele + "\t");
}
[Link]();
}
}

Contact : 911 955 6789 145


[Link]

Program to create Array with only even numbers from the given array:
class Code
{
public static void main(String[] args) {
int[] arr = {5, 2, 3, 8, 1, 4, 6, 7, 9};
int count=0;
for (int i=0 ; i<[Link] ; i++){
if(arr[i]%2==0)
count++;
}
int[] evens = new int[count];
int j=0;
for (int i=0 ; i<[Link] ; i++){
if(arr[i]%2==0){
evens[j]=arr[i];
j++;
} A
} M
E
[Link]("Even numbers array : ");
E
for (int i=0 ; i<[Link] ; i++){
R
[Link](evens[i]);
P
}
E
}
T
}
T
Program to divide the given array into 2 arrays with even numbers and odd numbers: E
class Code C
{ H
public static void main(String[] args) N
{ O
int[] arr = {5, 2, 3, 8, 1, 4, 6, 7, 9}; L
int count=0; O
for (int i=0 ; i<[Link] ; i++) G
{ I
if(arr[i]%2==0) E
count++; S
}
int[] evens = new int[count];
int[] odds = new int[[Link]-count];
int x=0, y=0;
for (int i=0 ; i<[Link] ; i++)
{
if(arr[i]%2==0)

Contact : 911 955 6789 146


[Link]

{
evens[x]=arr[i];
x++;
}
else
{
odds[y]=arr[i];
y++;
}
}
[Link]("Even numbers array : ");
for (int i=0 ; i<[Link] ; i++)
{
[Link](evens[i]);
}
[Link]("Odd numbers array : ");
for (int i=0 ; i<[Link] ; i++)
{
[Link](odds[i]);
}
}
}

Program to display pair of elements in array whose sum equals to 10:


class Code
{
public static void main(String[] args)
{
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5};
int n=8;
for (int i=0 ; i<n-1 ; i++)
{
for (int j=i+1 ; j<n ; j++)
{
if(arr[i]+arr[j]==10)
[Link]("("+arr[i]+","+arr[j]+")");
}
}
}
}

Contact : 911 955 6789 147


[Link]

Program to replace all duplicates with 0:


class Code{
public static void main(String[] args){
int[] arr = {6, 2, 3, 1, 6, 7, 6, 3, 6, 2, 3, 2, 1};
for (int i=0 ; i<[Link]-1 ; i++)
{
for (int j=i+1 ; j<[Link] ; j++)
{
if(arr[i]==arr[j])
arr[j]=0;
}
}
[Link]("Final array is : ");
for (int x : arr)
[Link](x);
}
}

Program to display pair of elements whose sum is 10 (contains duplicates in array):


class Code
{
public static void main(String[] args)
{
int[] arr = {6, 3, 9, 1, 2, 8, 4, 5, 7, 6, 2, 9, 1, 4};
int n=14;
for (int i=0 ; i<[Link]-1 ; i++)
{
for (int j=i+1 ; j<[Link] ; j++)
{
if(arr[i]==arr[j])
arr[j]=0;
}
for (int k=i+1 ; k<n ; k++)
{
if(arr[i]+arr[k]==10)
{
[Link]("("+arr[i]+","+arr[k]+")");
break;
}
}
}
}
}

Contact : 911 955 6789 148


[Link]

Program to find the common elements from 2 arrays.


import [Link];
class Code{
public static void main(String[] args) {
int[] a1 = {1, 2, 5, 5, 8, 9, 5, 7, 10};
int[] a2 = {3, 6, 15, 6, 5, 4, 7, 2, 1};
[Link]("Array1 : " + [Link](a1));
[Link]("Array2 : " + [Link](a2));
for (int i=0 ; i<[Link] ; i++)
{
for (int j=0 ; j<[Link] ; j++)
{
if(a1[i] == (a2[j]))
{
[Link]("Common element is : " + (a1[i]));
a2[j]=0;
}
}
}
}
}

Program to find the duplicates in the array


import [Link];
class Code{
public static void main(String[] args) {
int[] arr = {1, 2, 5, 5, 8, 9, 2, 5, 7, 1, 10, 1, 2};
[Link]("Array : " + [Link](arr));
for (int i=0 ; i<[Link] ; i++)
{
for (int j=i+1 ; j<[Link] ; j++)
{
if(arr[i]!=0 && (arr[i] == arr[j]))
{
[Link](arr[i] + " is duplicated");
arr[j]=0;
break;
}
}
}
}
}

Contact : 911 955 6789 149


[Link]

Program to find the second largest element in the given array:


import [Link];
class Code{
public static void main(String[] args) {
int[] arr = {4, 7, 2, 9, 3, 8, 6, 1, 5};
[Link]("Array : " + [Link](arr));
int f=arr[0];
int s=arr[1];
if(f<s){
int t=f;
f=s;
s=t;
}
for (int i=2 ; i<[Link] ; i++){
if(arr[i]>f){
s=f;
f=arr[i];
}
else if(arr[i]<f && arr[i]>s){
s=arr[i];
}
}
[Link]("First Largest : " + f);
[Link]("Second Largest : " + s);
}
}

Program to arrange even numbers to left side and odd numbers to right side of Array:
import [Link];
class Code {
public static void main(String[] args) {
int[] arr = {7, 2, 9, 8, 4, 1, 2, 6, 5};
[Link]("Given array : " + [Link](arr));
int i=0;
int j=[Link]-1;
while(i<j){
while(true){
if(arr[i]%2!=0)
break;
else
i++;
}
while(true){

Contact : 911 955 6789 150


[Link]

if(arr[j]%2==0)
break;
else
j--;
}
if(i<j){
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
i++; A
j--; M
} E
[Link]("Result array : " + [Link](arr)); E
} R
} P
E
Program to check 2 arrays are equal or not: T
import [Link];
T
class Code
E
{
C
public static void main(String[] args) {
H
int[] a1 = {3, 4, 5, 6, 7};
N
int[] a2 = {3, 4, 5, 6, 7};
O
String s1 = [Link](a1); L
String s2 = [Link](a2); O
if([Link](s2)) G
[Link]("Arrays are equal"); I
else E
[Link]("Arrays are not equal"); S
}
}

Program to test the equality of 2 arrays without using pre-defined method.


import [Link];
class Code
{
public static void main(String[] args)
{
int[] a1 = {3, 4, 5, 6, 7};
int[] a2 = {3, 4, 5, 6, 7};
if([Link] == [Link]){
boolean equal=true;

Contact : 911 955 6789 151


[Link]

for (int i=0 ; i<[Link] ; i++)


{
if(a1[i] != a2[i])
equal=false;
}
if(equal)
[Link]("Arrays are equal");
else
[Link]("Arrays are not equal");
}
else
[Link]("Arrays are not equal");
}
}

Program to find the missing number in the given array.


class Code
{
public static void main(String[] args)
{
int[] arr = {2, 3, 4, 5, 7, 8, 9};
int start=arr[0];
for (int i=1 ; i<[Link] ; i++)
{
if(++start != arr[i])
{
[Link]("Missing number is : " + start);
break;
}
}
}
}

Program to move all zero to the end of given array


import [Link];
class Code
{
public static void main(String[] args)
{
int[] arr = {9, 0, 0, 4, 0, 3, 0, 2, 0};
[Link]("Given array : " + [Link](arr));
int i=0;
int j=[Link]-1;

Contact : 911 955 6789 152


[Link]

while(i<j)
{
while(true)
{
if(arr[i]==0)
break;
else
i++;
}
while(true)
{
if(arr[j]!=0)
break;
else
j--;
}
if(i<j)
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
i++;
j--;
}
[Link]("Resultant array: " + [Link](arr));
}
}

Program to find the largest difference of Array elements:


import [Link];
class Code
{
public static void main(String[] args)
{
int[] arr = {5, 7, 3, 8, 6, 9, 4};
[Link]("Given array : " + [Link](arr));
int small=arr[0];
int big=arr[0];
for (int i=1 ; i<[Link] ; i++)
{
if(small>arr[i])
small=arr[i];

Contact : 911 955 6789 153


[Link]

if(big<arr[i])
big=arr[i];
}
[Link]("Largest Difference between elements : " + (big-small));
}
}

Program to find the index difference between smallest and largest elements:
import [Link];
class Code
{
public static void main(String[] args)
{
int[] arr = {5, 7, 3, 8, 6, 9, 4};
[Link]("Given array : " + [Link](arr));
int small=arr[0];
int big=arr[0];
int x=0, y=0;
for (int i=1 ; i<[Link] ; i++)
{
if(small>arr[i])
{
small=arr[i];
x=i;
}
if(big<arr[i])
{
big=arr[i];
y=i;
}
}
[Link]("Index difference between Small and Large Elements : " +
[Link](x-y));
}
}

Program to check array contains only positive numbers or not


import [Link];
class Code
{
public static void main(String[] args)
{
int[] arr = {5, 7, 3, 8, 6, -4, 4};

Contact : 911 955 6789 154


[Link]

[Link]("Given array : " + [Link](arr));


boolean found=false;
for (int i=0 ; i<[Link] ; i++)
{
if(arr[i]<0)
{
found=true;
break; A
} M
} E
if(found) E
[Link]("Array has negative elements"); R
else P
[Link]("Array has only positive elements"); E
} T
}
T
E
Program to print leader elements in the array : All elements to its right must be smaller to
C
Leader element
H
import [Link];
N
class Code
O
{ L
public static void main(String[] args) O
{ G
int arr[] = {10, 9, 14, 23, 15, 0, 9}; I
int size = [Link]; E
for (int i=0 ; i<size ; i++) S
{
int j;
for (j=i+1 ; j<size ; j++)
{
if (arr[i] <= arr[j])
break;
}
if (j==size)
[Link](arr[i] + " ");
}
}
}

Contact : 911 955 6789 155


[Link]

Two Dimensional Arrays

Two-dimensional Array:
• 2D arrays are also called Matrix in java.
• 2D arrays are used to store and process information of 2-dimensional format (rows and
columns).

In Java, you can create a two-dimensional array using the following syntax:
dataType[][] arrayName = new dataType[rows][columns];

An example of creating a 2D array of integers with 3 rows and 4 columns:


int[][] matrix = new int[3][4];

You can also initialize the array with values right away:
int[][] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

Find the length of Array:


public class Logic
{
public static void main(String args[]) {
int A[][] = {{10,20,30},{40,50},{70,80}};
[Link]("Length of array : " + (A[0].length + A[1].length + A[2].length));

}
}

Display Total length of array:


public class Code
{
public static void main(String[] args)
{
int[][] arr = new int[2][3];
int rows = [Link];
int cols = arr[0].length;
[Link]("Rows size = " + rows);
[Link]("Columns size = " + cols);
[Link]("Total length = " + (rows * cols));
}
}

Contact : 911 955 6789 156


[Link]

Processing 2D array: Display default values of 2 dimensional array


public class Logic
{
public static void main(String args[]) {
int arr[][] = new int[3][3];
[Link]("Elements are : ");
for (int i=0 ; i<3 ; i++)
{
for(int j=0 ; j<3 ; j++)
{
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}

We can initialize the 2D array directly using assignment operator as follows:


public class Logic
{
public static void main(String args[])
{
int arr[][] = { {1,2,3}, {3,4,5}, {5,6,7}};
[Link]("Elements are : ");
for (int i=0 ; i<3 ; i++)
{
for(int j=0 ; j<3 ; j++)
{
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}

Construct 2D matrix using end user input data:


import [Link];
public class Logic
{
public static void main(String args[])
{
Scanner scan = new Scanner([Link]);
[Link]("Row size : ");

Contact : 911 955 6789 157


[Link]

int m = [Link]();
[Link]("Column size : ");
int n = [Link]();
int arr[][] = new int[m][n];
[Link]("Enter " + (m*n) + " elements : ");
for (int i=0 ; i<m ; i++){
for(int j=0 ; j<n ; j++){
arr[i][j] = [Link]();
}
}
[Link](m + "X" + n + " matrix is : ");
for (int i=0 ; i<m ; i++){
for(int j=0 ; j<n ; j++){
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}

Jagged Array Length:


• In Java, a jagged array is an array of arrays, where each sub-array can have a different
length.
• To find the length of a jagged array, you need to iterate through its sub-arrays and sum
up the lengths of each sub-array.
public class Code
{
public static void main(String[] args)
{
int[][] jaggedArray =
{
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};
int totalLength = 0;
for (int i = 0; i < [Link]; i++) {
totalLength += jaggedArray[i].length;
}
[Link]("Total length of jagged array: " + totalLength);
}
}

Contact : 911 955 6789 158


[Link]

Display elements of Jagged array:


public class Display
{
public static void main(String[] args)
{
int[][] jaggedArray =
{
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};

for (int i = 0; i < [Link]; i++)


{
for (int j = 0; j < jaggedArray[i].length; j++)
{
[Link](jaggedArray[i][j] + " ");
}
[Link]();
}
}
}

Sum of all elements in two dimensional array:


public class Sum
{
public static void main(String[] args) {
int[][] twoDimArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int sum = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < twoDimArray[i].length; j++) {
sum += twoDimArray[i][j];
}
}
[Link]("Sum of all elements: " + sum);
}
}

Contact : 911 955 6789 159


[Link]

Smallest element in 2 dimensional array:


public class Smallest
{
public static void main(String[] args)
{
int[][] twoDimArray = {
{9, 4, 7},
{2, 8, 1},
{5, 3, 6}
A
};
M
E
int smallest = twoDimArray[0][0]; // Initialize with the first element
E
R
for (int i = 0; i < [Link]; i++) {
P
for (int j = 0; j < twoDimArray[i].length; j++) {
E
if (twoDimArray[i][j] < smallest) {
T
smallest = twoDimArray[i][j];
} T
} E
} C
[Link]("Smallest element: " + smallest); H
} N
} O
L
Each row sum in two dimensional array: O
public class RowSum{ G
I
public static void main(String[] args) {
E
int[][] twoDimArray = {
S
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

for (int i = 0; i < [Link]; i++) {


int rowSum = 0;
for (int j = 0; j < twoDimArray[i].length; j++) {
rowSum += twoDimArray[i][j];
}
[Link]("Sum of elements in row " + (i + 1) + ": " + rowSum);
}
}
}

Contact : 911 955 6789 160


[Link]

Find the Maximum row sum in 2 dimensional array:


public class MaxRowSum
{
public static void main(String[] args) {
int[][] twoDimArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int maxRowSum = 0;

int rowIndexWithMaxSum = -1;

for (int i = 0; i < [Link]; i++)


{
int rowSum = 0;
for (int j = 0; j < twoDimArray[i].length; j++)
{
rowSum += twoDimArray[i][j];
}
if (rowSum > maxRowSum)
{
maxRowSum = rowSum;
rowIndexWithMaxSum = i;
}
}

[Link]("Row with maximum sum: " + (rowIndexWithMaxSum + 1));


[Link]("Maximum sum: " + maxRowSum);
}
}

Addition of 2 matrixes:
import [Link];
public class Logic{
public static void main(String args[]) {
Scanner scan = new Scanner([Link]);
int A[][] = new int[2][2];
int B[][] = new int[2][2];
int C[][] = new int[2][2];
[Link]("Enter 4 elements into A : ");
for (int i=0 ; i<2 ; i++)

Contact : 911 955 6789 161


[Link]

for(int j=0 ; j<2 ; j++)


A[i][j] = [Link]();

[Link]("Enter 4 elements into B : ");


for (int i=0 ; i<2 ; i++)
for(int j=0 ; j<2 ; j++)
B[i][j] = [Link]();

for (int i=0 ; i<2 ; i++)


for(int j=0 ; j<2 ; j++)
C[i][j] = A[i][j] +B[i][j] ;

[Link]("Added matrix is : ");


for (int i=0 ; i<2 ; i++){
for(int j=0 ; j<2 ; j++){
[Link](C[i][j] + " ");
}
[Link]();
}
}
}

Matrix multiplication program:


import [Link];
public class Logic{
public static void main(String args[]) {
Scanner scan = new Scanner([Link]);
int A[][] = new int[2][2];
int B[][] = new int[2][2];
int C[][] = new int[2][2];
[Link]("Enter 4 elements into A : ");
for (int i=0 ; i<2 ; i++)
for(int j=0 ; j<2 ; j++)
A[i][j] = [Link]();
[Link]("Enter 4 elements into B : ");
for (int i=0 ; i<2 ; i++)
for(int j=0 ; j<2 ; j++)
B[i][j] = [Link]();
for (int i=0 ; i<2 ; i++){
for(int j=0 ; j<2 ; j++){
C[i][j] = 0;
for (int k=0 ; k<2 ; k++ ){
C[i][j] = C[i][j] + A[i][k] * B[k][j];

Contact : 911 955 6789 162


[Link]

}
}
}
[Link]("Mutliplied matrix is : ");
for (int i=0 ; i<2 ; i++){
for(int j=0 ; j<2 ; j++){
[Link](C[i][j] + " ");
}
[Link]();
}
}
}

Transpose of a Matrix:
public class Logic{
public static void main(String args[]) {
int A[][] = {{10,20,30},{40,50,60},{70,80,90}};
[Link]("Input Matrix is : ");
for (int i=0 ; i<3 ; i++){
for(int j=0 ; j<3 ; j++){
[Link](A[i][j] + " ");
}
[Link]();
}
for (int i=0 ; i<3 ; i++){
for(int j=0 ; j<3 ; j++){
if(i<j){
int temp = A[i][j];
A[i][j] = A[j][i];
A[j][i] = temp;
}
}
}
[Link]("Transposed Matrix is : ");
for (int i=0 ; i<3 ; i++){
for(int j=0 ; j<3 ; j++){
[Link](A[i][j] + " ");
}
[Link]();
}
}
}

Contact : 911 955 6789 163


[Link]

Program to sort Each row in given Matrix:


class Code
{
public static void main(String[] args)
{
int[][] a = { {5, 3, 7, 2, 8}, {9, 2, 8, 4, 6}, {6, 1, 9, 7, 3} };
[Link]("Given Matris is : ");
for(int i=0 ; i<[Link] ; i++)
{
for(int j=0; j<a[i].length ; j++)
{
[Link](a[i][j]+ " ");
}
[Link]();
}
for (int i=0 ; i<[Link] ; i++)
{
for (int j=0 ; j<a[i].length-1 ; j++)
{
for (int k=0 ; k<a[i].length-1 ; k++)
{
if(a[i][k] > a[i][k+1]){
int temp = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = temp;
}
}
}
}
[Link]("Sorted Matris is : ");
for(int i=0 ; i<[Link] ; i++)
{
for(int j=0; j<a[i].length ; j++)
{
[Link](a[i][j]+ " ");
}
[Link]();
}
}
}

Contact : 911 955 6789 164


[Link]

Strings in Java

What is Character?
Character is any symbol represent with single quotes (alphabets, digits, symbols);
Example: A-Z , a-z, 0-9, $, #, @, !, ^, &, ......

Identify characters from below list


C Ch $ 9 Z xyz

A
M
ASCII value table E
A-65 a-97 0-48 #-35 E
B-66 b-98 1-49 $-36 R
... .... .. ... P
... .... .. ... E
T
... .... .. ...
Z-90 z-122 9-57 ... T
E
Identify vowels from below? C
X A V I O U R H
N
O
L
O
Can we assign character to integer variable?
G
We can directly assign character to integer called “Implicit Cast”. The ASCII value will
I
store into integer variable.
E
S
Auto conversion Manually casting
char ch='a'; char ch='a';
int x=ch; int x=(int)ch;
print(x); print(x);

How to convert integer into character?


We cannot assign integer value directly to character variable. We must explicitly cast.

Direct assignment : Error Manually casting


int x=100; int x=100;
char ch=x; char ch=(char)x;
print(ch); //Error print(ch); //d

Contact : 911 955 6789 165


[Link]

Converting Upper case to lower case character as follows:


char ch='M';
ch = (char)(ch+32);
print(ch);

Converting Lower case to Upper case character as follows:


char ch='a';
ch = (char)(ch-32);
print(ch);

Convert Character to Digit:


char ch='5';
int x=ch-48;
print(x); // 5

Java Program to Check the character is vowel or not:


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter character : ");
char ch = [Link]().charAt(0);

if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')


[Link]("It is vowel");
else
[Link]("It is not a vowel");
}
}

Java program to check the given character is digit or not


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);

Contact : 911 955 6789 166


[Link]

[Link]("Enter character : ");


char ch = [Link]().charAt(0);

if(ch>='0' && ch<='9')


[Link]("It is Digit");
else
[Link]("It is not a Digit");
}
}

Java Program to check the character is upper case alphabet or not


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);

[Link]("Enter character : ");


char ch = [Link]().charAt(0);

if(ch>='A' && ch<='Z')


[Link]("It is Upper case Alphabet");
else
[Link]("It is not an Upper case Alphabet");
}
}

Java program to check the character is Alphabet or not


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);

[Link]("Enter character : ");


char ch = [Link]().charAt(0);

Contact : 911 955 6789 167


[Link]

if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))


[Link]("It is an Alphabet");
else
[Link]("It is not an Alphabet");
}
}

Java Program to check the character is special symbol or not


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter character : ");
char ch = [Link]().charAt(0);

if(!((ch>='A' && ch<='Z') || (ch>='a' && ch<='z') || (ch>='0' && ch<='9')))


[Link]("It is a symbol");
else
[Link]("It is not a symbol");
}
}

Java Program to display ASCII value of given character


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter character : ");
char ch = [Link]().charAt(0);
int val = ch;
[Link]("ASCII value is : " + val);
}
}

Contact : 911 955 6789 168


[Link]

Java Program to convert upper case character to lower case character


import [Link];
class Code
{
public static void main(String[] args)
{
Scanner scan = new Scanner([Link]);
[Link]("Enter Upper case character : ");
char ch = [Link]().charAt(0); A
if (ch >= 'A' && ch <= 'Z') M
{ E
ch = (char)(ch+32); E
R
[Link]("Lower case character is : " + ch);
P
} E
else T
{
T
[Link]("Invalid Character Entered");
E
}
C
} H
} N
O
Display the length of given character array L
class Code O
{ G
public static void main(String[] args) I
{ E
char[] arr = {'a' , 'x' , '3' , 'm', 'x', '@' , 'p', '7', 'm', '4', '$'}; S
int n = [Link];
[Link]("Length of Array is : " + n);
}
}

Display First character of character array:


public class Code
{
public static void main(String[] args)
{
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};
if ([Link] > 0)
{
char firstChar = arr[0];

Contact : 911 955 6789 169


[Link]

char lastChar = arr[[Link] - 1];

[Link]("First character: " + firstChar);


[Link]("Last character: " + lastChar);
}
else
[Link]("The array is empty.");
}
}

Check the First character is Alphabet or Not:


public class Code
{
public static void main(String[] args)
{
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};
if ([Link] > 0)
{
char ch = arr[0];
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
[Link]("The first character is an alphabet.");
else
[Link]("The first character is not an alphabet.");
}
else
[Link]("The array is empty.");
}
}

Display all characters in the array:


public class Code
{
public static void main(String[] args)
{
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};
[Link]("All characters in the array:");
for (int i = 0; i < [Link]; i++) {
[Link](arr[i] + " ");
}
}
}

Contact : 911 955 6789 170


[Link]

Display in reverse order:


public class DisplayReverse
{
public static void main(String[] args)
{
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};
[Link]("Characters in reverse order:");
for (int i = [Link] - 1; i >= 0; i--)
{
[Link](arr[i] + " ");
}
}
}

Display only alphabets in the array:


public class DisplayAlphabets
{
public static void main(String[] args)
{
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};

[Link]("Alphabetic characters in the array:");


for (int i = 0; i < [Link]; i++)
{
if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z'))
{
[Link](arr[i] + " ");
}
}
}
}

Count alphabets, digits and symbols in the array:


public class Count
{
public static void main(String[] args)
{
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};

int alphabets = 0;
int digits = 0;
int symbols = 0;

Contact : 911 955 6789 171


[Link]

for (int i = 0; i < [Link]; i++)


{
if ((arr[i] >= 'a' && arr[i] <= 'z') || (arr[i] >= 'A' && arr[i] <= 'Z'))
alphabets++;
else if (arr[i] >= '0' && arr[i] <= '9')
digits++;
else
symbols++;
}
[Link]("Alphabet count: " + alphabets);
[Link]("Digit count: " + digits);
[Link]("Symbol count: " + symbols);
}
}

Display Sum of Digits:


public class SumOfDigits
{
public static void main(String[] args)
{
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};
int digitSum = 0;
for (int i = 0; i < [Link]; i++)
{
if (arr[i] >= '0' && arr[i] <= '9')
digitSum += arr[i] - '0';
}
[Link]("Sum of digits: " + digitSum);
}
}

Display array using for each:


public class Display
{
public static void main(String[] args) {
char[] arr = {'a', 'x', '3', 'm', 'x', '@', 'p', '7', 'm', '4', '$'};
[Link]("Characters in the array:");
for (char c : arr) {
[Link](c + " ");
}
}
}

Contact : 911 955 6789 172


[Link]

Program display length of String:


class Code {
public static void main(String[] args)
{
String s = "Coding";
[Link]("Length is : " + [Link]());
}
}

Program to display First and Last characters of String:


class Code
{
public static void main(String[] args)
{
String s = "Coding";
[Link]("First Character : " + [Link](0));
[Link]("Last Character : " + [Link]([Link]()-1));
}
}

Convert String object into character array: String class providing toCharArray() method for
this conversion.
class Code {
public static void main(String[] args) {
String str = "Coding";
char[] arr = [Link]();
[Link]("Elements :");
for(char ele : arr)
[Link](ele);
}
}

Program to print String character by character:


class Code {
public static void main(String[] args) {
String s = "Coding";
for (int i=0 ; i<=[Link]()-1 ; i++){
[Link]([Link](i));
}
}
}

Contact : 911 955 6789 173


[Link]

Program to print the String in reverse order:


class Code {
public static void main(String[] args) {
String s = "Coding";
for (int i=[Link]()-1 ; i>=0 ; i--){
[Link]([Link](i));
}
}
}

Program to check the 2 strings equal or not: A


class Code { M
public static void main(String[] args) { E
String s1="Coding"; E
String s2="Code"; R
if([Link](s2)) P
[Link]("Equal"); E
else T
[Link]("Not equal");
T
}
E
}
C
H
Program to create reverse string from given string:
N
class Code { O
public static void main(String[] args) { L
String s = "Coding"; O
String rev=""; G
for (int i=[Link]()-1 ; i>=0 ; i--){ I
rev=rev+[Link](i); E
} S
[Link]("Reverse string is : " + rev);
}
}

Program to count Alphabets, Digits and Symbols in the given String


class Code {
public static void main(String[] args) {
String s = "Coding@365";
int c1=0, c2=0, c3=0;
for (int i=0 ; i<=[Link]()-1 ; i++){
char ch=[Link](i);
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))

Contact : 911 955 6789 174


[Link]

c1++;
else if(ch>='0'&&ch<='9')
c2++;
else
c3++;
}
[Link]("Alphabets : " + c1);
[Link]("Digits : " + c2);
[Link]("Symbols : " + c3);
}
}

How to check if a string is a palindrome?


class Code {
public static void main(String[] args) {
String line = "madam";
String rev = reverseString(line);
if ([Link](rev))
[Link]("Palindrome String");
else
[Link]("Not a palindrome String.");
}
public static String reverseString(String str) {
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--)
{
rev += [Link](i);
}
return rev;
}
}

Program to find the sum of digits in the given String:


class Code {
public static void main(String[] args) {
String s = "Coding@365";
int sum=0;
for (int i=0 ; i<=[Link]()-1 ; i++){
char ch=[Link](i);
if(ch>='0'&&ch<='9')
sum = sum + (int)(ch-48);

Contact : 911 955 6789 175


[Link]

}
[Link]("String is : " + s);
[Link]("Sum of Digits : " + sum);
}
}

Display the ASCII values of characters in the given String:


class Code {
public static void main(String[] args) {
String s = "Coding";
[Link](s + " ASCII values : ");
for (int i=0 ; i<[Link]() ; i++){
char ch=[Link](i);
[Link](ch + " : " + (int)ch);
}
}
}

Program to convert upper case string to lower case string:


class Code {
public static void main(String[] args) {
String s = "CoDiNg@365";
String res="";
for (int i=0 ; i<[Link]() ; i++){
char ch=[Link](i);
if(ch>='A' && ch<='Z')
res=res+(char)(ch+32);
else
res=res+ch;
}
[Link]("Given string is : " + s);
[Link]("Lower case String : " + res);
}
}

Program to display highest digit in the given String:


class Code
{
public static void main(String[] args) {
String s = "coding@365";
int n=0;
for (int i=0 ; i<[Link]() ; i++){
char ch=[Link](i);

Contact : 911 955 6789 176


[Link]

if(ch>='0'&&ch<='9'){
int x=(int)(ch-48);
if(x>n)
n=x;
}
}
[Link]("Higher digit in String : " + n);
}
}

Program to find the length of String array


class Code {
public static void main(String[] args) {
String[] arr = {"java", "jsp", "servlets", "hibernate", "springBoot"};
[Link]("Length of array : " + [Link]);
}
}

Program to display Strings from Array:


class Code {
public static void main(String[] args) {
String[] arr = {"java", "jsp", "servlets", "hibernate", "springBoot"};
for (int i=0 ; i<[Link] ; i++){
[Link](arr[i]);
}
}
}

Program to display the length of each String in the array:


class Code {
public static void main(String[] args) {
String[] arr = {"java", "jsp", "servlets", "hibernate", "springBoot"};
for (int i=0 ; i<[Link] ; i++){
String s = arr[i];
[Link](s + " : " + [Link]());
}
}
}

Contact : 911 955 6789 177


[Link]

Program to display First and Last characters of each string in the array:
class Code
{
public static void main(String[] args)
{
String[] arr = {"java", "jsp", "servlets", "hibernate", "springBoot"};
for (int i=0 ; i<[Link] ; i++){
String s = arr[i];
[Link](s+" : "+[Link](0)+","+[Link]([Link]()-1));
}
}
}

Program to display String array in reverse order:


class Code {
public static void main(String[] args) {
String[] arr = {"java", "jsp", "servlets", "hibernate", "springBoot"};
for (int i=[Link]-1 ; i>=0 ; i--){
[Link](arr[i]);
}
}
}

Program to display each String in reverse order from Array:


class Code {
public static void main(String[] args) {
String[] arr = {"java", "jsp", "servlets", "hibernate", "springBoot"};
for (int i=0 ; i<=[Link]-1 ; i++){
String s = arr[i];
for (int j=[Link]()-1 ; j>=0 ; j--){
[Link]([Link](j));
}
[Link]();
}
}
}

Contact : 911 955 6789 178


[Link]

Program to Split the String into Words:


class Code {
public static void main(String[] args){
String s = "This is core java test";
String[] arr = [Link](" ");
[Link]("String is : " + s);
[Link]("Words count : " + [Link]);
}
}

Program to count number of words in the given string without split() method.
class Code { A
public static void main(String[] args) { M
String s = "This is core java test"; E
int spaces=0; E
R
for (int i=0 ; i<[Link]() ; i++){
P
if([Link](i)==' ') E
spaces++; T
}
T
[Link]("String is : " + s); E
[Link]("Words count : " + (spaces+1)); C
} H
} N
O
L
Program to check two strings are Anagrams or not: O
We can say if two strings are an anagram of each other if they contain the same G
characters but at different orders. For example, army & mary I
E
import [Link]; S
class Code
{
public static void main(String[] args) {
String s1 = "army";
String s2 = "mary";
[Link]("Check Anagram or not : "+ isAnagram(s1, s2));
}
public static boolean isAnagram(String s1, String s2){
char[] a1 = [Link]().toCharArray();
char[] a2 = [Link]().toCharArray();

Contact : 911 955 6789 179


[Link]

[Link](a1);
[Link](a2);
return [Link](a1, a2);
}
}

Program to display longest word in the given String:


class Code
{
public static void main(String[] args) {
String s = "This is the Longest Sentense in Java";
String[] arr = [Link](" ");
int loc=0;
int h=arr[0].length();
for (int i=1 ; i<[Link] ; i++){
if(arr[i].length()>h){
h=arr[i].length();
loc=i;
}
}
[Link]("Longest Word is : " + arr[loc]);
}
}

Program to remove duplicates in the String:


class Code
{
public static void main(String[] args) {
String str = "aaabbababaacccbabdcccbdddbac";
String res = "";
for (int i=0 ; i<[Link]()-1 ; i++){
int j=0;
for (j=0 ; j<i ; j++){
if([Link](i)==[Link](j))
break;
}
if(j==i)
res = res + [Link](i);

Contact : 911 955 6789 180


[Link]

}
[Link]("Given String : " + str);
[Link]("Resultant string : " + res);
}
}

Program to print String permutations using Iterator(loop):


class Code
{
public static void main(String[] args) {
String str = "abc";
for (char x='a' ; x<='c' ; x++){
for (char y='a' ; y<='c' ; y++){
for (char z='a' ; z<='c' ; z++){
if(x!=y && y!=z && z!=x)
[Link](x+""+y+""+z);
}
}
}
}
}

Program to print String permutations using Recursion:


class Code {
public static void main(String[] args) {
String str = "ABC";
permutations("", str);
}
static void permutations(String s, String rem){
if (rem == "")
return;
if ([Link]() == 0)
[Link](s);
for (int i=0 ; i<[Link](); i++)
{
String n = s + [Link](i);
String nrem = [Link](0,i) + [Link](i + 1);

Contact : 911 955 6789 181


[Link]

permutations(n, nrem);
}
}
}

Program to remove spaces in String:


class Code {
public static void main(String[] args) {
String s = "This is a String";
s = [Link](" ", "");
[Link]("Result : " + s);
}
}

Program to remove spaces in String using regular expression:


class Code {
public static void main(String[] args) {
String s = "This is a String";
s = [Link]("\\s", "");
[Link]("Result : " + s);
}
}

Program to remove multiple spaces in the given string:


class Code {
public static void main(String[] args) {
String s = "This is a String";
s = [Link]("\\s+", " ");
[Link]("Result : " + s);
}
}

Program to remove multiple spaces in string without using library method:


class Code {
public static void main(String[] args) {
String s1 = "This is a String";
String s2 = "";

Contact : 911 955 6789 182


[Link]

for (int i=0 ; i<[Link]() ; i++){


char ch = [Link](i);
if(ch != ' ')
s2=s2+ch;
else{
if([Link](i+1) != ' ')
s2=s2+ch;
}
}
[Link]("Result : " + s2);
}
}

Program to print characters specified by the number of times:


Input: abc3e2f4
Expected output: abccceeffff
class Code {
public static void main(String[] args) {
String s = "abc3e2f4";
[Link]("Input : " + s);
[Link]("Output : ");
for (int i=0 ; i<[Link]() ; i++){
char ch = [Link](i);
char ch1 = [Link](i+1);
if(ch1>='0' && ch1<='9'){
for (int j=1 ; j<=(ch1-48) ; j++){
[Link](ch);
}
i++;
}
else
[Link](ch);
}
[Link]();
}
}

Contact : 911 955 6789 183


[Link]

Program to swap the side-by-side characters in the given string:


class Code {
public static void main(String[] args) {
String s1 = "abcde";
String s2 = "";
for (int i=0 ; i<[Link]() ; i+=2){
if(i+2 < [Link]())
s2 = s2 + [Link](i+1) + [Link](i);
else
s2 = s2 + [Link](i);
}
[Link]("Input String is : " + s1);
[Link]("Output String is : " + s2);
}
}

Program to remove duplicates in the given String:


class Code
{
public static void main(String[] args)
{
String s1 = "aaaabbaababaaabccccaaab";
String s2 = "";
for (int i=0 ; i<[Link]() ; i+=2){
if(![Link](""+[Link](i)))
s2 = s2 + [Link](i);
}
[Link]("Input String is : " + s1);
[Link]("Output String is : " + s2);
}
}

Program to check how many times the given sub string is present:
class Code
{
public static void main(String[] args) {
String s = "abcaabcaaabcabacabcaabcaabc";

Contact : 911 955 6789 184


[Link]

String[] arr = [Link]("abc");


[Link]("abc repeated : " + ([Link]) + " times");
}
}

Program to display the charaacter count in the given string:


Input : aaabbbaccccddacdd
Output : a-5, b-3, c-5, d-4
class Code
{
public static void main(String[] args) {
String s = "aaabbbaccccddacdd";
int[] arr = new int[256];
for (int i=0 ; i<[Link]() ; i++){
arr[(int)[Link](i)]++;
}
for (int i=0 ; i<256 ; i++){
if(arr[i]!=0){
[Link]((char)i + " : " + arr[i]);
}
}
}
}

Program to remove the sub string in the given String:


class Code
{
public static void main(String[] args) {
String s = "This is Core Java String";
[Link]("Given String : " + s);
s = [Link]("Core", "");
[Link]("After removing Sub string : " + s);
}
}

Contact : 911 955 6789 185


[Link]

Program to remove sub string in the given string without library method:
class Code
{
public static void main(String[] args)
{
String s1 = "This is Core Java String"; A
String s2 = ""; M
E
String sub = "Core";
E
int n=[Link](); R
for (int i=0 ; i<[Link]() ; i++) P
{ E
T
char ch = [Link](i);
if(ch != [Link](0)) T
E
{ C
s2 = s2 + ch; H
} N
O
else
L
{ O
String match = [Link](i, i+n); G
I
i=i+n;
E
} S
}
[Link]("Given String : " + s1);
[Link]("After removing Sub string : " + s2);
}
}

Contact : 911 955 6789 186

You might also like