/*1.
Write a program in Java to store 20 numbers (even and odd numbers) in a Single
Dimensional Array (SDA). Calculate and display the sum of all even numbers and product of all odd
numbers .*/
import java.util.*;
public class even_odd
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
int a[]=new int[20];
System.out.println("Enter 20 numbers");
for(int i=0;i<20;i++)
{
a[i]=s.nextInt();
}
int sum=0,product=1;
for(int i=0;i<20;i++)
{
if(a[i]%2==0)
sum=sum+a[i];
else
product=product*a[i];
}
System.out.println("sum of even numbers="+sum);
System.out.println("Product of odd numbers="+product);
}
}
/*2. Define a class to accept values into a double array of size 20 and print the range of the array,
range is the difference between the largest and the smallest elements of the array .*/
import java.util.*;
public class max_min
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
double a[]=new double[20];
System.out.println("Enter 20 numbers");
for(int i=0;i<20;i++)
{
a[i]=s.nextDouble();
}
double max=a[0],min=a[0];
for(int i=0;i<20;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("Largest number="+max);
System.out.println("Smallest number="+min);
System.out.println("Range="+(max-min));
}
}
/*3. Define a class to accept 10 numbers in an single dimensional array and check for the existence of the
given number in the array using linear search ,if found print number found with the position of the number, if
not found print number not found .
*/
import java.util.*;
public class Linear
{
public static void main(String args[])
{
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 number to be searched");
int sh=sc.nextInt();
int temp=0,index=0;;
for(int i=0;i<10;i++)
{
if(a[i]==sh)
{
temp=1;
index=i;
break;
}
}
if(temp==1)
{
System.out.println("number found");
System.out.println("number ="+sh+ "\t"+"position ="+(index+1));
}
else
System.out.println("number not found");
}
}
/*4. Define a class to accept the names of 10 students in an array and check for the existence of the given
name in the array using linear search ,if found print search successful with the index of the name where it is
present, if not found print search unsuccessful. Also print the names which begins with the word “SRI”.*/
import java.util.*;
public class Linear_search
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
String a[]=new String[10];
System.out.println("Enter 10 names");
for(int i=0;i<10;i++)
{
a[i]=s.nextLine();
}
System.out.println("Enter name to be searched");
String sh=s.nextLine();
int temp=0,index=0;
for(int i=0;i<10;i++)
{
if(a[i].equalsIgnoreCase(sh))
{
temp=1;
index=i;
break;
}
}
if(temp==1)
{
System.out.println("search successful");
System.out.println("name = "+sh+"\t"+"index = "+index);
}
else
System.out.println("search unsuccessful");
for(int i=0;i<10;i++)
{
if(a[i].startsWith("SRI"))
{
System.out.println(a[i]);
}
}
}
}
/*5. Write a program to search for an integer value input by the user in the sorted list given below using
binary search technique .If found display“ Search Successful”, otherwise display “Search Unsuccessful”.
{31,36,45,50,60,75, 86,90}*/
import java.util.*;
public class Binary_search
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
int a[]={31,36,45,50,60,75,86,90};
System.out.println("Enter the number to be searched");
int ns=s.nextInt();
int fb=0,lb=a.length-1,mid=0,temp=0;
while(fb<=lb)
{
mid=(fb+lb)/2;
if(ns>a[mid])
fb=mid+1;
if(ns<a[mid])
lb=mid-1;
if(ns==a[mid])
{
temp=1;
break;
}
}
if(temp==1)
System.out.println("Search successful");
else
System.out.println("Search unsuccessful");
}
}
/* 6 Write a program to input n numbers in an array. Arrange these numbers in ascending order using bubble
sort technique.
*/
import java.util.*;
public class numbers_ascending
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter n");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter 10 numbers");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int temp=0;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("sorted numbers in ascending order ");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
/* 7 Write a program to input n numbers in an array. Arrange these numbers in descending order using
bubble sort technique.
*/
import java.util.*;
public class numbers_descending
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter n");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter 10 numbers");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int temp=0;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("sorted numbers in descending order ");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
/* 8 Write a program to input twenty names in an array. Arrange these names in ascending order of letters,
using the bubble sort technique.
Sample Input:
Rohit, Devesh, Indrani, Shivangi, Himanshu, Rishi, Piyush, Deepak, Abhishek, Kunal , .....
Sample Output:
Abhishek, Deepak, Devesh, Himanshu, Indrani, Kunal, Piyush, Rishi, Rohit, Shivangi, ..…*/
import java.util.*;
public class names_ascending
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
String a[]=new String[20];
System.out.println("Enter 20 names");
for(int i=0;i<20;i++)
{
a[i]=s.nextLine();
}
String temp="";
for(int i=0;i<19;i++)
{
for(int j=0;j<19-i;j++)
{
if(a[j].compareTo(a[j+1])>0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("sorted names in Ascending order ");
for(int i=0;i<20;i++)
{
System.out.println(a[i]);
}
}
}
/* 9. Write a program to input twenty names in an array. Arrange these names in ascending order of letters,
using the bubble sort technique.
Sample Input:
Rohit, Devesh, Indrani, Shivangi, Himanshu, Rishi, Piyush, Deepak, Abhishek, Kunal , .....
Sample Output:
Shivangi,Rohit,Rishi, Piyush, Kunal, Indrani,, Himanshu,, Devesh,,Deepak , Abhishek.........
*/
import java.util.*;
public class names_descending
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
String a[]=new String[20];
System.out.println("Enter 20 names");
for(int i=0;i<20;i++)
{
a[i]=s.nextLine();
}
String temp="";
for(int i=0;i<19;i++)
{
for(int j=0;j<19-i;j++)
{
if(a[j].compareTo(a[j+1])<0)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("sorted names in descending order ");
for(int i=0;i<20;i++)
{
System.out.println(a[i]);
}
}
}
/*10 Write a program to input n numbers in an array. Arrange these numbers in ascending order using
selection sort technique.
*/
import java.util.*;
public class num_asc_selection
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter n");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter 10 numbers");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int temp=0,min=0;
for(int i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("sorted numbers in ascending order");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
/* 11 Write a program to input n numbers in an array. Arrange these numbers in descending order using
selection sort technique.
*/
import java.util.*;
public class num_des_selection
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter n");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter 10 numbers");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int temp=0,max=0;
for(int i=0;i<n;i++)
{
max=i;
for(int j=i+1;j<n;j++)
{
if(a[j]>a[max])
{
max=j;
}
}
temp=a[i];
a[i]=a[max];
a[max]=temp;
}
System.out.println("sorted numbers in descending order");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
/*12 Write a program to input 10 names in an array. Arrange these names in ascending order using selection
sort technique.
*/
import java.util.*;
public class name_asc_selection
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
String a[]=new String[10];
System.out.println("Enter 10 names");
for(int i=0;i<10;i++)
{
a[i]=s.nextLine();
}
String temp="";
int min=0;
for(int i=0;i<10;i++)
{
min=i;
for(int j=i+1;j<10;j++)
{
if(a[j].compareTo(a[min])<0)
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("sorted names in ascending order");
for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
}
/*13 Write a program to input 10 names in an array. Arrange these names in descending order using
selection sort technique.
*/
import java.util.*;
public class name_des_selection
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
String a[]=new String[10];
System.out.println("Enter 10 names");
for(int i=0;i<10;i++)
{
a[i]=s.nextLine();
}
String temp="";
int max=0;
for(int i=0;i<10;i++)
{
max=i;
for(int j=i+1;j<10;j++)
{
if(a[j].compareTo(a[max])>0)
{
max=j;
}
}
temp=a[i];
a[i]=a[max];
a[max]=temp;
}
System.out.println("sorted names in descending order ");
for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
}