You are on page 1of 28

National Public School

Koramangala

Grade 10
(2023 - 24)

Computer Science

Java Programming
Contents

Chapter Topic Pg. No.

1 While – Do While -- Nested For Loop 4

2 String Manipulation in Java 7

3 Java Packages for Mathematical Functions 16

4 Introduction to Arrays 18

5 Linear Search & Finding Maximum and Minimum 22

6 Sorting Techniques in array 24


Iteration Through Loops (Revision of Grade 9)
In computer programming, a loop is a sequence of instructions that is continually repeated until a
certain condition is reached.

A loop structure contains the following parts:

• Control Variable : A variable, which starts with an initial value and determines the duration of
the repetition is known as Control Variable or initial value.
• Body of the Loop: A set of statements, which are executed within the loop simultaneously.
• Test Condition : Each loop contains a test condition. Whether the loop has to be repeated or
terminated, depends upon the test condition. The control enters the body of the loop for
execution till the test condition is true, otherwise it terminated.
• Step Value: The step value in a looping structure determines the increment or decrement of
the control variable unless the test condition is false.

Based upon the nature of iterations the loops are of two types as shown below:

LOOP

Fixed Iterations Unfixed Iterations


for Loop while, do while

For Loop : A for-loop is a control flow statement for specifying iteration, which allows code to be
executed repeatedly.

Syntax:

for (initialization condition; testing condition; increment/decrement)

statement(s)

• Initialization condition: Here, we initialize the variable in use. It marks the start of a for loop.
An already declared variable can be used or a variable can be declared, local to loop only.
• Testing Condition: It is used for testing the exit condition for a loop. It must return a boolean
value. It is also an Entry Control Loop as the condition is checked prior to the execution of
the loop statements.
Page | 1
• Statement execution: Once the condition is evaluated to true, the statements in the loop
body are executed.
• Increment/Decrement: It is used for updating the variable for next iteration.
• Loop termination: When the condition becomes false, the loop terminates marking the end
of its life cycle.

// Java program to illustrate for loop.


public class forLoopDemo
{

public static void main()

{
int x=2;

for (x = 2; x <= 4; x++)

System.out.println("Value of x:" + x);

Output:
Value of x:2
Value of x:3
Value of x:4

Page | 2
Chapter 1
While – Do While -- Nested For Loop

When we apply a For loop within another For loop, the structure is termed as
nested for loop.

Syntax:

for (initialization condition; testing condition; increment/decrement)


{
statement(s)

for (initialization condition; testing condition; increment/decrement)


{
statement(s)
}
}

// Java program to illustrate nested for loop.

public class forLoopDemo


{
public static void main()
{
int x, y;
for (x = 1; x <= 5; x++)
{
for (y = 1; y <= x; y++)
System.out.print(y);
}
System.out.println();
}
}

Output:
1

12

123

1234

12345

Page | 3
While Loop: This loop can be applied to a program where number of iterations are not fixed. The
syntax of while statement is given below:

while (boolean condition)

loop statements...

• While loop starts with the checking of condition. If it evaluated to true, then the loop body
statements are executed otherwise first statement following the loop is executed. For this
reason it is also called Entry control loop
• Once the condition is evaluated to true, the statements in the loop body are executed.
Normally the statements contain an update value for the variable being processed for the
next iteration.
• When the condition becomes false, the loop terminates which marks the end of its life cycle.

// Java program to illustrate while loop

public class whileLoopDemo

public static void main()

int x = 1;
while (x <= 4)

System.out.println("Value of x:" + x);

//increment the value of x for next iteration


x++;
}
}
}

Output:

Value of x:1
Value of x:2
Value of x:3
Value of x:4

Page | 4
Do While Loop: This loop is used in a program where number of iterations is not fixed. In this
system, the control enters the loop without checking any condition, executes the given steps and
then checks the condition for further continuation of the loop. Thus, this type of the loop executes
the tasks at atleast once. If the condition is not satisfied, then the control exits the loop.

do
{
statements..
}
while (condition);

• do while loop starts with the execution of the statement(s). There is no checking of any
condition for the first time.
• After the execution of the statements, and update of the variable value, the condition is
checked for true or false value. If it is evaluated to true, next iteration of loop starts.
• When the condition becomes false, the loop terminates which marks the end of its life cycle.
• It is important to note that the do-while loop will execute its statements atleast once before
any condition is checked, and therefore is an example of exit control loop.

// Java program to illustrate do-while loop

public class dowhileloopDemo


{
public static void main()
{
int x = 21;
do
{
System.out.println("Value of x:" + x);
x++;
} while (x < 20);

}
}
Output:
Value of x: 21

Break Statement : A break statement is used for unusual termination of a block. As


soon as you apply a break statement, the control exits from the block.

for (initialization condition; testing condition; increment/decrement)

{
if (another condition is true)
break;
Statement(s);
}

Page | 5
// Java program to illustrate break.
import java.util.Scanner;
public class forLoopDemo

public static void main()

Scanner sc=new Scanner(System.in);

while(n!=0)

System.out.println(“Enter a number: “);


n=sc.nextInt();

if (n==0)
break;
if (n%2==0)

System.out.println(n + “ is an even number”);


else
System.out.println(n + “is an odd number”);

}
}
}
Exercises:

1. Write programs to display the following patterns:

a) 1 b) 1 c) 1 2 3 4 5
1 0 2 3 1 2 3 4
1 0 1 4 5 6 1 2 3
1 0 1 0 7 8 9 10 12
1 0 1 0 1 11 12 13 14 15 1

2. Write programs in Java to:

1. accept a number and check whether the number is prime or not.


2. accept a number and check if it is a palindrome number.
3. accept a number and display if the number is an Armstrong number or not.
(Armstrong number is a number that is equal to the sum of cubes of its
digits. For example 153, 370, 371 and 407 are the Armstrong numbers.)

Page | 6
Chapter 2
String Manipulation in Java

Introduction

In Java language, a single character and a set of characters (i.e., String) are considered
differently. Each character is assigned an ASCII code, which is taken into consideration during
Java programming. In Java, a variable is declared as given below.

Syntax : <Data type><variable> = <’character literal’>;


char p = ’m’;

It means that a character is always enclosed within single quotes.

Strings, which are widely used in Java programming, are a sequence of characters. In Java,
strings are objects rather than a primitive data type. The Java platform provides the String
class to create and manipulate strings. String class is encapsulated under java.lang package.

Although string literals are used in a manner similar to other literals in a program, they are
handled differently by the Java application. With a string literal, java stores the value as a
string object.

Strings can be created and used in following ways:

1. Using a String literal


String literal is a simple string enclosed in double quotes " ". A string literal is treated as a
String object.
String str1 = "Hello";

2. Using new Keyword.


String str2 = new String("Java");

3. Using another String object.


String str3 = new String(str1);

Java language provides relevant functions (i.e., character function and string function) to
perform the desired tasks. Some of the important functions are explained below.

Page | 7
Character Functions

These functions deal with only the character manipulations. Some of them are:
a) isLetter( )
This function is used to check whether a given argument is an alphabet or not. It returns
a boolean type value either true or false.

Syntax: boolean variable = Character.isLetter(character);


Eg: boolean p = Character.isLetter(‘c’);
The function will return ‘true’ to the variable p.

Eg:. boolean p = Character.isLetter(‘6’);


The function will return ‘false’ to the variable p.

b) isDigit( )

This function returns a Boolean type value ‘true’ if a given argument is a digit, otherwise
‘false’.

Syntax : boolean variable = Character.isDigit(character);

Eg: boolean p = Character.isDigit(‘7’);


It returns ‘true’ to the variable p.
Eg: boolean p = Character.isDigit(‘G’);
It returns ‘false’ to the variable p.

c) isWhitespace( )

This function can be used to check for existing blankspace or gap in a String. It will return
a Boolean type value (i.e., true) if the given argument is a white space (blank) and false,
otherwise.

Syntax : boolean variable = Character.isWhitespace(character);

Eg: boolean b = Character.isWhitespace(‘ ‘);


It returns ‘true’ to the Boolean variable b as the given character is a blank.

Eg: boolean b = Character.isWhitespace(‘*’);


It returns ‘false’ to the boolean variable b as the given character is not a
blank.

Page | 8
d) isUpperCase( )

This function will return ‘true’ if the given argument is an upper case letter, otherwise
‘false’.

Syntax : boolean variable = Character.isUpperCase(character);

Eg: boolean p = Character.isUpperCase(‘A’);


It returns ‘true’ to the variable p.

