0% found this document useful (0 votes)
6 views

Java Freshers Questions

Uploaded by

venkatbj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Freshers Questions

Uploaded by

venkatbj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 72

1.

what is class & object

A Class is a template that contains a set of properties and functionalities together

Object is an instance of the class

2. explain class & object for AC & CAR object

we create a class ‘Car‘ and create an object ‘Toyota‘ from


that car class, then we can say that the Toyota car object is
an instance of the class of objects known as Car. Toyota car have some
states and behavior. However, each brand of car will have its own state
and behaviors.

3. what is constructor ,what is real use of constructor

A Constructor is a special method used to provide initial values to the data members of the
class

4. waht is constructor overloading & constructor chaining

Creating a zero argument constructor and parameterized constructor for a class in called
constructor overloading

5. what is the use of this & super keyword

‘this’ represents current class

‘super’ represents parent class

6. what is the use of this() & super() statement

super() this()

Definition super() - refers immediate parent class this() - refers current class
instance. instance.

Invoke Can be used to invoke immediate Can be used to invoke current


parent class method. class method.

Constructo super() acts as immediate parent class this() acts as current class
r constructor and should be first line in constructor and can be used in
child class constructor. parametrized constructors.

Override When invoking a superclass version of When invoking a current version of


an overridden method the super an overridden method the this
keyword is used. keyword is used.
7. what is in inheritance & advantages of inheritance

Inheritance is the process of acquiring the functionalities of one class into another class

Reusability is the advantage of inheritance

8. expain type of inheritance

a. Single Inheritance

b. Multiple Inheritance

c. MultiLevel Inheritance

9. explain multiple inheritance & why it is not possible, how to achive multiple inheritance

Inheriting more than one class or more than one interface or one class and one interface
into a class is called multiple inheritance

Multiple inheritance from two classes into a class in NOT possible in java. Because when
both base classes are having the same method then there will be an ambiguity in calling the method

Multiple inheritance will be implemented by inheriting two interfaces into a class or one
class and interface into a class

10.what is death of diamond

The "diamond problem" (sometimes referred to as the "Deadly Diamond of Death") is an ambiguity
that arises when two classes B and C inherit from A, and class D inherits from both B and C. ... It is
called the "diamond problem" because of the shape of the class inheritance diagram in this
situation.
11.explain overLoading / overRiding with real time example

Overloading is about same function have different signatures. Overriding is about same function,
same signature but different classes connected through inheritance. Overloading is an example of
compiler time polymorphism and overriding is an example of run time polymorphism.
Overloading Example:

