Java Assignment 1
Java Assignment 1
Rno.-45
Sap id 500046982
ASSIGNMENT 1
1. Write the Java code for the following pattern
2.
Ans.
package pro1;
import java.util.*;
public class s1 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int x = sc.nextInt();
while(k<=x*x)
{
for(int i=c1;i<=c2;i++)
{
A[r1][i]=k++;
}
for(int j=r1+1;j<=r2;j++)
{
A[j][c2]=k++;
}
for(int i=c2-1;i>=c1;i--)
{
A[r2][i]=k++;
}
for(int j=r2-1;j>=r1+1;j--)
{
A[j][c1]=k++;
}
c1++;
c2--;
r1++;
r2--;
}
2.
Write Java program to check whether two strings are anagrams or not, string is assumed to
consist of alphabets only. Two words are said to be anagrams of each other if the letters from
one word can be rearranged to form the other word. From the above definition it is clear that
two strings are anagrams if all characters in both strings occur same number of times. For
example "abc" and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only
one time in both strings. Our algorithm tries to find how many times characters appear in the
strings and then comparing their corresponding counts.
Ans.
package pro1;
import java.util.Arrays;
public class s2 {
static void isAnagram(String s1, String s2)
{
if(copyOfs1.length() != copyOfs2.length())
{
status = false;
}
else
{
Arrays.sort(s1Array);
Arrays.sort(s2Array);
if(status)
{
System.out.println(s1+" and "+s2+" are anagrams");
}
else
{
System.out.println(s1+" and "+s2+" are not anagrams");
}
}
isAnagram("keEp", "peeK");
isAnagram("Toss", "Shot");
isAnagram("joy", "enjoy");
}