Computer Practical Og
Computer Practical Og
A palindrome is a word that may be read the same way in either direction. Accept a
sentence in uppercase which is terminated by either “.”, “?”, or “!”. Each word of the
sentence is separated by a single blank space.
1.Perform the following task.
2.Display the count of palindrome words in the sentence.
3.Display the palindrome words in the sentence.
Example :
Sample Input: NITIN ARORA UESE LIRIL SOAP.
Answer :
import java.util.*;
class palipracti
{
public void main()
{
int count=0,l,l1;
char c,c1;
String s1="",s2="";
Scanner in=new Scanner(System.in);
System.out.println("Enter a string");
String s=in.nextLine().toUpperCase();
l=s.length();
if(s.charAt(l-1)=='.'||s.charAt(l-1)=='?'||s.charAt(l-1)=='!')
{
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{
s1=s1+c;
s1=s1.trim();
}
else
{
l1=s1.length();
for(int j=0;j<l1;j++)
{
c1=s1.charAt(j);
s2=c1+s2;
}
if(s1.equalsIgnoreCase(s2))
{
count++;
System.out.print(s2+" ");
}
s1="";
s2="";
}
}
System.out.println();
System.out.println("Number of Palindrome words : "+count);
}
else
System.out.println("INVALID OUTPUT");
}
}
OUTPUT:
Enter a string
Mom and dad enjoyed reading civic articles.
Some number of 3 digits or more are called Fascinating numbers. These numbers when
multiplied by 2 and 3, and both these products on concatenating with the original
number, all digits from 1 to 9 are present exactly once, regardless of the number of
zeros.
It could be observed that 192384576 consists of all digits from 1 to 9 exactly once.
Hence, it could be concluded that 192 is a fascinating number.
Some examples of fascinating numbers are 192,219,273,327,1902,1920,2019 etc.
Write a program in java to input a number and check whether it is a fascinating
number or not.
Answer :
import java.util.*;
class Fascinating
{
public void main ()
{
int n,n2,n3,l,a=0,count=0,z=0;
char c,c1;
String s="";
Scanner in =new Scanner(System.in);
System. out.println("Enter the number");
n=in.nextInt();
n2=n*2;n3=n*3;
s=s+n+n2+n3;
l=s.length() ;
System.out.println(s);
for(int i=0;i<l;i++)
{
c=s.charAt(i);
for(int j=0;j<l;j++)
{
c1=s.charAt(j);
if(c1=='0') //To remove '0' formed in the output
z++;
else
if(c==c1)
count++;
}
}
if(count==9)
System.out.println("It is a fascinating number");
else
System.out.println("It is not a fascinating number");
}
}
OUTPUT:
219438657
It is a Fascinating Number.
Program 3:
Write a program in java which accepts elements in an array of order MxN, where M &
N < 20. Do the following task.
i) Accept the elements of the array and display the input matrix.
ii) Create an additional row and column in the matrix such that sum of elements of each
row and respective columns should be displayed st the terminal positions.
iii) Display the new matrix.
Sample Input:
1 2 3 4
5 6 7 8
9 10 11 12
Sample output:
1 2 3 4 10
5 6 7 8 26
9 10 11 12 32
15 8 21 24
Answer :
import java.util.*;
class Sum_of_Matrix
{
public void main()
{
int r,c,rsum=0,csum=0,i,j;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows and columns");
r=in.nextInt();
c=in.nextInt();
int a[][]=new int[r+1][c+1];
System.out.println("Enter the Matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
a[i][j]=in.nextInt();
}
//Input Matrix
System.out.println("Input Matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
//row sum
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
rsum=rsum+a[i][j];
csum=csum+a[j][i];
a[i][c]=rsum;
a[r][i]=csum;
}
rsum=0;
}
//column sum
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
csum=csum+a[j][i];
a[r][i]=csum;
}
csum=0;
}
// Final Matrix
System.out.println("Final Matrix");
for(i=0;i<=r;i++)
{
for(j=0;j<=c;j++)
if(i==r&&j==c)
System.out.println(" ");
else
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
OUTPUT:
Input Matrix:
1 5 9 3
5 7 8 6
2 4 6 5
4 1 2 2
Final Matrix:
1 5 9 3 18
5 7 8 6 26
2 4 6 5 17
4 1 2 2 9
12 17 25 16
Program 4:
Sample Input:
M=10, n=100
Sample output:
The composite magic numbers are: 10,28,46,55,64,82,91,100
The frequency of composite magic numbers is 8
Answer :
import java.util.*;
class compositemagic
{
int composite(int n)
{
int i,c=0;
{
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c>2)
return(1);
else
return(0);
}
}
int magic(int n)
{
int s=n,a,c=0;
while(n>9)
{
s=0;
while(n!=0)
{
a=n%10;
s=s+a;
n=n/10;
}
n=s;
}
if(s==1)
return(1);
else
return(0);
}
public void main()
{
Scanner in=new Scanner(System.in);
int x,y,count=0;
System.out.println("Enter the two numbers m & n for finding compositemagic");
int m=in.nextInt();
int n=in.nextInt();
compositemagic ob=new compositemagic();
System.out.println("The compositemagic numbers between "+m +" and " + n +" is ");
for(int i=m;i<=n;i++)
{
x=ob.composite(i);
y=ob.magic(i);
if(x==1&&y==1)
{
System.out.print(i+" ");
count++;
}
}
System.out.println();
System.out.println("The frequency is "+ count);
}
}
OUTPUT:
Given a square matrices m[][] of order ‘N’. The maximum value possible foe N is 20.
Input the value for N and the positive integers in the matrix and perform the following
task.
a) Display the original matrix.
b)Print the row and column position of the largest element of the matrix. Print that row
and column position of the second largest element of the matrix.
c) Sort the elements of the rows in the ascending order and display the new matrix.
Sample input:
17 56 91
23 31 59
80 99 100
Sample output:
Sorted matrix:
17 23 31
56 59 80
91 99 100
Answer :
import java.util.*;
class Highest
{
public void main()
{
int a=0,b=0,c1=0,d=0,temp,k=0,n=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the row and column");
int r=in.nextInt();
int c=in.nextInt();
int m[][]=new int[r][c];
n=r*c;
int arr[]=new int[n];
//INPUT MATRIX
System.out.println("Enter the matrix");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
m[i][j]=in.nextInt();
}
//ORIGINAL MATRIX
System.out.println("The input matrix is");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print(m[i][j]+"\t");
System.out.println();
}
//Largest
int max=0,max2=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(max<m[i][j])
{
max=m[i][j];
a=i+1;
b=j+1;
}
}
}
m[a-1][b-1]=0;
//Second Largest
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(max2<m[i][j])
{
max2=m[i][j];
c1=i+1;
d=j+1;
}
}
}
System.out.println("The position of the largest element is "+a+","+b);
System.out.println("The position of the second largest element is "+c1+","+d);
m[a-1][b-1]=max;
85 61 20
15 76 99
34 49 70
15 20 34
49 61 70
76 85 99
Program 6:
An evil number is appositive whole number which has even number of 1’s in its binary
equivalent.
Binary equivalent of 9 is 1001 which contains even number of I’s.
A few evil numbers are 6,10,17,25,30…….
Design a program to accept a positive whole number and find the binary equialent of
the number and count the number of 1’s in it and display whether it is evil or not.
Answer :
import java.util.*;
class Evil
{
public void main()
{
Int b,s,sum=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number");
int n=in.nextInt();
b=n;
String a="";
while(n!=0)
{
s=n%2;
if(n%2==1)
sum++;
a=s+a;
n=n/2;
}
System.out.println("INPUT : "+b);
System.out.println("BINARY EQIVALENT : "+a);
System.out.println("NO OF ONE'S : "+sum);
if(sum%2==0)
System.out.println("OUTPUT : EVIL NUMBER");
else
System.out.println("OUTPUT : NOT AN EVIL NUMBER");
}
}
OUTPUT:
Enter the number
30
INPUT : 30
BINARY EQIVALENT : 11110
NO OF ONE'S :4
OUTPUT : EVIL NUMBER
Program 7:
Write a program that inputs the name of people into two different arrays A and B.
Array A has N number of names while array B has M number of names. Merge arrays
A and B into a single array such that the resulting array is sorted alphabetically.
Sample Input:
Enter number of names of array A, N=2
Enter number of names of array B, M=3
Sample Output:
Answer :
import java.util.*;
class Name_Sorting
{
public void main()
{
String temp;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the both arrays ");
int m=in.nextInt();
int n=in.nextInt();
for(int i=0;i<(m+n)-1;i++)
{
for(int j=0;j<(m+n)-1-i;j++)
{
if(c[j].compareTo(c[j+1])>0)
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
System.out.println("The sorted array is ");
for(int i=0;i<(m+n);i++)
System.out.println(c[i]);
}
}
OUTPUT:
7 2 1 6
3 5 0 4
8 11 10 20
15 12 13 14
Sample Output:
Original Matrix:
7 2 1 6
3 5 0 4
8 11 10 20
15 12 13 14
Rearranged matrix:
1 2 3 4
20 5 0 6
15 11 10 7
14 13 12 8
Answer :
import java.util.*;
class DDA_Sort
{
public void main()
{
int m,n,i,j,k,temp;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns");
m=in.nextInt();
int a[][]= new int[m][m];
System.out.println("Enter te array elements");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
a[i][j]=in.nextInt();
}
//INPUT MATRIX
System.out.println("The Input matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
//Entering elements from DDA to SDA
int p=2*(m+m)-4;
int b[]=new int[p];
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==0||j==0||i==m-1||j==m-1)
{
b[k]=a[i][j];
k++;
}
}
}
//Bubble sort for SDA
for(i=0;i<p-1;i++)
{
for(j=0;j<p-1-i;j++)
{
if(b[j]>b[j+1])
{
temp = b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
//Storing elements DDA
k=0;
for(j=0;j<m;j++)
a[0][j]=b[k++];
for(i=1;i<m-1;i++)
a[i][m-1]=b[k++];
for(j=m-1;j>=0;j--)
a[m-1][j]=b[k++];
for(i=m-2;i>=1;i--)
a[i][0]=b[k++];
//SORTED MATRIX
System.out.println("The sorted Matrix is");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
OUTPUT:
OUTPUT: 48 × 15 = 720
6×1=6
Remaining boxes =0
Total number of boxes = 726
Total number of cartons = 16
Example 2
INPUT: N= 140
OUTPUT: 48 x 2 = 96
24 x 1 = 24
12 x 1 = 12
6x1=6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6
Example 3
INPUT:
N = 4296
OUTPUT: INVALID INPUT
Answer :
import java.util.*;
class Carton
{
public void main()
{
int m=0,m1=0,count=0,a=0,a1=0,b=0,b1=0,c=0,c1=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of boxes");
int n=in.nextInt();
int k=n;
if(n<=1000)
{
m=n/48;
m1=n%48;
if(m!=0)
System.out.println("48 * "+m+" = "+48*m);
}
if(m1!=0)
{
a=m1/24;
a1=m1%24;
if(a!=0)
System.out.println("24 * "+a+" = "+24*a);
}
if(a1!=0)
{
b=a1/12;
b1=a1%12;
if(b!=0)
System.out.println("12 * "+b+" = "+12*b);
}
if(b1!=0)
{
c=b1/6;
c1=b1%6;
if(c!=0)
System.out.println("6 * "+c+" = "+6*c);
}
if(c1>0)
{
System.out.println("Remaining no of boxes = " +c1+" * 1= " +c1*1);
count=m+a+b+c+1;
}
else
count=m+a+b+c;
System.out.println("The total no of boxes = "+k);
System.out.println("The total no of cartons = "+count);
}
}
OUTPUT:
48 * 16 = 768
24 * 1 = 24
6*1=6
Remaining no of boxes = 1 * 1= 1
The total no of boxes = 799
The total no of cartons = 19
Program 10:
Sample Input:
Hello ! How are you?
Sample Output:
The Cipher text is:
Uryyb! Ubj ner lbh?
Answer :
import java.util.*;
class Ceaser_Cipher
{
public void main ()
{
int n,r=0, l;
char c,b;
String s;
Scanner in =new Scanner(System.in);
System.out.println("Enter the text to be ciphered");
s=in.nextLine();
l=s.length();
for(int i=0;i<l;i++)
{
c=s.charAt(i);
n=(int)(c);
if(c!=' ')
{
if(n>=65&&n<=90)
{
if(n<78)
r=n+13;
else if(n>77)
r=n-13;
}
else if(n>=97&&n<=122)
{
if(n<110)
r=n+13;
else if(n>109)
r=n-13;
}
b=(char)(r);
System.out.print(b);
}
else
System.out.print(" ");
}
}
}
OUTPUT:
Write a program to accept a sentence as input. The words in the string are to be
separated by a blank. Each word must be in uppercase. The sentence is terminated by
either “.”, “?” or “!”.
a) Obtain the length of the sentence.
b) Arrange the sentence I alphabetical order of the words.
Sample Input:
NECESSITY IS THE MOTHER OF INVENTION
Sample Output:
Length : 6
INVENTION IS MOTHER NECESSITY OF THE
Answer:
import java.util.*;
class alphabetical_order
{
public void main()
{
String s,s1,temp;
int l,count=0,a,j=0,i;
char c;
Scanner in =new Scanner (System.in);
System.out.println("Enter The Sentence");
s=in.nextLine().toUpperCase();
s=s+" ";
l=s.length();
if(s.charAt(l-1)=='.'||s.charAt(l-1)=='?'||s.charAt(l-1)=='!')
{
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(Character.isWhitespace(c))
count++;
}
String s2[]=new String[count];
s1="";
for(i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
s1=s1+c;
else
{
s2[j]=s1;
s1="";
j++;
}
}
s1="";
for(i=0;i<count;i++)
{
for(j=0;j<(count-1-i);j++)
{
if(s2[j].compareTo(s2[j+1])>0)
{
temp=s2[j];
s2[j]=s2[j+1];
s2[j+1]=temp;
}
}
}
System.out.println("Length :"+count);
for(i=0;i<count;i++)
System.out.print(s2[i]+" ");
}
else
System.out.println("INVALID OUTPUT");
}
}
OUTPUT:
Write a program to accept a sentence which may be terminated by either “.”, “?”, or
“!” only . The words may be separated by more than one blank space and are in
UPPER CASE.
Perform the following tasks:
a) Find the number of words beginning and ending with a vowel.
b) Place the words which begin and end with a vowel at the beginning, followed by
the remaining words as they occur in the sentence.
Example:
Sample Input:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE
Sample Output:
Number of words beginning and ending with a vowel = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL
Sample Input:
HOW ARE YOU@
Sample Output:
INVALID INPUT
Answer :
import java.util.*;
class vowel
{
public void main()
{
int count=0;
String s1="",s2="",s3="";
Scanner in=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=in.nextLine().toUpperCase();
s=s+" ";
int l=s.length();
if(s.charAt(l-1)=='.'||s.charAt(l-1)=='?'||s.charAt(l-1)=='!')
{
char c,c1,c2;
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c!=' ')
{
s1=s1+c;
s1=s1.trim();
}
else
{
int l1=s1.length();
c1=s1.charAt(0);
c2=s1.charAt(l1-1);
if((c1=='A'||c1=='E'||c1=='I'||c1=='O'||c1=='U') && (c2=='A'||c2=='E'||c2=='I'||
c2=='O'||c2=='U'))
{
count++;
s2=s2+" "+s1;
s2=s2.trim();
}
else
{
s3=s3+" "+s1;
s3=s3.trim();
}
s1="";
}
}
System.out.println("Number of words beginning and ending with a vowel = "+count);
System.out.println("The rearranged sentence "+s2+" "+s3);
}
else
System.out.println("INVALID INPUT");
}
}
OUTPUT:
Enter a sentence
You must aim to be a better person tomorrow than you are today
Number of words beginning and ending with a vowel = 2
The rearranged sentence A ARE YOU MUST AIM TO BE BETTER PERSON
TOMORROW THAN YOU TODAY
13. A stack is a kind of data structure which can store elements with the restriction that
an element can be added or removed from the top only. It follows LIFO nature.(Last In
First Out).
The details of the class stack is given below:
Class name : Stack
Data Members/Instance Variables:
st[] : the array to hold the names
size : the maximum capacity of the string array.
top: the index of the topmost element of the stack.
ctr: to count the number of elements of the stack.
Member Functions:
Stack(int cap): constructor to initialize size = cap and top =-1
void pushname(String n) : to push a name onto the stack. If the stack is full, display the
message “OVERFLOW”.
String popname() : removes a name from the top of the stack and returns it. If the stack
is empty, display the message “UNDERFLOW”
void display(): Display the elememts of the stack.
Specify the class stack giving details of the constructors(), void pushname(String n),
String popname() and void display()
Answer :
import java.util.*;
class stack
{
int size,top,ctr;
String st[]=new String[10];
Scanner in=new Scanner(System.in);
stack(int cap)
{
for(int i=0;i<10;i++)
st[i]="";
size=cap;
top=-1;
ctr=0;
}
void pushname(String n)
{
if(top==(size-1))
System.out.println("OVERFLOW");
else
{
top++;
st[top]=n;
ctr++;
}
}
String val="";
String popname()
{
if(top==-1)
System.out.println("UNDERFLOW");
else
{
val=st[top];
top--;
}
return(val);
}
void display()
{
System.out.println("The stack elements are :");
for(int i=top;i>=0;i--)
System.out.println(st[i]);
}
}
14.Queue is an entity which can hold a maximum of 100 integers. The queue enables the
user to add integers from the rear and remove integers from the front. Define a class
queue with the following details.
Class Name: Queue
Data Members/ Instance Variables
Que[] : array to hold the integer elements.
size : stores the size of the array.
front : to point the index of the front.
rear : to point the index of the rear.
Member Functions
Queue(int mm) : constructor to initialize the data size =mm, front =0, rear = 0
void delete (int v) : to add integer from the rear if possible else display the message
“Overflow”.
Int delete() : returns elements from front if present, otherwise displays the
message :Underflow” and return -9999
void display() : displays the array elements.
Specify the class Queue giving details of above methods.
Answer :
import java.util.*;
class Queue
{
int Que[]=new int[100];
int size,front,rear,val;;
Queue(int mm)
{
size=mm;
front=0;
rear=0;
}
void Insert (int v)
{
if(rear==size-1)
System.out.println("Overflow");
else
if(front==-1&&rear==-1)
{
front=0;
rear=0;
}
else
{
Que[rear]=v;
rear=rear+1;
}
}
int Delete()
{
if(front==-1&&rear==-1)
{
System.out.println("Underflow");
return(9999);
}
else
val=Que[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
return(val);
}
void display()
{
if(front==-1&&rear==-1)
System.out.println("Queue is empty");
else
{
System.out.println("The elements of the queue are: ");
for(int i=front;i<=rear;i++)
System.out.print(Que[i]+" ");
System.out.println();
}
}
}
Program 15:
Answer :
import java.util.*;
class Linearsearch_name
{
public void main()
{
int i,n,f=0;
String ns="";
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
n=in.nextInt();
String a[]=new String[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextLine();
System.out.println("Enter the element to be searched");
ns=in.nextLine();
for(i=0;i<n;i++)
{
if(a[i].equalsIgnoreCase(ns))
{
f=1;
break;
}
}
if(f==1)
System.out.println("Item found in "+(i+1)+" position");
else
System.out.println("Item not found");
}
}
OUTPUT:
Answer :
import java.util.*;
class Binarysearch
{
public void main()
{
int lb,ub,mid=0,ns,f=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
a[i]=in.nextInt();
lb=0;
ub=n-1;
System.out.println("Enter the element to be searched");
ns=in.nextInt();
while(lb<=ub)
{
mid=lb+ub/2;
if(a[mid]>ns)
ub=mid-1;
if(a[mid]<ns)
lb=mid+1;
if(a[mid]==ns)
{
f=1;
break;
}
}
if(f==1)
System.out.println("Item found in "+(mid+1)+" position");
else
System.out.println("Item not found");
}
}
OUTPUT:
Write a program to accept n names in an array and sort the elements in descending
order using Bubble sort technique.
Answer :
import java.util.*;
class Bubble_Sort_name
{
public void main()
{
String temp;
int i;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
String a[]=new String[n];
System.out.println("Enter the array elements");
for( i=0;i<n;i++)
a[i]=in.nextLine();
for(i=0;i<n-1;i++)
{
for(int j=0;j<n-1-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("the sorted elements are....");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT:
Write a program to accept n numbers of double type and sort the elements in ascending
order by using Selection Sort technique.
Answer :
import java.util.*;
class Selection_Sort
{
public void main()
{
double temp,min;
int i;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
double a[]=new double[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextDouble();
for( i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n-1;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
System.out.println("The sorted elements are....");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT:
Write a program to accept n integer elements in an array and sort the elements using
insertion sort technique
Answer :
import java.util.*;
class Insertion_Sort
{
public void main()
{
int temp,i,j;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextInt();
//Sorting of the element
for( i=1;i<n;i++)
{
j=i-1;
while(j>-1)
{
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
i--;
}
j--;
}
}
System.out.println("The sorted elements are ");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT:
Answer :
import java.util.*;
class Bubble_Sort_number
{
public void main()
{
int temp,i;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
a[i]=in.nextInt();
for( 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("The sorted elements are....");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
OUTPUT: