Beruflich Dokumente
Kultur Dokumente
string by
using recursive function. Display the original string and reversed string.
VARIABLE DESCRIPTION
OUTPUT
PROGRAM 2 : Write a program in java to perform binary search on the given array
using recursive technique.
import java.util.*;
import java.io.*;
class recursion
{
static void binarySearch(int arr[], int first, int last, int key)
{
int mid = (first + last)/2;
while( first <= last )
{
if ( arr[mid] < key )
{
first = mid + 1;
}
else if ( arr[mid] == key )
{
System.out.println("Element is found at index: " + mid);
break;
}
else
{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the no. of elements");
int n=sc.nextInt();
int arr[] =new int[n];
for(int i=0;i<n;i++)
{
System.out.println("enter array element");
arr[i]=sc.nextInt();
}
System.out.println("enter a the no. to be searched");
int key = sc.nextInt();
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1. N int To store number of elements in array
2. I int Loop control variable
3. Key int Enter number to be searched
4. Last int Store length of array
5. Mid int Find middle location
OUTPUT
PROGRAM 3: Write a program to determine how many Kaprekar numbers are
there in the range between 'p' and 'q' (p and q are <1000) and output them also
print the numbers.
Example -
i) 45² =2025, Left part=20 and right part =25, sum of both parts =20+25 = 45
ii) 297²=88209 Ieft part-88 and right part=209,sum of both parts = 88+209=297
VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 P int Enter lower limit
2 Q int Enter upper limit
3 Count int To keep count of numbers
4 I int Loop control variable
5 S string New string
6 D int Store length of string
7 Sq int Square of i
8 Rd int Store remainder
9 Ld int Store divided value
OUTPUT
PROGRAM 4: write a program in java to sort an array using insertion sort technique
// program to sort an array
import java.util.*;
public class InsertionSort
{
static void insertionSort(int array[])
{
int n = array.length;
for (int j = 1; j < n; j++)
{
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) )
{
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
public static void main(String a[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the no. of elements");
int n=sc.nextInt();
int arr[] =new int[n];
for(int i=0;i<n;i++)
{
System.out.println("enter array element");
arr[i]=sc.nextInt();
}
System.out.println("Before Insertion Sort");
for(int i:arr){
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr);
System.out.println("After Insertion Sort");
for(int i:arr){
System.out.print(i+" ");
}
}
}
VARIABLE DESCRIPTION
OUTPUT
PROGRAM 5: Write a program to input an integer number and check the given
number is a Magic Number or not. A number is a magic number, if sum of all digits
of the number is equal to 1 and the number itself is a single digit number.( i.e. keep
adding the digits of the number itself till it becomes a single digit numbers) if the
sum is one the number is a magic number otherwise number is not a magic
number.
Example: 55=3+5 -10,10 - 1+0-1 Hence, 55 is a magic number
OUTPUT
PROGRAM 6: Write a program in java to find the difference between two matrices
of the array objects passed as parameter and store the difference in the current
object (using void calDiff( Array Ob1, Array Ob2) function and display the sum
using void Display() function of a class)
VARIABLE DESCRIPTION
import java.util.*;
class Transpose
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
m = in.nextInt();
n = in.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter the elements of matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
matrix[c][d] = in.nextInt();
int transpose[][] = new int[n][m];
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
transpose[d][c] = matrix[c][d];
System.out.println("Transpose of the matrix:");
for (c = 0; c < n; c++)
{
for (d = 0; d < m; d++)
System.out.print(transpose[c][d]+"\t");
System.out.print("\n");
}
}
}
VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 m Int store number of rows
2 n Int store number of columns
3 c Int Loop control variable
4 d Int Loop control variable
OUTPUT
PROGRAM 9: Write a program which inputs positive natural number N and Prints
the possible consecutive number combinations, which when added give N.
A positive natural number(for eg 27) can represented as follows:
2+3+4+5+6+7=27
8+9+10=27
13+14=27
while(sum<n)
{
sum=sum+j;
j++;
}
if(sum==n)
{
for(int k=i;k<j;k++)
{
if(k==i)
System.out.print(k);
else
System.out.print(" + "+k);
}
System.out.println();
}
}
}
}
VARIABLE DESCRIPTION
OUTPUT
PROGRAM 10: Write a program in java to merge the current object array elements
with parameterized element
import java.util.*;
class Mixer
{
int arr[ ];
int n;
static Scanner sc=new Scanner(System.in);
Mixer(int nn)
{ n=nn;
arr=new int[n];
}
void accept( )
{ System.out.println("Enter "+ n+ " elements in ascending order");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
}
Mixer mix( Mixer A)
{ Mixer B=new Mixer(n+A.n);
int x=0;
for(int i=0 ;i<n ;i++)
B.arr[x++]=arr[i];
for(int j =0 ;j<A.n ;j++)
B.arr[x++]=A.arr[j] ;
return B ;
}
void display()
{
for(int i=0;i<n;i++)
System.out.println(arr[i]);
}
public static void main(String args[])
{
System.out.println("enter the length of first array");
int a=sc.nextInt();
System.out.println("enter the length of second array");
int b=sc.nextInt();
Mixer P=new Mixer(a);
Mixer Q=new Mixer(b);
Mixer R=new Mixer(a+b);
P.accept();
Q.accept();
R=P.mix(Q);
R.display();
}
}
VARIABLE DESCRIPTION
VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 L int store length of string
2 Rev string Store reversed string
3 S string Input a sentence In upper case
4 I int Loop control variable
5 Count int To count palindrome words in sentence
OUTPUT
PROGRAM 12: Write a program to perform insertion and deletion in a stack as an
array.
VARIABLE DESCRIPTION
OUTPUT
PROGRAM 14: write a program to perform insertion and deletion in a queue as an
array.
}
}
VARIABLE DESCRIPTION
SNO. NAME TYPE USE
1 Rear int To enter elements
2 front int To delete elements
3 Size int To initialize size of array
4 i int Loop control variable
5 num int Number to be entered
6 c int Choice 1 to enter elements and 2 to delete elements
PROGRAM 15: write a program to convert word to piglatin form.
OUTPUT
PROGRAM 16: Write a program to input any string find the shortest and the
longest word present in a sentence and print them along with their length.
OUTPUT
PROGRAM 17: Write a program to input a sentence and interchange the first and
the last alphabet of each word of the given string and print both strings
VARIABLE DESCRIPTION
SNO NAME TYPE USE
.
1 str strin Enter a sentence
g
2 k int To make a copy of loop control positioned word
3 i int Loop control variable
OUTPUT
PROGRAM 18: Write a program to input a number.Count and print the frequency
of each digit present in that number.
word++;
}
while(Kb.hasNext())
{
String line=Kb.nextLine();
String[] arr=line.split(".");
for(int i=0;i<arr.length;i++)
sentence++;
}
System.out.println(word);
System.out.println(sentence);
}
}
VARIABLE DESCRIPTION
SNO NAME TYPE USE
.
1 Word int To count number of words
2 sentenc int To count number of sentence
e
3 I int Loop control variable
OUTPUT
PROGRAM 20: program to count words in a paragraph
VARIABLE DESCRIPTION
SNO NAM TYP USE
. E E
1 Len int To trim length of para
2 Ch char To find delimeter (.)
3 Count int To count words
4 I int Loop control variable
PROGRAM 21: Write a program to print the address(row index and column index
of the largest element of the matrix of M Rows and N columns
//program to find the largest and second largest number in the array
import java.util.*;
class secondlargest
{
public static void main (String args[])
{
int i,j,a=0,b=0,c=0,d=0,x=0,y=0,l1=0,l2=0;
int arr[][]=new int[3][3];
Scanner sc= new Scanner(System.in);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.println("enter array elements");
arr[i][j]=sc.nextInt();
}
}
x=arr[0][0];
for(i=1;i<3;i++)
{
for(j=1;j<3;j++)
{
if(x>arr[i][j])
{
l1=arr[i][j];
a=i; b=j;
}
else
{
l1=arr[0][0];
a=0; b=0;
}
}
}
y=arr[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(y<x&&y>arr[i][j])
{
l2=y; c=I; d=j;
}
}
}
System.out.println("largest element:"+l1);
}
}
VARIABLE DESCRIPTION
OUTPUT
PROGRAM 22: program to create pascal’s triangle
import java.io.*;
class Pascal
{
public voidpascalw()throwsIOException //pascalw() function
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a no.”);
intn=Integer.parseInt(br.readLine()); //accepting value int [ ] pas = new int[n+1];
pas[0] = 1;
for (int i=0;i<n;i++) //loop evaluating theelements
{for (int j=0; j<=i;++j)
System.out.print(pas[j]+""); //printing the Pascal Triangle elements
System.out.println();
for (int j=i+1; j>0; j--) pas[j]=pas[j]+pas[j-1];
}}}
Variable description
variabledescription