1.
FIND NUMBER OF POSITIVE, NEGATIVE AND ZERO USING
JAVA (ARRAY)
import [Link];
public class ArrayTest {
public static void main(String[] args) {
// create Scanner class object to take input
Scanner scan = new Scanner([Link]);
// read size of the array
[Link]("Enter size of the array: ");
int n = [Link]();
// create an int array of size n
int numbers[] = new int[n];
// take input for the array
[Link]("Enter array elements: ");
for (int i = 0; i < n; ++i) {
1
numbers[i] = [Link]();
// find of +ve, -ve, and zero
checkNumbers(numbers);
// close Scanner
[Link]();
// method to count +ve, -ve, zero
public static void checkNumbers(int[] numbers) {
// variables
int positive = 0;
int negative = 0;
int zero = 0;
// check each element
2
for (int num : numbers) {
if (num > 0) { // +ve
++positive;
} else if (num < 0) { // -ve
++negative;
} else { // 0
++zero;
// display resultant values
[Link]("Positive numbers = " + positive);
[Link]("Negative numbers = " + negative);
[Link]("Zeros = " + zero);
2. GREATEST AND SMALLEST’S NUMBER IN THE ARRAY
USING JAVA
/*
Java program to Find Largest and Smallest Number in an Array
3
*/
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< [Link]; i++)
{
if(arr[i] > largest)
largest = arr[i];
else if (arr[i] < smallest)
smallest = arr[i];
}
[Link]("Smallest Number is : " + smallest);
[Link]("Largest Number is : " + largest);
}
}
3. START WITH SRI AND END WITH ROY
4
class StartsWithAndEndsWith
{
public static void main(String arg[])
{
String status = "Merit Campus is a great place to learn, practise and compete";
if ([Link]("Merit")) // LINE A
{
[Link]("status string starts with Merit");
}
else
{
[Link]("status string does not start with Merit");
}
if ([Link]("learn", 33)) // LINE B
{
[Link]("status string contains substring learn that starts at
index 33");
}
else
{
[Link]("status string does not contain substring learn");
}
if ([Link]("compete")) // LINE C
{
[Link]("status string ends with compete");
5
}
else
{
[Link]("status string does not end with compete");
}
[Link]("status starts with merit : " + [Link]("merit"));
}
}
4. ARRAY WITH SWAPPING ONE ARRAY USING JAVA
package selenium1;
import [Link];
class Main {
public static void main(String args[]) {
int i, t;
int arr[] = new int[4];
Scanner sc = new Scanner([Link]);
6
[Link]("Enter 6 numbers:");
for (i = 0; i < 4; i++) {
arr[i] = [Link]();
i = 0;
while (i < 4 - 1) {
t = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = t;
i = i + 2;
[Link]("After swap list are:");
for (i = 0; i < 4; i++) {
[Link](" " + arr[i]);
7
}
5. FINDOUT EVEN ,ODD AND MULTIPLES OF 2 UISNG JAVA
public class OddEvenInArrayExample{
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
[Link]("Odd Numbers:");
for(int i=0;i<[Link];i++){
if(a[i]%2!=0){
[Link](a[i]);
[Link]("Even Numbers:");
for(int i=0;i<[Link];i++){
if(a[i]%2==0){
[Link](a[i]);
}}
Number of multiples of 2
8
class Multiples{
static void findMultiples(int n){
for(int i = 0; i <= n; i++)
if(i % 2 == 0)
[Link](i);
public static void main(String[] args){
findMultiples(120);
6. ODD AND EVEN NUMBER SUM OF SEPARATELY
import [Link];
public class KboatSDAOddEvenSum
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
9
int arr[] = new int[20];
[Link]("Enter 20 numbers");
for (int i = 0; i < [Link]; i++) {
arr[i] = [Link]();
int oddSum = 0, evenSum = 0;
for (int i = 0; i < [Link]; i++) {
if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
[Link]("Sum of Odd numbers = " + oddSum);
[Link]("Sum of Even numbers = " + evenSum);
10
7. ARRAY SIZE 10, IF THERE ELEMENT ON LIST, IT WILL
SHOW PRESENT OTHERWISE IT WILL SHOW NOT
AVAILABLE
class Main {
public static void main(String[] args) {
int[] num = {1, 2, 3, 4, 5};
int toFind = 3;
boolean found = false;
for (int n : num) {
if (n == toFind) {
found = true;
break;
if(found)
[Link](toFind + " is found.");
else
11
[Link](toFind + " is not found.");
package selenium1;
// Java program to find index of
// an element in N elements
import [Link].*;
public class index {
// Linear-search function to find the index of an element
public static int findIndex(int arr[], int t)
// if array is Null
if (arr == null) {
return -1;
12
// find length of array
int len = [Link];
int i = 0;
// traverse in the array
while (i < len) {
// if the i-th element is t
// then return the index
if (arr[i] == t) {
return i;
else {
i = i + 1;
return -1;
// Driver Code
13
public static void main(String[] args)
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 5, 8, 9 };
my_array[0]=5;
my_array[7]=5;
// find the index of 5
[Link]("Index position of 5 is: "
+ findIndex(my_array,5));
// find the index of 7
[Link]("Index position of 7 is: "
+ findIndex(my_array, 5));
14