Java Coding Book
Java Coding Book
Java Sparkles
(Complete Logical Programming Guide)
SRINIVAS GARAPATI
Contents
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 1
Practice @ www.onlinejavacompiler.com
Formatting Output
Introduction: It is important to format the output before display the results to end user.
Proper formatting makes the user more understandable about program results.
We always display results in String format. To format the output, we concatenate the
values such as int, char, double, boolean with messages (string type) as follows:
Syntax Example
int + int = int 10 + 20 = 30
String + String = String “Java” + “Book” = JavaBook
“10” + “20” = 1020
String + int = String “Book” + 1 = Book1
“123” + 456 = 123456
String + int + int = String “Sum = “ + 10 + 20 = Sum1020
String + (int + int) = String “Sum = “ + (10 + 20) = Sum30
String + double = String “Value = “ + 23.45 = Value = 23.45
String – int = Error
Practice Codes
class Pro1
{
public static void main(String[] args)
{
int a=10;
System.out.println("a value is : " + a);
}
}
Output: a value is : 10
class Pro2
{
public static void main(String[] args)
{
int a=10, b=20 ;
System.out.println(a + " , " + b);
}
}
Output : 10, 20
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 2
Practice @ www.onlinejavacompiler.com
class Pro3
{
public static void main(String[] args)
{
int a=10, b=20 ;
System.out.println("a = " + a + ", b = " + b);
}
}
Output : a=10, b=20
class Pro4
{
public static void main(String[] args) {
int a=10, b=20 ;
System.out.println("a = " + a + "\n" + "b = " + b);
}
}
Output: a=10
b=20
class Pro5
{
public static void main(String[] args) {
String name = "Amar" ;
String course = "Java" ;
String book = "Crackles";
System.out.println(name + " practice " + course + " from " + book);
}
}
Output: Amar practice Java from Crackles
class Pro6
{
public static void main(String[] args) {
int id = 101 ;
String name = "Amar" ;
double salary = 38000 ;
S.o.P("ID = " + id + "\nName = " + name + "\nSalary = " + salary);
}
}
Output: ID = 101
Name = Amar
Salary = 38000
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 3
Practice @ www.onlinejavacompiler.com
class Pro7
{
public static void main(String[] args) {
int amt = 3000;
int num = 102 ;
S.o.p("update account set bal = bal + " + amt + " where num = " + num);
}
}
Output: update account set bal = bal + 3000 where num=102
class Pro8
{
public static void main(String[] args) {
String lang = "Java";
System.out.println("'" + lang + "' tutorial");
}
}
Output: ‘Java’ tutorial
class Pro9
{
public static void main(String[] args) {
int id = 101 ;
String name = "amar" ;
double salary = 35000 ;
S.o.p("insert into employee values(" + id + ",'"+name+"',"+salary+")");
}
}
Output: insert into employee values(101, 'amar' , 35000)
class Pro10
{
public static void main(String[] args) {
String lang = "Java";
System.out.println("\"" + lang + "\" is awesome");
}
}
Output: “Java” is awesome
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 4
Practice @ www.onlinejavacompiler.com
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(System.in) while creating Scanner class object.
o Scanner scan = new Scanner(System.in);
We access all the methods using object reference name.
Reading integer value: nextInt() method read and returns an integer value
import java.util.Scanner;
class ReadInt {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter integer : ");
int n = scan.nextInt();
System.out.println("n value is : " + n);
}
}
Reading Boolean value: nextBoolean() method returns Boolean value that we entered
import java.util.Scanner;
class ReadBoolean {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter boolean value : ");
boolean b = scan.nextBoolean();
System.out.println("b value is : " + b);
}
}
Reading String: using next() method we can read single word string from the user
import java.util.Scanner;
class Code {
public static void main(String[] args) {
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 5
Practice @ www.onlinejavacompiler.com
Reading character: There is no method in Scanner class to read single character, hence we read
first character of String to read single character as follows
import java.util.Scanner;
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter character : ");
char ch = scan.next().charAt(0);
System.out.println("Input character is : " + ch);
}
}
Reading multi-word String: nextLine() method can read string includes spaces
import java.util.Scanner;
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter string with spaces : ");
String s = scan.nextLine();
System.out.println("Input String is : " + s);
}
}
InputMismatchException: A runtime error occurs if the user enters invalid input instead of
expected. We use exception handling process(try-catch) to display the error message.
import java.util.Scanner;
import java.util.InputMismatchException;
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 6
Practice @ www.onlinejavacompiler.com
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
try{
System.out.print("Enter integer : ");
int n = scan.nextInt();
System.out.println("Input n value : " + n);
}
catch (InputMismatchException e){
System.out.println("Exception : Please enter integer only");
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 7
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 8
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 9
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 10
Practice @ www.onlinejavacompiler.com
a = scan.nextInt();
b = scan.nextInt();
c = 180 - (a + b);
System.out.println("Third angle is : " + c);
}
}
Output: Enter two angles of triangle :
40
65
Third angle is : 75
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 11
Practice @ www.onlinejavacompiler.com
Display Amount based on Quantity: Accept the rate for a dozen bananas and the
quantity required to determine the cost:
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
double cost, quantity, amount;
Scanner scan = new Scanner(System.in);
System.out.println("Enter cost for dozen bananas: ");
cost = scan.nextDouble();
System.out.println("Enter quantity : ");
quantity = scan.nextDouble();
amount = quantity/12 * cost ;
System.out.println("Amount is : " + amount);
}
}
Output: Enter cost for dozen bananas : 40
Enter quantity : 60
Amount is : 200.0
import java.util.Scanner;
class Code{
public static void main(String[] args) {
int days, years, weeks;
Scanner scan = new Scanner(System.in);
System.out.println("Enter days : ");
days = scan.nextInt();
years = (days / 365);
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));
System.out.println(years+" years " + weeks + " weeks " + days + " days");
}
}
Output: Enter days : 500
1 years 19 weeks 2 days
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 12
Practice @ www.onlinejavacompiler.com
If Block Programs
if(Condition)
{
If-block-logic;
}
Program to give 15% discount on bill if the bill amount is greater than 5000
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter bill amount : ");
double bill = scan.nextDouble();
if(bill > 5000){
double discount = 0.15 * bill;
bill = bill - discount;
}
System.out.println("Final bill to pay : " + bill);
}
}
Program to give 20% bonus on salary if the employee has more than 5 years of
experience:
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter salary : ");
double salary = scan.nextDouble();
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 13
Practice @ www.onlinejavacompiler.com
Program to give 200 rupees cash back if the customer pay minimum 50% amount
of credit bill:
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Credit card bill amount : ");
double bill = scan.nextDouble();
System.out.println("Enter amount to pay : ");
double amount = scan.nextDouble();
double min = 0.5*bill;
int cashback = 0;
if(amount >= min){
cashback = 200 ;
}
System.out.println("Thank you for payment of : " + amount);
System.out.println("Your cash back is : " + cashback);
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 14
Practice @ www.onlinejavacompiler.com
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;
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 15
Practice @ www.onlinejavacompiler.com
else
System.out.println("Not even number");
}
}
Output: Enter number: 7
Not even number
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 16
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 17
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 18
Practice @ www.onlinejavacompiler.com
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;
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 19
Practice @ www.onlinejavacompiler.com
char ch = scan.nextLine().charAt(0);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
System.out.println("Alphabet");
else if(ch>='0' && ch<='9')
System.out.println("Digit");
else
System.out.println("Special Symbol");
}
}
Output: Enter character: 0
Digit
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 20
Practice @ www.onlinejavacompiler.com
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of units : ");
int units = sc.nextInt();
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;
System.out.println("Total bill amount : " + bill);
}
}
Output: Enter number of units: 150
Total bill amount : 140.0
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 21
Practice @ www.onlinejavacompiler.com
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 java.util.Scanner;
class Code{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number : ");
int n = scan.nextInt();
if(n>0){
if(n%2==0)
System.out.println("Even number");
else
System.out.println("Not even number");
}
else{
System.out.println("Negative number given");
}
}
}
Output: Enter number: 5
Not even number
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 22
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 23
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 24
Practice @ www.onlinejavacompiler.com
int b = scan.nextInt();
int c = scan.nextInt();
if(a==b && b==c)
System.out.println("Equilateral");
else if(a==b ||a==c||b==c)
System.out.println("Isosceles");
else
System.out.println("Scalene");
}
}
Output: Enter 3 sides of triangle :
70
70
40
Isosceles
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 25
Practice @ www.onlinejavacompiler.com
Introduction to Loops
Note: Block executes only once whereas Loop executes until condition become False
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 for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 26
Practice @ www.onlinejavacompiler.com
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.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 27
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 28
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 29
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 30
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 31
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 32
Practice @ www.onlinejavacompiler.com
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.
while(condition)
{
statements;
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 33
Practice @ www.onlinejavacompiler.com
int n = scan.nextInt();
n = n/10;
System.out.println("After removing last digit : " + n);
}
}
Output: Enter Num : 1234
After removing last digit : 123
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 34
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 35
Practice @ www.onlinejavacompiler.com
Program to Find the Sum of First and Last digits of given number
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Num : ");
int n = scan.nextInt();
int first = n%10;
n=n/10;
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 36
Practice @ www.onlinejavacompiler.com
while(n>=10){
n = n/10;
}
int last = n%10;
System.out.println("Sum of First & Last Digits : " + (first+last));
}
}
Palindrome Number: The number become same when we reverse is called Palindrome number
Examples: 121, 1001, 123321
import java.util.Scanner;
class Code
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Num : ");
int n = scan.nextInt();
int rev=0, r, temp=n;
while(n>0){
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 37
Practice @ www.onlinejavacompiler.com
r = n%10;
rev = rev*10 + r;
n = n/10;
}
if(temp==rev)
System.out.println("Palindrome Number");
else
System.out.println("Not Palindrome Number");
}
}
Output: Enter Num : 1221
Palindrome Number
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 38
Practice @ www.onlinejavacompiler.com
int n = scan.nextInt();
int temp, sum=0, r;
temp=n;
while(n>0){
r = n%10;
sum = sum + r*r*r;
n = n/10;
}
if(temp==sum)
System.out.println("ArmStrong Number");
else
System.out.println("Not an ArmStrong Number");
}
}
Output: Enter 3 digit num : 145
Not an Armstrong Number
import java.util.Scanner;
class Code {
public static void main(String[] args) {
int n, r, sum=0, temp, c=0, s, i;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Num : ");
n = scan.nextInt();
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;
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 39
Practice @ www.onlinejavacompiler.com
n = n/10;
}
if(temp==sum)
System.out.println("ArmStrong Number");
else
System.out.println("Not an ArmStrong Number");
}
}
while(num/10!=0){
sum = 0;
while(num!=0){
dig=num%10;
sum+=dig;
num/=10;
}
System.out.print(sum + "->");
num=sum;
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 40
Practice @ www.onlinejavacompiler.com
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 java.util.Scanner;
class Code
{
public static void main(String[] args) {
int num, temp, r1, r2, sq, rev1 = 0, rev2 = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Num : ");
num = scan.nextInt();
temp = num * num;
System.out.println("Square of the num : " + temp);
while (temp != 0){
r1 = temp % 10;
rev1 = rev1 * 10 + r1;
temp = temp / 10;
}
System.out.println("Reverse Num : " + rev1);
sq = (int)Math.sqrt(rev1);
System.out.println("Sqrt num : " + sq);
while (sq != 0){
r2 = sq % 10;
rev2 = rev2 * 10 + r2;
sq = sq / 10;
}
System.out.println("Reverse Num : " + rev2);
if (rev2 == num)
System.out.println(num + " is an Adam number");
else
System.out.println(num + " is not an Adam number");
}
}
Output: Enter Num: 12
12 is an Adam number
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 41
Practice @ www.onlinejavacompiler.com
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;
}
System.out.print(i + " ");
}
}
}
Output: 1 2 3 4
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;
}
System.out.print(i + " ");
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 42
Practice @ www.onlinejavacompiler.com
Switch case
Switch: It is a conditional statement that executes specific set of statements(case) based on
given choice. 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 java.util.Scanner;
class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter character(r, g, b) : ");
char ch = sc.next().charAt(0);
switch(ch){
case 'r' : System.out.println("Red");
break;
case 'g' : System.out.println("Green");
break;
case 'b' : System.out.println("Blue");
break;
default : System.out.println("Weird");
}
}
}
Output: Enter character(r, g, b): g
Green
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 43
Practice @ www.onlinejavacompiler.com
Output : 1. Add
2. Subtract
3. Multiply
Enter your choice : 2
30
10
Difference : 20
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 44
Practice @ www.onlinejavacompiler.com
Do-While Loop
do-while: Executes a block at least once and continue iteration until condition is false.
do
{
statements;
} while(condition);
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 45
Practice @ www.onlinejavacompiler.com
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++;
}
}
System.out.println("Number of Iterations : " + count);
}
}
Output : Number of Iterations : 25
Nested while loop: Defining while loop inside another while loop
while(cond1){
while(cond2){
statements;
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 46
Practice @ www.onlinejavacompiler.com
Example:
class Code{
public static void main(String[] args) {
int count=0;
int i=1;
while(i<=5){
int j=1;
while(j<=5){
count++;
j++;
}
i++;
}
System.out.println("Number of Iterations : " + count);
}
}
If break statement executes inside the inner loop, it terminates the flow of Inner loop only
class Code
{
public static void main(String[] args) {
for (int i=1 ; i<=4 ; i++){
for (int j=1 ; j<=4 ; j++){
if(i==j)
break;
System.out.println(i+","+j);
}
}
}
}
Output: 2, 1
3, 1
3, 2
4, 1
4, 2
4, 3
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 47
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 48
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 49
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 50
Practice @ www.onlinejavacompiler.com
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.
Pattern Logic
for (int i=1 ; i<=5 ; i++){
11111 for (int j=1 ; j<=5 ; j++){
22222
System.out.print(i);
33333
44444 }
55555 System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
***** for (int j=1 ; j<=5 ; j++){
*****
System.out.print("*");
*****
***** }
***** System.out.println();
}
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
54321 for (int j=5 ; j>=1 ; j--){
54321
System.out.print(j);
54321
54321 }
54321 System.out.println();
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 51
Practice @ www.onlinejavacompiler.com
Pattern Logic
for (int i=5 ; i>=1 ; i--){
55555 for (int j=5 ; j>=1 ; j--){
44444
System.out.print(i);
33333
22222 }
11111 System.out.println();
}
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
10101 for (int j=1 ; j<=5 ; j++){
10101
System.out.print(j%2);
10101
10101 }
10101 System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
11111 for (int j=1 ; j<=5 ; j++){
00000
System.out.print(i%2);
11111
00000 }
11111 System.out.println();
}
Pattern Logic
ABCDE for(char x='A' ; x<='E'; x++){
ABCDE for(char y='A' ; y<='E' ; y++){
ABCDE
System.out.print(y);
ABCDE
ABCDE }
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 52
Practice @ www.onlinejavacompiler.com
Pattern Logic
for(char x='E' ; x>='A'; x--){
EEEEE for(char y='E' ; y>='A' ; y--){
DDDDD
System.out.print(x);
CCCCC
BBBBB }
AAAAA System.out.println();
}
Pattern Logic
for(char x='E' ; x>='A'; x--){
EDCBA for(char y='E' ; y>='A' ; y--){
EDCBA
System.out.print(y);
EDCBA
EDCBA }
EDCBA System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
$$$$$
for (int j=1 ; j<=5 ; j++){
#####
$$$$$ if(i%2==0)
##### System.out.print("#");
$$$$$ else
System.out.print("$");
}
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
$#$#$
for (int j=1 ; j<=5 ; j++){
$#$#$
$#$#$ if(j%2==0)
$#$#$ System.out.print("#");
$#$#$ else
System.out.print("$");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 53
Practice @ www.onlinejavacompiler.com
Pattern Logic
int k=1;
12345 for (int i=1 ; i<=5 ; i++){
67891
for (int j=1 ; j<=5 ; j++){
23456
78912 System.out.print(k++%10);
34567 }
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
11111
for (int j=1 ; j<=5 ; j++){
12345
33333 if(i%2==0)
12345 System.out.print(j);
55555 else
System.out.print(i);
}
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
55555
for (int j=1 ; j<=5 ; j++){
54321
33333 if(i%2==0)
54321 System.out.print(j);
11111 else
System.out.print(i);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 54
Practice @ www.onlinejavacompiler.com
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)
+ +
+ + System.out.print("+");
+ + else
+ + System.out.print(" ");
+++++++ }
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==4||j==4)
+
+++++++ System.out.print("+");
+ else
+ System.out.print(" ");
+ }
System.out.println();
}
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)
+ + +
+++++++ System.out.print("+");
+ + + else
+ + + System.out.print(" ");
+++++++ }
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==j)
+
+ System.out.print("+");
+ else
+ System.out.print(" ");
+ }
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 55
Practice @ www.onlinejavacompiler.com
Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==8-i)
+
+ System.out.print("+");
+ else
+ System.out.print(" ");
}
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(i==j || j==8-i)
+ +
+ System.out.print("+");
+ + else
+ + System.out.print(" ");
+ + }
System.out.println();
}
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)
+ + + +
+ + + System.out.print("+");
+ + + + else
+ + + + System.out.print(" ");
+ + + + + + + }
System.out.println();
}
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))
+
+ System.out.print("+");
+ else
+ System.out.print(" ");
++++ }
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 56
Practice @ www.onlinejavacompiler.com
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))
+++++++
+ System.out.print("+");
+ else
+ System.out.print(" ");
}
System.out.println();
}
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))
+ + System.out.print("+");
+ ++++ else
System.out.print(" ");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 57
Practice @ www.onlinejavacompiler.com
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))
+ + + + +
+ System.out.print("+");
+ else
+ + + + + System.out.print(" ");
}
System.out.println();
}
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)
+ + + + +
+ System.out.print("+");
+ else
+ + + + + System.out.print(" ");
}
System.out.println();
}
Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==5 || i==5 || j==6-i)
+ +
+ + + + + + + + System.out.print("+");
+ else
+ System.out.print(" ");
}
System.out.println();
}
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))
+ + + + +
+ System.out.print("*");
+ else
+ + + + + System.out.print(" ");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 58
Practice @ www.onlinejavacompiler.com
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))
+ + + + +
+ + System.out.print("*");
+ + else
+ + + + + System.out.print(" ");
}
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=7 ; i++){
+ + + + + + for (int j=1 ; j<=5 ; j++){
+
if(i==1||j==7-i)
+
+ System.out.print("*");
+ else
+ System.out.print(" ");
}
System.out.println();
}
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)
+ + + + +
+ + System.out.print("*");
+ + else
+ + + + + System.out.print(" ");
}
System.out.println();
}
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)
+ + + + +
+ System.out.print("*");
+ else
+ + + + + System.out.print(" ");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 59
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 60
Practice @ www.onlinejavacompiler.com
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)
+ + + + +
+ System.out.print("+");
+ else
+ + + + + System.out.print(" ");
}
System.out.println();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==1||i==4)
+ + + + +
+ System.out.print("+");
+ else
+ System.out.print(" ");
}
System.out.println();
}
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))
+ + +
+ + System.out.print("+");
+ + else
+ + + + + System.out.print(" ");
}
System.out.println();
}
Pattern Logic
+ + for (int i=1 ; i<=7 ; i++){
+ + for (int j=1 ; j<=7 ; j++){
+ +
if(j==1||i==4||j==7)
+ + + + +
+ + System.out.print("+");
+ + else
+ + System.out.print(" ");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 61
Practice @ www.onlinejavacompiler.com
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==1||j==4||i==7)
+
+ System.out.print("+");
+ else
+ + + + + System.out.print(" ");
}
System.out.println();
}
Pattern Logic
+ + + + + for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(i==1||j==4||i==j+3)
+ +
+ + System.out.print("+");
+ else
System.out.print(" ");
}
System.out.println();
}
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))
+ +
+ System.out.print("+");
+ + else
+ + System.out.print(" ");
+ + }
System.out.println();
}
Pattern Logic
+ for (int i=1 ; i<=7 ; i++){
+ for (int j=1 ; j<=7 ; j++){
+
if(j==1||i==7)
+
+ System.out.print("+");
+ + + + + else
System.out.print(" ");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 62
Practice @ www.onlinejavacompiler.com
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))
+ + +
+ + System.out.print("+");
+ + else
+ + System.out.print(" ");
}
System.out.println();
}
Pattern Logic
+ + for (int i=1 ; i<=7 ; i++){
+ + + for (int j=1 ; j<=7 ; j++){
+ + +
if(j==1||j==7||(i==j))
+ + +
+ + + System.out.print("+");
+ + + else
+ + System.out.print(" ");
}
System.out.println();
}
Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
12 {
123
for (int j=1 ; j<=i ; j++){
1234
12345 System.out.print(j);
}
System.out.println();
}
Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
21 {
321
for (int j=i ; j>=1 ; j--){
4321
54321 System.out.print(j);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 63
Practice @ www.onlinejavacompiler.com
Pattern Logic
12345 for (int i=5 ; i>=1 ; i--)
1234 {
123
for (int j=1 ; j<=i ; j++){
12
1 System.out.print(j);
}
System.out.println();
}
Pattern Logic
12345 for (int i=1 ; i<=5 ; i++)
2345 {
345
for (int j=i ; j<=5 ; j++){
45
5 System.out.print(j);
}
System.out.println();
}
Pattern Logic
5 for (int i=5 ; i>=1 ; i--){
54 for (int j=5 ; j>=i ; j--){
543
System.out.print(j);
5432
54321 }
System.out.println();
}
Pattern Logic
5 for (int i=5 ; i>=1 ; i--){
45 for (int j=i ; j<=5 ; j++){
345
System.out.print(j);
2345
12345 }
System.out.println();
}
Pattern Logic
54321 for (int i=1 ; i<=5 ; i++)
5432 {
543
for (int j=5 ; j>=i ; j--){
54
5 System.out.print(j);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 64
Practice @ www.onlinejavacompiler.com
Pattern Logic
54321 for (int i=5 ; i>=1 ; i--)
4321 {
321
for (int j=i ; j>=1 ; j--){
21
1 System.out.print(j);
}
System.out.println();
}
Pattern Logic
1 for (int i=1 ; i<=5 ; i++)
22 {
333
for (int j=1 ; j<=i ; j++){
4444
55555 System.out.print(i);
}
System.out.println();
}
Pattern Logic
11111 for (int i=1 ; i<=5 ; i++)
2222 {
333
for (int j=i ; j<=5 ; j++){
44
5 System.out.print(i);
}
System.out.println();
}
Pattern Logic
5 for (int i=5 ; i>=1 ; i--)
44 {
333
for (int j=i ; j<=5 ; j++){
2222
11111 System.out.print(i);
}
System.out.println();
}
Pattern Logic
55555 for (int i=5 ; i>=1 ; i--){
4444 for (int j=1 ; j<=i ; j++){
333
System.out.print(i);
22
1 }
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 65
Practice @ www.onlinejavacompiler.com
Pattern Logic
int k=1;
for (int i=1 ; i<=5 ; i++){
1
for (int j=1; j<=i ; j++){
23
456 System.out.print(k++);
7891 if(k>9)
23456 k=1;
}
System.out.println();
}
Pattern Logic
int k=1;
for (int i=5 ; i>=1 ; i--){
12345
for (int j=1; j<=i ; j++){
6789
123 System.out.print(k++);
45 if(k>9)
6 k=1;
}
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++)
{
1
for (int j=i ; j<5 ; j++){
21
321 System.out.print(" ");
4321 }
54321 for (int k=i ; k>=1 ; k--){
System.out.print(k);
}
System.out.println();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=i ; j<5 ; j++){
12345
System.out.print(" ");
1234
123 }
12 for (int k=1 ; k<=i ; k++){
1 System.out.print(k);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 66
Practice @ www.onlinejavacompiler.com
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<i ; j++){
12345
System.out.print(" ");
2345
345 }
45 for (int k=i ; k<=5 ; k++){
5 System.out.print(k);
}
System.out.println();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=1 ; j<i ; j++){
5
System.out.print(" ");
45
345 }
2345 for (int k=i ; k<=5 ; k++){
12345 System.out.print(k);
}
System.out.println();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=1 ; j<i ; j++){
5
System.out.print(" ");
54
543 }
5432 for (int k=5 ; k>=i ; k--){
54321 System.out.print(k);
}
System.out.println();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--){
for (int j=i ; j<5 ; j++){
54321
System.out.print(" ");
4321
321 }
21 for (int k=i ; k>=1 ; k--){
1 System.out.print(k);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 67
Practice @ www.onlinejavacompiler.com
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<i ; j++){
54321
System.out.print(" ");
5432
543 }
54 for (int k=5 ; k>=i ; k--){
5 System.out.print(k);
}
System.out.println();
}
Characters Triangle Patterns
Pattern Logic
for (char i='A' ; i<='E' ; i++){
A for (char j='A' ; j<=i ; j++){
AB
System.out.print(j);
ABC
ABCD }
ABCDE System.out.println();
}
Pattern Logic
for (char i='A' ; i<='E' ; i++){
A for (char j=i ; j>='A' ; j--){
BA
System.out.print(j);
CBA
DCBA }
EDCBA System.out.println();
}
Pattern Logic
for (char i='E' ; i>='A' ; i--){
ABCDE for (char j='A' ; j<=i ; j++){
ABCD
System.out.print(j);
ABC
AB }
A System.out.println();
}
Pattern Logic
for (char i='A' ; i<='E' ; i++){
ABCDE for (char j=i ; j<='E' ; j++){
BCDE
System.out.print(j);
CDE
DE }
E System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 68
Practice @ www.onlinejavacompiler.com
Pattern Logic
E for (char i='E' ; i>='A' ; i--)
ED {
EDC
for (char j='E' ; j>=i ; j--){
EDCB
EDCBA System.out.print(j);
}
System.out.println();
}
Pattern Logic
for (char i='E' ; i>='A' ; i--){
E for (char j=i ; j<='E' ; j++){
DE System.out.print(j);
CDE
}
BCDE
ABCDE System.out.println();
}
attern Logic
EDCBA for (char i='A' ; i<='E' ; i++){
EDCB for (char j='E' ; j>=i ; j--){
EDC
System.out.print(j);
ED
E }
System.out.println();
}
Pattern Logic
EDCBA for (char i='E' ; i>='A' ; i--){
DCBA for (char j=i ; j>='A' ; j--){
CBA
System.out.print(j);
BA
A }
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 69
Practice @ www.onlinejavacompiler.com
Pattern Logic
for (int i=5 ; i>=1 ; i--){
***** for (int j=1 ; j<=i ; j++){
****
System.out.print("*");
***
** }
* System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=i ; j<5 ; j++){
*
System.out.print(" ");
**
*** }
**** for (int k=1 ; k<=i ; k++){
***** System.out.print("*");
}
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
for (int j=1 ; j<i ; j++){
*****
System.out.print(" ");
****
*** }
** for (int k=i ; k<=5 ; k++){
* System.out.print("*");
}
System.out.println();
}
Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
*
for(int j=1 ; j<=i ; j++){
**
*** System.out.print("*");
**** }
***** System.out.println();
**** }
*** else{
** for(int k=i ; k<10 ; k++){
*
System.out.print("*");
}
System.out.println();
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 70
Practice @ www.onlinejavacompiler.com
Pattern Logic
for(int i=1 ; i<10 ; i++)
{
if(i<=5){
for(int x=i ; x<=5 ; x++){
* System.out.print(" ");
** }
*** for(int j=1 ; j<=i ; j++){
**** System.out.print("*");
*****
}
****
*** System.out.println();
** }
* else{
for(int x=i ; x>=5 ; x--){
System.out.print(" ");
}
for(int k=i ; k<10 ; k++){
System.out.print("*");
}
System.out.println();
}
}
Pattern Logic
for(int i=1 ; i<10 ; i++)
{
if(i<=5){
*****
**** for(int j=i ; j<=5 ; j++){
*** System.out.print("*");
** }
* System.out.println();
** }
*** else{
****
***** for(int k=5 ; k<=i ; k++){
System.out.print("*");
}
System.out.println();
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 71
Practice @ www.onlinejavacompiler.com
Pattern Logic
for(int i=1 ; i<10 ; i++){
if(i<=5){
for(int x=1 ; x<=i ; x++){
System.out.print(" ");
}
***** for(int j=i ; j<=5 ; j++){
**** System.out.print("*");
*** }
**
System.out.println();
*
** }
*** else{
**** for(int x=i ; x<10 ; x++){
***** System.out.print(" ");
}
for(int k=5 ; k<=i ; k++){
System.out.print("*");
}
System.out.println();
}
}
Pattern Logic
int k=1;
for (int i=5 ; i>=1 ; i--)
10101
{
0101
010 for (int j=1; j<=i ; j++){
10 System.out.print(k++%2);
1 }
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 72
Practice @ www.onlinejavacompiler.com
Pattern Logic
for (int i=1 ; i<=5 ; i++)
1 {
10
for (int j=1; j<=i ; j++)
101
1010 {
10101 System.out.print(j%2);
}
System.out.println();
}
Pattern Logic
for (int i=1 ; i<=5 ; i++){
1 for (int j=1; j<=i ; j++)
00
{
111
0000 System.out.print(i%2);
11111 }
System.out.println();
}
Pattern Logic
for (int i=5 ; i>=1 ; i--)
11111 {
0000
for (int j=1; j<=i ; j++)
111
00 {
1 System.out.print(i%2);
}
System.out.println();
}
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)
* * System.out.print("*");
******** else
System.out.print(" ");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 73
Practice @ www.onlinejavacompiler.com
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)
* * System.out.print("*");
** else
* System.out.print(" ");
}
System.out.println();
}
Pattern Logic
for(int i=1 ; i<=7 ; i++)
{
********
for(int j=1 ; j<i ; j++){
* *
* * System.out.print(" ");
* * }
* * for(int k=i ; k<=7 ; k++){
* * if(i==1 || i==7 || k==i || k==7)
** System.out.print("*");
* else
System.out.print(" ");
}
System.out.println();
}
Pattern Logic
for(int i=1 ; i<=7 ; i++)
{
*
for(int j=i ; j<7 ; j++){
**
* * System.out.print(" ");
* * }
* * for(int k=1 ; k<=i ; k++){
* * if(i==1 || i==7 || k==1 || k==i)
* * System.out.print("*");
******** else
System.out.print(" ");
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 74
Practice @ www.onlinejavacompiler.com
Pyramid Patterns
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++)
*
{
***
***** for(int j=i ; j<n ; j++){
******* System.out.print(" ");
********* }
for(int k=1 ; k<=2*i-1 ; k++){
System.out.print("*");
}
System.out.println();
}
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++)
{
*
* * for(int j=i ; j<n ; j++){
* * System.out.print(" ");
* * }
********* for(int k=1 ; k<=2*i-1 ; k++){
if(i==1 || i==n || k==1 || k==2*i-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
Pattern Logic
1 int n=7;
222 for(int i=1 ; i<=n ; i++)
33333
{
4444444
555555555 for(int j=i ; j<n ; j++){
System.out.print(" ");
}
for(int k=1 ; k<=2*i-1 ; k++){
System.out.print(i);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 75
Practice @ www.onlinejavacompiler.com
Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--)
{
*********
* * for(int j=i ; j<n ; j++){
* * System.out.print(" ");
* * }
* for(int k=1 ; k<=2*i-1 ; k++){
if(i==1 || i==n || k==1 || k==2*i-1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--)
{
555555555
4444444 for(int j=i ; j<n ; j++){
33333 System.out.print(" ");
222 }
1 for(int k=1 ; k<=2*i-1 ; k++){
System.out.print(i);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 76
Practice @ www.onlinejavacompiler.com
Pattern Logic
int r=6, c=1;
for(int i=0; i<r ; i++) {
for(int s=1; s<r-i; s++) {
1
1 1 System.out.print(" ");
1 2 1 }
1 3 3 1 for(int j=0; j<=i; j++) {
1 4 6 4 1 if (j==0 || i==0)
1 5 10 10 5 1 c=1;
else
c=c*(i-j + 1)/j;
System.out.printf("%4d", c);
}
System.out.println();
}
for (int i = 5; i >= 1; i--){
for (int j = 5 - i; j >= 1; j--){
123454321
System.out.print(" ");
1234321
12321 }
121 for (int j = 1; j <= i; j++){
1 System.out.print(j);
}
for (int j = i - 1; j >= 1; j--){
System.out.print(j);
}
System.out.println();
}
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 System.out.print(" ");
}
for(int k=1 ; k<=i ; k++){
System.out.print(k);
}
for(int l=i-1 ; i>=1 ; i--){
System.out.print(l);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 77
Practice @ www.onlinejavacompiler.com
Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--){
for(int j=i ; j<n ; j++){
123454321
1234321 System.out.print(" ");
12321 }
121 for(int k=1 ; k<=i ; k++){
1 System.out.print(k);
}
for(int l=i-1 ; l>=1 ; l--){
System.out.print(l);
}
System.out.println();
}
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++){
for(int j=i ; j<n ; j++){
System.out.print(" ");
1 }
212 for(int k=i ; k>=1 ; k--){
32123 System.out.print(k);
4321234 }
543212345 for(int l=1+1 ; l<=i ; l++){
System.out.print(l);
}
System.out.println();
}
Pattern Logic
int n=7;
for(int i=n ; i>=1 ; i--){
for(int j=i ; j<n ; j++){
543212345
4321234 System.out.print(" ");
32123 }
212 for(int k=i ; k>=1 ; k--){
1 System.out.print(k);
}
for(int l=1+1 ; l<=i ; l++){
System.out.print(l);
}
System.out.println();
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 78
Practice @ www.onlinejavacompiler.com
Pattern Logic
int n=7;
for(int i=1 ; i<=n ; i++){
for(int j=i ; j<n ; j++){
* System.out.print(" ");
*** }
***** for(int k=1 ; k<=2*i-1 ; k++){
******* System.out.print("* ");
********* }
***********
System.out.println();
*********
******* }
***** for(int i=n-1 ; i>=1 ; i--){
*** for(int j=i ; j<n ; j++){
* System.out.print(" ");
}
for(int k=1 ; k<=2*i-1 ; k++){
System.out.print("* ");
}
System.out.println();
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 79
Practice @ www.onlinejavacompiler.com
Methods
Syntax Example
returntype identity(arguments) int add(int a, int b)
{ {
statements; int c=a+b;
} return c;
}
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 for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 80
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 81
Practice @ www.onlinejavacompiler.com
Recursion
Recursion:
Calling method itself is called Recursion.
Invoking the method from the body of same method.
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 82
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 83
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 84
Practice @ www.onlinejavacompiler.com
}
else if(ch==5){
System.out.println("End of Program");
System.exit(1);
}
else{
System.out.println("Invalid Choice");
}
}
}
}
Output:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Quit
Enter your choice : 4
Enter 2 numbers :
5
2
5/2=2
1. Add
2. Subtract
3. Multiply
4. Divide
5. Quit
Enter your choice : 5
End of Program
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 85
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 86
Practice @ www.onlinejavacompiler.com
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 : System.out.println("End of Program");
System.exit(1);
default: System.out.println("Invalid choice");
}
}
}
static void add(int a, int b)
{
System.out.println(a+" + "+b+" = "+(a+b));
}
static void subtract(int a, int b)
{
System.out.println(a+" - "+b+" = "+(a-b));
}
static void multiply(int a, int b)
{
System.out.println(a+" * "+b+" = "+(a*b));
}
static void divide(int a, int b)
{
System.out.println(a+" / "+b+" = "+(a/b));
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 87
Practice @ www.onlinejavacompiler.com
Arrays
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 88
Practice @ www.onlinejavacompiler.com
Check the sum of first and last elements in Array equals to 10 or not
class Code
{
public static void main(String[] args) {
int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
int first = arr[0];
int last = arr[arr.length-1];
if(first+last==10)
System.out.println("Equals");
else
System.out.println("Not Equals");
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 89
Practice @ www.onlinejavacompiler.com
class Code
{
public static void main(String[] args) {
int[] arr = {6, 2, 8, 9, 1, 6, 7, 5};
int n = arr.length;
if(n%2!=0){
System.out.println("Mean : " + arr[n/2]);
}
else{
int x = arr[n/2-1];
int y = arr[n/2];
System.out.println("Mean : " + ((x+y)/2));
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 90
Practice @ www.onlinejavacompiler.com
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/arr.length;
for(int x : arr)
{
if(x>avg)
System.out.println(x);
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 91
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 92
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 93
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 94
Practice @ www.onlinejavacompiler.com
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};
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 95
Practice @ www.onlinejavacompiler.com
int i, j;
for (i=0 ; i<arr.length ; i++){
if(arr[i]%2==0)
break;
}
for (j=arr.length-1 ; j>=0 ; j--){
if(arr[j]%2!=0)
break;
}
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
System.out.println("Array after swap : ");
for (i=0 ; i<arr.length ; i++){
System.out.println(arr[i]);
}
}
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 96
Practice @ www.onlinejavacompiler.com
Program to reverse all elements in the given array without second array.
class Code {
public static void main(String[] args) {
int[] arr = {5, 9, 2, 1, 8, 4, 6};
int i=0;
int j=arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
System.out.println("Reverse Array : ");
for (i=0 ; i<arr.length ; i++){
System.out.println(arr[i]);
}
}
}
import java.util.Scanner ;
class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter array size : ");
int n = scan.nextInt();
int arr[ ] = new int[n];
System.out.println("Enter " + n + " values : ");
for (int i=0 ; i<n ; i++){
arr[i] = scan.nextInt();
}
System.out.println("Enter element to search : ");
int ele = scan.nextInt();
boolean found=false;
for (int i=0 ; i<n ; i++){
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 97
Practice @ www.onlinejavacompiler.com
if(arr[i] == ele){
System.out.println("Element found at location : " + i);
found = true;
break;
}
}
if(!found)
System.out.println("Element not found");
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 98
Practice @ www.onlinejavacompiler.com
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;
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 99
Practice @ www.onlinejavacompiler.com
Program to divide the given array into 2 arrays with even numbers and odd numbers:
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<arr.length ; i++){
if(arr[i]%2==0)
count++;
}
int[] evens = new int[count];
int[] odds = new int[arr.length-count];
int x=0, y=0;
for (int i=0 ; i<arr.length ; i++){
if(arr[i]%2==0){
evens[x]=arr[i];
x++;
}
else{
odds[y]=arr[i];
y++;
}
}
System.out.println("Even numbers array : ");
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 100
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 101
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 102
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 103
Practice @ www.onlinejavacompiler.com
if(arr[i]+arr[j]==10)
System.out.println("("+arr[i]+","+arr[j]+")");
}
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 104
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 105
Practice @ www.onlinejavacompiler.com
Program to arrange even numbers to left side and odd numbers to right side of Array:
import java.util.Arrays;
class Code {
public static void main(String[] args) {
int[] arr = {7, 2, 9, 8, 4, 1, 2, 6, 5};
System.out.println("Given array : " + Arrays.toString(arr));
int i=0;
int j=arr.length-1;
while(i<j){
while(true){
if(arr[i]%2!=0)
break;
else
i++;
}
while(true){
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 106
Practice @ www.onlinejavacompiler.com
if(arr[j]%2==0)
break;
else
j--;
}
if(i<j){
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
i++;
j--;
}
System.out.println("Result array : " + Arrays.toString(arr));
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 107
Practice @ www.onlinejavacompiler.com
}
if(equal)
System.out.println("Arrays are equal");
else
System.out.println("Arrays are not equal");
}
else
System.out.println("Arrays are not equal");
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 108
Practice @ www.onlinejavacompiler.com
else
j--;
}
if(i<j){
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
i++;
j--;
}
System.out.println("Resultant array: " + Arrays.toString(arr));
}
}
Program to find the index difference between smallest and largest elements:
import java.util.Arrays;
class Code {
public static void main(String[] args) {
int[] arr = {5, 7, 3, 8, 6, 9, 4};
System.out.println("Given array : " + Arrays.toString(arr));
int small=arr[0];
int big=arr[0];
int x=0, y=0;
for (int i=1 ; i<arr.length ; i++){
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 109
Practice @ www.onlinejavacompiler.com
if(small>arr[i]){
small=arr[i];
x=i;
}
if(big<arr[i]){
big=arr[i];
y=i;
}
}
System.out.println("Index difference between Small and Large Elements : " +
Math.abs(x-y));
}
}
Program to print leader elements in the array : All elements to its right must be smaller to
Leader element
import java.util.Arrays;
class Code {
public static void main(String[] args) {
int arr[] = {10, 9, 14, 23, 15, 0, 9};
int size = arr.length;
for (int i=0 ; i<size ; i++) {
int j;
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 110
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 111
Practice @ www.onlinejavacompiler.com
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). We can create 2D array
directly using any one of the following syntax
int arr[][] = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
or
int[][] arr = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
or
int[] arr[] = new int[][] { { 1, 2, 3 }, { 4, 5, 6 } };
or
int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 } };
Processing 2D array: We can perform all operations like reading, modifying, displaying using
nested loops.
public class Logic
{
public static void main(String args[])
{
int arr[][] = new int[3][3];
System.out.println("Elements are : ");
for (int i=0 ; i<3 ; i++)
{
for(int j=0 ; j<3 ; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 112
Practice @ www.onlinejavacompiler.com
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 113
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 114
Practice @ www.onlinejavacompiler.com
Addition of 2 matrixes:
import java.util.Scanner;
public class Logic{
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int A[][] = new int[2][2];
int B[][] = new int[2][2];
int C[][] = new int[2][2];
System.out.println("Enter 4 elements into A : ");
for (int i=0 ; i<2 ; i++)
for(int j=0 ; j<2 ; j++)
A[i][j] = scan.nextInt();
Matrix multiplication:
import java.util.Scanner;
public class Logic{
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int A[][] = new int[2][2];
int B[][] = new int[2][2];
int C[][] = new int[2][2];
System.out.println("Enter 4 elements into A : ");
for (int i=0 ; i<2 ; i++)
for(int j=0 ; j<2 ; j++)
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 115
Practice @ www.onlinejavacompiler.com
A[i][j] = scan.nextInt();
System.out.println("Enter 4 elements into B : ");
for (int i=0 ; i<2 ; i++)
for(int j=0 ; j<2 ; j++)
B[i][j] = scan.nextInt();
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];
}
}
}
System.out.println("Mutliplied matrix is : ");
for (int i=0 ; i<2 ; i++){
for(int j=0 ; j<2 ; j++){
System.out.print(C[i][j] + " ");
}
System.out.println();
}
}
}
Transpose of a Matrix:
public class Logic{
public static void main(String args[]) {
int A[][] = {{10,20,30},{40,50,60},{70,80,90}};
System.out.println("Input Matrix is : ");
for (int i=0 ; i<3 ; i++){
for(int j=0 ; j<3 ; j++){
System.out.print(A[i][j] + " ");
}
System.out.println();
}
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;
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 116
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 117
Practice @ www.onlinejavacompiler.com
Strings
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 118
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 119
Practice @ www.onlinejavacompiler.com
import java.util.Scanner;
class Code {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter Upper case character : ");
char ch = scan.next().charAt(0);
if (ch >= 'A' && ch <= 'Z'){
ch = (char)(ch+32);
System.out.println("Lower case character is : " + ch);
}
else
System.out.println("Invalid Character Entered");
}
}
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 = str.toCharArray();
System.out.println("Elements :");
for(char ele : arr)
System.out.println(ele);
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 120
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 121
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 122
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 123
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 124
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 125
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 126
Practice @ www.onlinejavacompiler.com
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<arr.length ; i++){
String s = arr[i];
System.out.println(s+" : "+s.charAt(0)+","+s.charAt(s.length()-1));
}
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 127
Practice @ www.onlinejavacompiler.com
Program to count number of words in the given string without split() method.
class Code {
public static void main(String[] args) {
String s = "This is core java test";
int spaces=0;
for (int i=0 ; i<s.length() ; i++){
if(s.charAt(i)==' ')
spaces++;
}
System.out.println("String is : " + s);
System.out.println("Words count : " + (spaces+1));
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 128
Practice @ www.onlinejavacompiler.com
import java.util.Arrays;
class Code
{
public static void main(String[] args) {
String s1 = "army";
String s2 = "mary";
System.out.println("Check Anagram or not : "+ isAnagram(s1, s2));
}
public static boolean isAnagram(String s1, String s2){
char[] a1 = s1.toLowerCase().toCharArray();
char[] a2 = s2.toLowerCase().toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
return Arrays.equals(a1, a2);
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 129
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 130
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 131
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 132
Practice @ www.onlinejavacompiler.com
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 133
Practice @ www.onlinejavacompiler.com
Program to check how many times the given sub string is present:
class Code
{
public static void main(String[] args) {
String s = "abcaabcaaabcabacabcaabcaabc";
String[] arr = s.split("abc");
System.out.println("abc repeated : " + (arr.length) + " times");
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 134
Practice @ www.onlinejavacompiler.com
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";
String s2 = "";
String sub = "Core";
int n=sub.length();
for (int i=0 ; i<s1.length() ; i++){
char ch = s1.charAt(i);
if(ch != sub.charAt(0)){
s2 = s2 + ch;
}
else {
String match = s1.substring(i, i+n);
i=i+n;
}
}
System.out.println("Given String : " + s1);
System.out.println("After removing Sub string : " + s2);
}
}
Contact for Online Classes: +91 – 911 955 6789 / 7306021113 Page - 135