public class Add{

public int add(int a , int b){

return (a + b);
}

public int add(int a , int b , int c){

return (a + b + c) ;

public double add(double a , double b){

return (a + b);

public static void main( String args[]){

Add ob = new Add();

ob.add(15,25);

ob.add(15,25,35);

ob.add(11.5 , 22.5);

Overriding Example:

class Father {
void shoot() {
System.out.println("I am the father! I am a right-handed shooter! ");
}
}
public class Child extends Father {
void shoot() {
System.out.println("I am the son! I am a left-handed shooter!");
}
public static void main(String[] args) {
Father f = new Father();
f.shoot(); //Here the parent class method gets called.
Father fc = new Child();
fc.shoot(); //This is dynamic methpod dispatch. The compiler decides method call at runtime.
}
}

12.what is abstraction & abstract

Data abstraction is the process of hiding certain details and showing only essential information to
the user.

Abstract keyword is used for methods and classes


13.how many ways we can acheive abstraction

Using interfaces and abstract classes

14.Can we have constructor in abstract class?

yes

15.Can we have main method in abstract class?

yes

16.can interface inherit object class?

No

17.Can abstract class inherit object class?

yes

18.which is the first statement inside the default constructor?

Constructor call

19.Can interface have a construtor?

no

20.Can interface have a main method?

yes

21.Can we achieve multiple inheritance by using a abstract class?

no

22.Can we achieve multiple inheritance by using a interface?

yes

23.Can we achieve loose coupling by using abstract class and interface?

24.Can we have a private abstract method in abstract class?

no

25.Can we have a static variables inside the interface?

yes

25.how do you achieve multiple inheritance in java by using interface?

26.Explain the interface with real time example?

26.What is exception?

Deviation from expected result

27.Diffrence betweem exception and error?

Exception can be handled, error can’t be handled


28.Explain the types of exception?

Checked Exception(Compiler error)


Unchecked Exception(Runtime error)

29.what is the purpose of try and catch block?

Try block contains executable statements

Catch block contains exception handling code

30.can we have a nested try blocks?

yes

31.can we have a more than one finally block in a single program?

no

32.what is the diffrence between throw and throws and throwable?

‘throw’ is used to raise an exception

‘throws’ is used to declare an exception

The Throwable class is the superclass of all errors and exceptions in the Java language

33.what are custom exception?How to write custom excpetion?

35.which is supermost class of exception?

Exception

36.what are the inbuilt class present in java?

37.what are the inbuilt methods are present in java?

38.what is the diffrence between final,finally,finalize?

39.List out the exceptions are available in java?

40.Difference between array and collection?

41.difference between array and arraylist?

42.difference between array and linkedlist?

43.difference between arraylist and linkedlist?

44.difference between arraylist and vector?

45.difference between list and set?

46.difference between set and map?

47.difference between hashset and linkedhashset?

48.difference between treeset and treemap?


49.difference between comparable and comparator?

50.how many cursors are available in java?

51.how arraylist is implemented?

52.what is the default size of arraylist?

53.difference between collection and collections?

54.what is the role of iterator in collection framework?

55.difference between hashmap and hashtable?

56. what is early binding and late binding?

56.what is wrapper class, List of the Wrapper class avilable in Java

57.What is boxing & unboxing

58.how to integer to String

59.how to Stringto Intiger

60.what is premetive casting, types of premetive casting

61.what of object casting expalin with example

62.Explain Run time ploymorphism with upcasting example

63.what is acheive down casting in java

64.What is classCASTING exception & when will get ?

65.what is Object class in Java

66.explain inbuilt class avilable in OBJECT class

67.can we create object array? what is the use of Object array

68.explain toString(). eqaul(), hashcode() mtd in Object class

69. What is String class , why is required

70. can we create object to final String class

71. how many ways to create String object

72. why String is immutable , explain with program

73. difference between Constant pool & non constant pool in String class

74. differce between equals() and == and compareTo() method in java?

75. Difference between equals() and contains() method?

76.Diffrence between stringbuffer and stringbuilder?

77.Explain the inbuilt methods of String class?

78.Explain public sttaic void main(String[] args)?


79.Explain system.out.println()?

80.Does arrays are objects in java?

81.What is two dimensional array?

82.What is jagged array?

83.Can we declare array as a final?

84.What is marker interafce?

85.what is serialization?

86.what is deserialisation?

87.which class do we need to use for a serialisation?

88.what are nested class?

89.Can we change the sequence of public static in main method?

90.what are commandline arguments?

91.what are threads?how many ways to create a thread?

92.what is synchronisation?

93.what is singleton class?

94.why singleton class is required?explain with real time example?

95.How to declare ,create, and initialise array in single line?

96.what are the disadvantages of array?

97.which is the default package in java?

98.what is the use of package?

99.what is the use of import keyword?

100.what are inbuilt packages are available in java?

101.Explain oops concepts with real time examples?

102. diff b/n static and non-static.

Programs

1.W.A.P PALINDROME?

Palindrome number algorithm


o Get the number to check for palindrome

o Hold the number in temporary variable


o Reverse the number

o Compare the temporary number with reversed number

o If both numbers are same, print "palindrome number"

o Else print "not palindrome numbe

1. class PalindromeExample{
2. public static void main(String args[]){
3. int r,sum=0,temp;
4. int n=454;//It is the number variable to be checked for palindrome
5.
6. temp=n;
7. while(n>0){
8. r=n%10; //getting remainder
9. sum=(sum*10)+r;
10. n=n/10;
11. }
12. if(temp==sum)
13. System.out.println("palindrome number ");
14. else
15. System.out.println("not palindrome");
16.}
17.}

2.w.a.p for reverse number?

To reverse a number, follow the steps


given below:
o First, we find the remainder of the given number by using the modulo (%)
operator.
o Multiply the variable reverse by 10 and add the remainder into it.

o Divide the number by 10.

Repeat the above steps until the number becomes 0.

1. public class ReverseNumberExample1


2. {
3. public static void main(String[] args)
4. {
5. int number = 987654, reverse = 0;
6. while(number != 0)
7. {
8. int remainder = number % 10;
9. reverse = reverse * 10 + remainder;
10.number = number/10;
11.}
12.System.out.println("The reverse of the given number is: " + reverse);
13.}
14.}

3.armstrong number?

Armstrong Number in Java: A positive number is called armstrong number if it is


equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.

Ex: 153

1. 153 = (1*1*1)+(5*5*5)+(3*3*3)
2. where:
3. (1*1*1)=1
4. (5*5*5)=125
5. (3*3*3)=27
6. So:
7. 1+125+27=153

1. class ArmstrongExample{
2. public static void main(String[] args) {
3. int c=0,a,temp;
4. int n=153;//It is the number to check armstrong
5. temp=n;
6. while(n>0)
7. {
8. a=n%10;
9. n=n/10;
10. c=c+(a*a*a);
11. }
12. if(temp==c)
13. System.out.println("armstrong number");
14. else
15. System.out.println("Not armstrong number");
16. }
17.}

4.prime number or not?

Prime number in Java: Prime number is a number that is greater than 1 and
divided by 1 or itself only. In other words, prime numbers can't be divided by other
numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime
numbers.

1. public class PrimeExample{


2. public static void main(String args[]){
3. int i,m=0,flag=0;
4. int n=3;//it is the number to be checked
5. m=n/2;
6. if(n==0||n==1){
7. System.out.println(n+" is not prime number");
8. }else{
9. for(i=2;i<=m;i++){
10. if(n%i==0){
11. System.out.println(n+" is not prime number");
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0) { System.out.println(n+" is prime number"); }
17. }//end of else
18.}
19.}

5.w.a.p to generate prime numbers between 1 to 1000?


class PrimeNumbers
{
public static void main (String[] args)
{
int i =0;
int num =0;
//Empty String
String primeNumbers = "";

for (i = 1; i <= 1000; i++)


{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 1000 are :");
System.out.println(primeNumbers);
}
}

6.w.a.p strong number?

A strong number is a special number that can be defined as an addition of

factorial of each digit of the number, which is equal to the number itself. To

better understand the concept of a strong number, have a look at the

below example:

The number 145 is a strong number. This is because if we add the factorials

of each digit of this number, you will get the number, which is 145 itself, as

the sum. 1! + 4! + 5! = 1 + 24 + 120 = 145.

import java.util.*;
public class Main
{
public static void main(String[] args) {
int n,i;
int fact_n,lastdig;
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the number : " );
n = sc.nextInt();
int total = 0;
int temp_n = n;
while(n != 0)
{
i = 1;
fact_n = 1;
lastdig = n % 10;
while(i <= lastdig)
{
fact_n = fact_n * i;
i++;
}
total = total + fact_n;
n = n / 10;
}
if(total == temp_n)
System.out.println(temp_n + " is a strong number\n");
else
System.out.println(temp_n + " is not a strong number\
n");
System.out.println();
}
}
Output :
Enter the Number : 145
145 is strong number

7.perfect number?

Perfect Number in Java


Any number can be a Java Perfect Number if the sum of its positive divisors
excluding the number itself is equal to that number. For example, 28 is a perfect
number because 28 is divisible by 1, 2, 4, 7, 14 and 28 and the sum of these values
is 1 + 2 + 4 + 7 + 14 = 28. Remember, we have to exclude the number itself. That is
why we have not added 28 here. Some of the Java perfect numbers are 6, 28, 496,
8128, and 33550336, so on.

package FrequentPrograms;

import java.util.Scanner;

public class PerfectNumberUsingFor {

private static Scanner sc;

public static void main(String[] args) {

int i, Number, Sum = 0 ;

sc = new Scanner(System.in);

System.out.println("\n Please Enter any Number: ");

Number = sc.nextInt();

for(i = 1 ; i < Number ; i++) {

if(Number % i == 0) {

Sum = Sum + i;

}
if (Sum == Number) {

System.out.format("\n% d is a Perfect Number",


Number);

else {

System.out.format("\n% d is NOT a Perfect


Number", Number);

Output :

Enter a number : 6

6 is a string number

8.summation of odd and even numbers?


1.import java.util.Scanner;
2.public class Sum_Odd_Even
3.{
4. public static void main(String[] args)
5. {
6. int n, sumE = 0, sumO = 0;
7. Scanner s = new Scanner(System.in);
8. System.out.print("Enter the number of elements in array:");
9. n = s.nextInt();
10. int[] a = new int[n];
11. System.out.println("Enter the elements of the
array:");
12. for(int i = 0; i < n; i++)
13. {
14. a[i] = s.nextInt();
15. }
16. for(int i = 0; i < n; i++)
17. {
18. if(a[i] % 2 == 0)
19. {
20. sumE = sumE + a[i];
21. }
22. else
23. {
24. sumO = sumO + a[i];
25. }
26. }
27. System.out.println("Sum of Even Numbers:"+sumE);
28. System.out.println("Sum of Odd Numbers:"+sumO);
29. }
30. }

Output:

Enter the number of elements in array:6


Enter the elements of the array:
1
3
2
6
7
9
Sum of Even Numbers:8
Sum of Odd Numbers:20

9.fibonacci?

In fibonacci series, next number is the sum of previous two numbers for example 0,
1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The first two numbers of fibonacci series are 0
and 1.

1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15.}}

10.factorial?
Factorial Program in Java: Factorial of n is the product of all positive descending
integers. Factorial of n is denoted by n!. For example:

1. 4! = 4*3*2*1 = 24
2. 5! = 5*4*3*2*1 = 120

Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".

1. class FactorialExample{
2. public static void main(String args[]){
3. int i,fact=1;
4. int number=5;//It is the number to calculate factorial
5. for(i=1;i<=number;i++){
6. fact=fact*i;
7. }
8. System.out.println("Factorial of "+number+" is: "+fact);
9. }
10.}

11.sum of digits?
1.import java.util.Scanner;
2.public class Digit_Sum
3.{
4. public static void main(String args[])
5. {
6. int m, n, sum = 0;
7. Scanner s = new Scanner(System.in);
8. System.out.print("Enter the number:");
9. m = s.nextInt();
10. while(m > 0)
11. {
12. n = m % 10;
13. sum = sum + n;
14. m = m / 10;
15. }
16. System.out.println("Sum of Digits:"+sum);
17. }
18. }

Enter the number:456


Sum of Digits:15

12.Sum of 'n' natural numbers?

1. public class SumOfNaturalNumber2


2. {
3. public static void main(String[] args)
4. {
5. Scanner sc=new Scanner(System.in);
6. System.out.println(“Enter how many numbers sum to be calculated : “);
7. int num = sc.nextInt();
8. int i = 1, sum = 0;
9. //executes until the condition returns true
10.while(i <= num)
11.{
12.//adding the value of i into sum variable
13.sum = sum + i;
14.//increments the value of i by 1
15.i++;
16.}
17.//prints the sum
18.System.out.println("Sum of First “ + n + “ Natural Numbers is = " + sum);
19.}
20.}

13.bubblesort?

In bubble sort algorithm, array is traversed from first element to last element. Here,
current element is compared with the next element. If current element is greater
than the next element, it is swapped.

1. public class BubbleSortExample {


2. static void bubbleSort(int[] arr) {
3. int n = arr.length;
4. int temp = 0;
5. for(int i=0; i < n; i++){
6. for(int j=1; j < (n-i); j++){
7. if(arr[j-1] > arr[j]){
8. //swap elements
9. temp = arr[j-1];
10. arr[j-1] = arr[j];
11. arr[j] = temp;
12. }
13.
14. }
15. }
16.
17. }
18. public static void main(String[] args) {
19. int arr[] ={3,60,35,2,45,320,5};
20.
21. System.out.println("Array Before Bubble Sort");
22. for(int i=0; i < arr.length; i++){
23. System.out.print(arr[i] + " ");
24. }
25. System.out.println();
26.
27. bubbleSort(arr);//sorting array elements using bubble sort
28.
29. System.out.println("Array After Bubble Sort");
30. for(int i=0; i < arr.length; i++){
31. System.out.print(arr[i] + " ");
32. }
33.
34. }
35.}

14.w.ap. to display 1 to 100 without using any loop?

class GFG
{
// Prints numbers from 1 to n
static void printNos(int n)
{
if(n > 0)
{
printNos(n - 1);
System.out.print(n + " ");
}
return;
}

// Driver Code
public static void main(String[] args)
{
printNos(100);
}
}
15.Can a method return more than one value?if not how to return?

16.sum of array elements?

1. public class SumOfArray {


2. public static void main(String[] args) {
3. //Initialize array
4. int [] arr = new int [] {1, 2, 3, 4, 5};
5. int sum = 0;
6. //Loop through the array to calculate sum of elements
7. for (int i = 0; i < arr.length; i++) {
8. sum = sum + arr[i];
9. }
10. System.out.println("Sum of all the elements of an array: " + sum);
11. }
12.}

17.binary search?

Binary search is used to search a key element from multiple elements. Binary search
is faster than linear search.

In case of binary search, array elements must be in ascending order. If you have
unsorted array, you can sort the array using Arrays.sort(arr) method.

1. class BinarySearchExample{
2. public static void binarySearch(int arr[], int first, int last, int key){
3. int mid = (first + last)/2;
4. while( first <= last ){
5. if ( arr[mid] < key ){
6. first = mid + 1;
7. }else if ( arr[mid] == key ){
8. System.out.println("Element is found at index: " + mid);
9. break;
10. }else{
11. last = mid - 1;
12. }
13. mid = (first + last)/2;
14. }
15. if ( first > last ){
16. System.out.println("Element is not found!");
17. }
18. }
19. public static void main(String args[]){
20. int arr[] = {10,20,30,40,50};
21. int key = 30;
22. int last=arr.length-1;
23. binarySearch(arr,0,last,key);
24. }
25.}

18.addition of two matrix?

We can add two matrices in java using binary + operator. A matrix is also known as
array of arrays. We can add, subtract and multiply matrices.

1. public class MatrixAdditionExample{


2. public static void main(String args[]){
3. //creating two matrices
4. int a[][]={{1,3,4},{2,4,3},{3,4,5}};
5. int b[][]={{1,3,4},{2,4,3},{1,2,4}};
6.
7. //creating another matrix to store the sum of two matrices
8. int c[][]=new int[3][3]; //3 rows and 3 columns
9.
10.//adding and printing addition of 2 matrices
11.for(int i=0;i<3;i++){
12.for(int j=0;j<3;j++){
13.c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
14.System.out.print(c[i][j]+" ");
15.}
16.System.out.println();//new line
17.}
18.}}

19.transpose matrix?

Converting rows of a matrix into columns and columns of a matrix into row is called
transpose of a matrix.

1. public class MatrixTransposeExample{


2. public static void main(String args[]){
3. //creating a matrix
4. int original[][]={{1,3,4},{2,4,3},{3,4,5}};
5.
6. //creating another matrix to store transpose of a matrix
7. int transpose[][]=new int[3][3]; //3 rows and 3 columns
8.
9. //Code to transpose a matrix
10.for(int i=0;i<3;i++){
11.for(int j=0;j<3;j++){
12.transpose[i][j]=original[j][i];
13.}
14.}
15.
16.System.out.println("Printing Matrix without transpose:");
17.for(int i=0;i<3;i++){
18.for(int j=0;j<3;j++){
19.System.out.print(original[i][j]+" ");
20.}
21.System.out.println();//new line
22.}
23.System.out.println("Printing Matrix After Transpose:");
24.for(int i=0;i<3;i++){
25.for(int j=0;j<3;j++){
26.System.out.print(transpose[i][j]+" ");
27.}
28.System.out.println();//new line
29.}
30.}}

20.contigious array?

21.w.a.p to find biggest and least elemet is present in the array?


package org.arpit.java2blog;
/*
Java program to Find Largest and Smallest Number in an Array
*/
public class FindLargestSmallestNumberMain {

public static void main(String[] args) {

//array of 10 numbers
int arr[] = new int[]{12,56,76,89,100,343,21,234};

//assign first element of an array to largest and smallest


int smallest = arr[0];
int largest = arr[0];

for(int i=1; i< arr.length; i++)


{
if(arr[i] > largest)
largest = arr[i];
else if (arr[i] < smallest)
smallest = arr[i];

}
System.out.println("Smallest Number is : " + smallest);
System.out.println("Largest Number is : " + largest);
}
}

When you run above program, you will get below output:
1
2
Largest Number is : 343
3
Smallest Number is : 12
4

22.w.a.p to reverse an integer array and display?


1. public class ReverseArray {
2. public static void main(String[] args) {
3. //Initialize array
4. int [] arr = new int [] {1, 2, 3, 4, 5};
5. System.out.println("Original array: ");
6. for (int i = 0; i < arr.length; i++) {
7. System.out.print(arr[i] + " ");
8. }
9. System.out.println();
10. System.out.println("Array in reverse order: ");
11. //Loop through the array in reverse order
12. for (int i = arr.length-1; i >= 0; i--) {
13. System.out.print(arr[i] + " ");
14. }
15. }
16.}

23.w.a.p to find duplicate elemenst in array?

1. public class DuplicateElement {


2. public static void main(String[] args) {
3.
4. //Initialize array
5. int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3};
6.
7. System.out.println("Duplicate elements in given array: ");
8. //Searches for duplicate element
9. for(int i = 0; i < arr.length; i++) {
10. for(int j = i + 1; j < arr.length; j++) {
11. if(arr[i] == arr[j])
12. System.out.println(arr[j]);
13. }
14. }
15. }
16.}

25.reverese string?
Reverse string by CHARACTERS:
public static void main(String[] args) {
// Using traditional approach
String result="";
for(int i=string.length()-1; i>=0; i--) {
result = result + string.charAt(i);
}
System.out.println(result);

// Using StringBuffer class


StringBuffer buffer = new StringBuffer(string);
System.out.println(buffer.reverse());
}
Reverse string by WORDS:
public static void reverseStringByWords(String string) {
StringBuilder stringBuilder = new StringBuilder();
String[] words = string.split(" ");

for (int j = words.length-1; j >= 0; j--) {


stringBuilder.append(words[j]).append(' ');
}
System.out.println("Reverse words: " + stringBuilder);
}
}

26.reverse sentence?

27.palindrome in string?
import java.util.Scanner;

class ChkPalindrome

public static void main(String args[])

String str, rev = "";

Scanner sc = new Scanner(System.in);

System.out.println("Enter a string:");

str = sc.nextLine();

int length = str.length();


for ( int i = length - 1; i >= 0; i-- )

rev = rev + str.charAt(i);

if (str.equals(rev))

System.out.println(str+" is a palindrome");

else

System.out.println(str+" is not a palindrome");

Program Output:
Enter a string:

radar

radar is a palindrome

28.w.a.p to find length of the string without using length fuction?

public class StringLength {

public static void main(String args[]) throws Exception {

String str = "sampleString";

int i = 0;

for(char c: str.toCharArray()) {

i++;

System.out.println("Length of the given string ::"+i);

}
}

29.w.a.p to count number of vowels,space and digits present in the array?

class Main {

public static void main(String[] args) {


String line = "This website is aw3som3.";
int vowels = 0, consonants = 0, digits = 0, spaces = 0;

line = line.toLowerCase();
for (int i = 0; i < line.length(); ++i) {
char ch = line.charAt(i);

// check if character is any of a, e, i, o, u


if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
++vowels;
}

// check if character is in between a to z


else if ((ch >= 'a' && ch <= 'z')) {
++consonants;
}

// check if character is in between 0 to 9


else if (ch >= '0' && ch <= '9') {
++digits;
}

// check if character is a white space


else if (ch == ' ') {
++spaces;
}
}

System.out.println("Vowels: " + vowels);


System.out.println("Consonants: " + consonants);
System.out.println("Digits: " + digits);
System.out.println("White spaces: " + spaces);
}
}

30.w.a.p to find repeated characters are present instring?


public class Example {

public static void main(String argu[]) {

String str = "beautiful beach";

char[] carray = str.toCharArray();

System.out.println("The string is:" + str);

System.out.print("Duplicate Characters in above string are:


");

for (int i = 0; i < str.length(); i++) {

for (int j = i + 1; j < str.length(); j++) {

if (carray[i] == carray[j]) {

System.out.print(carray[j] + " ");

break;

31.w.a.p to find non-repeated characters in String?

32.anagram?

Two strings are called anagrams if they contain same set of characters but in
different order.

"keep ? peek", "Mother In Law - Hitler Woman".

1. import java.util.Arrays;
2.
3. public class AnagramString {
4. static void isAnagram(String str1, String str2) {
5. String s1 = str1.replaceAll("\\s", "");
6. String s2 = str2.replaceAll("\\s", "");
7. boolean status = true;
8. if (s1.length() != s2.length()) {
9. status = false;
10. } else {
11. char[] ArrayS1 = s1.toLowerCase().toCharArray();
12. char[] ArrayS2 = s2.toLowerCase().toCharArray();
13. Arrays.sort(ArrayS1);
14. Arrays.sort(ArrayS2);
15. status = Arrays.equals(ArrayS1, ArrayS2);
16. }
17. if (status) {
18. System.out.println(s1 + " and " + s2 + " are anagrams");
19. } else {
20. System.out.println(s1 + " and " + s2 + " are not anagrams");
21. }
22. }
23.
24. public static void main(String[] args) {
25. isAnagram("Keep", "Peek");
26. isAnagram("Mother In Law", "Hitler Woman");
27. }
28.}

Output:

Keep and Peek are anagrams


MotherInLaw and HitlerWoman are anagrams

33.w.a.p tp remove the space which is present in the sentence?

class BlankSpace {
public static void main(String[] args)
{
String str = " Geeks for Geeks ";

// Call the replaceAll() method


str = str.replaceAll("\\s", "");

System.out.println(str);
}
}

34.w.a.p to find to count the number of words present in the text file?

35.w.a.p to count commented line number available in textfile?


36.All pattern programs?

37.w.a.p to find out whether the year is leap or not?

Finding a year is a leap or not is a bit tricky. We generally assume that if a year
number is evenly divisible by 4 is a leap year. But it is not the only case. A year is a
leap year if −

 1. It is evenly divisible by 100


 2. If it is divisible by 100, then it should also be divisible by 400
 3. Except this, all other years evenly divisible by 4 are leap years.
Algorithm
 1. Take integer variable year
 2. Assign a value to the variable
 3. Check if the year is divisible by 4 but not 100, DISPLAY "leap year"
 4. Check if the year is divisible by 400, DISPLAY "leap year"
 5. Otherwise, DISPLAY "not leap year"

 import java.util.Scanner;

 public class LeapYear {

 public static void main(String[] args){

 int year;

 System.out.println("Enter an Year :: ");

 Scanner sc = new Scanner(System.in);

 year = sc.nextInt();

 if (((year % 4 == 0) && (year % 100!= 0)) || (year%400


== 0))

 System.out.println("Specified year is a leap


year");

 else

 System.out.println("Specified year is not a leap


year");

 }

 }

 Output 1
 Enter an Year ::
 2020
 Specified year is a leap year

All Pattern Programs

Star Pattern
1. Right Triangle Star Pattern

1. public class RightTrianglePattern


2. {
3. public static void main(String args[])
4. {
5. //i for rows and j for columns
6. //row denotes the number of rows you want to print
7. int i, j, row=6;
8. //outer loop for rows
9. for(i=0; i<row; i++)
10.{
11.//inner loop for columns
12.for(j=0; j<=i; j++)
13.{
14.//prints stars
15.System.out.print("* ");
16.}
17.//throws the cursor in a new line after printing each line
18.System.out.println();
19.}
20.}
21.}

Output:

2. Left Triangle Star Pattern


1. public class LeftTrianglePattern
2. {
3. public static void main(String args[])
4. {
5. //i for rows and j for columns
6. //row denotes the number of rows you want to print
7. int i, j, row = 6;
8. //Outer loop work for rows
9. for (i=0; i<row; i++)
10.{
11.//inner loop work for space
12.for (j=2*(row-i); j>=0; j--)
13.{
14.//prints space between two stars
15.System.out.print(" ");
16.}
17.//inner loop for columns
18.for (j=0; j<=i; j++ )
19.{
20.//prints star
21.System.out.print("* ");
22.}
23.//throws the cursor in a new line after printing each line
24.System.out.println();
25.}
26.}
27.}

Output:

3. Pyramid Star Pattern

1. public class PyramidPattern


2. {
3. public static void main(String args[])
4. {
5. //i for rows and j for columns
6. //row denotes the number of rows you want to print
7. int i, j, row = 6;
8. //Outer loop work for rows
9. for (i=0; i<row; i++)
10.{
11.//inner loop work for space
12.for (j=row-i; j>1; j--)
13.{
14.//prints space between two stars
15.System.out.print(" ");
16.}
17.//inner loop for columns
18.for (j=0; j<=i; j++ )
19.{
20.//prints star
21.System.out.print("* ");
22.}
23.//throws the cursor in a new line after printing each line
24.System.out.println();
25.}
26.}
27.}

Output:

4. Diamond Shape Pattern

1. import java.util.Scanner;
2. public class DiamondPattern
3. {
4. public static void main(String args[])
5. {
6. int row, i, j, space = 1;
7. System.out.print("Enter the number of rows you want to print: ");
8. Scanner sc = new Scanner(System.in);
9. row = sc.nextInt();
10.space = row - 1;
11.for (j = 1; j<= row; j++)
12.{
13.for (i = 1; i<= space; i++)
14.{
15.System.out.print(" ");
16.}
17.space--;
18.for (i = 1; i <= 2 * j - 1; i++)
19.{
20.System.out.print("*");
21.}
22.System.out.println("");
23.}
24.space = 1;
25.for (j = 1; j<= row - 1; j++)
26.{
27.for (i = 1; i<= space; i++)
28.{
29.System.out.print(" ");
30.}
31.space++;
32.for (i = 1; i<= 2 * (row - j) - 1; i++)
33.{
34.System.out.print("*");
35.}
36.System.out.println("");
37.}
38.}
39.}
Output:

5. Downward Triangle Star Pattern

1. public class DownwardTrianglePattern


2. {
3. public static void main(String[] args)
4. {
5. int rows=7;
6. //inner loop
7. for (int i= rows-1; i>=0 ; i--)
8. {
9. //outer loop
10.for (int j=0; j<=i; j++)
11.{
12.//prints star and space
13.System.out.print("*" + " ");
14.}
15.//throws the cursor in the next line after printing each line
16.System.out.println();
17.}
18.}
19.}

Output:
6. Mirrored Right Triangle Star Pattern

1. public class MirroredRightTrianglePattern


2. {
3. public static void main(String[] args)
4. {
5. int n=7;
6. //inner loop
7. for (int i= 0; i<= n; i++)
8. {
9. //outer loop
10.for (int j=1; j<=n-i; j++)
11.{
12.System.out.print(" ");
13.}
14.for (int k=0;k<=i;k++)
15.{
16.System.out.print("*");
17.}
18.System.out.println("");
19.}
20.}
21.}

Output:

7. Reverse Pyramid Star Pattern


1. public class ReversePyramidPattern
2. {
3. public static void main(String[] args)
4. {
5. int rows=8;
6. for (int i= 0; i<= rows-1; i++)
7. {
8. for (int j=0; j<=i; j++)
9. {
10.System.out.print(" ");
11.}
12.for (int k=0; k<=rows-1-i; k++)
13.{
14.System.out.print("*" + " ");
15.}
16.System.out.println();
17.}
18.}
19.}

Output:

8. Right Down Mirror Star Pattern

1. public class RightDownMirrorPattern


2. {
3. public static void main(String args[])
4. {
5. int row=7;
6. for (int i= row; i>= 1; i--)
7. {
8. for (int j=row; j>i;j--)
9. {
10.System.out.print(" ");
11.}
12.for (int k=1;k<=i;k++)
13.{
14.System.out.print("*");
15.}
16.System.out.println("");
17.}
18.}
19.}

Output:

9. Right Pascal's Triangle

1. import java.util.Scanner;
2. public class RightPascalTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10.for (i= 0; i<= rows-1; i++)
11.{
12.for (j=0; j<=i; j++)
13.{
14.System.out.print("*"+ " ");
15.}
16.System.out.println("");
17.}
18.for (i=rows-1; i>=0; i--)
19.{
20.for(j=0; j <= i-1;j++)
21.{
22.System.out.print("*"+ " ");
23.}
24.System.out.println("");
25.}
26.}
27.}

Output:

10. Left Pascal's Triangle

1. import java.util.Scanner;
2. public class LeftPascalTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10.for (i= 1; i<= rows ; i++)
11.{
12.for (j=i; j <rows ;j++)
13.{
14.System.out.print(" ");
15.}
16.for (k=1; k<=i;k++)
17.{
18.System.out.print("*");
19.}
20.System.out.println("");
21.}
22.for (i=rows; i>=1; i--)
23.{
24.for(j=i; j<=rows;j++)
25.{
26.System.out.print(" ");
27.}
28.for(k=1; k<i ;k++)
29.{
30.System.out.print("*");
31.}
32.System.out.println("");
33.}
34.sc.close();
35.}
36.}

Output:

11. Sandglass Star Pattern


1. import java.util.Scanner;
2. public class SandglassPattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, n;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. n = sc.nextInt();
10.for (i= 0; i<= n-1 ; i++)
11.{
12.for (j=0; j<i; j++)
13.{
14.System.out.print(" ");
15.}
16.for (k=i; k<=n-1; k++)
17.{
18.System.out.print("*" + " ");
19.}
20.System.out.println("");
21.}
22.for (i= n-1; i>= 0; i--)
23.{
24.for (j=0; j<i; j++)
25.{
26.System.out.print(" ");
27.}
28.for (k=i; k<=n-1; k++)
29.{
30.System.out.print("*" + " ");
31.}
32.System.out.println("");
33.}
34.sc.close();
35.}
36.}
Output:

12. Alphabet Star Pattern

1. import java.util.*;
2. public class AlphabetPattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, n=8;
7. // Outer for loop for number of lines
8. for (i = 0; i<=n; i++)
9. {
10.// Inner for loop for logic execution
11.for (j = 0; j<= n / 2; j++)
12.{
13.// prints two vertical lines
14.if ((j == 0 || j == n / 2) && i != 0 ||
15.// print first line of alphabet
16.i == 0 && j != n / 2 ||
17.// prints middle line
18.i == n / 2)
19.System.out.print("*");
20.else
21.System.out.print(" ");
22.}
23.System.out.println();
24.}
25.}
26.}

Output:

13. Triangle Star Pattern

1. import java.util.Scanner;
2. public class TrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows=9;
7. for (i=1; i<= rows ; i++)
8. {
9. for (j = i; j < rows ; j++)
10.{
11.System.out.print(" ");
12.}
13.for (k = 1; k <= (2*i -1) ;k++)
14.{
15.if(k==1 || i == rows || k==(2*i-1))
16.{
17.System.out.print("*");
18.}
19.else
20.{
21.System.out.print(" ");
22.}
23.}
24.System.out.println("");
25.}
26.}
27.}

Output:

14. Down Triangle Pattern

1. import java.util.Scanner;
2. public class DownTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows=9;
7. for (i=rows; i>= 1 ; i--)
8. {
9. for (j = i; j<rows ; j++)
10.{
11.System.out.print(" ");
12.}
13.for (k = 1; k <= (2*i -1) ;k++)
14.{
15.if( k==1 || i == rows || k==(2*i-1))
16.{
17.System.out.print("*");
18.}
19.else
20.{
21.System.out.print(" ");
22.}
23.}
24.System.out.println("");
25.}
26.}
27.}

Output:

15. Diamond Star Pattern

1. import java.util.*;
2. public class DiamondPattern
3. {
4. public static void main(String[] args)
5. {
6. Scanner sc = new Scanner(System.in);
7. System.out.println("Enter the number of rows you want to print: ");
8. int rows = sc.nextInt();
9. for (i=1; i<= rows ; i++)
10.{
11.for (j = rows; j > i ; j--)
12.{
13.System.out.print(" ");
14.}
15.System.out.print("*");
16.for (k = 1; k < 2*(i -1) ;k++)
17.{
18.System.out.print(" ");
19.}
20.if( i==1)
21.{
22.System.out.println("");
23.}
24.else
25.{
26.System.out.println("*");
27.}
28.}
29.for (i=rows-1; i>= 1 ; i--)
30.{
31.for (int j = rows; j > i ; j--)
32.{
33.System.out.print(" ");
34.}
35.System.out.print("*");
36.for (int k = 1; k < 2*(i -1) ;k++)
37.{
38.System.out.print(" ");
39.}
40.if(i==1)
41.System.out.println("");
42.else
43.System.out.println("*");
44.}
45.}
46.}

Output:
Number Pattern
1. Pattern-1

1. public class Pattern1


2. {
3. public static void main(String args[])
4. {
5. int i, j,number, n=7;
6. //loop for rows
7. for(i=0; i<n; i++)
8. {
9. number=1;
10.//loop for columns
11.for(j=0; j<=i; j++)
12.{
13.//prints num
14.System.out.print(number+ " ");
15.//incrementing the value of number
16.number++;
17.}
18.//throws the cursor at the next line after printing each row
19.System.out.println();
20.}
21.}
22.}

Output:

2. Pattern-2

1. public class Pattern2


2. {
3. public static void main(String[] args)
4. {
5. int i, j, k = 1;
6. //inner loop
7. for (i = 1; i <= 7; i++)
8. {
9. //outer loop
10.for (j = 1; j< i + 1; j++)
11.{
12.//prints the value of k
13.System.out.print(k++ + " ");
14.}
15.//throws the cursor at the next line
16.System.out.println();
17.}
18.}
19.}

Output:
3. Pattern-3

1. public class Pattern3


2. {
3. public static void main(String[] args)
4. {
5. int n = 8; //n is the number of rows you want to print
6. for (int i = 0; i < n; i++)
7. {
8. int number = 1;
9. System.out.printf("%" + (n - i) * 2 + "s", "");
10.for (int j = 0; j <= i; j++)
11.{
12.System.out.printf("%4d", number);
13.number = number * (i - j) / (j + 1);
14.}
15.System.out.println();
16.}
17.}
18.}

Output:

4. Pattern-4

1. public class Pattern4


2. {
3. public static void main(String[] args)
4. {
5. for (int i = 1; i <= 4; i++)
6. {
7. int n = 8;
8. for (int j = 1; j<= n - i; j++)
9. {
10.System.out.print(" ");
11.}
12.for (int k = i; k >= 1; k--)
13.{
14.System.out.print(k);
15.}
16.for (int l = 2; l <= i; l++)
17.{
18.System.out.print(l);
19.}
20.System.out.println();
21.}
22.for (int i = 3; i >= 1; i--)
23.{
24.int n = 10;
25.for (int j = 0; j<= n - i; j++)
26.{
27.System.out.print(" ");
28.}
29.for (int k = i; k >= 1; k--)
30.{
31.System.out.print(k);
32.}
33.for (int l = 2; l <= i; l++)
34.{
35.System.out.print(l);
36.}
37.System.out.println();
38.}
39.}
40.}
Output:

5. Pattern-5

1. import java.util.*;
2. public class Pattern5
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10.for (i = 1; i <= rows; i++)
11.{
12.for (j = 1; j <= i; j++)
13.{
14.System.out.print(i+" ");
15.}
16.System.out.println();
17.}
18.}
19.}

Output:
6. Pattern-6

1. import java.util.*;
2. public class Pattern6
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows youy want to print: ");
9. rows = sc.nextInt();
10.for (i = rows; i >= 1; i--)
11.{
12.for (j = rows; j >= i; j--)
13.{
14.System.out.print(j+" ");
15.}
16.
17.System.out.println();
18.}
19.}
20.}

Output:

7. Pattern-7

1. import java.util.Scanner;
2. public class Pattern7
3. {
4. public static void main(String[] args)
5. {
6. int i, j, n;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. n = sc.nextInt();
10.for (i = 1; i <= n; i++)
11.{
12.for (j = i; j >= 1; j--)
13.{
14.System.out.print(j+" ");
15.}
16.System.out.println();
17.}
18.}
19.}

Output:

8. Pattern-8

1. public class Pattern8


2. {
3. public static void main(String[] args)
4. {
5. int rows=9; //number of rows to print
6. for (int i = 1; i <= rows; i++)
7. {
8. int num;
9. if(i%2 == 0)
10.{
11.num = 0;
12.for (int j = 1; j <= rows; j++)
13.{
14.System.out.print(num);
15.num = (num == 0)? 1 : 0;
16.}
17.}
18.else
19.{
20.num = 1;
21.for (int j = 1; j <= rows; j++)
22.{
23.System.out.print(num);
24.num = (num == 0)? 1 : 0;
25.}
26.}
27.System.out.println();
28.}
29.}
30.}

Output:

9. Pattern-9

1. import java.util.Scanner;
2. public class Pattern9
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows=9;
7. for (i = 1; i <= rows; i++)
8. {
9. for (j = 1; j <= i; j++)
10.{
11.if(j%2 == 0)
12.{
13.System.out.print(0);
14.}
15.else
16.{
17.System.out.print(1);
18.}
19.}
20.System.out.println();
21.}
22.}
23.}

