Professional Documents
Culture Documents
Algorithm :
Step 1: Start
Step 2: Input a number from the user.
Step 3: Pass the number to the function isFascinating.
Step 4: Multiply the number by 1, 2 and 3 and store them in three different variables.
Step 5: Concatenate the variables in a String variable.
Step 6: Loop Starts.
Step 7: The "i" loop takes the number at index number 'i' and stores in variable 'c'. The
"j" loop starts from index number 'i+1' and checks whether the variable c is repeated in
the remaining string or not.
Step 8: If the variable 'c' becomes equal to any other character in the remaing string
then the counter variable 'count' increments by 1.
Step 9: If count >1 then the 'i loop' breaks and the compiler comes out of the i loop.
Step 10: Then the compiler checks whter if i==num.length() or i<num.length().
Step 11: If i==num.length() then it means that there was no repeated words and the
number becomes a "Fascinating Number".
Step 12: End
Programming Code:
import java.util.*;
class E-form
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][]=new int[5][5];
System.out.pritln(Enter Array Containing 25 elements);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println(Original Matrix);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
{
If(i%2==0)
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
Question 20:
Write a program to accept a string and print the string in reverse order.
Sample Input: wood
Sample Output: doow
Algorithm:
Step 1: Start
Step 2: Input a string by the user.
Step 3: Append a blank character at the end of the string.
Step 4: Calculate the length of the string. i.e. x=st1.length()
Step 5: Start a reverse loop from the length of the string to 0. i.e. for(y=--x;y>=0;y--)
Step 6: Find the character at each yth position inside the loop and concatenate it in a
variable initialized to null initally.
Step 7: When the loop ends the concatenated variable will represent the reverse string.
Step 8: Print the reverse string.
Step 9: End
Programming Code:
import java.io.*;
public class StringReverse
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter a String");
s=in.readLine();
String w="",maxw="";int max=0,le=0,co=0;
for(i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c!=' ')
{
w+=c;
}
else
{
co=0;
for(j=0;j<w.length();j++)
{
char ch=w.charAt(j);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
co++;
}
}
if(co>max)
{
maxw=w;
max=co;
}
}
}
System.out.println("Word with maximum number of vowels is "+maxw);
}
}
Question 17:
Write a program to input m words in a S.D.A and print only those words that either start
with a vowel or end with a.
Algorithm:
Step 1: Start
Step 2: Input any string by the user.
Step 3: Convert the input string into lower case using toLowerCase() method.
s=s.toLowerCase();
i.e.
Programming Code:
import java.io.*;
class AlphabetFreq
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any string: ");
String s = br.readLine();
int l=s.length();
String w="";
for(i=0;i<m;i++)
{
String s=a[i];
char c=s.charAt(0);
char ch=s.charAt(s.length()-1);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||ch=='a')
w=w+s+" ";
}
System.out.println("All words either starting from vowels or ending with 'a' are ");
System.out.println(w);
}
}
Question 12:
Write a program to declare a square matrix A[][] of order (M x M) where M must be greater
than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform
the following tasks on the matrix:
(a) Sort the non-boundary elements in ascending order using any standard sorting
technique
and
rearrange
them
in
the
matrix.
(b)
Calculate
the
sum
of
both
the
diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Algorithm :
Programming Code :
import java.util.*;
class NonBoundrySorting
{
int A[][],B[],m,n;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
m=sc.nextInt();
if(m<4 || m>10)
System.out.println("Invalid Range");
else
{
A = new int[m][m];
n = (m-2)*(m-2);
B = new int[n];
}
}
}
}
void convert(int s)
{
int x=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i != 0 && j != 0 && i != m-1 && j != m-1)
{
if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
}
}
}
}
void sortArray()
{
int c = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(B[i]>B[j])
{
c = B[i];
B[i] = B[j];
B[j] = c;
}
}
}
}
void printArray()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
void printDiagonal()
{
int sum = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j || (i+j)==m-1)
{
System.out.print(A[i][j]+"\t");
sum = sum + A[i][j];
}
else
System.out.print("\t");
}
System.out.println();
}
System.out.println("Sum of the Diagonal Elements : "+sum);
}
System.out.println("*********************");
System.out.println("The Rearranged matrix:");
System.out.println("*********************");
ob.printArray();
System.out.println("*********************");
System.out.println("The Diagonal Elements:");
System.out.println("*********************");
ob.printDiagonal();
}
}
import java.util.*;
class VowelChar
{
public static void main(String arg[])
{
char a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");
m=ob.nextInt( );
if(m>1 && m<=20)
{
a=new char[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=ob.nextLine().charAt(0 );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
char c=a[i][j];
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{
a[i][j]='*';
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Question 11:
Write a program to generate first 20 terms of the Fibonacci series in descending order in a
S.D.A of size 20. Store the numbers in another array which are primes.
Algorithm :
Step 1: Start
Programming Code:
import java.util.*;
class Fibonacci
{
public static void main(String arg[])
{
int a[20];
int i, j, m;
Scanner ob=new Scanner(System.in);
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
a[19]=1;a[18]=1;
int b[]=new int[20];
for(i=19;i>=2;i--)
{
a[i-2]=a[i]+a[i-1];
}
System.out.println("Fibonacci Series in descending order is :");
for(i=0;i<19;i++)
System.out.print(a[i]+" ");
for(i=19;i>=0;i--)
{
int t=a[i];
for(j=2;j<t;j++)
{
if(t%j==0)
{
co++;
}
}
if(co==0)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<k;i++)
{
System.out.print(b[i]+" ");
}
}
}
{
for(j=0;j<m;j++)
{
a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println(Array in T form is :);
for(i=0;i<n;i++)
{
System.out.print(a[0][n]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j==(n-1)/2)
System.out.println(a[i][j]);
}
}
Question 15:
Algorithm :
mahendra singh dhoni
dhoni mahendra sing
Programming Code:
import java.io.*;
class ThreeWords
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any word : ");
String s = br.readLine();
int l = s.length();
int la=s.lastIndexOf(" ");
s1=s.substring(la);
s2=s.substring(0,la);
s3=s3+s1+" "+s2;
}
}
Question 21:
Write a program to input a Sentence from the user and print the number of 2 letters 3
letters and 4 letters words.
Algorithm :
Programming Code:
import java.util.*;
public class Replace
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence ");
String s=sc.nextLine();
if(c!=' ')
{
w+=c;
}
else
{
int le=w.length();
if(le==2)
l1++;
if(le==3)
l2++;
if(le==4)
l3++;
}
{
maxw=w;
max=le;
}
}
}
Step1 :- Start
Step2 :- input a limit n and a 2-d array a[][] of dimension n*n.
Step3 :- initialize r, c, x ,c1, r1 with 0, n/2, 1 respectively.
Step4 :- take a while loop of x(= 1) till it is less than equals to n*n.
Step5 :- a[r][c]=x++ , r1= r , c1= c,r-- , c++.
Step6 :- if (r<0) then r=n-1.
Step7 :- if(c==n) then c=0.
Step8 :- if (a[r][c]!=0) then r=r1 , c=c1, r++.
Step9 :- print array a[][] which is now the required magical matrix.
Step10 :- STOP
class magic
{
public static void main(int n)
{
int i,j,c=0,r=0,c1,r1;
int a[][]=new int[n][n];
c=n/2;
int d=1;
int s=n*n;
while(d!=(s+1))
{
a[r][c]=d;
c1=c;
r1=r;
if(r==0)
r=n;
if(c==0)
c=n;
r--;
c--;
d++;
if(a[r][c]!=0)
{
r=r1;
c=c1;
r++;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}
Input:N=3
Output:-
Algorithm :
Step 1 - start
step 2 - pas[0] = 1
step 3 - if i=0 then goto step 4
step 4 - if j=0 then goto step 5
Step 5 - print pas[j]+" "
Step 6 - i++& if i<=i goto step 5
Step 8 - if j=i+1 then goto step 7
step 9 - pas[j]=pas[j]+pas[j-1]
Step 10 - j--& if j>0 goto step 9
Step 11 end
import java.util.*;
class Pascal
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++)
{
for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" ");
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}
Step 10 - sum = 0
Step11 end
import java.util.*;
class ColoumnSum
{
public static void main(String args[])
{
int a[][]=new int[4][4];
Scanner sc=new Scanner(System.in);
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
a[x][y]=sc.nextInt();
}
}
System.out.println("Array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.println();
}
for(y=0;y<4;y++)
{
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
}
System.out.println("Sum of Column "+(y+1)+" is "+Sum);
Sum=0;
}
}
}
{
System.out.print(a[x][y]+" ");
}
System.out.println();
}
y=0;
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
y=y+1;
}
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
if((x+y)==3)
{
sum+=a[x][y];
}
}
}
System.out.println("Sum of Right diagonal is "+Sum);
System.out.println("Sum of Left diagonal is "+sum);
Sum=0;
}
}
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
int max=a[k-1], min=a[0];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(A[i][j]==max)
{
System.out.println("LARGEST NUMBER\t:"+max);
System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);
}
if(A[i][j]==min)
{
System.out.println("SMALLEST NUMBER\t:"+A[0][0]);
System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);
}
}
}
System.out.println("REARRANGED MATRIX");
k=0;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(a[k]+"\t");
k++;
}
System.out.println();
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Prime Element\t Row\t Column");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
int c=0;
for(k=1;k<=a[i][j];k++)
{
if(a[i][j]%k==0)
{
c++;
}
}
if(c==2){
System.out.println(a[i][j]+"\t\t"+i+"\t\t"+j);
}
}
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(b[i]>b[j])
{
int t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
int c=0;
for(i=0;i<n;i++)
a[0][i]=b[c++];
for(i=1;i<n;i++)
a[i][n-1]=b[c++];
for(i=n-2;i>=0;i--)
a[n-1][i]=b[c++];
for(i=n-2;i>0;i--)
a[i][0]=b[c++];
System.out.println("Sorted Array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ") ;
}
System.out.println();
}
}
}
A[i][j]=sc.nextInt();
}
}
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int p=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
if(A[i][j]!=0)
{
p=1;
break;
}
}
}
if(p==0)
System.out.println("The matrix is Lower Triangular");
else
System.out.println("The matrix is not Lower Triangular");
}
}
{
s=0;
for(j=0;j<n;j++)
s+=a[i][j];
a[m][i]=s;
}
for(i=0;i<m;i++)
{
s=0;
for(j=0;j<n;j++)
s+=a[i][j];
a[i][n]=s;
}
System.out.println(" Final Matrix with the sum of each row and column is below
:");
for(i=0;i<=m;i++)
{
for(j=0;j<=m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(j=0;j<4;j++)
{
t=a[0][j];
a[0][j]=a[3][j];
a[3][j]=t;
}
System.out.println("New Array after Swapping ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) System.out.print(a[i][j]+" ");
System.out.println();
}
}
import java.util.*;
public class Ascending_Row
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m[][]=new int[4][4];
int i,j,k,t;
System.out.println("Enter Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=sc.nextInt();
}
}
System.out.println("Original MAtrix ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
{
if(m[i][k]>m[i][k+1])
{
t=m[i][k];
m[i][k]=m[i][k+1];
m[i][k+1]=t;
}
}
}
}
System.out.println("The Array after Sorting each row ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}
Algoriththm:
Step 1: Start
Step 2: Input a sentence from the user.
Step 3: Input the word to be searched from the user.
Step 4: Calculate the length of the sentence.
Step 5: Start a loop from 0 to length of the sentence.
Step 6: Find each word of the sentence using indexOf and substring methods.
IndexOf method will calculate the position of the space i.e. int n=s.indexOf(' ',i) and
substring will get the word. i.e. String w=s.substring(i,n)
Step 7: Compare the word ignoring case to be searched with each word found in
step 6.
Step 8: If the word is equal to the word to be searched then increment a counter
that was initially initialized to 0.
Step 9: At the end of the loop the counter value will give the count of the occurance
of the word in the sentence.
Step 10: If the counter value is 0, then that means the word is not present in the
sentence.
Step 11: End.
import java.util.*;
public class Word_Frequency
{ public static void main(String args[]) {
int c=0;String wd=new String();
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence");
String s=sc.nextLine()+" ";
System.out.println();
System.out.println("Enter The Word To Be Searched");
wd=sc.next();
int len=s.length();
for(int i=0;i<len;i++)
{
int n=s.indexOf(' ',i);
String w=s.substring(i,n);
if(wd.equalsIgnoreCase(w)==true)
c++;
i=n;
}
if(c!=0)
System.out.println("The Word "+wd+" occurs in the Sentence "+c+" times");
else
System.out.println("Word Not Found");}}
}
while(n2>0)
{
a2[l2]=(n2%10);
n2/=10;
l2++;
}
s1=a1[0]+a2[l2-1];
if(l1%2==0)
s2=0.5d*(a1[(l1/2)]+a1[(l1/2)-1]);
else
s2=a1[l1/2];
if(l2%2==0)
s2=s2+0.5d*(a2[(l2/2)]+a2[(l2/2)-1]);
else
s2=s2+a2[l2/2];
System.out.println("The sum of the digit of the first number and the last digit of the
second number="+s1);
System.out.println("The sum of the central digit of both the numbers="+s2);
}
}
For eg-
1101+1001+1100=100010.
Algorithm.
class sumbinary
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int c,i,n1,n2,n3,sum1=0,sum2=0,sum3=0,p1=0,p2=0,p3=0,p,k=0,sum4;
int a[]=new int[10];
for(i=n1;i>0;i/=10)
{
c=i%10;
sum1=sum1+(int)Math.pow(2,p1)*c;
p1++;
}
for(i=n2;i>0;i/=10)
{
c=i%10;
sum2=sum2+(int)(Math.pow(2,p2)*c);
p2++;
}
for(i=n3;i>0;i/=10)
{
c=i%10;
sum3=sum3+(int)(Math.pow(2,p3)*c);
p3++;
}
sum4=sum1+sum2+sum3;
System.out.print(sum4);
while(sum4>0)
{
p=sum4%2;
a[k]=p;
k++;
sum4=sum4/2;
}
System.out.println("the added binary number is=");
for(i=k-1;i>=0;i--)
{
System.out.print(a[i]);}
}
}
}
Algorithm
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int dd1,mm1,yy1,dd2,mm2,yy2,p,i,sum=0,k,sum1=0,sum2=0,p1=0,k3,k2,c,k1;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(yy1==yy2)
{
if(mm1!=mm2)
{
p=(a[mm1-1]-dd1)+dd2;
for(i=mm1;i<=mm2-2;i++)
{
sum=sum+a[i];
}
if(mm1<=2&&mm2>2&&yy1%4==0)
k=sum+p+1;
else
k=sum+p;
System.out.println("the no of days between the two dates="+k);
}
else
{
k=dd2-dd1;;
p1=p1+a[i];
c=a[mm1-1]-dd1;
if(mm1<=2&&yy1%4==0)
k1=p1+c+1;
else
k1=p1+c;
for(i=yy1+1;i<=yy2-1;i++)
{
if(i%4!=0)
sum1=sum1+365;
else
sum1=sum1+366;
for(i=0;i<=mm2-2;i++)
{
sum2=sum2+a[i];
}
if(yy2%4==0&&yy2>2)
{
k2=sum2+dd2+1;
}
else
{
k2=sum2+dd2;
}
k3=k2+k1+sum1;
System.out.println("the no of days between two dates="+k3);
}
}
Algorithm
Step1.Declare variables n,i,j,k,k1,t.
Step 2.Input size of array in n from user.
Step 3.Declare array a[] of size (n*2).
Step 4.Assign (n+n)-4 to k and (n+n)-2 to k1.
Step 5.Input n numbers from user.
Step 6.Begin loop for i from 0 to n-1 and repeat step 7 to 12.
Step 7.Begin loop for j from i+1 to n-1 and repeat step 8 to12.
Step 8.if(a[i]<a[j])
Step 9.Assign value of a[i] to t.
Step 10.Assign value of a[j] to a[i].
Step 11.Assign value of t to a[j].
Step 12.Assign 0 to t.
Step 13.Assign a[0] to a[(n+n)-3].
Step 14.Begin loop for i from 1 to n-1 and repeat step 15 to 20.
Step 15.if(i%2!=0)
Step 16.Assign a[i] to a[k].
Step 17.Assign k-1 to k.
Step 18.else if(i%2= =0)
Step 19.Assign a[i] to a[k1].
Step 20.Assign k1+1 to k1.
Step 21.Print elements of array a[] from n to (n+n)-1.
import java.io.*;
class arraypat
{
}
}
for(y=x+1;y<=(n-x);y++)
{
for(z=y;z<=(n-x);z++)
{
if(a[y]<a[z])
{
temp=a[y];
a[y]=a[z];
a[z]=temp;
}
}
}
}
if(n%2==0)
{
for(x=0;x<p;x++)
{
temp=a[x];
a[x]=a[n-x];
a[n-x]=temp;
}
}
System.out.println("the final array is");
for(x=0;x<=n;x++)
System.out.println(a[x]);}}}
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
Step4
Step5
Step6
Step7
Step8
Step9
Step10
Step11
Step12
End.
import java.io.*;
class magic
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[10][10];
int n,r=0,i,r1,c1,c;
System.out.println("Enter your odd limit");
n=Integer.parseInt(num.readLine());
c=n/2;
for(i=1;i<=n*n;i++)
{
a[r][c]=i;
r1=r;
c1=c;
r--;
c++;
if(r==-1)
{
r=n-1;
}
if(r>n-1)
{
r=0;
}
if(c>n-1)
{
c=0;
}
if(a[r][c]!=0)
{
r=r1;
c=c1;
r++;
}}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(a[r][c]);
}
System.out.println();}}}
2
0
3
9
3
8
3
7
3
6
2
1
8
2
2
9
1
9
1
8
1
7
2
3
1
0
5
1
5
2
4
1
1
1
2
1
3
1
4
2
5
2
6
2
7
2
8
2
9
1
6
35
34
33
32
31
30
Step1
Start.
Step2
Declare suitable variables like p,c,x,n,r1,c1,c2,c3,k,n,r,i,j and
declare a 2-d array of limit
Step3
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
Step6
Perform c++.
Step7
circular order.
Step8
Step9
import java.io.*;
class circular
{
public static void main(String args[])throws Exception
{
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;
for(r=n/2-1;r<=n/2;r++)
{
a[c][r]=p++;
}
c++;
for(r=n/2;r>=n/2-1;r--)
{
a[c][r]=p++;
}
r=n/2-2;
r1=n/2+1;
x=n/2-2;
for(j=1;j<=n/2-1;j++)
{
for(c1=r;c1<=r1;c1++)
{
a[x][c1]=p++;
}
for(c2=x+1;c2<=r1;c2++)
{
a[c2][r1]=p++;
}
for(c3=r1-1;c3>=r;c3--)
{
a[r1][c3]=p++;
}
k=r1-1;
for(i=r1;i>r+1;i--)
{
a[k][c3+1]=p++;
k--;
}
r--;
r1=r1+1;
x--;
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(a[r][c]+""+"");
}
System.out.println();
}
}
}
By swami Vivekananda.
Then the output is
Vivekananda swami by characters is work to directed and
controlled emotions
Algorithm
Step1
Start.
Step2
Declare variables like n,I,j,z=0 and declare two string of arraysen[],word[] of limit 20 and 200 respectively.
Step3
Step4
Step5
Step6
Extract the jth character of sen[i]th string and check if they are
equal to any of the punctuation marks
Step7
Step8
str1= ;
Step9
Step10 Start a loop from i=z-1 till i>=0 and then print the contents of
string array word[i].
Step11
End.
import java.io.*;
class ISC2007STRING
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
for(i=0;i<n;i++)
{
sen[i]=num.readLine();
sen[i]=sen[i]+"";
}
for(i=0;i<n;i++)
{
for(j=0;j<sen[i].length();j++)
{
char ch=sen[i].charAt(j);
if(ch==','||ch=='.'||ch==';'||ch==':')
{
continue;
}
if(ch==' ')
{
word[z++]=str1;
str1="";
}
else
{
str1=str1+ch;
}
}
}
for(i=z-1;i>=0;i--)
{
System.out.print(word[i]+"");
}
}
}
Algorithm
Step1
Start.
Step2
Declare variables like i,j,n and declare two empty strings str1 and
str2 and two strings str andstr3.
Step3
Step4
Start a loop from i=0 to i<n and extract 0 th character of the str3 in
ch2.
Step5
Step7
Step8
Extract the ith character of str string and check if it is equal to ch2.
If step6 is true then perform str1=str1substring(i,i+n).
If str1=str3 then perform i=i+n-1.
Step9
Start a loop from j=0 till j<str1.length() and Extract jth character
of str1 in ch1 and perform str2=ch+str2.
Step10 Print str2.
Step11
End.
import java.io.*;
class bear
{ public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
String str,str1="",str2="",str3;
int i,j,n;
{
char ch=str.charAt(i);
if(ch==ch2)
{
str1=str.substring(i,i+n);
if(str1.equals(str3))
{
i=i+n-1;
for(j=0;j<str1.length();j++){
char ch1=str1.charAt(j);
str2=ch1+str2;
}
System.out.print(str2);
}
else
{
System.out.print(ch);
}}
else
{
System.out.print(ch);
}
str2="";
}
}
}
Algorithm
Step1
Step2
of limit 5.
Step3
Start.
Declare suitable variables like i,j,k=0and declare an array a[],b[],c[]
Start a loop from i=0 to i<5.
Step4
Start a loop from j=0 to j>5 and then perform c[k]=a[i] if a[i]=b[j]
and after that do k++.
Step5
Step6
import java.io.*;
class arraycommon
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[]=new int[5];
int b[]=new int[5];
int c[]=new int[5];
int i,k=0,j;
for(i=0;i<5;i++)
{
System.out.println("Enter your first array number");
a[i]=Integer.parseInt(num.readLine());
}
for(i=0;i<5;i++)
{
System.out.println("enter your second array number");
b[i]=Integer.parseInt(num.readLine());
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]==b[j])
{
c[k]=a[i];
k++;
}
}
}
System.out.println("the common numbers in the array are");
for(i=0;i<k;i++)
{
System.out.println(c[i]);}}}
Input--
31
54
87
47
27
30
28
6
4
58
3
4
21
Output
87
47
64
58
54
34
31
30
28
27
21
Algorithm
Step 1 Declare and initialize variables like r1,c1,temp,r,c and a double dimensional
array a[][].
Step 2 Store the numbers from the user in 2-d array.
Step 3 Begin a loop from r1=0 till r<4.
Step 4 Begin a loop from c1=0 till c1<4.
Step 5 Begin a loop from r=0 till r<4.
Step 6 Begin a loop from c=0 till c<4.
Step 7 Check if a[r1][c1]>a[r][c].
Step 8 If step7 is true then perform the swapping of elements.
Step 9 Print the final 2-d array at the end.
Step 10 End of process.
import java.io.*;
class twodsorting
{
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++)
{
for(c=0;c<4;c++)
{
System.out.println("Enter your number");
a[r][c]=Integer.parseInt(num.readLine());
}
}
for(r1=0;r1<4;r1++)
{
for(c1=0;c1<4;c1++)
{
for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{
if(a[r1][c1]>a[r][c])
{
temp=a[r1][c1];
a[r1][c1]=a[r][c];
a[r][c]=temp;
}
}
}
}
}
}
}
Algorithm
Step 1 Declare and initialize variables like r1,c1,temp,r,c,z and a double dimensional
array a[][].
Step 2 Store the numbers from the user in 2-d array.
Step 3 Begin a loop from r=0 till r<4.
Step 4 Begin a loop from c=0 till c<4.
Step 5 Begin a loop from z=0 till z<4.
Step 6 Check if a[r][c]>a[r][z].
Step 8 If step7 is true then perform the swapping of elements.
Step 9 Print the final 2-d array at the end.
Step 10 End of process.
import java.io.*;
class rowsorting
{
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,z;
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
{
System.out.println("Enter your number");
a[r][c]=Integer.parseInt(num.readLine());
}
}
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
{
for(z=0;z<4;z++)
{
if(a[r][c]>a[r][z])
{
temp=a[r][c];
a[r][c]=a[r][z];
a[r][z]=temp;
}
}
}
}
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
{
System.out.print(a[r][c]+" ");
}
System.out.println();
}}}
class subdates
{
static
BufferedReader
InputStreamReader(System.in));
in
new
BufferedReader(new
int mon[] =
{31,31,28,31,30,31,30,31,31,30,31,30,31,31};
a[0] = Integer.parseInt(in.readLine());
a[1] = Integer.parseInt(in.readLine());
a[2] = Integer.parseInt(in.readLine());
b[1] = Integer.parseInt(in.readLine());
b[2] = Integer.parseInt(in.readLine());
if(valid(a[0],a[1],a[2])==false || valid(b[0],b[1],b[2])==false)
System.out.println("Invalid Date");
System.exit(0);
//subtracting dates
else
{
if(b[2]%400==0 || b[2]%4==0 && b[2]%100!=0)
b[2] = 29;
b[0] = a[0]-b[0];
while(b[0]<0)
{
b[0] = b[0]+mon[b[1]];
a[1]--;
}
if(a[1]<0)
{
a[1] = 12;
a[2]--;
}
b[1] = a[1]-b[1];
if(b[1]<0)
{
b[1] = b[1]+12;
a[2]--;
}
b[2] = a[2]-b[2];
}
//displaying result
System.out.println("Date is : "+b[0]+" days "+b[1]+" month "+b[2]+"
year") ;
}
//returning whether date is valid or not
public static boolean valid(int dd,int mm,int yy)
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%400==0 && yy%100==0 || yy%4==0)
mon[2]=29;
if(mm>12)
{
System.out.print("Date is Invalid");
System.exit(0);
}
if(dd>0 && dd<=mon[mm] && mm>0 && mm<13 && yy>0)
return true;
else
return false;
}
}
Output:
STEP1:Start
STEP2:Input details about date 1.
STEP3:Create an array and store very entry in the array location, i.e, date, month
and year.
STEP4:Input details about date 2.
STEP5:Repeat step 3 by creating a different array.
STEP6:Check for the validity of the date.
STEP7:If valid, continue, else display error message.
STEP8:Add the dates.
STEP9:Keep on checking that the date lies in the limit of a specific month.
STEP10:Display the date.
STEP11:END.
import java.io.*;
class adddates
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int a[] = new int[3],b[] = new int[3];
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31,31};
//inputting details of date 1
System.out.println("Enter about Date 1 : ");
System.out.print("Enter date : ");
a[0] = Integer.parseInt(in.readLine());
System.out.print("Enter month : ");
a[1] = Integer.parseInt(in.readLine());
System.out.print("Enter year : ");
a[2] = Integer.parseInt(in.readLine());
//inputting details of date 2
{
b[1] = b[1]%12;
b[2]++;}
b[2] = b[2]+a[2];
}
//displaying result
System.out.println("Date Is : "+b[0]+":"+b[1]+":"+b[2]);
}
public static boolean valid(int dd,int mm,int yy)
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%400==0 || yy%4==0 && yy%100!=0)
mon[2] = 29;
if(mm>12)
{
System.out.print("Date is Invalid");
System.exit(0);
}
if(dd>0 && dd<=mon[mm] && mm>0 && mm<13 && yy>0)
return true;
else
return false;
}
}
Output :
.