Eg: boolean p = Character.isUpperCase(‘g’);


It returns ‘false’ to the variable p.

e) isLowerCase( )

This function will return ‘true’ if the given argument is a lower case letter, otherwise ‘false’.

Syntax : boolean variable = Character.isLowerCase(character);

Eg: boolean p = Character.isLowerCase(‘U’);


It returns ‘false’ to the variable p.

Eg: boolean p = Character.isLowerCase(‘g’);


It returns ‘true’ to the variable p.

f) toUpperCase( )

This function returns the given argument in upper case character. The given character
remains same if it is already in upper case. It returns the same character if the character
used with the function is non-alphabet.

Syntax : char variable = Character.toUpperCase(character);

Eg: char c = Character.toUpperCase(‘a’);


It returns letter ‘A’ to the variable c.

Eg: char c = Character.toUpperCase(‘G’);


It returns ‘G’ to the variable c.

g) toLowerCase( )

This function returns the given argument in lower case character. The given character
remains same if it is already in lower case. It returns the same character if the character
used with the function is non-alphabet.

Page | 9
Syntax : char variable = Character.toLowerCase(character)

Eg: char c = Character.toLowerCase(‘A’);


It returns letter ‘a’ to the variable c.

Eg: char c = Character.toLowerCase(‘*’);


It will return the same character as the given character is non-
alphabet.

String Functions

a) length( )
This function is used to return the length of a string i.e., number of characters present
in the string. The length is an integer number and hence it returns an integer value.
Syntax: int variable = string variable.length();
Eg: String str = “PROGRAMMING”
int k = str.length();

Here, k = 11, as number of characters in the string str is 11.


Eg: String str = “COMPUTER IS FUN”
int k = str.length();

Here, k = 15, as number of characters including the spaces in the string is 15.

b) charAt ( )
This function returns a character from the given index i.e., the position number of the
string. The index number starts from zero.

Syntax : char variable = string variable.charAt(index);

Eg: String s = “COMPUTER”;


char c = s.charAt(3);

Here c = ‘P’ as the character at 3rd index is ‘P’. The index should not exceed the
length of the string.

Page | 10
c) indexOf ( )
This function returns the index i.e., the position number of the string.

Syntax: int variable = string variable.indexOf(character);


Eg: String s = “COMPUTER”;
int n = s.indexOf(‘P’);
Here, the value returned by the function is 3. Hence, n=3.

Eg: String s = “MALAYALAM”;


int n = s.indexOf(‘A’, 4);

The function will return the index of ‘A’ available in the string from the 4th index.
Hence,
n=5.
Eg: String s = “COMPUTER”;
int n = s.indexOf(‘C’, 3);

Checks for 'C' from the 3rd position onwards. It returns -1 as character ‘C’ is not available
after 3rd index.

d) toLowerCase( )
This function returns the characters of the string in lower case letters. If any character
is already in lower case or the character is a special character then it remains same.

Syntax: String variable1 = string variable.toLowerCase( );


Eg: String s = “COMPUTER”;
String p = s.toLowerCase( );
Here, p results in “computer”.
e) toUpperCase( )
This function returns a string after converting all the characters to upper case letters. If
any character is already in upper case or the character is a special character then it
remains the same.

Syntax: String variable1 = string variable.toUpperCase( );

Eg: String s = “computer”;


String p = s.toUpperCase( );
Here, p results in “COMPUTER”.

Page | 11
f) concat( )

This function is used to concatenate two strings together.

Syntax: String variable = string variable1.concat(string variable2);

Eg: String x = “COMPUTER”;


String y = “APPLICATIONS”;
String z = x.concat(y);

Here, both the strings x and y will be joined together. Hence, z results in
“COMPUTERAPPLICATIONS”.

Java language also uses an operator ‘+’ to concatenate two or more strings together.

Eg: String x = “PROGRAMMING”;


String y = “IS”;
String z = “FUN”;
String p = x + “ “ + y + “ “ + z;
Here, p results in PROGRAMMING IS FUN

g) substring( )
This function is used to extract a part of string characters from one index to another.

Syntax : String variable1 = string variable.substring(index);

Eg: String s = “COMPUTER”;


String p = s.substring(3);
This function will return all the characters from the string starting from 3 rd index.
Hence, p = “PUTER”.

Eg: String p = s.substring (3, 6);