Output:

10. Pattern-10

1. public class Pattern10


2. {
3. public static void main(String[] args)
4. {
5. int n = 10;
6. for (int i = 1; i <= n; i++)
7. {
8. for (int j = 1; j < i; j++)
9. {
10.System.out.print(" ");
11.}
12.for (int k = i; k <= n; k++)
13.{
14.System.out.print(k+" ");
15.}
16.System.out.println();
17.}
18.for (int i = n-1; i >= 1; i--)
19.{
20.for (int j = 1; j < i; j++)
21.{
22.System.out.print(" ");
23.}
24.for (int k = i; k <= n; k++)
25.{
26.System.out.print(k+" ");
27.}
28.System.out.println();
29.}
30.}
31.}

Output:

11. Pattern-11

1. public class Pattern11


2. {
3. public static void main(String[] args)
4. {
5. int rows=8;
6. //Prints upper half pattern
7. for (int i = 1; i <= rows; i++)
8. {
9. for (int j = 1; j <= i; j++)
10.{
11.System.out.print(j+" ");
12.}
13.System.out.println();
14.}
15.//prints lower half pattern
16.for (int i = rows-1; i >= 1; i--)
17.{
18.for (int j = 1; j <= i; j++)
19.{
20.System.out.print(j+" ");
21.}
22.System.out.println();
23.}
24.}
25.}

