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

Java Assignment Program

The document discusses several Java programs for performing operations on arrays: 1. A program that counts the positive, negative, and zero elements in an integer array. 2. Programs that find the greatest and smallest number in an integer array, and determine if a string starts with or ends with a given substring. 3. Additional programs that swap elements in an integer array, find odd/even numbers and their sums in an integer array, and check if an integer is present in an integer array.

Uploaded by

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

Java Assignment Program

The document discusses several Java programs for performing operations on arrays: 1. A program that counts the positive, negative, and zero elements in an integer array. 2. Programs that find the greatest and smallest number in an integer array, and determine if a string starts with or ends with a given substring. 3. Additional programs that swap elements in an integer array, find odd/even numbers and their sums in an integer array, and check if an integer is present in an integer array.

Uploaded by

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

1.

FIND NUMBER OF POSITIVE, NEGATIVE AND ZERO USING


JAVA (ARRAY)

import java.util.Scanner;

public class ArrayTest {

public static void main(String[] args) {

// create Scanner class object to take input

Scanner scan = new Scanner(System.in);

// read size of the array

System.out.print("Enter size of the array: ");

int n = scan.nextInt();

// create an int array of size n

int numbers[] = new int[n];

// take input for the array

System.out.println("Enter array elements: ");

for (int i = 0; i < n; ++i) {

1
numbers[i] = scan.nextInt();

// find of +ve, -ve, and zero

checkNumbers(numbers);

// close Scanner

scan.close();

// 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

System.out.println("Positive numbers = " + positive);

System.out.println("Negative numbers = " + negative);

System.out.println("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< 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);
}
}

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 (status.startsWith("Merit")) // LINE A
{
System.out.println("status string starts with Merit");
}
else
{
System.out.println("status string does not start with Merit");
}
if (status.startsWith("learn", 33)) // LINE B
{
System.out.println("status string contains substring learn that starts at
index 33");
}
else
{
System.out.println("status string does not contain substring learn");
}
if (status.endsWith("compete")) // LINE C
{
System.out.println("status string ends with compete");

5
}
else
{
System.out.println("status string does not end with compete");
}
System.out.println("status starts with merit : " + status.startsWith("merit"));
}
}

4. ARRAY WITH SWAPPING ONE ARRAY USING JAVA

package selenium1;

import java.util.Scanner;

class Main {

public static void main(String args[]) {

int i, t;

int arr[] = new int[4];

Scanner sc = new Scanner(System.in);

6
System.out.print("Enter 6 numbers:");

for (i = 0; i < 4; i++) {

arr[i] = sc.nextInt();

i = 0;

while (i < 4 - 1) {

t = arr[i];

arr[i] = arr[i + 1];

arr[i + 1] = t;

i = i + 2;

System.out.print("After swap list are:");

for (i = 0; i < 4; i++) {

System.out.print(" " + 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};

System.out.println("Odd Numbers:");

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

if(a[i]%2!=0){

System.out.println(a[i]);

System.out.println("Even Numbers:");

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

if(a[i]%2==0){

System.out.println(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)

System.out.println(i);

public static void main(String[] args){

findMultiples(120);

6. ODD AND EVEN NUMBER SUM OF SEPARATELY

import java.util.Scanner;

public class KboatSDAOddEvenSum

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

9
int arr[] = new int[20];

System.out.println("Enter 20 numbers");

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

arr[i] = in.nextInt();

int oddSum = 0, evenSum = 0;

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

if (arr[i] % 2 == 0)

evenSum += arr[i];

else

oddSum += arr[i];

System.out.println("Sum of Odd numbers = " + oddSum);

System.out.println("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)

System.out.println(toFind + " is found.");

else

11
System.out.println(toFind + " is not found.");

package selenium1;

// Java program to find index of

// an element in N elements

import java.util.*;

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 = arr.length;

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

System.out.println("Index position of 5 is: "

+ findIndex(my_array,5));

// find the index of 7

System.out.println("Index position of 7 is: "

+ findIndex(my_array, 5));

14

You might also like