It returns a part of string from 3rd positon to the 6th position by excluding the
character, which is available at 6th position. Hence, p results in PUT.

Page | 12
h) replace( )
This function is used to replace a character by another character or to replace a String
by another String at all its occurrence in the given String.
Syntax :

a. String variable1 = string variable.replace(character to replace, character to


appear)
b. String variable1 = string variable.replace(substring to replace, substring to
appear)
Eg: 1. String s = “MXLXYXLXM”;
String p = s.replace(‘X’, ‘A’);

Here, each character ‘X’ of the string is replaced by ‘A’. Hence, the new string p
results in “MALAYALAM”.

Eg: 2. String s = “The green bottle is in green bag”;


String p = s.replace(“green”, “red”);

Here, each sub string “green” of the given string is replaced by “red”.
Hence, the new string p results in “The red bottle is in the red bag”.

i) equals( )

This function is used to compare two strings together to check whether they are
identical or
not. It returns a Boolean type value ‘true’ if both are same, ‘false’ otherwise.

Syntax: boolean variable = string variable1.equals(string variable2);

Eg: 1 String x = “COMPUTER”;


String y = “SCIENCE”;
boolean z = x.equals(y);
Hence, z results in false.

Eg: 2 String x = “COMPUTER”;


String y = “computer”;
if(x.equals(y))
System.out.println(“same”);
else
System.out.println(“different”);

The given snippet displays “different” message.

Page | 13
j) equalsIgnoreCase( )
This function is also used to compare two strings to ensure whether both are identical
or not after ignoring the case i.e., corresponding upper and lower case characters are
treated the same. It also returns boolearn data as true or false.

Syntax: boolean variable = string variable1.equalsIgnoreCase(string variable2);

Eg: 1 String x = “COMPUTER”;


String y = “computer”;
boolean p = x.equalsIgnoreCase(y);
System.out.println(p);

The value printed out is true.

If(x.equalsIgnoreCase(y))
System.out.println(“same”);
else
System.out.println(“different”);

The given program snippet displays the message as “same”.

Exercises:

Write a program in Java to :

1. display the following patterns when the input is given as BLUEJ.

a) c)
B B L U E J
L L U E J B
U E J B L
U
E J B L U
E J B L U E
J

b)
B
B L
B L U
B L U E
B L U E J

Page | 14
2. accept a String in lower case and replace ‘e’ with ‘*’ in the given String. Display the new
string.
Sample Input: computer science
Sample Output : comput*r sci*nc*

3. accept a string and find the count of:


a) Number of blank spaces
b) Number of words
c) Number of characters
d) Number of vowels

4. accept a string in lower case. Convert all the first characters of the string in upper case
and display the new string.
Sample Input : programming is fun
Sample Output : Programming Is Fun

5. accept a word and check whether the word is Palindrome or not.


A word is said to be Palindrome, if the new word formed after reversing the alphabet is
same as the original word.
Sample Input : madam
Sample Output : It is a Palindrome

6. accept a name and display only the initials in the name.


Sample Input : Mohandas Karamchand Gandhi
Sample Output : M K G

Page | 15
Chapter 3
Java Packages for Mathematical Functions
1. Math.pow( )

This function is used to find the power raised to a specified base. It always returns a
double type value.

Syntax : <Return Data type><variable> = Function name (base, exponent);


E.g., double d = Math.pow(2.0, 3.0);
This function returns a double type value to d as 2.03.0 = 8.0
double d = Math.pow(5.0, -2.0);
The value of d will be returned as a double value as 0.04.

2. Math.sqrt( )

This function is used to find the square root of a positive number. It returns a double
type value.

Syntax : <Return Data type><variable> = Function name (Positive number);


E.g., double n = Math.sqrt(4.0);
It returns a double type value for n as 2.0
double n = Math.sqrt(6.25);
It returns a double type value of n as 2.5
double n = Math.sqrt(-3.25)
It will return a value for n as NaN (Not a Number).

3. Math.abs( )

This function is used to return absolute value i.e., only magnitude of the number. It
returns int/long/double value depending upon the arguments supplied.

Syntax : <Return Data type><variable> = Function name(number);


E.g., int n = Math.abs(3); It returns an integer value to n as 3.
double n = Math.abs(-8.9); It returns double value to the variable as 8.9

Page | 16
4. Math.round( )

