Array SDA
Array SDA
import java.util.*;
class A1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int A[]=new int[5];
import java.util.*;
class A2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int A[]=new int[10];
intEsum=0,Osum=0;
for(int j=0;j<10;j++)
{
if(A[j]%2==0)
Esum=Esum+A[j];
else
Osum=Osum+A[j];
}
import java.util.*;
class A3
{
public void main()
{
Scanner sc=new Scanner(System.in);
int A[]=new int[10];
int Great=A[0];
for(int j=0;j<10;j++)
{
if(A[j]>Great)
Great=A[j];
}
for(int j=0;j<10;j++)
{
if(A[j]%3==0 || A[j]%5==0)
sum=sum+A[j];
}
System.out.println("Sum = "+sum);
}
}
5. WAP to accept 10 different numbers in a Single Dimensional Array(SDA). Now, enter a
number and search whether the number is present or not in the list of array elements by using
the ‘Linear Search’ technique.
import java.util.*;
class A5
{
public void main()
{
Scanner sc=new Scanner(System.in);
int A[]=new int[5];
int item,flag=0;
for(int j=0;j<5;j++)
{
if(A[j]==item)
{
flag=1;
System.out.println("Number is present in the list");
break;
}
}
if(flag==0)
System.out.println("Number is not present in the list");
}
}
6. WAP to initialize the ‘Seven Wonders of the World along with their locations in two
different arrays. Search for a name of the country input by the user. If found, display the
country along with its Wonder.
Seven Wonders:Chichen Itza, Christ the Redeemer, TajMahal, Great Wall of China, Machu
Picchu, Petra, Colosseum.
Location: Maxico, Brazil, India, China, Peru, Jordan, Italy
import java.util.*;
class A6
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String WON[]={"Chichen Itza", "Christ the Redeemer", "TajMahal", "Great Wall of
China", "Machu Picchu", "Petra", "Colosseum"};
for(int j=0;j<LOC.length;j++)
{
if(LOC[j].equalsIgnoreCase(country))
{
flag=1;
System.out.println(WON[j]+"\t"+LOC[j]);
break;
}
}
if(flag==0)
System.out.println("Sorry! Not found");
}
}
7. WAP to accept 10 states and 10 capitals of a country in two different Single Dimensional
Array(SDA). Now, enter a state of the country to display it’s capital. If it is present then display
it’s capital otherwise, display a relevant message.
import java.util.*;
classA7
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String state[]=new String[5];
String capital[]=new String[5];
String state_name;
int flag=0;
for(int j=0;j<state.length;j++)
{
if(state[j].equalsIgnoreCase(state_name))
{
flag=1;
System.out.println("Search successful");
System.out.println(state[j]+"\t"+capital[j]);
break;
}
}
if(flag==0)
System.out.println("State name not in the list");
}
}
8. WAP to accept the names of 10 cities in a single dimensional string array and their
STD(Subscribers Trunk Dialing) codes in another single dimensional integer array. Search for
the name of a city input by the user in the list. If found, display the message “Search
unsuccessful, no such city in the list”.
import java.util.*;
class A8
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String CITY[]=new String[5];
long STD[]=new long[5];
String City_name;
int flag=0;
for(int j=0;j<5;j++)
{
if(CITY[j].equalsIgnoreCase(City_name))
{
flag=1;
System.out.println(CITY[j]+"\t"+STD[j]);
break;
}
}
if(flag==0)
System.out.println("Seacrch unsuccessful");
}}
9: WAP to stores 10 words in a Single Dimensional Array. Display only those words which are
Palindromes.
Sample Input: MADAM, TEACHER, SCHOOL, NITIN
Sample Output: MADAM
NITIN
import java.util.*;
class A9
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String A[]=new String[5];
System.out.println("Enter the 5 words in an Array");
for(int i=0;i<5;i++)
{
A[i]=sc.next();
}
for(int j=0;j<5;j++)
{
String n=A[j];
String Rev="";
char ch;
int len=n.length();
for(int k=len-1;k>=0;k--)
{
ch=n.charAt(k);
Rev=Rev+ch;
}
if(n.equalsIgnoreCase(Rev))
System.out.println(n);
Rev="";
}
}
}
10.Write a program in Java to store 20 temperatures in °F in a Single Dimensional Array
(SDA) and display all the temperatures after converting them into °C.
Hint: (c/5) = (f - 32) / 9
import java.util.Scanner;
public class A10
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
Sample Output: -32, -41, -19, -44, 15, 21, 54, 61, 71, 52
import java.util.Scanner;
public class A11
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter 10 numbers");
for (int i = 0; i < arr.length; i++)
{
arr[i] = in.nextInt();
}
n[0 n[1 n[2 n[3 n[4 n[5 .. n[16 n[17 n[18 n[19
] ] ] ] ] ] . ] ] ] ]
..
45 65 77 71 90 67 82 19 31 52
.
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++)
{
arr[i] = in.nextInt();
}
System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++)
{
int c = 0;
for (int j = 1; j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
c++;
}
}
if (c == 2)
System.out.print(arr[i] + ", ");
}
}}
13. Write a program to accept name and total marks of N number of students in two single
subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]
import java.util.Scanner;
Name Marks
..... .....
..... .....
Write a program to input the names and marks of the students in the subject.
Calculate and display:
(a) The subject average marks (subject average marks = subject total/50).
(b) The highest marks in the subject and the name of the student. (The maximum marks in the
subject are 100.)
import java.util.Scanner;
public class A14
{
public static void main(String args[]) {
final int TOTAL_STUDENTS = 50;
Scanner in = new Scanner(System.in);
From 80 to 100 A
From 60 to 79 B
From 40 to 59 C
Less than 40 D
import java.util.Scanner;
System.out.println();
System.out.println("Enter 20 numbers:");
for (i = 0; i < NUM_COUNT; i++) {
arr[i] = in.nextInt();
}
System.out.println("Even Numbers:");
for (i = 0; i < eIdx; i++) {
System.out.print(even[i] + " ");
}
System.out.println("\nOdd Numbers:");
for (i = 0; i < oIdx; i++) {
System.out.print(odd[i] + " ");
}
}}
Question 18
Write a program to store 20 numbers in a Single Dimensional Array (SDA). Now, display only
those numbers that are perfect squares.
n[0 n[1 n[2 n[3 n[4 n[5 .. n[16 n[17 n[18 n[19
] ] ] ] ] ] . ] ] ] ]
..
12 45 49 78 64 77 81 99 45 33
.
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("Truncated numbers");
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + ", ");
}
}
}
Question 20.
The annual examination result of 50 students in a class is tabulated in a Single Dimensional Array
(SDA) is as follows:
Write a program to read the data, calculate and display the following:
(a) Average marks obtained by each student.
(b) Print the roll number and the average marks of the students whose average is above. 80.
(c) Print the roll number and the average marks of the students whose average is below 40.
import java.util.Scanner;
Question 21.
Write a program to store 6 elements in an array P and 4 elements in an array Q. Now, produce a
third array R, containing all the elements of array P and Q. Display the resultant array.
P[ ] Q[ ] R[ ]
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
import java.util.Scanner;
i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}
int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}
Question 22.
Write a program to accept the year of graduation from school as an integer value from the user.
Using the binary search technique on the sorted array of integers given below, output the message
"Record exists" if the value input is located in the array. If not, output the message "Record does
not exist".
Sample Input:
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
198 198 199 199 199 200 200 200 200 201
2 7 3 6 9 3 6 7 9 0
import java.util.Scanner;
if (idx == -1)
System.out.println("Record does not exist");
else
System.out.println("Record exists");
}}
Question 23.
Write a program to input and store roll numbers, names and marks in 3 subjects of n number of
students in five single dimensional arrays and display the remark based on average marks as given
below:
85 — 100 Excellent
75 — 84 Distinction
60 — 74 First Class
40 — 59 Pass
import java.util.Scanner;
System.out.println("Roll No\tName\tRemark");
for (int i = 0; i < n; i++) {
String remark;
if (avg[i] < 40)
remark = "Poor";
else if (avg[i] < 60)
remark = "Pass";
else if (avg[i] < 75)
remark = "First Class";
else if (avg[i] < 85)
remark = "Distinction";
else
remark = "Excellent";
System.out.println(rollNo[i] + "\t"
+ name[i] + "\t"
+ remark);
}
}
}
Question 24.
Write a program that reads ten integers and displays them in the reverse order in which they were
read.
Answer
import java.util.Scanner;
System.out.println("Enter 10 integers:");
for (int i = 0; i < 10; i++) {
arr[i] = in.nextInt();
}
Question 25.
Write a program that reads a long number, counts and displays the occurrences of each digit in it.
Answer
import java.util.Scanner;
while (num != 0) {
int d = (int)(num % 10);
dCount[d] = dCount[d] + 1;
num /= 10;
}
System.out.println("Digit\tOccurence");
for (int i = 0; i < 10; i++) {
if (dCount[i] != 0) {
System.out.println(i + "\t" + dCount[i]);
}
}
}
}
Question 26.
Write a program to perform binary search on a list of integers given below, to search for an element
input by the user. If it is found display the element along with its position, otherwise display the
message "Search element not found".
5, 7, 9, 11, 15, 20, 30, 45, 89, 97
Answer
import java.util.Scanner;
if (index == -1) {
System.out.println("Search element not found");
}
else {
System.out.println(n + " found at position " + index);
}
}}
Question 27
Declare a single dimensional array of size 28 to store daily temperatures for the month of February.
Using this structure, write a program to find:
1. The hottest day of the month
2. The coldest day of the month
3. The average temperature of the month
Answer
import java.util.Scanner;
sum += febTemp[i];
}
29.WAP to initialize 5 different city names in a Single Dimensional Array. Arrange the names in
ascending order by using the ‘Bubble Sort’ technique and display them.
Input: Delhi, Bangalore, Agra, Mumbai, Calcutta.
Output: Agra, Bangalore, Calcutta, Delhi, Mumbai.
30. WAP in Java to stores the runs scored by 11 Indian Cricket Players in an innings along with their
names. Now display the name of the cricketer who has made the highest score in that innings along
with the runs.
31. Design a class to accept and store 10 strings into an array and print the string starting with vowel.
32. Write a program to accept 10 characters into an array and perform the following operations.
a. Count the uppercase letters
b. Count the digits
c. Display the vowels present in the array
33. Define a class accept and store 10 strings into the array and print the strings with even number of characters.
34. Write a program to store n real numbers in an array A, Shift the integer part in another array B and shifting
fractional part in array C. Print minimum integer from Array B and maximum fractional number from
array C along with index numbers.
35. Write a program to store a single dimensional array, count and print only those name and length which
are starting and ending with vowel.