0% found this document useful (0 votes)
25 views8 pages

Java Array Operations and Calculations

The document contains multiple Java programs demonstrating various array manipulations, including finding intersections, performing calculations based on mathematical symbols, inserting and deleting elements, and sorting. It also includes programs for searching elements, calculating statistics from arrays, and handling user input for country and city data. Each program is structured with input handling and output display, showcasing fundamental programming concepts in Java.

Uploaded by

deypriyanshu46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

Java Array Operations and Calculations

The document contains multiple Java programs demonstrating various array manipulations, including finding intersections, performing calculations based on mathematical symbols, inserting and deleting elements, and sorting. It also includes programs for searching elements, calculating statistics from arrays, and handling user input for country and city data. Each program is structured with input handling and output display, showcasing fundamental programming concepts in Java.

Uploaded by

deypriyanshu46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Please change the Input process (Scanner or BufferReader or DataInputStream)

1. Accept data into two integers array A & B of size 5 elements each. The program should create
another array T that finds the intersection of the two arrays.
For e.g. if A = {1,3,5,7,8} & B = {7,4,2,8,9} Then T = {7,8}

public class pro1


{
public static void main()
{
int i,j,x = 0;
int a[] = {1,2,3,4,5};
int b[] = {3,4,5,6,7};
int c[] = new int[5];
for(i = 0;i<5;i++)
{
for(j = 0;j<5;j++)
{
if(a[i] = = b[j])
{
c[x] = a[i];
x++;
}
}
}
for(i = 0;i<x;i++)
[Link] (c[i]);
}
}

2. Accept numbers into an integer array A of size 4, create another string array B of size 3. Accept any
three mathematical symbols like (+, -, *, /) in array B. Then according to mathematical symbol
calculate the answer.
For e.g. if A = [8, 6, 4, 2] & B = [+, -, *], then answer will be (((8 + 6) - 4) * 2) = 20

import [Link].*;
public class pro2
{
public static void main()throws IOException
{
DataInputStream x = new DataInputStream([Link]);
int i;
double s = 0.0;
int a[] = new int[4];
String b[] = new String[3];
for(i = 0;i<4;i++)
{
[Link] ("Enter a Number: ");
a[i] = [Link] ([Link]());
}
for(i = 0;i<3;i++)
{
[Link] ("Operator[+,-,*,/]: ");
b[i] = [Link]();
}
s = a[0];
for(i = 0;i<3;i++)
{
if(b[i].equals("+"))
s = s+a[i+1];
if(b[i].equals("*"))
s = s*a[i+1];
if(b[i].equals("-"))
s = s-a[i+1];
if(b[i].equals("/"))
s = s/a[i+1];
}
[Link] ("Result is -->"+s);
}
}

2. Create an array of size 10 then accept 9 numbers into it. Then accept another number and its
position where you want to insert it. Insert the given number in given position then Display the array.
11. Accept 10 numbers into an array. Then accept a position number and delete the number present
in that position.

import [Link].*;
public class pro
{
public static void main()throws IOException
{
DataInputStream x = new DataInputStream([Link]);
int a[] = new int[10];
int i,p;
for(i = 0;i<10;i++)
{
[Link] ("enter a number: ");
a[i] = [Link] ([Link]());
}
[Link] ("enter position number: ");
p = [Link] ([Link]());
for(i = p;i<9;i++)
a[i] = a[i+1];
for(i = 0;i<9;i++)
[Link] (a[i]);
}
}

4. Accept the name, physics, chemistry and math marks of 25 students. The display a list of the given
data with Total and Average.

import [Link].*;
public class ques12
{
public static void main()throws IOException
{
DataInputStream x = new DataInputStream([Link]);
String a[] = new String[5];
int b[] = new int[5];
int c[] = new int[5];
int i;
for(i = 0;i<5;i++)
{
[Link] ("enter name:");
a[i] = [Link]();
[Link] ("enter physics marks:");
b[i] = [Link] ([Link]());
[Link] ("enter chemistry marks:");
c[i] = [Link] ([Link]());
}
[Link] ("Name \t\t Phy \t\t Chem \t\t Total \t\t Average");
for(i = 0;i<5;i++)
{
int total = b[i]+c[i];
float avg = total/2;
[Link] (a[i]+ "\t\t"+b[i]+"\t\t"+c[i]+ "\t\t"+total+"\t\t"+avg);
}
}
}

5. Write a program in Java to open 3 arrays of name A, P & N. Store 15 numbers in array A. Shift all the
positive even numbers in array P and all the negative odd numbers in array N. Finally print the array P
& N.

public class abc


{
public static void main()throws IOException
{
DataInputStream x = new DataInputStream([Link]);
int a[] = new int[5];
int p[] = new int[5];
int n[] = new int[5];
int i,k = 0,l = 0;
for(i = 0;i<5;i++)
{
[Link] ("enter a number: ");
a[i] = [Link] ([Link]());
}
for(i = 0;i<5;i++)
{
if(a[i]>0 && a[i]%2 = = 0)
{
p[k] = a[i];
k++;
}
}
for(i = 0;i<5;i++)
{
if(a[i]<0 && a[i]%2! = 0)
{
n[l] = a[i];
l++;
}
}
[Link] ("List of positive numbers");
for(i = 0;i<k;i++)
[Link] (p[i]);
[Link] ("List of negative numbers");
for(i = 0;i<l;i++)
[Link] (n[i]);
}
}

6. Write a program to find from the following data: 17, 20, 24, 29, 16, 87, 19, 52
i) The largest and smallest element b) Product of the odd numbers c) sum of the even numbers

public class abc


