You are on page 1of 13

Computer Science

Name : Vaibhav Dixit


Class : XI A3
Submitted to : Rishi Sir
I. Design a class to reverse each element of a matrix.
Code :
import java.util.*;
class MatRev
{
int arr[][];
int m,n;
MatRev(int mm, int nn)
{
m=mm;
n=nn;
arr=new int[m][n];
}
void Input( )
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter matrix elements::");
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++)
arr[i][j] = sc.nextInt();  }
}
int reverse(int x)
{
int rev = 0;
for(int i = x; i != 0; i /= 10)
rev = rev*10 + i%10;
return rev;
}
void revMat(MatRev F)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
this.arr[i][j] = reverse(F.arr[i][j]);
}
}
void display()
{
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++)
System.out.print(arr[i][j]+"\t");
System.out.println();     
}}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows:");
int x = sc.nextInt();
System.out.print("Enter number of columns:");
int y = sc.nextInt();
MatRev ob1 = new MatRev(x,y);
MatRev ob2 = new MatRev(x,y);
ob1.Input();
ob2.revMat(ob1);
System.out.println("Original Matrix is ::");
ob1.display();
System.out.println("Matrix with reversed elements ::");
ob2.display();
}
}
Sample Output :
Enter number of rows:3
Enter number of columns:3
Enter matrix elements::
12
11
23
34
56
45
76
47
82
Original Matrix is ::
12 11 23
34 56 45
76 47 82
Matrix with reversed elements ::
21 11 32
43 65 54
67 74 28
II. Program to calculate HCF/GCD and LCM of two numbers.
Code :
import java.util.*;
class GCD_LCM
{
  void main()
 {
    Scanner sc=new Scanner(System.in);
    System.out.println("Input the numbers.");
    int n1=sc.nextInt();
    int n2=sc.nextInt();
    lcm(n1,n2);
    hcf(n1,n2);
 }
  void lcm(int a, int b)
 {
   int lcm;
   lcm = (a > b) ? a : b;
   while(true)
    {
      if(lcm % a == 0 && lcm % b == 0)
      {
        System.out.println("The LCM of "+a+" and "+b+" is: "+lcm);
        break;
      }
      lcm++;
    }
 }
  void hcf(int x, int y)
 {
    int dvd=x>y?x:y;
    int div=x<y?x:y;
    while(dvd%div!=0)
    {
      int rem = dvd%div;
      dvd=div;
      div=rem;
    }   
   System.out.println("HCF of "+x+" and "+y+" is: "+div);
   }
}
Sample Output :
Input the numbers.
360
180
The LCM of 360 and 180 is: 360
HCF of 360 and 180 is: 180

Imput the numbers.


465
285
The LCM of 465 and 285 is: 8835
HCF of 465 and 285 is: 15
III. Express a number as sum of consecutive numbers.
If there is no solution, return '-1’.
Code :
import java.util.*;
class cons
{
static void printConsec(int last, int first)
{
System.out.print(first++);
for (int x = first; x<=last; x++)
  System.out.print(" + " +  x);
}
static void searchConsec(int N)
{
for (int last = 1; last < N; last++)
{
  for (int first = 0; first < last; first++)
 {
   if(2*N == (last-first)*(last+first+1))
   {
    System.out.print(N+ " = ");
    printConsec(last, first+1);
    return;
   }
 }
}
  System.out.print("-1");
}
public static void main(String[] args)
{
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter a number");
  int n=sc.nextInt();
  searchConsec(n);
}
}
Sample Output :
 Enter a number
24
24 = 7 + 8 + 9

 Enter a number
465
465 = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 +
22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30

 Enter a number
8
-1
IV. Create a program to check whether a number is circular prime.
Code :
import java.util.*;
class circular
{
static boolean Prime(int n)
{
  int flag=0;
  for(int i=1;i<=n;i++)
   {
     if(n%i==0)
     flag++;
   }
  if(flag==2)
   return true;
  else
   return false;
}
static boolean circprime(int N)
{
  int count = 0, temp = N, num = N;
  for(;temp>0; temp /= 10)
    count++;
  while (Prime(num)==true)
 {
    int rem = num % 10;
    int div = num / 10;
    num = (int)((Math.pow(10, count-1))*rem)+div;
       if (num == N)
          return true;
   }
   return false;
}
public static void main(String args[])
{
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter a number.");
  int N = sc.nextInt();
  if (circprime(N))
   System.out.println("Entered number is a circular prime");
  else
   System.out.println("Entered number is NOT a circular prime");
}}
Sample Output :
 Enter a number.
1193
Entered number is a circular prime
 Enter a number.
113
Entered number is a circular prime
 Enter a number.
23
Entered number is NOT a circular prime
V. Create a program to shift rows of a matrix (upwards).
Code :
import java.util.*;
class shift
{
int r,c,s;
int ar[][];
Scanner sc=new Scanner(System.in);
shift(int rr, int cc, int ss)
{
  s=ss; r=rr; c=cc;
  ar=new int[r+1][c];
}
void input()
{
System.out.println("Enter elements of matrix.");
for(int i=1; i<r+1; i++)
{
   for(int j=0; j<c; j++)
   ar[i][j]=sc.nextInt();
}
}
void process()
{
for(int sh=1; sh<=s; sh++)
{
   for(int i=1; i<r+1; i++)
   {
     for(int j=0; j<c; j++)
     ar[i-1][j]=ar[i][j];
   }
for(int i=0; i<c; i++)
ar[r][i]=ar[0][i];
}
}
void display()
{
for(int i=1; i<r+1; i++)
{
   for(int j=0; j<c; j++)
   System.out.print(ar[i][j]+"  ");
   System.out.println();
}
}
public static void main(String args[])
{
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter dimensions of matrix.");
   int rr=sc.nextInt();
   int cc=sc.nextInt();
   System.out.println("Enter no. of rows to be shifted.");
   int ss=sc.nextInt();
   shift ob=new shift(rr,cc,ss);
   ob.input();
   System.out.println("Before shifting :");
   ob.display();
   ob.process();
   System.out.println("After shifting :");
   ob.display();
}
}
Samole Output :
Enter dimensions of matrix.
3
3
Enter no. of rows to be shifted.
2
Enter elements of matrix.
1
2
3
4
5
6
7
8
9
Before shifting :
1  2  3 
4  5  6 
7  8  9 
After shifting :
7  8  9 
1  2  3 
4  5  6

You might also like