This function returns the value in rounded form. It always returns in double data type. If
the fractional part of the number is below 0.5, it returns the same integer value
otherwise, it returns the next integer value in double data type.

Syntax : <Return data type><variable> = Function name(argument)


E.g. double n = Math.round(6.25); It returns an integer value to n in double
data type.
double n = Math.round(6.85); It returns an integer value to n in double
data type. i.e., n=7.0
double n = Math.round(-8.92); It returns an integer value to n in double
data type. i.e., n=-9.0

Exercise:

1. Write a program to find the area, perimeter and diagonal of a rectangle.


2. Write a program to accept perpendicular and base of a right angled triangle.
Calculate and display hypotenuse, area and perimeter of the triangle.

Page | 17
Chapter 4
Introduction to Arrays

An array is a data structure to store a collection of elements of the same type. It is a container
object and holds a fixed number of values of a single type. The length of an array is established
when the array is created. After creation, its length is fixed. Arrays are sequential in nature.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you
can declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables.

Element at index 7

First
Index 0 1 2 3 4 5 6 7 8 9
10 20 30 40 50 60 70 80 90 100

Array length is 10
Fig 1 - An array of 10 elements.

The size of the array is 10. Each item in an array is called as an element, and each element is
accessed by its numerical index. As shown in the above illustration, numbering begins at 0.
The 9th element, for example, would therefore be accessed at index 8. All the elements are
stored in a sequential manner.

Array declarations
Arrays of various data types may be declared as shown below.
1. int A[] = new int[10]
Creates an array A of type int of size10. Array A can store 10 elements of type integer.
2. String C[] = new String[5]
Creates an array of strings of size 5. Now array C may store 5 names like “Suman”,
“Harish”,”Rahul”, “Pooja”, “Neha”
In the same manner, arrays of other data types may be created.
Incidentally, arrays may also be initialized during declaration. This is as shown below.
1. int A[] ={3,5,7,9,11} Creates an array of size 5.
2. float wts[] ={3.5, 5.6, 7.8,12.9} Creates an array of size 4.
3. String names[]= {“Neha”,”Kiran”,”Pooja”} Creates an array of size 3.