Output:

12. Pattern-12
1. public class Pattern12
2. {
3. public static void main(String[] args)
4. {
5. int rows=9;
6. for (int i = 1; i <= rows; i++)
7. {
8. for (int j = rows; j >= i; j--)
9. {
10.System.out.print(j+" ");
11.}
12.System.out.println();
13.}
14.}
15.}

Output:

13. Pattern-13

1. public class Pattern14


2. {
3. public static void main(String[] args)
4. {
5. int i, j, rows=9;
6. for (i = rows; i >= 1; i--)
7. {
8. for (j = 1; j <= i; j++)
9. {
10.System.out.print(j+" ");
11.}
12.System.out.println();
13.}
14.}
15.}

Output:

14. Pattern-14

1. public class Pattern14


2. {
3. public static void main(String[] args)
4. {
5. int rows=7, i, j;
6. for (i = rows; i >= 1; i--)
7. {
8. for (j = i; j >= 1; j--)
9. {
10.System.out.print(j+" ");
11.}
12.System.out.println();
13.}
14.}
15.}

Output:
15. Pattern-15

1. public class Pattern15


2. {
3. public static void main(String[] args)
4. {
5. int i, j, rows=9;
6. for (i = 1; i <= rows; i++)
7. {
8. //Prints first half of the row
9. for (j = 1; j <= i; j++)
10.{
11.System.out.print(j+" ");
12.}
13.//Prints second half of the row
14.for (j = i-1; j >= 1; j--)
15.{
16.System.out.print(j+" ");
17.}
18.System.out.println();
19.}
20.}
21.}