{
public static void main()
{
int i;
int a[] = {17,20,24,29,16,87,19,52};
int max = a[0],min = a[0],oddSum = 0,evenSum = 0;
for(i = 0;i<8;i++)
{
if(a[i]%2 = = 0)
evenSum+ = a[i];
if(a[i]%2 = = 1)
oddSum+ = a[i];
if(a[i]>max)
max = a[i];
if(a[i]<min)
min = a[i];
}
[Link] ("Sum of Odd nos = "+oddSum);
[Link] ("Sum of Even nos = "+evenSum);
[Link] ("Maximum = "+max);
[Link] ("Minimum = "+min);
}
}

Questions Based on Searching and Sorting:

1. Accept numbers into an array of size 10. Then accept a number and search that number in array. If
the number is present in array, then display the array element number where number is found. In
case of multiple found display all the positions. Display a proper message if the number is not present
in array.

// sequential search or linear search


public class abc
{
public static void main(int number)
{
int i,j;
boolean flag = false;
int a[] = {2,6,8,5,1,4,8,0,2,7};
for(i = 0;i<10;i++)
{
if(a[i] = = number)
{
[Link] ("Match found in position:"+(i+1));
flag = true;
}
}
if(flag = = false)
[Link] ("Number not found in Array");
}
}

2. Write a program to accept 10 numbers in an array and sort it.

public class bubbleSort


{
public static void main()
{
int i,j,k;
int a[] = {2,8,1,9,4,3};
for(i = 0;i<5;i++)
{
for(j = 0;j<5-i;j++)
{
if(a[j]>a[j+1])
{
k = a[j];
a[j] = a[j+1];
a[j+1] = k;
}
}
}
for(i = 0;i<6;i++)
[Link] (a[i]);
}
}

3. Create a list of names of 25 Country's and then display name alphabetically.

public class stringSort


{
public static void main()
{
String a[] = {"Germany", "Nepal", "India", "Pakistan", "Iraq"};
String k;
int i,j;
for(i = 0;i<4;i++)
{
for(j = 0;j<4-i;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
k = a[j];
a[j] = a[j+1];
a[j+1] = k;
}
}
}
for(j = 0;j<5;j++)
[Link] (a[j]);
}
}

3. There are 100 elements in an array, Write a program in JAVA to arrange first 50 elements of the
array in ascending order and rest 50 elements into descending order.

public class abc


{
public static void main()
{
int i,j,k,l;
int a[] = {2,8,1,9,4,3,7,6,3,5};
for(i = 0;i<5;i++)
{
for(j = i+1;j<6;j++)
{
if(a[i]>a[j])
{
k = a[i];
a[i] = a[j];
a[j] = k;
}
}
}
for(i = 5;i<9;i++)
{
for(j = i+1;j<10;j++)
{
if(a[i]<a[j])
{
k = a[i];
a[i] = a[j];
a[j] = k;
}
}
}
for(i = 0;i<10;i++)
[Link] (a[i]);
}
}

4. Write a program in JAVA to accept the name and marks in computer science of forty students in an
array and then print the name and marks of students according to their merit.

/* Generate the Marit List */


import [Link].*;
public class ques19
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream([Link]);
String n[] = new String[5];
int m[] = new int[5];
int i,j,k;
for(i = 0;i<5;i++)
{
[Link] ("Enter Name : ");
n[i] = std [Link]() ;
[Link] ("Enter Comp. Sc. Marks : ");
m[i] = [Link] (std [Link]() );
}
// Sort the array in Descending order
String x = null;
for(i = 0;i<4;i++)
{
for(j = i+1;j<5;j++)
{
if(m[i]<m[j])
{
k = m[i]; m[i] = m[j]; m[j] = k;
x = n[i]; n[i] = n[j]; n[j] = x;
}
}
}
// display Merit List
[Link] ("Name \t\t Marks");
for(i = 0;i<5;i++)
[Link] (n[i]+"\t\t"+m[i]);
} // end of method
}

5. Write a JAVA program to accept the temperature of any 10 cities in degrees Fahrenheit. Convert
temperature to degree centigrade using the given formula: Centigrade = (Fahrenheit - 32) X 5/9
Display the information in the given format. Also at the end print the total number of cities where the
temperature is more then 35 degree centigrade and the city name with maximum temperature.
City Name Fahrenheit Temperature Centigrade Temperature
--------- --------------------------------- ---------------------------------

Number of cities more then 35-degree centigrade temperature:


Name of the city with maximum temperature:

6. Write a program in JAVA that reads the following list of countries and their respective cities into
two separate one-dimensional arrays. The program should accept the name of a country as input and
give the name of the corresponding city as an output. The program should be designed to give an
error message where a city is asked for a country whose name is not given in the list. To stop the
program, "XXX" is to be entered as input.
GERMANY BERLIN
NEPAL KATMANDU
JAPAN TOKYO
CANADA MONTREAL
IRAQ BAGHDAD
SRI LANKA COLOMBO
BRAZIL BRAZILIA
AUSTRALIA PERTH
INDIA DELHI
SOUTH AFRICA PRETORIA
import [Link].*;
public class abc
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream([Link]);
String cou[] = {"Germany", "Nepal", "India", "Pakistan", "Itaq"};
String cap[] = {"Berlin", "Katmandu", "Delhi", "Islamabad", "Baghdad"};
int i;
int size = [Link];
String search = null;
do
{
boolean flag = false;
[Link] ("Enter Searching Name : ");
search = std [Link]() ;
for(i = 0;i<size;i++)
{
if([Link](cou[i]) = = 0 || [Link](cap[i]) = = 0)
{
[Link] ("Capital of Country "+cou[i]+" is "+cap[i]);
flag = true;
}
}
if(flag = = false)
[Link] ("Not found in list");
}while(![Link]("xxx"));
} // end of method
}

You might also like