Page | 18
Input/output of array elements
Typically a for loop is used while entering values into or reading values from an array. Since
the size of the array is fixed and known before execution of the program, for loop is quite
apt for this purpose. This is shown in the following program.
import java.util.Scanner;
public class testingArrays
{
public static void main(()
{
Scanner sc= new Scanner(System.in);
int A[]=new int[5];
for(int i=0; i<5;i++) //Storing values
{
System.out.println(“enter a value”);
A[i] = sc.nextInt();
}
//Displaying all the values
for(int j=0; j<5;j++)
System.out.println(A[j]);
}
}

Solved programs:
1. Program to input age of 10 students and display the same in the reverse order.
import java.util.Scanner;
public class arr1
{
public static void main()
{
Scanner sc= new Scanner(System.in);
int Age[]=new int[10];
for(int i=0; i<10;i++) //Storing values
{
System.out.println(“enter age”);
Age[i] = sc.nextInt();
}
//Displaying in reverse order
for(int i=9; i>=0; i--)
System.out.println(Age[i]);
}
}

Page | 19
2. Program to find the average height of a class of 6 students.
import java.util.Scanner;
public class arr2
{
public static void main()
{
Scanner sc= new Scanner(System.in);
float ht[]=new float[6];
float sum =0.0,avg;
for(int i=0; i<6; i++) //Storing values
{
System.out.println(“enter height”);
ht[i] = sc.nextFloat();
sum= sum+ht[i];
}
avg = sum/6;
System.out.println(“The average height of the class is “+ avg);
}
}

3. Display the word “cop”, one letter on each line.

public class str


{
public static void main()
{
char s[]={'c','o','p'};
for(int i=0;i<3;i++)
System.out.println(s[i]);
}
}
Exercise:
Write programs for the following.
1. Input Names and Sales Amount done by 5 salesmen. Calculate and display the
average of all the Sales Amount and also the names of only those whose sales are
above Rs.20,000.
2. Input an array of 10 integers. Display all odd and even numbers along with their
count.
3. Input an array of 10 integers. Swap adjacent elements and display the modified
array.

Page | 20
4. A company has announced 10% of the salary as the bonus amount. Input basic
salaries of 5 employees and display their net salary after adding the bonus amount
to their basic salaries.
5. Using an array display the first 10 terms of the Fibonacci series
(0,1,1,2,3,5,8,13,21,34).
6. Input an array of 6 integers and display only the prime numbers.

***********************

Page | 21
Chapter 5
Linear Search & Finding Maximum and Minimum
Searching is one of the basic operations that can be performed on arrays. It is a process to
determine whether a given item i.e., a number, a character or a word is present in the array
or not. There are different search algorithms, such as Linear Search, Binary Search etc.

Linear Search
It is one of the simplest techniques in which the searching of an item begins at the start of the
array, i.e., at the 0th index position of the array. The process continues, where each element
is checked and compared with the given data item until either the item is found or end of the
array is reached. This process is also called as Sequential Search.
Linear Search works well for small and unsorted arrays.

Algorithm:

Step 1: Traverse the array

Step 2: Match the key element with array element

Step 3: If key element is found, return the index position of the array element

Step 4: If key element is not found, return -1

Page | 22
Exercise:
Write programs for the following.
1. Declare an integer array of 10 elements. Input a number and search if it is present or
not using linear search. Display appropriate messages upon successful or
unsuccessful search.
2. Declare an array of 5 names. Now, input a name and check if it is present in the
array or not using linear search.
3. Declare an array to store 10 alphabets input by the user. Display the count of vowels.

Maximum and Minimum in an array


The maximum and the minimum element in an array can be found using the linear search
technique.
In this method, to start with, the very first element of the array is assumed as the maximum
/ minimum element. Subsequently, every other element is compared with the maximum /
minimum element, if found greater / lesser, the test value becomes the maximum /
minimum value. The process continues till the end of the array.

Exercise:

Write programs for the following.


1. Find the largest and the smallest element in an unsorted integer array of size 10.
2. Input noonday temperatures for a week. Find the hottest, the coolest and the
average temperature.

Page | 23
Chapter 6

Sorting Techniques in array

Sorting is a process of arranging data in a specified order; ascending or descending. It


applies to all types of data; numbers, character or may be String. There are many popular
sorting techniques; Linear sort, Selection sort, Bubble sort, Quick sort etc.

Selection sort:

The basic idea of a selection sort is to repeatedly select the smallest element (or largest
element if descending) in the remaining unsorted array and bringing it to its appropriate
position.

It is illustrated in the following example. The array is sorted in the ascending order.

Step 1: Consider the following array. At first the smallest element is selected through
iteration from the unsorted array and interchanged with the number at index 0.

0 1 2 3 4 5 6 7
44 97 49 56 89 27 15 77
.

Step 2: The next smallest element from 1st index onward is found and exchanged with the
element at 1st index position.

0 1 2 3 4 5 6 7
15 97 49 56 89 27 44 77
.

Step 3: The next smallest element from 2nd index onward is found and exchanged with the
element at 2nd index position.

0 1 2 3 4 5 6 7
15 27 49 56 89 97 44 77

Page | 24
Step 4: The next smallest element from 3rd index onward is found and exchanged with the
element at 3rd index position.

0 1 2 3 4 5 6 7
15 27 44 56 89 97 49 77
.

Step 5: The next smallest element from 4th index onward is found and exchanged with the
element at 4th index position.

0 1 2 3 4 5 6 7
15 27 44 49 89 97 56 77
.

Step 6: The next smallest element from 5th index onward is found and exchanged with the
element at 5th index position.

0 1 2 3 4 5 6 7
15 27 44 49 56 97 89 77
.

Step 7: The next smallest element from 6th index onward is found and exchanged with the
element at 6th index position. No exchange happens in this case.

0 1 2 3 4 5 6 7
15 27 44 49 56 77 89 97
.

Thus the numbers are arranged in the ascending order.

Algorithm for Selection Sort:

Step 1 − Set min to the first location


Step 2 − Search the minimum element in the array
Step 3 – swap the first location with the minimum value in the array
Step 4 – assign the second element as min.
Step 5 − Repeat the process until we get a sorted array.

Page | 25
Exercise:

Write programs for the following:

1. Sort an integer array of size 6 in descending order.


2. Sort a String array of size 5 containing 5 names in the ascending order.
3. Display the merit list of 5 students. The list contains details such as Roll no., Name
and Total marks. Input details from the user.

Page | 26

You might also like