Output:

16. Pattern-16

1. public class Pattern16


2. {
3. public static void main(String[] args)
4. {
5. int i, j, rows=9;
6. //Prints upper half pattern
7. for (i = rows; i >= 1; i--)
8. {
9. for (j = 1; j <= i; j++)
10.{
11.System.out.print(j+" ");
12.}
13.System.out.println();
14.}
15.//Prints lower half pattern
16.for (i = 2; i <= rows; i++)
17.{
18.for (j = 1; j <= i; j++)
19.{
20.System.out.print(j+" ");
21.}
22.System.out.println();
23.}
24.}
25.}

Output:

17. Pattern-17
1. public class Pattern17
2. {
3. public static void main(String[] args)
4. {
5. int rows=9;
6. //Prints upper half pattern
7. for (int i = 1; i <= rows; i++)
8. {
9. //Prints i spaces at the beginning of each row
10.for (int j = 1; j < i; j++)
11.{
12.System.out.print(" ");
13.}
14.//Prints i to rows value at the end of each row
15.for (int j = i; j <= rows; j++)
16.{
17.System.out.print(j);
18.}
19.System.out.println();
20.}
21.//Prints lower half pattern
22.for (int i = rows-1; i >= 1; i--)
23.{
24.//Prints i spaces at the beginning of each row
25.for (int j = 1; j < i; j++)
26.{
27.System.out.print(" ");
28.}
29.//Prints i to rows value at the end of each row
30.for (int j = i; j <= rows; j++)
31.{
32.System.out.print(j);
33.}
34.System.out.println();
35.}
36.}
37.}
Output:

18. Pattern-18

1. public class Pattern18


2. {
3. public static void main(String[] args)
4. {
5. int rows=8;
6. for (int i = 1; i <= rows; i++)
7. {
8. for (int j = 1; j <= rows-i; j++)
9. {
10.System.out.print(1);
11.}
12.for (int j = 1; j <= i; j++)
13.{
14.System.out.print(i);
15.}
16.System.out.println();
17.}
18.}
19.}

Output:
19. Pattern-19

1. public class Pattern19


2. {
3. public static void main(String args[])
4. {
5. int rows=9;
6. for (int i = 1; i <= rows; i++)
7. {
8. int num = i;
9. for (int j = 1; j <= i; j++)
10.{
11.System.out.print(num+" ");
12.num = num+rows-j;
13.}
14.System.out.println();
15.}
16.}
17.}

Output:

20. Pattern-20

1. public class Pattern20


2. {
3. public static void main(String[] args)
4. {
5. int i, j, k, rows=9;
6. for(i=1;i< rows+1 ;i++)
7. {
8. for(j=i; j < rows+1 ;j++)
9. {
10.System.out.print(j + " ");
11.}
12.for(k=1; k < i ;k++)
13.{
14.System.out.print(k + " ");
15.}
16.System.out.println();
17.}
18.}
19.}

