0% found this document useful (0 votes)
4 views16 pages

Array Solved Programs Done26

The document contains multiple Java programs that demonstrate various array manipulations, including counting even and odd numbers, calculating student averages, sorting numbers, searching for capitals, and more. Each program is structured to accept user input and perform specific operations on arrays, showcasing fundamental programming concepts. The document serves as a collection of coding exercises for practicing array handling and control structures in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Array Solved Programs Done26

The document contains multiple Java programs that demonstrate various array manipulations, including counting even and odd numbers, calculating student averages, sorting numbers, searching for capitals, and more. Each program is structured to accept user input and perform specific operations on arrays, showcasing fundamental programming concepts. The document serves as a collection of coding exercises for practicing array handling and control structures in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ARRAY SOLVED PROGRAM

[Link] a program to accept 20 integer numbers in a single Dimensional Array. Find and
Display the following:
i. Number of even numbers.
ii. Number of odd numbers.
iii. Number of multiples of 4
import [Link].*; public
class Numbers
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]); int
i,j,c1=0,c2=0,c3=0;
int a[] = new int[20]; [Link]("Enter
20 numbers"); for(i=0;i<20;i++)
{
a[i] = [Link]();
}
for( i=0;i<20;i++)
{
if(a[i]%2 == 0) c1++;
if(a[i]%2 !=0) c2++;
if(a[i]%4 ==0) c3++;
}
[Link]("Number of even numbers\t "+c1);
[Link]("Number of odd numbers\t "+c2);
[Link]("Number of multiple of 4\t "+c3);
}
}
[Link] a program to accept the marks in Physics, Chemistry and Maths secured
by 40 students of a class in a single Dimensional Array. Find and display the
following:
i. Number of students securing 80% and above in aggregate.
ii. Number of students securing 30% and below in aggregate.
import [Link].*;
public class Average
{
public static void main()

{
Scanner in = new Scanner([Link]);
int i,j,c1=0, c2=0;
int phy[] = new int[40];
int chem[] = new int[40];
int maths[] = new int[40];
for(i=0;i<40;i++)
{
[Link]("Maths secured in Physics");
phy[i]=[Link]();
[Link]("Marks secured in Chemistry");
chem[i]=[Link]();
[Link]("Marks secured in Maths");
maths[i]=[Link]();
}
for(i=0;i<40;i++)
{
if((phy[i]+chem[i]+maths[i])/3.0 >=80)
c1++;

if((phy[i]+chem[i]+maths[i])/3.0 <=34)
c2++;
}
[Link]("Number of students gettinng 80% and above\t"+c1);
[Link]("Number of students gettinng 34% and below\t"+c2);
}
}
4. Write a program in Java to accept 20 numbers in a single dimensional array
arr[20]. Transfer and store all the even numbers in an array even[ ] and all the
odd numbers in another array odd[ ]. Finally, print the elements of both the
arrays.

import [Link];
public class Even_odd
{
public static void main(String args[]) {

Scanner in = new Scanner([Link]);


int i = 0;

int arr[] = new int[20];


int even[] = new int[20];
int odd[] = new int[20];

[Link]("Enter 20 numbers:");
for (i = 0; i < 20; i++) {
arr[i] = [Link]();
}
int e = 0, od = 0;
for (i = 0; i < 20; i++) {
if (arr[i] % 2 == 0)
even[e++] = arr[i];
else
odd[od++] = arr[i];
}
[Link]("Even Numbers:");
for (i = 0; i < even[i]; i++) {
[Link](even[i] + " ");
}
[Link]("Odd Numbers:");
for (i = 0; i < odd[i]; i++) {
[Link](odd[i] + " ");
}
}
}
5. The class teacher wants to store the marks obtained in English, Maths
and Science of her class having 40 students. Write a program to input marks
in Eng, Science and Maths by using three single dimensional arrays.
Calculate and print the following information:
(i) Average marks secured by each student.
(ii) Class average in each subject.
[Hint: Class average is the average marks obtained by 40 students in a particular
subject.]

