Tuesday, 30 November 2021

Java Program to divide a string in 'N' equal parts

N_divide

Java Program to divide a string in 'N' equal parts.

Here, our task is to divide the string S into n equal parts. We will print an error message if the string cannot be divisible into n equal parts otherwise all the parts need to be printed as the output of the program.

To check whether the string can be divided into N equal parts, we need to divide the length of the string by n and assign the result to variable chars.

If the char comes out to be a floating point value, we can't divide the string otherwise run for loop to traverse the string and divide the string at every chars interval.

string 1

"aaaabbbbcccc"

Class

Program

class N_divide {

  public static void main(String[] args)   {

    //declare array

   String str1="Brag";

   int len = str.length();

   int n=3, temp=0, chars=len/n

   String[] equalStr = new String [n];

     //Check whether a string can be divided into n equal parts

   if (len % n != 0) {

       System.out.println("Sorry this string cannot be divided into " + n + " equal parts." ); }

   else {

       for(int i = 0; i < len; i = i+chars) {

            String part = str.substring(i, i+chars);

           equalStr[temp] = part;

             temp++;

         }

      }

      System.out.println(" equal parts of given string are " + n);

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

          System.out.println(equalStr[i]);
       }

   }

   }

output

3 equal parts of given string are
aaaa bbbb cccc

Theory

Monday, 29 November 2021

Java Program to count the total number of vowels and consonants in a string

vowel

Java Program to count the total number of vowels and consonants in a string

In this program, our task is to count the total number of vowels and consonants present in the given string. As we know that, the characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known as the consonant.

To solve this problem, First of all, we need to convert every upper-case character in the string to lower-case so that the comparisons can be done with the lower-case vowels only not upper-case vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match each character with all the vowels, i.e., a, e, i, o, u. If the match is found, increase the value of count by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given below.

string

this is a comman name

Class

Program

class vowel {

public static void main(String[] args)

//declare String

String str1="this is a comman name";

int vCount = 0, Ccount = 0;

//Converting both the string to lower case.

str1 = str1.toLowerCase();

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

{

if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='u' || str.charAt(i)=='o' || str.charAt(i)=='i' )

{

vcount++;

}

else if (str.charAt(i)>='a' && str.charAt(i) < ='z')

{

Ccount++;

}

System.out.println("total vowel is = "+ vcount);
System.out.println("constants is =" + Ccount);

}

}

output

total vowel is = 7
constants is =10

Theory:-

In this problem to firstly know what is vowels and constants .we have to five alphabate 'a' 'e' 'u' 'i' 'o' is the vowels . renaming alphabate are the constants expect black space and syball are not the constants.

To calculate of given string to how many vowels and constants are the present in string.

we are fristly to given string convert to either uppercase or lowercase .then use for loop to strating 0 and end to string length. in loop to specific word compair to vowels is use ChartAt() method in string. if condition is true then increase count of vowels. if condition is false then count constants of the given string.

Program to determine whether two strings are the anagram

Anagram

Program to determine whether two strings are the anagram

Two Strings are called the anagram if they contain the same characters. However, the order or sequence of the characters can be different. In this program, our task is to check for two strings that, they are the anagram or not. For this purpose, we are following a simpler approach.

First of all, Compare the length of the strings, if they are not equal in the length then print the error message and make an exit, otherwise, convert the string into lower-case for the easy comparisons. Sort both the strings using bubble sort or other sorting methods. If the strings are found to be identical after sorting, then print that strings are anagram otherwise print that strings are not the anagram.

string 1

Brag

String 2

Grab

Class

Program

