Loreto solved assignments class X 2013

/*1. Wap in java to demonstrate the technic of
selection sort and bubble sort depending upon the choice given by the user.
The program will accept 10 values from the and sort them in ascending order.
The element of the array will be displayed after every search.*/

import java.io.*;
class lc1
{
public static void main(String args[])throws IOException
{
BufferedReader in =new BufferedReader (new InputStreamReader (System. in));
int i, j, ch,a[],k,n,c, t,p,min,tt;
a=new int [10];
n=10;
for(i=0; i<10; i++)
{
System.out.print("Enter a no.");
a[i] = Integer.parseInt (in.readLine ());
}
System.out.println ("1 for Selection sort");
System.out.println ("2 for Bubble sort");
System.out.println ("Enter your choice");
ch= Integer.parseInt (in.readLine ());

switch (ch)
{ case 1 :

for (i=0;i<=n-2;i++)
{
p=i;
min=a[i];
for (j=i+1;j<=n-1;j++)
{
if (min>a[j])
{ min=a[j];
p=j;
}
}
tt =a[i];
a[i] =a[p] ;
a[p]=tt;

for (c=0; c<10; c++)
{
System.out.print(a[c] +" ");
}
 System.out.println();

}//end of i loop

break;

case 2:

for (i=0; i<=10-2; i++)
{
for (k=0;k<=10-2-i;k++)
{
if (a[k]> a[k+1])
{
t=a[k];
a[k] =a[k+1];
a[k+1] =t;
}
}
for (c=0; c<10; c++)
{
System.out.print(a[c] +" ");
}
System.out.println();
}

break;
default: System.out.println ("Invalid choice");
}//End of switch
}//End of main
}//End of class

//===================================================================

/*3. Design a class alpha which enables a character array arranged in ascending order according to its 10 alphabet . use function read() to accept the array element , void arrange() to arrange alphabet in order and void display ()to display the arranged array.*/
import java.util.*;
class lc2
{
char a[], t ;
int i,k ;

public static void main(String args[])
{
lc2 ob=new lc2();
ob.read();
ob.arrange();
ob.display();

}//end of main()

public void read()
{
Scanner in =new Scanner(System. in);
a=new char[10];

System.out.println("Enter a characters");
for (i=0; i<10;i++)
{
a[i] =in.next().charAt(0);
}
}
//--------end of read()----

public void arrange()
{
for (i=0; i<=10-2; i++)
{
for (k=0;k<=10-2-i;k++)
{
 if (a[k]> a[k+1])
 {
   t=a[k];
   a[k]=a[k+1];
   a[k+1] =t;
 } //End of if
}//End of for loop (counter k)
}//End of for loop (counter i)

}
//--------end of arrange()----

public void display()
{
for (i=0; i<10;i++)
{
System.out.print(a[i] + " ");
}//End of for loop
}
//---------end of display()---

}//End of class alpha

//=================================================================

/*2.WAP to input 15 real numbers and arrange them in descending order  of their integer values.Display the maximum and minimum integer. */

import java.io.*;
class lc4
{
public static void main(String args[]) throws IOException
{
InputStreamReader br = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(br);

int i, j,first,temp,max,min;
float b ;
float ar[]=new float [15];
int a[]=new int[15];


for(i=0;i<15;i++)
{
System.out.print("Enter real number :");
ar[i]=Float.parseFloat(in.readLine());
a[i]=(int)ar[i];
}

for(i=0;i<15;i++)
{
b=ar[i]-a[i];

if(b>=0.5)
 a[i]=a[i]+1;
else
 a[i]=a[i]+0;

}

for (i=0;i<=a.length-2;i++)
{

for(j=0;j<=a.length-2-i;j++)
{
if(a[j] < a[j+1])      
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}        
}
}
System.out.println("In descending order: " );
for(i=0;i<15;i++)
{
System.out.print(" " + a[i]);
}


max=a[0];
min=a[0];

for(i=1;i<15;i++)
{

if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("MAXIMUM integer value is"+ max);
System.out.println("MINIMUM integer value is"+ min);
}
}

//=======================================================
/*3.WAP in java that will accept a list of  10 numerical values from the user and store
 only the palindrome numbers in a new array palin [ ].  Display the new array palin [ ] in descending order. The list terminates when the user enters zero. */

import java.io.*;
class palindrome
{
public static void main(String args[]) throws IOException
{
InputStreamReader br = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(br);

int k=0,s,b,i,j,temp,first,max,min,n;
int num[]=new int[10];
int palin[]=new int[10];

for(i=0;i<10;i++)
{
System.out.println("Enter a number");
num[i]=Integer.parseInt(in.readLine());
if(num[i]==0)
break;
}
n=i;

for(i=0;i<n;i++)
{
b=num[i];
s=0;
while(num[i]>0)
{
s=s*10 + num[i]%10;
num[i]=num[i]/10;
}
if(s==b)
palin[k++]=b;
}

for (i=0;i<=k-2;i++)  
{
first=i;   
for(j=i+1;j<=k-1;j++)   
{
if(palin[j] > palin[first])         
first=j;
}
temp = palin[first];   
palin[first] = palin[i];
palin[i] = temp; 
}           
System.out.println("In descending order: ");
for(i=0;i<k;i++)
{
System.out.print(" " + palin[i]);
}

}
}

//==================================================================
/*11. Wap in java to accept any 10 no. in an array and delete the duplicate elements and print the new array arranging in descending order.*/

import java.io.*;
class dup
{
 public static void main(String args[])throws IOException
 {
   int a[],b[],i,j,t,c=0,k=0;
   a=new int[10];
   b=new int[10];
   DataInputStream in=new DataInputStream(System.in);

   for(i=0;i<10;i++)
    {
      System.out.print("Enter a no:");
      a[i]=Integer.parseInt(in.readLine());
    }

    for(i=0;i<10;i++)
     {
        c=0;
      for(j=i;j<10;j++)
       {
        if(a[i]==a[j])
          c++;
       }
      if(c==1)
       b[k++]=a[i];
    }

   System.out.println("after duplicates removal nos are");
    for(i=0;i<k;i++)
     System.out.print(" "+b[i]);



for(i=0;i<=k-2;i++)
 {
  for(j=0;j<=k-2-i;j++)
  {
    if(b[j]<b[j+1])
    {
     t=b[j];
     b[j]=b[j+1];
     b[j+1]=t;
    }
  }
 }
   System.out.println("\n after sorting nos are");
    for(i=0;i<k;i++)
     System.out.print(" "+b[i]);


 }
}








No comments:

Post a Comment