import [Link].*;
public class Average_Marks
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int e[] = new int[40];
int m[] = new int[40];
int s[] = new int[40];
double avg[] = new double[40];
for (int i = 0; i < 40; i++) {
[Link]("Enter student " + (i+1) );
[Link]("Marks in English: ");
e[i] = [Link]();
[Link]("Marks in Maths: ");
m[i] = [Link]();
[Link]("Marks in Science: ");
s[i] = [Link]();
avg[i] = (e[i] + m[i] + s[i]) / 3.0;
}
int t1 = 0,t2 = 0, t3= 0;
for (int i = 0; i < 40; i++) {
[Link]("Average marks of student " + (i+1) + " = " + avg[i]);
t1=t1+ e[i];
t2=t2+ m[i];
t3 = t3+s[i];
[Link]("Class Average in English = " + ((double)t1 / 40));
[Link]("Class Average in Maths = " + ((double)t2 / 40));
[Link]("Class Average in Science = " + ((double)t3 /
40));
}
}
6. Write a program in Java to accept 20 numbers in a single dimensional
array arr[20]. Transfer and store all the even numbers in an array
even[ ] and all the odd numbers in another array odd[ ].
Finally, print the elements of both the arrays.

import [Link];
public class Even_Odd
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int i = 0;
int arr[] = new int[20];
int even[] = new int[20];
int odd[] = new int[20];
[Link]("Enter 20 numbers:");
for (i = 0; i < 20; i++) {
arr[i] = [Link]();
}
int e = 0, od = 0;
for (i = 0; i < 20; i++) {
if (arr[i] % 2 == 0)
even[e++] = arr[i];
else
odd[od++] = arr[i]; }
[Link]("Even Numbers:");
for (i = 0; i < even[i]; i++) {
[Link](even[i] + " ");
}
[Link]("Odd Numbers:");
for (i = 0; i < odd[i]; i++) {
[Link](odd[i] + " "); }}]
7. Create an array of size 10. Automatically fill the array with the factorial of
number between 1 to 10, and then display the content of array.

import java .util.*;


public class Array_factorial
{
public static void main(String args[])
{
int i,f = 1;
Scanner sc=new Scanner([Link]);
int x[] = new int[10];
for(i = 0;i<10;i++)
{
f = f*(i+1);
x[i] = f;
}
for(i = 0;i<10;i++)
[Link] (x[i]);
}
}
8. Write a program to accept 20 integer numbers in a single Dimensional Array.
Using menu driven approach display the following, as per user's choice:
i. All the perfect numbers store in the array.
ii. All the Buzz numbers store in the array.[A number whose sum of factors
(including 1 and excluding the number itself)
is the same is side to be a perfect number. A number that is divisible by 7 or has
last digiit as 7,
is said to buzz nymber.]

import [Link].*;
class Number1
{
public static void main()
{
Scanner in = new Scanner([Link]);
int a[] = new int [20];
int n,s,i,j;
[Link]("Enter integer numbers");

for(i=0;i<20;i++)
a[i]=[Link]();
[Link]("Menu Items");
[Link](" 1. To display all the perffect numbers");
[Link]("1. To display all the Buzz numbers");
[Link]("Enter your choice");
n=[Link]();
switch(n)
{
case 1:
for(i=0;i<20;i++)
{
s=0;
for(j=1;j<a[i];j++)
{
if(a[i]%j == 0)
s = s + j;
}
if(s == a[i])
[Link](a[i]);
}break;
case 2:
for(i=0;i<20;i++)
{
if(a[i]%7 == 0 || a[i]%10 == 7)
[Link](a[i]);
}
break;
default:
[Link]("Invalid choice");
}}}
9. Write a program to accept 10 states and 10 capitals of country in two
different single dimensional array. Now, enter a state of the country to
display its capital.
If it is present then display its capital otherwise, display a relevant message.
Sample input: enter the state and the capital
Bihar
Patna
West Bengal
Kolkata and so on------------
sample Output: enter the state whose capital is to be searched:
West Bengal
The capital is Kolkata
import [Link].*;
public class Capital
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,a=0,k=0;
String st;
String m[]=new String[10];
String n[]=new String[10];
for(i=0;i<10;i++)
{
[Link]("Enter states in the cell :");
m[i]=[Link]();
[Link]("Entter capital in the cell :");
n[i]=[Link]();
}
[Link]("Enter the states whose capital to be searched");
st=[Link]();
for(i=0;i<10;i++)
{
if(m[i].equals(st))
{
k=1;
a=i;
}
}
if(k==1)

[Link]("The capital is "+n[a]);


else
[Link]("The state"+st+ "is not found at any location");
}}

10 .Using scanner class, write a program to input 10 important cities with their
STD code in two different single dimensional arrays. The program display only
those cities which begin with a vowel with their corresponding STD code.
To display the STD code of cities which begins with a vowel.