Output:

21. Pattern-21

1. import java.util.Scanner;
2. public class Pattern21
3. {
4. public static void main(String[] args)
5. {
6. int i, j, min, n; //n is the number up to you want to print
7. System.out.print("Enter the value of n: ");
8. Scanner sc= new Scanner(System.in);
9. n=sc.nextInt();
10.//loo for upper left part
11.for (i = 1; i <= n; i++)
12.{
13.for (j = 1; j <= n; j++)
14.{
15.min = i < j ? i : j;
16.System.out.print(n - min + 1+ " ");
17.}
18.//loop for upper right part
19.for (j = n - 1; j >= 1; j--)
20.{
21.min = i < j ? i : j;
22.System.out.print(n - min + 1+ " ");
23.}
24.System.out.println();
25.}
26.//loop for lower left part
27.for (i = n - 1; i >= 1; i--)
28.{
29.for (j = 1; j <= n; j++)
30.{
31.min = i < j ? i : j;
32.System.out.print(n - min + 1+ " ");
33.}
34.//loop for lower right part
35.for (j = n - 1; j >= 1; j--)
36.{
37.min = i < j ? i : j;
38.System.out.print(n - min + 1+ " ");
39.}
40.System.out.println();
41.}
42.}
43.}

Output:
Character Pattern
1. Right Triangle Alphabetic Pattern

