Algorithm-: Variable Discription
Algorithm-: Variable Discription
Algorithm-: Variable Discription
within the string. The characters with multiple frequencies should be displayed only
once.
Algorithm-
Step1 Start.
Step2 Declare suitable variables like n,i,jand str variable of string type.
Step3 Start a loop from i=65 to i<=90.
Step4 Start a loop from j=0 to j<=length of string.
Step5 Check if int(ch)==i and if true then increment p by 1 and then print
the value of p.
Step6 Repeat step3 and step4 with i=97 and i<= 122 and then repeat
step5.
Step7 End.
Variable Discription :
* TYPE NAME PURPOSE
* int n Store length of str1
* String str1 to add character.
* Int i looping variable
* Int j looping variable
* Int p counter variable
* char ch to store present character
* char ch2 to compare with ch2
PROGRAM-
import java.io.*;
class frequencyofeachletter // defining class
{
public static void main(String args[])throws Exception //defining main function
{
BufferedReader num=new BufferedReader(new
InputStreamReader(System.in));
String str,str1=new String();//declaring string
str1=" ";
int n,i,j,p=0;
System.out.println("Enter your string");
str=num.readLine(); //accepting data from the user
n=str.length();
for(i=65;i<=90;i++) //generating loop
{
for(j=0;j<n;j++) //generating loop
{
char ch=str.charAt(j); // extracting a character
if((int)ch==i)
{
p++;
}
}
char ch2=(char)i;
System.out.print("frequency of "+ch2+"= "+" "+" "); // printing
System.out.print(p+" ");
System.out.println();
p=0;
}
for(i=97;i<=122;i++)// generating loop
{
for(j=0;j<n;j++)
{
char ch=str.charAt(j);
if((int)ch==i)
{
p++;
}
}
char ch2=(char)i;
System.out.print("frequency of "+ch2+"= "+" "+" ");//printing
System.out.print(p+" ");
System.out.println();
p=0;
}
}
}
output-
INPUT—
FISSION OCCURS WHEN ONE ELEMENT BREAKS INTO
SEVERALOTHERS,RELEASING ENERGY IN PROCESS.
OUTPUT—
frequency of H= 2
frequency of I= 5
frequency of J= 0
frequency of K= 1
frequency of L= 3
frequency of M= 1
frequency of N= 8
frequency of O= 6
frequency of P= 1
frequency of Q= 0
frequency of R= 7
frequency of S= 9
frequency of T= 3
frequency of U= 1
frequency of V= 1
frequency of W= 1
frequency of X= 0
frequency of Y= 1
frequency of Z= 0
(2) Write a program to input three binary numbers and display the sum of them.
For eg-
1101+1001+1100=100010.
Algorithm-
Step1. Initialize array arr [] with 100.
Step 2. Declare a,b,c,i,s,s1,s2,d,d1,d2,c,c0,c1,c2,sum,k.
Step 3. Initilize s,s1,s2,d,d1,d2,sum,k with 0 and c0,c1,c2 with 1;
Step 4. Input three binary number from user.
Step 5. Begin loop for i from a to1 and repeat step 6 to 8.
Step 6. Assign value of i%10 to s.
Step 7. d=d+s*c0.
Step 8. Assign value of c0*2 to c0.
Step 9. Begin loop for i from b to1 and repeat step 10 to 12.
Step 10. Assign value of i%10 to s1.
Step 11. d1=d1+s1*c1.
Step 12. Assign value of c1*2 to c1.
Step 13. Begin loop for i from c to1and repeat step 14 to 16.
Step 14. Assign value of i%10 to s2.
Step 15. d2=d2+s2*c2.
Step 16. Assign value of c2*2 to c2.
Step 17. Add d,d1,d2 and store the value to sum.
Step 18 Begin while loop for sum upto 0 and repeat step 19 to 21.
Step 19. Assign value of sum%2 to sum.
Step 20. Assign value of sum/2 to sum.
Step 21. k++.
Variable Discription :
TYPE NAME PURPOSE
int c To get the remainder
Int i looping variable
Int n1,n2,n3 To store 3 nos. by user
Int sum1,sum2,sum3 To get the sum of binary nos.
int p1,p2,p3 to store powers
int k array variable
int sum4 sum of all 3 nos.
int a[] array
PROGRAM-
import java.io.*;
output-
Input—
Enter three numbers
1001
1101
1100
Output--
the added binary number is=100010.
(3) Write a program to input a number and display the following pattern.
N=4
1 8 13 16
5 2 9 14
11 6 3 10
15 12 7 4
Algorithm-
Variable Discription :
TYPE NAME PURPOSE
int a[][] Store 2-D array.
int c1,c2 counter variable
Int i looping variable
Int n input by user
Int r row of 2-D array
int c column of 2-D array
PROGRAM-
import java.io.*;
class matrix // class begins
{
public static void main()throws Exception // defining main function
{
BufferedReader num=new BufferedReader(new
InputStreamReader(System.in));
int a[][]=new int[20][20]; // declaring matrix
int p=1,c1=0,c2=0,k1=1,k=1,r,c,n,i;
output-
Input--
enter your limit
Output--
1 10 17 22 25
6 2 11 18 23
14 7 3 12 19
20 15 8 4 13
24 21 16 9 5
( 4) Write a program to generate a magical matrix.A magical matrix is such that the sum of each
row,each column and each diagonal is always same.
Eg.
8+1+6
3+5+7
4+9+2
Algorithm-
Step1 Start.
Step2 Declare suitable variables like n,r=0,I,r1,c1,c and declare an 2-d
array of limit 10.
Step3 Take an odd limit in n and perform c=n/2.
Step4 Start a loop from i=1 to i<=n*n.
Step5 Strore a[r][c] in I and then put r1=r,c1=c.
Step6 Perform r-- and c++.
Step7 Check if r==-1 then put r=n-1.
Step8 Check if r>n-1 then put r=0.
Step9 Check if c>n-1 then put c=0.
Step10 Check if a[r][c]!=0,if so then put r=r1,c=c1,r++.
Step11 Now print the elements of 2-d array.
Step12 End.
Variable Discription :
* TYPE NAME PURPOSE
* int a[][] Store 2-D array.
* int i looping variable
* int n input by user
* int r row of 2-D array
* int c column of 2-D array
PROGRAM-
import java.io.*;
class magic // defining class
{
public static void main(String args[])throws Exception
{ // defining main function
BufferedReader num=new BufferedReader
(new InputStreamReader(System.in));
int a[][]=new int[10][10]; // declaring array
int n,r=0,i,r1,c1,c;
System.out.println("Enter your odd limit");
n=Integer.parseInt(num.readLine()); // taking input from the user
c=n/2;
output-
Input--
Enter your odd limit
Output-
8 1 6
3 5 7
4 9 2
(5) A square of square matrix can be generated by taking any square matrix of even
dimensions (>=2) and fill the innermost number n is circular order. Then fill its
surrounding square one by one.
Eg N=4 size=6
20 21 22 23 24 25
39 8 9 10 11 26
38 19 4 5 12 27
37 18 7 6 13 28
36 17 16 15 14 29
35 34 33 32 31 30
Algorithm-
Step1 Start.
Step2 Declare suitable variables like p,c,x,n,r1,c1,c2,c3,k,n,r,i,j and
array of limit declare a 2-d 10.
Step3 Take an limit in N and perform p=N and c=n/2-1.
Step4 Start a loop from r=n/2-1 to r<=n/2 and perform a[c][r]=p++ and
after executing the loop do c++;
Step5 Start a loop from r=n/2 to r>=n/2-1,r—and perform a[c][r]=p++.
Step6 Perform c++.
Step7 Start the loops with j,c1,c2,c3 and i to store the numbers in
circular order.
Step8 Now print the elements of 2-d array.
Step9 End.
Variable Discription :
* TYPE NAME PURPOSE
* int a[][] 2-D array
* int c,p counter variable
* int c1,c2,c3 columns of array
* int i,j looping variable
* int N input from user
PROGRAM-
import java.io.*;
class circular // class definition starts
{
public static void main(String args[])throws Exception//main function
starts
{
BufferedReader num=new BufferedReader(new
InputStreamReader(System.in));
int a[][]=new int[10][10];
int p,c,x,n,r1,c1,c2,c3,k,N,r,i,j; // initializing variables
OUTPUT-
Input--
Enter the value of N
5
enter the dimensions
Output--
21 22 23 24 25 26
40 9 10 11 12 27
39 20 5 6 13 28
38 19 8 7 14 29
37 18 17 16 15 30
36 35 34 33 32 31
(6) Write a program to sort the elements in a two dimensional array.
For eg—
Input--
2 31 54 87
1 2 3 47
64 58 27 30
34 21 2 28
Output—
87 64 58 54
47 34 31 30
28 27 21 3
2 2 2 1
Algorithm-
Variable Discription :
* TYPE NAME PURPOSE
* int a[][] Store 2-D array.
* int temp for swapping aray elements
* int r,r1 row of 2-D array
* int c,c1 column of 2-D array
PROGRAM-
import java.io.*;
class twodsorting // class definition starts
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new
InputStreamReader(System.in));
int a[][]=new int[6][5];
int r,c,temp,r1,c1;
for(r=0;r<4;r++) // generating for loop
{
for(c=0;c<4;c++)
{
System.out.println("Enter your number");
a[r][c]=Integer.parseInt(num.readLine()); // input by user
}
}
for(r1=0;r1<4;r1++) // for loop
{
for(c1=0;c1<4;c1++)// for loop
{
for(r=0;r<4;r++)// for loop
{
for(c=0;c<4;c++)// for loop
{
if(a[r1][c1]>a[r][c]) // condition
{
/* swapping process begins */
temp=a[r1][c1];
a[r1][c1]=a[r][c];
a[r][c]=temp;
/* swapping process ends */
}
}
}
}
}
System.out.println("The sorted array is");
for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{
System.out.print(a[r][c]+" ");
}
System.out.println();
}
}
}
output-
Input—
Enter your number
2
Enter your number
31
Enter your number
54
Enter your number
87
Enter your number
1
Enter your number
2
Enter your number
3
Enter your number
47
Enter your number
64
Enter your number
58
Enter your number
27
Enter your number
30
Enter your number
34
Enter your number
21
Enter your number
2
Enter your number
28
Output—
The sorted array is
87 64 58 54
47 34 31 30
28 27 21 3
2 2 2 1
(7) Write a program in input two matrix and print the product of them.
them
Algorithm-
Step-1 Declare array A[][],B[][],C[][] and m,n,p,q for size.
Step-2 Input size of matrix A[][] in m,n.
Step-3 Input size of matrix B[][] in p,q.
Step-4 Input elements of matrix A[][] of m rows and n columns.
Step-5 Input elements of matrix B[][] of p rows and q columns.
Step-6 Check if n=p then goto step 7 otherwise goto step 15.
Step-7 Begins a loop for i from 0 to m-1 and repeat step 8 to 11.
Step-8 Begins a loop for j from 0 to q-1 and repeat step 9 to 11.
Step-9 Assign 0 to C[i][j].
Step-10 Begins a loop for k from 0 to n-1 and repeat step 11.
Step-11 Multiply matrix A[][] and B[][] as
C[i][j]=C[i][j] +(A[i][k]*B[k][j]).
Step-12 Print matrix A[][] of m rows and n columns using nested loops.
Step-13 Print matrix B[][] of p rows and q columns using nested loops.
Step-14 Print matrix C[][] of m rows and q columns using nested loops.
Step-15 End of algorithm.
Variable Discription :
* TYPE NAME PURPOSE
* a[][]
* int b[][] Store 2-D array.
* cr[][]
* int m,n row &column of matrix A
* int p,q row &column of matrix B
* int i,j looping variable
* int r,c row &column of cr[][]
PROGRAM-
import java.io.*;
class Product_Matrix // class starts
{
public static void main(String args[])throws Exception // main function
starts
{
BufferedReader num=new BufferedReader(new
InputStreamReader(System.in));
int m,n,p,q,i,j,k,r,c; // declaring variables
int a[][]=new int[20][20];
int b[][]=new int[20][20];
int cr[][]=new int[20][20];
System.out.println("Input no. of rows and columns matrix A");
m=Integer.parseInt(num.readLine()); // input from user
n=Integer.parseInt(num.readLine()); // input from user
System.out.println("Input no. of rows and columns matrix B");
p=Integer.parseInt(num.readLine()); // input from user
q=Integer.parseInt(num.readLine()); // input from user
System.out.println("Input elements of A");
for(r=0;r<m;r++)
for(c=0;c<n;c++)
{
System.out.println("Enter numbers for matrix A");
a[r][c]=Integer.parseInt(num.readLine()); // input from user
}
System.out.println("Input elements of B");
for(r=0;r<p;r++)
for(c=0;c<q;c++)
{
System.out.println("Enter numbers for matrix B");
b[r][c]=Integer.parseInt(num.readLine()); // input from user
}
if(n==p) // condition
{
for(i=0;i<m;i++) // for loop
{
for(j=0;j<q;j++) // for loop
{
for(k=0;k<q;k++) // for loop
{
cr[i][j]+=a[i][k]*b[k][j]; // updating new 2-D array
}
}
}
}
System.out.println("Matrix A");
for(r=0;r<m;r++) // for loop
{
for(c=0;c<n;c++) // for loop
System.out.print(a[r][c]+"\t"); // printing array
System.out.println();
}
Input—
Enter numbers for matrix A
2
Enter numbers for matrix A\
3
Enter numbers for matrix A
4 System.out.println("Matrix B");
Enter numbers for matrix A for(r=0;r<p;r++) // for loop
{ 5
Enter numbers for matrix A for(c=0;c<q;c++) // for loop
6 System.out.print(b[r][c]+"\t"); //
Enter numbers for matrix A printing array
7 System.out.println();
} Enter numbers for matrix A
8 System.out.println("Matrix C");
Enter numbers for matrix A for(r=0;r<m;r++) // for loop
{ 9
Input elements of B for(c=0;c<q;c++) // for loop
Enter numbers for matrix B System.out.print(cr[r][c]+"\t"); //
1 printing array
Enter numbers for matrix B System.out.println();
} 2
} Enter numbers for matrix B
} 3
Enter numbers for matrix B
4
Enter numbers for matrix B
5
Enter numbers for matrix B
6
Enter numbers for matrix B
7
Enter numbers for matrix B
8
Enter numbers for matrix B
9
Output--
Matrix A
1 2 3
4 5 6
7 8 9
Matrix B
1 2 3
4 5 6
7 8 9
Matrix C
(8) Write a program to input a four letter word and print all the possible combinations of
that word.
Algorithm-
Variable Discription :
TYPE NAME PURPOSE
int n Store length of str1
String str1 to add character.
Int i looping variable
Int j looping variable
Int p counter variable
char ch to store present character
char ch2 to compare with ch2
PROGRAM-
import java.io.*;
class permutation
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new
InputStreamReader(System.in));
String str;
int n,j,k,l,i;
System.out.println("Enter your four letter word");
str=num.readLine();
n=str.length();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
for(l=0;l<n;l++)
{
if(i!=j&&i!=k&&i!=l&&j!=k&&j!=l&&k!=l)
{
System.out.print(str.charAt(i));
System.out.print(str.charAt(j));
System.out.print(str.charAt(k));
System.out.print(str.charAt(l));
System.out.println();
}
}
}
}
}
}
}
output-
Input-
Enter your four letter word
Raju
Output--
raju
rauj
rjau
rjua
ruaj
ruja
arju
aruj
ajru
ajur
aurj
aujr
jrau
jrua
jaru
jaur
jura
juar
uraj
urja
9) Write a program to input a matrix and remove the central numbers and then print the
sum of remaining numbers in the matrix.
Original Matrix
1 2 3
4 5 6
7 8 9
Then the output is-
Square Matrix
1 2 3
4 6
7 8 9
Sum is : 40
Algorithm-
Step 1 Start.
Step 2 Declare variables like r,c,m=0,s=0,l=n and a 2d array of limit n which
is to be entered from the user.
Step 3 Start a while loop such that n>1.
Step 4 Start a loop in which r=m,c=m tll c<l-m and then perform s=s+a[r][c]
and then print it.
Step 5 Change the line..
Step 6 Start for loop from r=m+1 till r<l-m-1.
Step7 Start another loop from c=m till c<l-m.
Step8 Check if (c==m||c==l||c==l-m-1).
Step9 Start another loop from r=l-m-1 till c=m and repeat second half of
step4.
Step10 Initialize n=n-2,m++ and s=0.
Step11 End.
Variable Discription :
TYPE NAME PURPOSE
int a[][] Store 2-D array.
int n input by user
int r row of 2-D array
int c column of 2-D array
int s Sum of elements
PROGRAM-
import java.io.*;
class Square_sum
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter order of matrix(odd)");
int n=Integer.parseInt(num.readLine());
int a[][]=new int[n][n];
int r,c,m=0,s=0,l=n;
for(r=0;r<n;r++)
for(c=0;c<n;c++)
{
System.out.println("Enter numbers for array");
a[r][c]=Integer.parseInt(num.readLine());
}
System.out.println("Original Matrix");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
System.out.print(a[r][c]+"\t");
System.out.println();
}
System.out.println("Square Matrix");
while(n>1)
{
for(r=m,c=m;c<l-m;c++)
{
s=s+a[r][c];
System.out.print(a[r][c]+"\t");
}
System.out.println();
for(r=m+1;r<l-m-1;r++)
{
for(c=m;c<l-m;c++)
{
if(c==m||c==l-m-1)
{
s=s+a[r][c];
System.out.print(a[r][c]+"\t");
}
else
System.out.print("\t");
}
System.out.println();
}
for(r=l-m-1,c=m;c<l-m;c++)
{
s=s+a[r][c];
System.out.print(a[r][c]+"\t");
}
System.out.print("Sum is : "+s);
System.out.print("\n");
n-=2;
m++;
s=0;
}
}
}
output-
Input--
Enter order of matrix(odd)
3
Enter numbers for array
1
Enter numbers for array
2
Enter numbers for array
3
Enter numbers for array
4
Enter numbers for array
5
Enter numbers for array
6
Enter numbers for array
7
Enter numbers for array
8
Enter numbers for array
9
Original Matrix
1 2 3
4 5 6
7 8 9
Square Matrix
1 2 3
4 6
7 8 9 Sum is : 40