0% found this document useful (0 votes)
17 views

Assignment 21

The document describes a program to check if a number is a Smith number. It accepts a number as input, calculates the sum of its digits and sum of the digits of its prime factors, and checks if they are equal to determine if the number is a Smith number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment 21

The document describes a program to check if a number is a Smith number. It accepts a number as input, calculates the sum of its digits and sum of the digits of its prime factors, and checks if they are equal to determine if the number is a Smith number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment 21:

Write a program in Java to enter natural numbers in a double dimensional array m x n (where
m is the number of rows and n is the number of columns). Display the new matrix in such a
way that the new matrix is the mirror image of the original matrix.
Sample Input:
8 15 9 18
9 10 7 6
10 8 11 13
12 16 17 19
Sample Output:
18 9 15 8
6 7 10 9
13 11 8 10
19 17 16 12
Algorithm:
Step 1: Start
Step 2: Accept number of rows (M) and number of columns (N).
Step 3: Check whether M>2, M<=10, N>2 and N<=10. If true then go to step 4 else go to
step 11.
Step 4: Create a Matrix A[][] of order MxN and store elements.
Step 5: Print the matrix A[][].
Step 6: Repeat the steps 7 to 9 for c=0 to N-1 increase by 1.
Step 7: Repeat the steps 8 to 9 for i=0 to M-1 increase by 1.
Step 8: Repeat the step 9 for j=N-1 to c+1 decrease by 1.
Step 9: Swap the elements of A[i][j] and A[i][j-1].
Step 10: Print the Mirror Image of the Matrix A[][].
Step 11: Stop

Program:
import java.util.*;
class Mirror
{
public void coumnSort()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of rows:");
int M=sc.nextInt();
System.out.println("Enter number of columns:");
int N=sc.nextInt();
if(M<=2||N<=2||M>10||N>10)
System.out.println("OUT OF RANGE");
else
{
int A[][]= new int[M][N];
//Storing elements in the array
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.println("Enter a number:");
A[i][j]=sc.nextInt();
}
}
//Printing the Original Matrix
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
//Creating Mirror Image in the same array by shifting columns
int t=0;
for(int c=0;c<N;c++)
{
for(int i=0;i<M;i++)
{
for(int j=N-1;j>c;j--)
{
t=A[i][j];
A[i][j]=A[i][j-1];
A[i][j-1]=t;
}
}
}
//Printing the Mirror Imageof the Matrix
System.out.println("MIRROR IMAGE OF THE MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}
}

Assignment 22:
A Smith number is a composite number, whose sum of the digits is equal to the sum of its
prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.
Write a program in Java to enter a number and check whether it is a Smith number or not.
Sample Input: 666
Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
Thus, 666 is a Smith Number.
Algorithm:
Step 1: Start
Step 2: Accept a number n.
Step 3: Check whether it not a composite number . If true then go to step 4 else go to step 5.
Step 4: Print n, “is not a Composite Number” and go to step
Step 5: Calculate the sum of digit of digits of n(dsum).
Step 6: Calculate the sum of digits of its prime factor(sp);
Step 7: Check whether dsum=sp. If true then go to step 8 else go to step 9.
Step 8: Print n, “is a Smith Number.” and go to step 10.
Step 9: Print n, is not a Smith Number”.
Step 10: Stop
Program:
import java.util.*;
public class Smith
{
int n;
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number:");
n = sc.nextInt();
}
int sumDigits(int x)
{
int s=0;
while(x>0)
{
s=s+x%10;
x=x/10;
}
return s;
}
boolean isPrime(int a)
{
int c=0;
for(int i=1;i<=a;i++)
{
if(a%i==0)
c++;
}
if (c==2)
return true;
else
return false;
}
void check()
{
//checking whether n is composite or not
int f=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
f=1;
break;
}

}
if(f==0)
System.out.println(n+"is not a Composite Number.");
else
{
int dsum=sumDigits(n);
int sp=0,j=2;
int a=n;
while(a>1)
{
if(a%j==0&&isPrime(j)==true)
{
System.out.print(j+" ");
sp=sp+sumDigits(j);
a=a/j;
continue;
}
j++;
}
System.out.println("\nSum of digits:"+sp);
System.out.println("Sum of digits of Prime Factors:"+dsum);
if(dsum==sp)
System.out.println(n+" is a Smith Number.");
else
System.out.println(n+"is not a Smith Number.");
}
}
public static void main(String args[])
{
Smith obj = new Smith();
obj.accept();
obj.check();
}
}

You might also like