computer11
computer11
ANUBHAB MUKHERJEE
class xi
ROLL NO 20
COMPUTER PROJECT.
Topic 2:
Write a program that collects a natural number as input and check
whather the number is a ‘Circular Prime Number ‘ or not.
Source code:
import java.util.Scanner;
class Circularprime
{
int n;
void input()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number");
n=sc.nextInt();
}
boolean isPrime(int m){
int c=0;
for(int i=1;i<=m;i++)
if(m%i==0)
c++;
return c==2?true:false;
}
int count(int m){
int c=0;
while(m>0){
m=m/10;
c++;
}
return c;
}
boolean calculate()
{
int digit=count(n);
for(int i=1;i<=digit;i++){
if(isPrime(n))
{
int x=n%10;
int y=n/10;
n=x*(int)Math.pow(10,digit-1)+y;
}
else
return false;
}
return true;
}
public static void main()
{
Circularprime obj=new Circularprime();
obj.input();
if(obj.calculate())
System.out.println("Circular Prime");
else
System.out.println("Not Circular Prime");
}
}
OUTPUT:
TOPIC 3:
Write a program that collects input for an integer and checks whether
the given number is a ‘Good Prime Number ‘ or not.
Source code:
import java.util.Scanner;
class goodprime
{
double n;int i;int count;double ch;int d;int count2;double ch1;int sum;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
n=sc.nextDouble();ch=n;ch1=n;
}
void calculate()
{
for(i=1;i<=n;i++)
{ if(n%i==0)
{
count++;
}
}
if(count==2)
{
ch=ch*ch;
while(ch>0)
{
d=(int)ch%10;
sum=sum+d;
ch=ch/10;
}
for(i=1;i<=sum;i++)
{
if(sum%i==0)
{
count2++;
}
}
if(count2==2)
{
System.out.println("The number is a good prime number");
}
else
{
System.out.println("The number is not a good prime number" );
}
}
else
{
System.out.println("not a prime number");
}
}
public static void main()
{
goodprime obj=new goodprime();
obj.input();
obj.calculate();
}
}
Output:
Topic 4:
Write a program to find the Highest Common Factor (HCF) of four
natural numbers collected as inputs.
Source code:
import java.util.Scanner;
class HCF
{
int n;int a;int c;int d;int z;int hcf;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the four numbers");
n= sc.nextInt();
a=sc.nextInt();
c=sc.nextInt();
d=sc.nextInt();
z=n*a*c*d;
}
void calculate()
{
for(int i =1;i<=z;i++)
{
if (n%i==0 && a%i==0 && c%i==0 && d%i==0)
{
hcf=i;
}
}
System.out.println("HCF of the four numbers is = "+hcf);
}
public static void main()
{
HCF obj=new HCF();
obj.input();
obj.calculate();
}
}
Output:
Topic 5:
Write a program to collect an array from the user and display the
original array and the mirror image of the original array.
Source code:
import java.util.Scanner;
class lateral
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of rows");
int r=sc.nextInt();
System.out.println("enter the number of columns");
int c=sc.nextInt();
System.out.println("enter the array");
int a[][]=new int[r][c];int b[][]=new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("original array");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("inverted array");int n=c-1;
for(int i=0;i<r;i++)
{
n=c-1;
for(int j=0;j<c;j++)
{
b[i][j]=a[i][n--];
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT:
Topic 6:
Write a program to collect an array from the user and display the
original array and the vertically inverted image of the original array.
Source code:
import java.util.Scanner;
class vertical
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of rows");
int r=sc.nextInt();
System.out.println("enter the number of columns");
int c=sc.nextInt();
System.out.println("enter the array");
int a[][]=new int[r][c];int b[][]=new int[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("original array");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("inverted array");int x=0;
for(int i=r-1;i>=0;i--)
{
for(int j=0;j<c;j++)
{
b[x][j]=a[i][j];
}
x++;
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT:
TOPIC 7:
Source code:
Write a program to input an integer number from a user and check
whether it is a magic number or not
Source code
import java.util.Scanner;
class magic
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int num=sc.nextInt();
int sum=num;int d=0;
do
{
num=sum;sum=0;
do
{
d=num%10;
sum=sum+d;
num=num/10;
}while(num>0);
}while (sum>9);
if(sum==1)
System.out.println("magic number");
else
System.out.println("not a magic number");
}
}
OUTPUT:
TOPIC 8
Write a program to accept an input for a sentence which must be
terminated wit a ‘.’ or ‘?’ or ‘!’.Hence count the number of words and
display those words with start with a vowel
Source code:
import java.util.Scanner;
class SNV
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a sentence");
String a= sc.nextLine();int count =0;char b;String str="";int x=0;
if(a.charAt(a.length()-1)=='.'||a.charAt(a.length()-1)=='?'||
a.charAt(a.length()-1)=='!')
{
a=" "+a;
for(int i=0;i<a.length();i++)
{
if(a.charAt(i)==' ')
count++;
}
System.out.println("the number of words are = "+count);
a=a.trim();a=a+" ";
System.out.println("words starting with vowel are:-");
for(int i=0;i<a.length();i++)
{
if(a.charAt(i)==' ')
{
str=a.substring(x,i);
b=str.charAt(0);
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'||b=='A'||b=='E'||
b=='I'||b=='O'||b=='U')
System.out.println(str);
x=i+1;
}
}
}
else
{
System.out.println("Sorry! wrong input!");
System.exit(1);
}
}
}
OUTPUT:
TOPIC 9:
Write a program where declared ,a two dimensional array, consider it
as a matrix ,collect row order and column order for it ,from user as
inputs.If ther inputs confirm the matrix to be a square matrix, then
collect the element value of it and display the original matrix there
after check ,whether it is skew symmetric it is a skew symmetric matrix
or not and display message accordingly with a tect message.
Source code:
import java.util.*;
class SkewSymmetricMatrix
{
static int[][] matrix;
public static void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows for matrix: ");
int rows = sc.nextInt();
System.out.print("Enter number of columns for matrix: ");
int cols = sc.nextInt(); matrix = new int[rows][cols];
System.out.println("Enter elements of matrix: ");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
}
public static void print()
{
System.out.println("Matrix: ");
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[0].length; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static boolean isSkewSymmetric()
{
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix[0].length; j++)\
{
if (matrix[i][j] != -matrix[j][i])
{
return false;
}
}
}
return true;
}
public static void main(String[] args)
{
input();
print();
if (isSkewSymmetric())
{
System.out.println("Matrix is skew symmetric.");
}
else
{
System.out.println("Matrix is not skew symmetric.");
}
}
}
OUTPUT
Examples Of Skew-Symmetric Matrix:
TOPIC 10:
Write a program that collects input for two words and checks whether
they are anagram word or not.
Source code:
import java.util.*;
class anagram
{
String s1,s2;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two words to check whether they are
anagram");
s1=sc.next();
s2=sc.next();
}
void sort (char c[])
{
for(int i=0;i<c.length;i++)
{
for(int j=i+1;j<c.length;j++)
{
if(c[i]>c[j])
{
char t=c[i];
c[i]=c[j];
c[j]=t;
}
}
}
}
boolean isAnagram()
{
if (s1.length() != s2.length())
{
System.out.println("strings are not anagram");
return false;
}
char c1[] = new char[s1.length()];
char c2[] = new char[s2.length()];
c1 = s1.toLowerCase().toCharArray();
c2 = s2.toLowerCase().toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return Arrays.equals(c1, c2);
}
public static void main()
{
anagram obj=new anagram();
obj.input();
if(obj.isAnagram()==true)
System.out.println("Strings are anagram");
else
System.out.println("Strings arenot anagram");
}
}
Output:
Topic 11:
Write a program to collect two words as inputs from the user and
calculate and display the hamming distance between the two words:
Source code:
import java.util.*;
public class HammingDistance
{
String str1, str2;
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string: ");
str1 = sc.next();
System.out.print("Enter second string: ");
str2 = sc.next();
}
public void printStrings()
{
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
}
public int hammingDistance()
{
int distance = 0;
if (str1.length() != str2.length())
{
System.out.println("Strings must be of same length.");
return -1;
}
for (int i = 0; i < str1.length(); i++)
{
if (str1.charAt(i) != str2.charAt(i))
{
distance++;
}
}
return distance;
}
static void main()
{
HammingDistance obj=new HammingDistance();
obj.input();
obj.printStrings();
int distance = obj.hammingDistance();
if (distance != -1)
{
System.out.println("Hamming Distance: " + distance);
}
}
}
Output:
Topic 12:
Write a program to collect intergers as inputs from the user and store
the values in a 2d array. Display the original array and take the
boundary elements and sort them in ascending order and then print the
boundary elents in the boundary in an anticlockwise manner and keep
the middle part of the matrix hollow.
Source code:
import java.util.Scanner;
class boundary
{
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of rows");
int r=sc.nextInt();
System.out.println("enter the number of columns");
int c=sc.nextInt();
System.out.println("enter the array");
int a[][]=new int[r][c];int b[]=new int[c*2+((r-2)*2)];int n=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("ORIGINAL ARRAY");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if (i==0||i==(r-1)||j==0||j==(c-1))
{
b[n++]=a[i][j];
}
}
}
int t=0;
for(int i=0;i<b.length;i++)
{
for(int j=0;j<((b.length-1)-i);j++)
{
if(b[j]>b[j+1])
{
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
int z[][] = new int[r][c];
int i = 0, j = 0;
int flag = 1;