class Anagram {

public static void main(String[] args)

//declare array

String str1="Brag";

String str2="Grab";

//Converting both the string to lower case.

str1 = str1.toLowerCase();

str2 = str2.toLowerCase();

//Checking for the length of strings

if (str1.length() != str2.length()) {

System.out.println( "Both the strings are not anagram" );

}

else{

//Converting both the arrays to character array

char[] string1 = str1.toCharArray();

char[] string2 = str2.toCharArray();

//Sorting the arrays using in-built function sort ()

Arrays.sort(string1);
Arrays.sort(string2);

//Comparing both the arrays using in-built function equals ()

if (Arrays.equals(string1, string2) == true ) {

System.out.println( "Both the strings are anagram" );

}

else{

System.out.println( "Both the strings are not anagram" );

}

}

output

Both the strings are anagram

Theory:-

the two string are called the anagram ,so that is some word/letter in the oder of sequnce is differnt. in this program taks to find the given two strings are anagram or not anagram.for this purpose the following method are given below:-

first of all, the given strings are to convert either uppercase or lowercase .then to find length of the given string by useing length() function of string. firstly we have to check the strings are equals length then to find out the anagram string but not length are equals then show out the error of the given program.

then , first of all string are converted into array .it is easy to sorting the string(array) as basis of the our requirement .in this problem we have to use Arrays sort() method use that is to sorted the given string. and then compaire two string are the true (quals).then print that strings are anagram otherwise that string are not anagram.

Sunday, 28 November 2021

Top 40 DAA Interview Questions (2021)

oops/c++

DAA Interview Questions

The name 'Algorithm' refers to the sequence of instruction that must be followed to clarify a problem.
The logical description of the instructions which may be executed to perform an essential function.

The time complexity of an algorithm denoted the total time needed by the program to run to completion. It is generally expressed by using the big O notation.

Any subset that satisfies these constraints is known as a feasible solution. A feasible solution, which either maximizes or minimizes a given purpose method is known as an optimal solution.

DP is another method for problems with optimal substructure: An optimal solution to a problem include optimal solutions to subproblems. This doesn't necessarily define that every optimal solution to a subproblem will contribute to the primary solution.

For divide and conquer (top-down), the subproblems are independent, so we can resolve them in any order.

Warshall's algorithm is a function of dynamic programming procedure, which is used to find the transitive closure of a directed graph.

A greedy technique for an optimization problem always makes the option which look best at the moment and add it to the current subsolution.

1.The greedy method produces a feasible solution

2.The greedy method is very easy to solve a problem

3.The greedy method implements an optimal solution directly

A spanning tree for a linked graph is a tree whose vertex set is the same as the vertex set of the given graph, and whose edge set is a subgroup of the edge set of the given graph. i.e., any linked graph will have a spanning tree.

This is a greedy method. A greedy method chooses some local optimum (i.e., selection an edge with the smallest weight in an MST).

Floyd's algorithm is a function, which is used to find all the pairs shortest paths problem. Floyd's algorithm is relevant to both directed and undirected weighted graph, but they do not include a cycle of a negative length.

Prim's algorithm is a greedy and efficient technique, which is used to find the minimum spanning the tree of a weighted linked graph.

Dijkstra's algorithm solves the single-source shortest path method of finding shortest paths from a given vertex (the source), to another vertex of a weighted graph or digraph. Dijkstra's algorithm implements a correct solution for a graph with non-negative weights.

A Huffman tree is a binary tree which reduces the weighted path length from the root to the leaves, including a set of predefined weights. The essential application of Huffman trees is a Huffman code.

A Huffman code is an optimal prefix tree variable-length encoding technique which assign bit strings to characters based on their frequency in a given text.

Depth-first node generation with bounding method is known as backtracking. The backtracking technique has its virtue the ability to yield the solution with far fewer than m trials.

Dynamic programming Greedy method
Many numbers of decisions are generated. Only one sequence of decision is generated.
It gives an optimal solution always It does not require to provide an optimal solution always.

The problem is to area n-queens on an n-by-n chessboard so that no two queens charge each other by being same row or in the same column or the same diagonal.

The types of Notations used for Time Complexity includes:-

Big Oh:It indicates “fewer than or the same as” < expression >iterations

Big Omega:It indicates “more than or same as” < expression >iterations

Big Theta:It indicates “the same as”< expression >iterations

Recursive algorithm is a method of solving a complicated problem by breaking a problem down into smaller and smaller sub-problems until you get the problem small enough that it can be solved easily. Usually, it involves a function calling itself.

It is a language feature that allows a subclass or child class to provide a specific implementation of a method which is already provided by one of its super classes or parent classes.

The constructor has the same name as the class. A constructor is also a special kind of method. It is used to initialize objects of the class.

Access modifiers or access specifiers are the keywords in object-oriented languages. It helps to set the accessibility of classes, methods, and other members.

If you derive a class from another class that is known as inheritance. The child class will inherit all the public and protected properties and methods from the parent class. The child class can also have its own properties and methods. An inherited class is defined by using the extends keyword.

combination of multiple and multi-level inheritance is known as hybrid inheritance.

Hierarchical inheritance is basically when one base class has more than one subclasses. For example, the fruit class can have ‘apple’, ’mango’, ’banana’, ‘cherry’ etc. as its subclasses.

It Increases the execution time and effort. It also requires jumping back and forth between different classes. The parent class and the child class is always tightly coupled.

Afford modifications in the program would require changes for parent and the child class both. Inheritance requires careful implementation otherwise it would lead to incorrect results.

A superclass or base class is also a class which works as a parent to some other class/ classes.

For example, the Vehicle class is a superclass of class Bike.

A subclass is a class that inherits from another class.

For example, the class Bike is a subclass or a derived of Vehicle class.

Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters

Static polymorphism or static binding is a one kind of polymorphism which comes at compile time. example of compile-time polymorphism is: method overloading.

Dynamic polymorphism, dynamic binding or Runtime polymorphism is also part of polymorphism which is basically solved during runtime. An example of runtime polymorphism: method overriding.

Operator overloading is used to implement operators using user-defined types, based on the arguments passed along with it.

Data abstraction is one of the most important features of OOPs. It only allows important information to be displayed. It helps to hide the implementation details.

For example, while using a mobile, you know, how can you message or call someone but you don’t know how it actually happens.

This is data abstraction as the implementation details are hidden from the user.

Data abstraction can be achieved using two ways:

1. Abstract class
2. Abstract method

An abstract class is also a class which is consists of abstract methods.

These methods are basically declared but not defined and If these methods need to be used later in some subclass that time those methods haveto be exclusively defined in the subclass.

Virtual functions are also part of the functions which are present in the parent class and they are overridden by the subclass.
These functions help to achieve runtime polymorphism.

Garbage Collection is a part of automatic memory management. The Garbage collector helps to free the occupied spaces by objects. Those spaces are no longer in existence.

Zero instances will be created for an abstract class. In other words, you cannot create an instance of an Abstract Class.

An exception is a kind of message that interrupts and comes up when there is an issue with normal execution of a program. Exceptions provide a error and transfer that error to the exception handler to resolve it.

Exception handling in Object-Oriented Programming is the most important concept. It is used to manage errors. An exception handler help to throw errors and then catch the error in order to solve them.

A try/ catch block helps to handle exceptions. The try block explains a set of statements that may have an error. The catch block basically catches the exception.

An inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code.

A friend function is a friend of a class that is allowed to access to Public, private, or protected data in that same class. If the function is defined outside the class cannot access such information.

The super keyword is used to invoke the overridden method, which overrides one of its superclass methods. This keyword allows to access overridden methods and also to access hidden members of the superclass. It also forwards a call from a constructor, to a constructor in the superclass.

Operator keyword is used for overloading.

An interface is a collection of an abstract method. If the class implements an interface, it thereby inherits all the abstract methods of an interface. Java uses Interface to implement multiple inheritances.

Early binding refers to the assignment of values to variables during design time, whereas late Binding refers to the assignment of values to variables during run time.

A pure virtual function is a function which can be overridden in the derived class but cannot be defined. A virtual function can be declared as Pure by using the operator =0.

Tuesday, 23 November 2021

oop_short_question

oops/c++

OOPs Interview Questions

# Large program are divied into smaller program knows as function

*most of the function share global data.

*Function transfrom data from one from to another.

*Employs top-down approach in program design.

1. Follow bottom-up approach in program design.
2. Data is hidden and can not be accessed by external function.
3. programs are divied into what are known as object .

1. Inheritance
2. Encapsulation
3. Polymorphism
4. Data Abstraction

1 It follows a bottom-up approach.
2.It models the real word well.
3. Minimizes the complexity.
4. Programmer are able to reach their goals faster.
5. Avoids unnecessary data exposure to the user by using the abstraction.
6. It allows us the reusability of code.

1. Proper planning is required.

2. Program design is tricky.

3. Classes tend to be overly generalized.

object-oriented programming structural programming
It follows a bottom-up approach. It follows a top-down approach.
It provides data hiding. Data hiding is not allowed.
It is used to solve complex problems. It is used to solve moderate problems.
It allows reusability of code that reduces redundancy of code Reusability of code is not allowed.
Class Object
It is a logical entity.. It is a real-world entity.
It is conceptual. It is real.
It does not occupy space in the memory. It occupies space in the memory.
A class can exist without any object. Objects cannot exist without a class.
It is declared once. Multiple objects can be declared as and when required.
Class Structure
Class is a group of common objects that shares common properties The structure is a collection of different data types.
It deals with data members and member functions. It deals with data members only.
It does not occupy space in the memory. It occupies space in the memory.
A class can exist without any object. Objects cannot exist without a class.
It is declared once. Multiple objects can be declared as and when required.
An instance of a class is an object. An instance of a structure is a structure variable.

1 It cannot have a return type.

2 It must have the same name as the Class name.

3. It cannot be marked as static. It cannot be marked as abstract.

4. It cannot be overridden.It cannot be final.

Constructor Method
Constructor has the same name as the class name. The method name and class name are not the same.
It creates an instance of a class. It is used to execute Java code.
It cannot be inherited by the subclass. It can be inherited by the subclass
It does not have any return type. It must have a return type.
It cannot be overridden in Java. It can be overridden in Java.
Java compiler automatically provides a default constructor Java compiler does not provide any method by default.
Procedural Oriented Programming Object-Oriented Programming
It is based on functions. It is based on real-world objects.
It follows a top-down approach. It follows a bottom-up approach.
It is less secure because there is no proper way to hide data. It provides more security.
Data is visible to the whole program.
It cannot be overridden in Java. It can be overridden in Java.
Java compiler automatically provides a default constructor Java compiler does not provide any method by default.
Copy Constructor Assignment Operator
It is an overloaded constructor. It is an operator.
It creates a new object as a copy of an existing object. It assigns the value of one object to another object both of which already exist.
If no copy constructor is defined in the class, the compiler provides one. If the assignment operator is not overloaded then the bitwise copy will be made.
Inheritance Polymorphism
Inheritance is one in which a derived class inherits the already existing class's features. Polymorphism is one that you can define in different forms.
It refers to using the structure and behavior of a superclass in a subclass. It refers to changing the behavior of a superclass in the subclass.
It supports code reusability and reduces lines of code. It allows the object to decide which form of the function to be invoked at run-time (overriding) and compile-time (overloading).

A class is simply a representation of a type of object. It is the blueprint/plan/template that describes the details of an object.

An object is an instance of a class. It has its own state, behavior, and identity.

Encapsulation is an attribute of an object, and it contains all data which is hidden. That hidden data can be restricted to the members of that class.

Polymorphism is nothing but assigning behavior or value in a subclass to something that was already declared in the main class. Simply, polymorphism takes more than one form.

It has flexibility and simplicity in solving complex problems. Reuseage of code is easy as Inheritance concept helps to reduce redundancy of code. Data and code are bound together by encapsulation

OOPs has features for data hiding, so private data can be store and maintain confidentiality. Problems can be divided into different parts making it simple to solve. The concept of polymorphism has flexibility for that a single entity can have multiple forms.

Abstraction is an OOPs concept to build the structure of the real-world objects. It “shows” only essential attributes and “hides” unnecessary information from the outside. The main focus of abstraction is to hide the unnecessary details from the users. It is one of the most important concepts of OOPs.

There is a concept where two or more methods can have the same name. But they should have different parameters, different numbers of parameters, different types of parameters, or both. These methods are known as overloaded methods and this feature is called method overloading.

It is a language feature that allows a subclass or child class to provide a specific implementation of a method which is already provided by one of its super classes or parent classes.

The constructor has the same name as the class. A constructor is also a special kind of method. It is used to initialize objects of the class.

Access modifiers or access specifiers are the keywords in object-oriented languages. It helps to set the accessibility of classes, methods, and other members.

If you derive a class from another class that is known as inheritance. The child class will inherit all the public and protected properties and methods from the parent class. The child class can also have its own properties and methods. An inherited class is defined by using the extends keyword.

combination of multiple and multi-level inheritance is known as hybrid inheritance.

Hierarchical inheritance is basically when one base class has more than one subclasses. For example, the fruit class can have ‘apple’, ’mango’, ’banana’, ‘cherry’ etc. as its subclasses.

It Increases the execution time and effort. It also requires jumping back and forth between different classes. The parent class and the child class is always tightly coupled.

Afford modifications in the program would require changes for parent and the child class both. Inheritance requires careful implementation otherwise it would lead to incorrect results.

A superclass or base class is also a class which works as a parent to some other class/ classes.

For example, the Vehicle class is a superclass of class Bike.

A subclass is a class that inherits from another class.

For example, the class Bike is a subclass or a derived of Vehicle class.

Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters

Static polymorphism or static binding is a one kind of polymorphism which comes at compile time. example of compile-time polymorphism is: method overloading.

Dynamic polymorphism, dynamic binding or Runtime polymorphism is also part of polymorphism which is basically solved during runtime. An example of runtime polymorphism: method overriding.

Operator overloading is used to implement operators using user-defined types, based on the arguments passed along with it.

Data abstraction is one of the most important features of OOPs. It only allows important information to be displayed. It helps to hide the implementation details.

For example, while using a mobile, you know, how can you message or call someone but you don’t know how it actually happens.

This is data abstraction as the implementation details are hidden from the user.

Data abstraction can be achieved using two ways:

1. Abstract class
2. Abstract method

An abstract class is also a class which is consists of abstract methods.

These methods are basically declared but not defined and If these methods need to be used later in some subclass that time those methods haveto be exclusively defined in the subclass.

Virtual functions are also part of the functions which are present in the parent class and they are overridden by the subclass.
These functions help to achieve runtime polymorphism.

Garbage Collection is a part of automatic memory management. The Garbage collector helps to free the occupied spaces by objects. Those spaces are no longer in existence.

Zero instances will be created for an abstract class. In other words, you cannot create an instance of an Abstract Class.

An exception is a kind of message that interrupts and comes up when there is an issue with normal execution of a program. Exceptions provide a error and transfer that error to the exception handler to resolve it.

Exception handling in Object-Oriented Programming is the most important concept. It is used to manage errors. An exception handler help to throw errors and then catch the error in order to solve them.

A try/ catch block helps to handle exceptions. The try block explains a set of statements that may have an error. The catch block basically catches the exception.

An inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code.

A friend function is a friend of a class that is allowed to access to Public, private, or protected data in that same class. If the function is defined outside the class cannot access such information.

The super keyword is used to invoke the overridden method, which overrides one of its superclass methods. This keyword allows to access overridden methods and also to access hidden members of the superclass. It also forwards a call from a constructor, to a constructor in the superclass.

Operator keyword is used for overloading.

An interface is a collection of an abstract method. If the class implements an interface, it thereby inherits all the abstract methods of an interface. Java uses Interface to implement multiple inheritances.

Early binding refers to the assignment of values to variables during design time, whereas late Binding refers to the assignment of values to variables during run time.

A pure virtual function is a function which can be overridden in the derived class but cannot be defined. A virtual function can be declared as Pure by using the operator =0.

Saturday, 20 November 2021

Q8_ascending_descending

oder

Java Program to sort the elements of an array in ascending order and descending order

In this program, we need to compaire each elemrnt of the given array to each element adding each other and thorugh out the addition of the given array is . This can be accomplished by looping through the array and store the elements of the array at the corresponding position.

Original array

1,22,13,5,50

ascending oder :-

1 5 13 22 50

descending oder :-

50 22 12 5 1

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={1,2,3,4,5}
  • 3.i=a.lenght;i>0,i--
  • 4.int j=i+1;j< arr.length;j++
  • 5. arr[i] < arr[j] // asceding
  • 6.swap
  • 7. arr[i] > arr[j] //descending
  • 8. swap
  • 4.display of array

Program

class Q8_ascending_descending {

public static void main(String[] args)

//declare array

int arr[]={11,2,33,45,59};

System.out.println("original array is");

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

{

System.out.print(arr[i] + " ");

}

//descending array

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

{

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

{

if(arr[i] < arr[j])

{

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

}

}

//display descending oder array

System.out.println("after Descending order");

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

{

System.out.print(arr[i] + " ");

}

//ascending array

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

{

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

{

if(arr[i] > arr[j])

{

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

}

}

//display ascending oder array

System.out.println("after ascending order");

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

{

System.out.print(arr[i] + " ");

}

output

Original array

1,22,13,5,50

ascending oder :-

1 5 13 22 50

descending oder :-

50 22 12 5 1

Next Topic

Q7_sum_array

Largest

7. Java Program to print the sum of all the items of the array in Java

In this program, we need to compaire each elemrnt of the given array to each element adding each other and thorugh out the addition of the given array is . This can be accomplished by looping through the array and store the elements of the array at the corresponding position.

Original array

1,2,3,4,5

additionof element array :-

15

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={1,2,3,4,5}
  • 3.i=a.lenght;i>0,i--
  • 4.sum=sum=arr[i]
  • 5.sum store in addition varible
  • 4.display max element of array

Program

class Q7_sum {

public static void main(String[] args)

//declare array

int arr[]={11,2,33,45,59};

System.out.println("original array is");

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

{

System.out.print(arr[i] + " ");

}

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

{

sum=sum+arr[i];

}

//display largest element of array

System.out.println("addition of element of the given array is ="+sum);

output

original array is:

1,2,3,4,5

addition element of array is:

15

Next Topic

Q6_smallest_array

smallest

6. Java Program to print the smallest element in an array

In this program, we need to compaire each elemrnt of the given array to each other and find out the smallest element in the present array . This can be accomplished by looping through the array and store in less the elements of the array into min variable array at the corresponding position.

Original array

11,2,33,45,59

smallest element of array :-

2

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={11,2,33,45,59}
  • 3.i=a.lenght;i>0,i--
  • 4.(arr[i]< min)
  • 5.max=arr[i]; store in max varible
  • 4.display max element of array

Program

class Q6_smallest {

public static void main(String[] args)

//declare array

int arr[]={11,2,33,45,59};

System.out.println("original array is");

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

{

System.out.print(arr[i] + " ");

}

int min=arr[0];

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

{

if(arr[i]< min)

{

min=arr[i];

}

}

//display smallest element of array

System.out.println("smallest elementof the given array is ="+min);

output

original array is:

11,2,33,45,59

smallest element of array is:

2

Next Topic

Q5_largest_array

Largest

5. Java Program to print the largest element in an array

In this program, we need to compaire each elemrnt of the given array to each other and find out the largest element in the present array . This can be accomplished by looping through the array and store in greater the elements of the array into max variable array at the corresponding position.

Original array

11,2,33,45,59

Largest element of array :-

59

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={11,2,33,45,59}
  • 3.i=a.lenght;i>0,i--
  • 4.(arr[i]>max)
  • 5.max=arr[i]; store in max varible
  • 4.display max element of array

Program

class Q5_largest {

public static void main(String[] args)

//declare array

int arr[]={11,2,33,45,59};

System.out.println("original array is");

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

{

System.out.print(arr[i] + " ");

}

int max=arr[0];

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

{

if(arr[i]>max)

{

max=arr[i];

}

}

//display largest element of array

System.out.println("Largest elementof the given array is ="+max);

output

original array is:

11,2,33,45,59

largest element of array is:

59

Next Topic

Q4_even_odd_array

even_odd

4. Java Program to print the elements of an array present on even position and odd position

In this program, we need to kwon the given array element indexing that is how to indetife to odd position and even positon element which is ? . This can be accomplished by looping through the array and store the elements of the array into the odd and even element array at the corresponding position.

Original array

23 4 31 8 5

Even position

23 31 5

odd position

4 8

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={1, 2, 3, 4, 5}
  • 3.start for loop
  • 4.if(i%2==0) is even position
  • 5.if(i%2!=0) is odd position
  • 4.display array

Program

class Q4_odd_even {

public static void main(String[] args)

//declare array

int arr[]={23, 4 ,31, 8, 5};

System.out.println("original array is");

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

{

System.out.print(arr[i] + " ");

}

//display even position of array

System.out.println("even postiton of array element \n");

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

{

if(i%2==0)

{

System.out.print(arr[i] + " ");

}

}

//display odd position element of array

System.out.println("odd postiton of array element \n");

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

{

if(i%2!=0)

{

System.out.print(arr[i] + " ");

}

}

output

original array is:

23 4 31 8 5

even position of array is:

23 31 5

odd position of array is:

4 8

Next Topic

Q3_reverse_array

COPY

Program to reverse all elements of array in Java

In this program, we need to kwon how to work looping in descending /decreasing oder . This can be accomplished by looping through the array and store the elements of the array into the reverse oder array at the corresponding position.

Original array

1 2 3 4 5

Reverse array

5 4 3 2 1

Algorithm

  • 1.START
  • 2.INITIALIZE arr1[] ={1, 2, 3, 4, 5}
  • 3.i=a.lenght;i>0,i--
  • 4.display reverse oder array

Program

class Q3_reverse {

public static void main(String[] args)

//declare array

int arr[]={1,2,33,4,5};

System.out.println("original array is");

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

{

System.out.print(arr[i] + " ");

}

//display reverse array

System.out.println("reverse the array");

for(int i=arr.length;i>0;i--)

{

System.out.print(arr[i] + " ");

}

output

original array is:

1 2 33 4 5

reverse array is:

5 4 33 2 1

Next Topic

How do you select data from a table in SQL?

Create Table How do you select data a table in SQL? The SELECT statement is used to se...