1. public class RightAlphabaticPattern


2. {
3. public static void main(String[] args)
4. {
5. int alphabet = 65; //ASCII value of capital A is 65
6. //inner loop for rows
7. for (int i = 0; i <= 8; i++)
8. {
9. //outer loop for columns
10.for (int j = 0; j <= i; j++)
11.{
12.//adds the value of j in the ASCII value of A and prints the corresponding alphabet
13.System.out.print((char) (alphabet + j) + " ");
14.}
15.System.out.println();
16.}
17.}
18.}

Output:
2. Repeating Alphabet Pattern

1. public class RepeatingPattern


2. {
3. public static void main(String[] args)
4. {
5. int letter = 65; //ASCII value of capital A is 65
6. //inner loop for rwos
7. for (int i = 0; i<= 9; i++)
8. {
9. //outer loop for columns
10.for (int j = 0; j <= i; j++)
11.{
12.//prints the character
13.System.out.print((char) letter + " ");
14.}
15.letter++;
16.System.out.println();
17.}
18.}
19.}

Output:
3. K-shape Alphabet Pattern

1. public class KshapePattern


2. {
3. public static void main(String[] args)
4. {
5. for (int i = 8; i >= 0; i--)
6. {
7. int alphabet = 65;
8. for (int j = 0; j <= i; j++)
9. {
10.System.out.print((char) (alphabet + j) + " ");
11.}
12.System.out.println();
13.}
14.for (int i = 0; i<= 8; i++)
15.{
16.int alphabet = 65;
17.for (int j = 0; j <= i; j++)
18.{
19.System.out.print((char) (alphabet + j) + " ");
20.}
21.System.out.println();
22.}
23.}
24.}

Output:
4. Triangle Character Pattern

1. public class TriangleCharacterPattern


2. {
3. public static void main(String[] args)
4. {
5. for (int i = 0; i <= 8; i++)
6. {
7. int alphabet = 65;
8. for (int j = 8; j > i; j--)
9. {
10.System.out.print(" ");
11.}
12.for (int k = 0; k <= i; k++)
13.{
14.System.out.print((char) (alphabet + k) + " ");
15.}
16.System.out.println();
17.}
18.}
19.}

Output:
5. Diamond Character Pattern

1. import java.util.Scanner;
2. public class DiamondCharacterPattern
3. {
4. public static void main(String[] args)
5. {
6. char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
7. int alphabet _number = 0;
8. String[] diamond = new String[26]; // array of strings
9. System.out.print("Enter a Character between A to Z : ");
10.Scanner reader = new Scanner(System.in);
11.try
12.{
13.char user_ alphabet = reader.next("[A-Z]").charAt(0);
14.// search for letter number in the array letter
15.for (int i = 0; i < alphabet.length; i++)
16.{
17.if (letter[i] == user_ alphabet)
18.{
19.alphabet _number = i;
20.break;
21.}
22.}
23.//construct diamond
24.for (int i = 0; i <= alphabet _number; i++)
25.{
26.diamond[i] = "";
27.//add initial spaces
28.for (int j = 0; j < alphabet _number - i; j++)
29.{
30.diamond[i] += " ";
31.}
32.// add alphabet
33.diamond[i] += alphabet
34.//add space between letters
35.if (alphabet[i] != 'A')
36.{
37.for (int j = 0; j < 2 * i - 1; j++)
38.{
39.diamond[i] += " ";
40.}
41.// add alphabet
42.diamond[i] += alphabet[i];
43.}
44.// Draw the first part of the diamond
45.System.out.println(diamond[i]);
46.}
47.for (int i = alphabet _number - 1; i >= 0; i--)
48.{
49.// Draw the second part of the diamond
50.// prints the diamondArray in the reverse order
51.System.out.println(diamond[i]);
52.}
53.}
54.catch (Exception e)
55.{
56.e.printStackTrace();
57.}
58.finally
59.{
60.reader.close();
61.}
62.}
63.}
Output:

You might also like