Computer Project Work 3
Computer Project Work 3
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 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 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 :-