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

Computer Project Work 3

The document contains 12 questions related to arrays in Java. Each question provides a problem statement and the code solution. The problems involve tasks like accepting user input into arrays, calculating sums/products of array elements based on certain conditions, searching arrays, sorting arrays, and identifying prime numbers. The solutions demonstrate the use of basic array operations like initialization, traversal, conditional checking and output display.

Uploaded by

Soma Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Computer Project Work 3

The document contains 12 questions related to arrays in Java. Each question provides a problem statement and the code solution. The problems involve tasks like accepting user input into arrays, calculating sums/products of array elements based on certain conditions, searching arrays, sorting arrays, and identifying prime numbers. The solutions demonstrate the use of basic array operations like initialization, traversal, conditional checking and output display.

Uploaded by

Soma Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

ARRAY PROGRAMMS

Q. Accept 10 numbers into an array and then calculate the sum of numbers
present in odd positions and even positions respectively.

Soln.
import java.util.Scanner;
class array_1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
System.out.println("Enter 10 integers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
int e=0,o=0;
for(int i=0;i<10;i++)
{
if((i+1)%2==0)
{
e=e+a[i];
}
else
{
o=o+a[i];
}
}
System.out.println("The sum of the numbers in Odd Positions & Even
Positions are "+o+" and "+e+" resepectively");
}
}
OUTPUT :-
Q. WAP to find from the following data :-
17,20,24,29,16,87,19,52
i) The largest and smallest element
ii) Product of the odd numbers
iii) Sum of the even numbers

Soln.
import java.util.Scanner;
class array_2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]={17,20,24,29,16,87,19,52};
int max=a[0],min=a[0],p=1,e=0,l=a.length;
for(int i=0;i<l;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
if(a[i]%2==0)
{
e=e+a[i];
}
else
{
p=p*a[i];
}
}
System.out.println("The Largest and Smallest Elements are "+max+"
and "+min+" respectively");
System.out.println("The Product of the Odd Numbers are "+p);
System.out.println("The sum of Even Elements is "+e);
}
}
OUTPUT :-
Q. 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.

Soln.
import java.util.Scanner;
class array_3
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter the number to be searched !!!");
int s=sc.nextInt();
int f=0,c=0;
for(int i=0;i<10;i++)
{
if(a[i]==s)
{
if(c==0)
{
System.out.println("The number "+s+" is present in "+(i+1)+"
Position");
}
else
{
System.out.println("The number "+s+" is also present in
"+(i+1)+" Position");
}
f=1;
c++;
}
}
if(f==0)
{
System.out.println("The number "+s+" is not found !!!!!");
}
}
}
OUTPUT :-

Q. WAP in java to accept the temperature of any 10 cities in degree


Fahrenheit. Convert temperature to degree centigrade using the given
formula :
Centigrade = (Fahrenheit-32) X 5/9
Soln.
import java.util.Scanner;
class array_4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double t[]=new double[10];
System.out.println("Enter the temperatures of any 10 Cities in Degree
Fahrenheit !!!");
for(int i=0;i<10;i++)
{
t[i]=sc.nextDouble();
}
for(int i=0;i<10;i++)
{
t[i]=(t[i]-32.0)*(5.0/9.0);
}
System.out.println("The Temperatures of 10 Cities converted from
Degree Fahrenheit to Degree Celsius are :-");
for(int i=0;i<10;i++)
{
System.out.println(t[i]);
}
}
}
OUTPUT :-

Q. WAP in java to store all the even numbers from array a to array b.
Soln.
import java.util.Scanner;
class arr_5
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of elements to be entered!!!");
int l=sc.nextInt();
int a[]=new int[l];
int j=0;
int b[]=new int[l];
System.out.println("Enter "+l+" numbers");
for(int i=0;i<l;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<l;i++)
{
if(a[i]%2==0)
{
b[j]=a[i];
j++;
}
}
System.out.println("The even elements of 'a' stored in 'b' are/is :-");
for(int i=0;i<j;i++)
{
System.out.print(b[i]+"\t");
}
}
}
OUTPUT :-

Q. WAP in java to input 10 values in an array and calculate the sum of even
number present in odd index.
Soln.
import java.util.Scanner;
class array_6
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
System.out.println("Enter 10 integers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
int e=0;
for(int i=0;i<10;i++)
{
if((i+1)%2!=0)
{
if(a[i]%2==0)
{
e=e+a[i];
}
}
}
System.out.println("The sum of even elements in odd positions is "+e);
}
}
OUTPUT :-

Q. WAP in java to find the duplicate values of an array of string values.


Soln.
import java.util.Scanner;
class arr_7
{
String[] sort(String a[],int l)
{
String t="";
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(a[i].compareTo(a[j])>0)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
return a;
}
void display()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of string elements to be
entered");
int l=sc.nextInt();
String a[]=new String[l];
int c=0;
for(int i=0;i<l;i++)
{
a[i]=sc.next();//abc.dbc,abc,efg,abc
}
a=sort(a,l);//abc,abc,abc,dbc,efg
int d=0;
System.out.println("The duplicate values of the array are or is :-");
for(int i=0;i<l-1;i++)
{
d=0;
while(a[i+1].equals(a[i]))
{
if(i==l-2 && a[i].equals(a[i+1]))
{
System.out.println(a[i]);
break;
}
i++;
d=1;
}
if(d==1)
{
System.out.println(a[i]);
}
else
{
c++;
}
}
if(c==l-1)
{
System.out.println("Sorry there are no duplicate elements !!!!!");
}
}
}
OUTPUT :-
Q. WAP in java to find the second largest element in an array.
Soln.
import java.util.Scanner;
class arr_8
{
void display()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements");
int l=sc.nextInt();
int a[]=new int[l];
System.out.println("Enter "+l+" numbers");
for(int i=0;i<l;i++)
{
a[i]=sc.nextInt();
}
int t=0;
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
System.out.println("The second largest element is "+a[1]);
}
}
OUTPUT :-
Q. WAP in java to find the second smallest element in an array.
Soln.
import java.util.Scanner;
class arr_9
{
void display()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements");
int l=sc.nextInt();
int a[]=new int[l];
System.out.println("Enter 10 numbers");
for(int i=0;i<l;i++)
{
a[i]=sc.nextInt();
}
int t=0;
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
System.out.println("The second smallest element is "+a[1]);
}
}
OUTPUT :-
Q. WAP to input 10 integer values in a single-dimensional array and count
the number of prime elements in that array.
Soln.
import java.util.Scanner;
class arr_10
{
boolean prime(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
return true;
else
return false;
}
void count()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int c=0;
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<10;i++)
{
if(prime(a[i])==true)
{
c++;
}
}
System.out.println("The number of prime elements is "+c);
}
}
OUTPUT :-

Q. WAP in java to input 10 integers and calculate the sum of all the prime
numbers in the array.
Soln.
import java.util.Scanner;
class arr_11
{
boolean prime(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
return true;
else
return false;
}
void sum()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int s=0;
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<10;i++)
{
if(prime(a[i])==true)
{
s+=a[i];
}
}
if(s==0)
{
System.out.println("There are no prime elements in the array !!!!!");
}
else
{
System.out.println("The sum of prime elements is "+s);
}
}
}
OUTPUT :-

Q. WAP in java to input 10 integers and replace the prime elements in the
array with 0.
Soln.
import java.util.Scanner;
class arr_12
{
boolean prime(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
return true;
else
return false;
}
void replace()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The Original Elements are :-");
for(int i=0;i<10;i++)
{
System.out.print(a[i]+"\t");
}
System.out.println();
for(int i=0;i<10;i++)
{
if(prime(a[i])==true)
{
a[i]=0;
}
}
System.out.println("The Replaced elements are :-");
for(int i=0;i<10;i++)
{
System.out.print(a[i]+"\t");
}
}
}
OUTPUT :-
SORTING
Q. WAP in java to input 10 integer numbers in an array and sort the
elements in ascending by Method of Bubble Sort.
Soln.
import java.util.Scanner;
class Sort
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
void input()
{
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
}
void sort_data()
{
System.out.println("The Original Array was ");
for(int i=0;i<10;i++)
{
System.out.print(a[i]+"\t");
}
for(int i=0;i<10;i++)
{
for(int j=0;j<10-i-1;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void display()
{
System.out.println();
System.out.println("The Sorted Array is ");
for(int i=0;i<10;i++)
{
System.out.print(a[i]+"\t");
}
}
void main()
{
Sort ob=new Sort();
ob.input();
ob.sort_data();
ob.display();
}
}
OUTPUT :-

Q. WAP in java to input 10 integer numbers in an array and sort the


elements in ascending by Method of Selection Sort.
Soln.
import java.util.Scanner;
class Sort_sele
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
void input()
{
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
}
void sort_data()
{
System.out.println("The Original Array was ");
for(int i=0;i<10;i++)
{
System.out.print(a[i]+"\t");
}
for(int i=0;i<9;i++)
{
int p=i;
int m=a[i];
for(int j=i+1;j<10;j++)
{
if(a[j]<m)
{
m=a[j];
p=j;
}
}
int t=a[i];
a[i]=m;
a[p]=t;
}
}
void display()
{
System.out.println();
System.out.println("The Sorted Array is ");
for(int i=0;i<10;i++)
{
System.out.print(a[i]+"\t");
}
}
void main()
{
Sort_sele ob=new Sort_sele();
ob.input();
ob.sort_data();
ob.display();
}
}
OUTPUT :-

You might also like