import [Link];
public class STDcode
{

public static void main(String[] args)


{
Scanner in=new Scanner([Link]);
int i;
char ch;
String st[]=new String[10];
int code[]=new int[10];
for(i=0;i<10;i++)
{
[Link]("Enter city and STD code one by one");
st[i]=[Link]();
code[i]=[Link]();
}
[Link]("The city begins wih a vowel & corresponding STD code
:");
for(i=0;i<10;i++)
{
ch=st[i].charAt(0);

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='
O'|| ch=='U')

[Link](st[i]+" "+code[i]);
}
}
}
[Link] a program that reads a long number, counts and displays the
occurrences of each digit in it.

import [Link].*;

public class Number_digit


{
public static void main(String args[]) {

Scanner in = new Scanner([Link]);


[Link]("Enter a number: ");
long num = [Link]();
int count[] = new int[10];

while (num != 0) {
int d =(int) num % 10;
count[d] = count[d] + 1;
num =num/ 10;
}

[Link]("Digit occurance");
for (int i = 0; i < 10; i++) {
if (count[i] != 0) {
[Link](i + "\t" + count[i]);
}
}
}
}
12Write a program to initialilze 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 fouond, display the country alonng with its
Wonder, otherwise display 'Sorry Not Found!'
seven wonders:chichen Itza, Christ the redeemer, Taj Mahal, Great wall of
china, Machu Picchu, Petra, Colosseum.
location: Mexico, Brazil, india, china, Peru, Jordan, Italy
Example: Input: Country Name India
Output: India Taj Mahal
Country name USA
Output: 'Sorry Not Found'!
import [Link].*;
class Seven_Wonders
{
public static void main(String argd[])
{
Scanner in=new Scanner([Link]);
String wond[]=new String[7];
String locn[]=new String[7];
int i,f=0;String w="";
String l;
[Link]("Enter seven wonders and location");
for(i=0;i<7;i++)
{
wond[i]=[Link]();
locn[i]=[Link]();
}
[Link]("Enter the country name");
l=[Link]();

f=0;
for(i=0;i<7;i++)
{
if([Link](locn[i]))
{
f=f+1;
w=wond[i];
l=locn[i];
}}
if(f==1)
{
[Link]("Search successful:");
[Link](l+":\tWonders:\t"+w);
}
else
[Link]("Search unsuccessful, no such location in the list");
}}

13. Write a program to accept 10 different numbers is a single dimension Array.


Arrange the number in ascending order by using the 'selection Sort' technique
and display them.
import [Link].*;
public class Selection_Short
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,j,t,min;
int m[]=new int[10];;
for(i=0;i<10;i++)
{
[Link]("Enter the number in the cell :");
m[i]=[Link]();
}
for(i=0;i<10;i++)
{
min=i;
for(j=i+ 1;j<10;j++)
{
if(m[j]<m[min])
min=j;
}
t=m[i];
m[i]=m[min];
m[min]=t;
}

[Link]("The number arranged in ascending order are");


for(i=0;i<10;i++)
[Link](m[i]);
}}

14. Write program to accept 10 different nos in a SDA. now enter a number and
by using binary search technique check whether the number is present or not.
import [Link].*;
public class BinarySearch
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int i,k=0,ns;int mid=0,first=0,last=9;
int m[]=new int[10]; for(i=0;i<10;i+
+)
{
[Link]("Enter the number in the cell:");
m[i]=[Link]();
}
[Link]("Enter the number to be searched:");
ns=[Link]();
while(first<=last)
{
mid=(first+last)/2;
if(m[mid]<ns)
first =mid+1;
if(m[mid]>ns)
last=mid-1;
if(m[mid]==ns)
{
k=1;
break;
}
}
if(k==1)
[Link]("The number is present");
else
[Link]("The number is not present");
}}

15. Write a program in Java to store 10 city names in a single


Dimensional Array. Display only those words/names which begin with a
consonant but end with a vowel.

import [Link].*;
public class Vowel_Consonant
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);

int i,l;String c[]=new String[10],st ;char fl,ll;


[Link]("Enter 10 city names");

for(i=0;i<10;i++)
{
c[i]=[Link]();
}
[Link]("Following are the cities starting with consonant and ending
with vowels");
for(i=0;i<10;i++)
{
st=c[i].toUpperCase();

l=[Link]();
fl=[Link](0);
ll=[Link](l-1);

if(fl!='A'&& fl!='E'&& fl!='I'&& fl!='O'&& fl!='U' )


{
if(ll=='A' || ll=='E' || ll=='I' ||ll=='O' || ll=='U')
[Link](c[i]);
}
}
}
}

You might also like