Write a program in Java to accept two positive integers
m and n, where m is less than n as user input and
display the number of Composite Magic integers within
m and n along with the frequency.
ALGORITHMS
Algorithm for void main()
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Step 9:
Step 10:
Step 11:
START.
Call function void input() to input 2 numbers.
Call a function void display() and read two numbers m, n.
If m>n then print INVALID INPUT: GOTO End.
else run a loop from im to n.
Call boolean isComposite(i) and check true.
Call boolean isMagic(i) and check true.
If true then print i: ff+1.
End loop.
Print the frequency, f.
End.
Algorithm for boolean isComposite(n)
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
c0.
Run a loop from i1 to n.
If n%i=0 then cc+1.
End loop.
If c>2 then return true else return false.
Algorithm for boolean isMagic(n)
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
sumcall function int sumDig(n).
Run a loop till sum>9.
Repeat Step 1.
End loop.
If sum=1 then return true else return false.
Algorithm for int sumDig(n)
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
6:
s0.
Run a loop till n!=0.
ss+n%10.
nn/10.
End loop.
return s.
DOCUMENTATION
import [Link];
import [Link].*;
class CompositeMagic
{ int m,n;
CompositeMagic()
{ m=0;
n=0;
}
public void input()
{ Scanner sc=new Scanner([Link]);
[Link]("Enter m and n");
m=[Link]();
n=[Link]();
}
public void display()
{ if(m<n)
{ int f=0;
[Link]("THE COMPOSITE MAGIC INTEGERS ARE: ");
for(int i=m;i<=n;i++)
{ if(isComposite(i)==true && isMagic(i)==true)
{
[Link](i+" ");
f++;
} }
if(f>0)
[Link]("\nFREQUENCY OF COMPOSITE MAGIC INTEGERS IS: "+f);
else
{ [Link]("Nil");
[Link]("FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: "+0);
else
[Link]("INVALID INPUT");
}
public boolean isComposite(int n)
{ int c=0;
for(int i=1;i<=n;i++)
if(n%i==0)
c++;
if(c>2)
return true;
else
return false;
}
public boolean isMagic(int n)
{ int sum=sumDig(n);
while(sum>9)
sum=sumDig(sum);
if(sum==1)
return true;
else
return false;
}
public int sumDig(int n)
{ int sum=0;
while(n!=0)
{ sum=sum+n%10;
n=n/10;
}
return sum;
}
public static void main(String args[])
{ CompositeMagic obj=new CompositeMagic();
[Link]();
[Link]();
}
}
}}
VARIABLE
NAME
DATATYPE
DESCRIPTION
int
stores the lower limit of the range.
int
stores the upper limit of the range.
int
int
stores the frequency of composite magic
integers.
loop variable from m to n.
int
sum
int
obj
CompositeMa
gic
counts the number of factors of a
number.
stores the summation of digits of a
number.
an object to call non-static functions.
INPUT
Enter m and n
1
100
OUTPUT
THE COMPOSITE MAGIC INTEGERS ARE: 10 28 46 55 64 82 91 100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8
INPUT
Enter m and n
1
9
OUTPUT
THE COMPOSITE MAGIC INTEGERS ARE: Nil
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 0
INPUT
Enter m and n
100
1
OUTPUT
INVALID INPUT
Write a program in Java to accept two positive integers
M and N, where M is between 100 and 10000 and N is
less than 100 as user input. Find the smallest integer that
is greater than M and whose digits add up to N and then
display it. Also display the total number of digits present
in the required number.
ALGORITHMS
Algorithm for void main()
Step
Step
Step
Step
1:
2:
3:
4:
START.
Call function void input() to input 2 numbers.
Call a function void compute() and read two numbers M, N.
if ((M>=100&&M<=10000)&&(N>=0&&N<100)) then GOTO Step 5 , else GOTO Step
7.
Step 5: Run a infinite loop from iM.
Step 6: Call long sum(i) and check equality with N, if true then print the reqired number &total
[Link] digits.
Step 7: print INVALID INPUT.
Step 8: End.
Algorithm for long sum(n)
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
s0.
Run a loop till n!=0.
s s+(n%10)
End loop.
return s.
Algorithm for int count(n)
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
c0.
Run a loop till n!=0.
cc+1.
nn%10.
return c.
DOCUMENTATION
import [Link];
import [Link].*;
class Program
{ long M,N;
Program()
{ M=0;
N=0; }
public void input()
{ Scanner sc=new Scanner([Link]);
[Link]("Enter M and N");
M=[Link]();
N=[Link]();
}
public void compute()
{ if((M>=100&&M<=10000)&&(N>=0&&N<100))
for(long i=M;;i++)
{ if(sum(i)==N)
{ [Link]("THE REQUIRED NUMBER : "+i);
[Link]("TOTAL NUMBER OF DIGITS= "+count(i));
break;
}
}
else
[Link]("INVALID INPUT");
}
public long sum(long n)
{ long s=0;
while(n!=0)
{ s=s+(n%10);
n=n/10;
}
return s;
}
public int count(long n)
{ int c=0;
while(n!=0)
{ c++;
n=n/10;
}
return c;
}
public static void main(String args[])
{ Program obj=new Program();
[Link]();
[Link]();
}
}
VARIABLE
NAME
DATATYPE
long
stores a number between 100 and 10000 .
long
stores a number less than 100.
long
loop variable from starting from M .
long
stores the summation of digits of a number.
int
obj
Program
INPUT
Enter M and N
100
11
OUTPUT
THE REQUIRED NUMBER : 119
TOTAL NUMBER OF DIGITS= 3
INPUT
Enter M and N
1500
25
OUTPUT
THE REQUIRED NUMBER : 1699
TOTAL NUMBER OF DIGITS= 4
INPUT
Enter M and N
99
11
OUTPUT
INVALID INPUT
DESCRIPTION
stores the number of digits of a number.
an object to call non-static functions.
Write a program which inputs a positive natural number
N and prints all the possible consecutive number
combinations, which when added give N.
ALGORITHMS
Algorithm for void main()
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
START.
Read a number n.
Create a class object, obj.
Call a function void display().
End.
Algorithm for void display()
Step
Step
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
6:
7:
8:
sum0, j0, c0.
If N>=0 then GOTO Step 3, else GOTO Step 7 .
Start a for loop from i1 till N. Repeat Steps 3, 4, 5, 6, 7.
sumI, ji+1.
Run a while loop till sum<N and repeat Step 6.
sumsum+j, jj+1.
If sum=N then print it.
Print INVALID INPUT.
DOCUMENTATION
import [Link];
import [Link].*;
class ConsecutiveCombinations
{
int N;
ConsecutiveCombinations(int n)
{
N=n;
}
public void display()
{
int sum=0,j=0;
int c=0;
if(N>=0)
{
for(int i=1;i<N;i++)
{
sum=i;
j=i+1;
while(sum<N)
{
sum=sum+j;
j++;
}
if(sum==N)
{
for(int k=i;k<j;k++)
{
if(k==i)
[Link](k);
else
[Link](" + "+k);
}
[Link]();
c++;
}
}
if(c==0)
[Link]("There are no Consecutive combinations of "+N);
}
else
[Link]("INVALID INPUT");
}
public static void main(String args[])throws IOException
{
Scanner sc =new Scanner([Link]);
[Link]("Enter a number: ");
int n=[Link]();
ConsecutiveCombinations obj=new ConsecutiveCombinations(n);
[Link]();
}
}
VARIABLE
NAME
DATATYPE
int
DESCRIPTION
stores a number inputted by the user.
int
stores a number.
sum
int
int
stores the summation of consecutive
combination of numbers.
a loop variable.
int
int
obj
ConsecutiveCombin
ations
stores the last number of consecutive
combinations.
checks whether there are any
consecutive combinations or not.
an object to call non-static functions.
INPUT
Enter a number: 15
OUTPUT
1+2+3+4+5
4+5+6
7+8
INPUT
Enter a number: 100
OUTPUT
9 + 10 + 11 + 12 + 13 + 14 + 15 + 16
18 + 19 + 20 + 21 + 22
INPUT
Enter a number: 4
OUTPUT
There are no Consecutive combinations of 4
INPUT
Enter a number: -10
OUTPUT
INVALID INPUT
Write a program in Java to accept a date and the first
day of that year, and display the day of the given date.
ALGORITHMS
Algorithm for void main()
Step
Step
Step
Step
1:
2:
3:
4:
START.
Create a class object, obj.
Call a function void accept().
End.
Algorithm for void accept()
Step 1: Accept the required date from user.
Step 2: flag0.
Step 3: Check for validity of xday, xmonth, xyear, If true then GOTO Step 4 else print INVALID
INPUT;
Step
Step
Step
Step
Step
4:
5:
6:
7:
8:
Accept first day of xyear and check for its validity.
Run a while loop till sum<N and repeat Step 6.
sumsum+j, jj+1.
If sum=N then print it.
Print INVALID INPUT.
Algorithm for void compute()
Step
Step
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
6:
7:
8:
If xyear%4=0 then M[1]29.
pos0.
Run a for loop from i0 till 7.
posi.
Run a for loop from i0 till xmonth-1 and repeat Step 6.
sumOfDays+xday-1.
index(sumOfDays%7+pos)&7
Print the required day.
DOCUMENTATION
import [Link];
import [Link].*;
class Date
{ int xday,xmonth,xyear;
String day1;
int M[]={31,28,31,30,31,30,31,31,30,31,30,31};
String D[]={"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"};
Date()
{ xday=0;xmonth=0;xyear=0;
day1="";
}
public void accept()
{ Scanner sc=new Scanner([Link]);
[Link]("Enter the required date in this format_(dd mm yyyy) :- ");
xday=[Link]();
xmonth=[Link]();
xyear=[Link]();
int flag=0;
if(xday>=1&&xday<=31 && xmonth>=1&&xmonth<=12 && xyear>=1000&&xyear<=9999)
{ if((xmonth==2&&xyear%4==0&& !(xday>=1&&xday<=29)) || (xmonth==2&&xyear%4!=0&& !
(xday>=1&&xday<=28)))
{ [Link]("INVALID INPUT");
[Link](0);
}
[Link]("Enter the first date of year "+xyear+" : ");
day1=[Link]();
for(int i=0;i<7;i++)
{ if(D[i].equalsIgnoreCase(day1))
{ flag=1;
break;
}
}
if(flag==1)
compute();
else
[Link]("INVALID INPUT");
}
else
[Link]("INVALID INPUT");
}
public void compute()
{
if(xyear%4==0)
M[1]=29;
int pos=0;
for(int i=0;i<7;i++)
{ if(D[i].equalsIgnoreCase(day1))
{ pos=i;
break;
}
}
int sumOfDays=0;
for(int i=0;i<xmonth-1;i++)
sumOfDays+=M[i];
sumOfDays+=xday-1;
int index=((sumOfDays%7)+pos)%7;
[Link](xday+"/"+xmonth+"/"+xyear+" is on "+D[index]);
}
public static void main(String args[])
{ Date obj=new Date();
[Link]();
}
}
VARIABLE
NAME
DATATYPE
DESCRIPTION
xday
int
stores the day inputted by the user.
xmonth
int
stores the month inputted by the user.
xyear
int
stores the year inputted by the user.
day1
String
stores the first date of the xyear.
M[]
int
D[]
String
flag
int
an indicator of truth values.
int
a loop variable.
pos
int
stores the position of the value of an array.
sumOfDays
int
index
int
stores the summation of all the days before
xday in a year.
stores the computed value of required date.
obj
Date
an array to store [Link] days in 12 months.
an array to store the days in a week.
an object to call non-static functions.
INPUT
Enter the required date in this format_(dd mm yyyy) :- 19 05 2015
Enter the first date of year 2015 : THURSDAY
OUTPUT
19/5/2015 is on Tuesday
INPUT
Enter the required date in this format_(dd mm yyyy) :- 07 12 1996
Enter the first date of year 1996 : MONDAY
OUTPUT
7/12/1996 is on SATURDAY
INPUT
Enter the required date in this format_(dd mm yyyy) :- 29 02 2016
Enter the first date of year 2016 : FRIDAY
OUTPUT
29/2/2016 is on MONDAY
INPUT
Enter the required date in this format_(dd mm yyyy) :- 29 02 2015
OUTPUT
INVALID INPUT
Write a program in Java to check whether a given
number is a Happy number or not.
ALGORITHMS
Algorithm for void main()
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
START.
Create a class object, obj.
Call a function void accept().
Call a function void isHappy().
End.
Algorithm for void accept()
Step 1: Accept a number from user.
Algorithm for void isHappy()
Step 1: acall a function sum_sq_digits and pass n.
Step 2: Run a while loop till a>9 and acall a function sum_sq_digits and pass a.
Step 3: If a=1 then print HAPPY NUMBER else print NOT A HAPPY NUMBER.
Algorithm for int sum_sq_digits(int x)
Step 1: If x=0 then return 0.
Step 2: else dx%10 and return d*d+ sum_sq_digits(x/10).
DOCUMENTATION
import [Link];
import [Link].*;
class Happy
{
int n;
Happy()
{
n=0;
}
public void accept()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any number: ");
n=[Link]();
}
public void isHappy()
{
int a=sum_sq_digits(n);
while(a>9)
a=sum_sq_digits(a);
if(a==1)
[Link]("HAPPY NUMBER");
else
[Link]("NOT A HAPPY NUMBER");
}
public int sum_sq_digits(int x)
{
if(x==0)
return 0;
else
{
int d=x%10;
return (d*d+ sum_sq_digits(x/10));
}
}
public static void main(String args[])
{
Happy obj=new Happy();
[Link]();
[Link]();
}
}
VARIABLE
NAME
DATATYPE
DESCRIPTION
int
stores a number inputted by the user.
int
stores the summation of digits of n.
int
stores a number.
int
stores the digits of x.
obj
Happy
an object to call non-static functions.
INPUT
Enter any number: 28
OUTPUT
HAPPY NUMBER
INPUT
Enter any number: 64
OUTPUT
NOT A HAPPY NUMBER
INPUT
Enter any number: 68
OUTPUT
HAPPY NUMBER
Write a program in Java to check whether a given 10
digit code is a valid ISBN number or not.
ALGORITHMS
Algorithm for void main()
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
START.
Read a String ss.
Create a class object, obj.
Call a function void check().
End.
Algorithm for void check()
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Step 9:
Step 10:
Step 11:
[Link]().
sum0, k10, flag1.
If l=10 then run a for loop from i0 till l-1 , else GOTO Step 11.
[Link](i).
If ab>=48&&ab<=57 then sumsum+k*(ab-48) else flag0.
kk-1 and end for loop.
[Link](l-1).
if ab='X'||ab='x') then sumsum+k*10.
else if ab>=48&&ab<=57 then sumsum+k*(ab-48) else flag0.
else flag0.
if flag=0 then print INVALID INPUT else call a function void display(sum).
Algorithm for void display(int sum)
Step 1: if sum%11=0 then print valid ISBN code.
Step 2: else print not a valid ISBN code.
DOCUMENTATION
import [Link];
import [Link].*;
class ISBN
{ String s;
ISBN(String ss)
{ s=ss;
}
public void check()
{ int l=[Link]();
char ab;
int sum=0,k=10;
int flag=1;
if(l==10)
{ for(int i=0;i<l-1;i++)
{ ab=[Link](i);
if(ab>=48&&ab<=57)
sum=sum+(k*(ab-48));
else
{ flag=0;
break;
}
k--;
}
ab=[Link](l-1);
if(ab=='X'||ab=='x')
sum=sum+(k*10);
else if(ab>=48&&ab<=57)
sum=sum+(k*(ab-48));
else
flag=0;
}
else
flag=0;
if(flag==0)
[Link]("INVALID INPUT");
else
display(sum);
}
public void display(int sum)
{ if(sum%11==0)
{ [Link]("Sum of ISBN code= "+sum);
[Link]("Sum leaves no remainder, so it is a valid ISBN code");
}
else
{ [Link]("Sum of ISBN code= "+sum);
[Link]("Sum leaves remainder, so it is not a valid ISBN code");
}
}
public static void main(String args[])
{ Scanner sc=new Scanner([Link]);
[Link]("Enter a 10 digit code: ");
String ss=[Link]();
ISBN obj=new ISBN(ss);
[Link]();
}
}
VARIABLE
NAME
DATATYPE
DESCRIPTION
String
int
stores the length of s.
ab
char
stores a character of s.
int
used to get values from 1 to 10.
sum
int
flag
int
ss
String
stores the summation of digits of ISBN code
multiplied by k.
an indicator which checks the validity of the
ISBN code.
stores a 10 digit code inputted by the user.
stores the 10 digit ISBN code.
obj
ISBN
an object to call non-static functions.
INPUT
Enter a 10 digit code: 0201530821
OUTPUT
Sum of ISBN code= 99
Sum leaves no remainder, so it is a valid ISBN code
INPUT
Enter a 10 digit code: 007462542X
OUTPUT
Sum of ISBN code= 176
Sum leaves no remainder, so it is a valid ISBN code
INPUT
Enter a 10 digit code: 0231428031
OUTPUT
Sum of ISBN code= 122
Sum leaves remainder, so it is not a valid ISBN code
INPUT
Enter a 10 digit code: 15054X9454
OUTPUT
INVALID INPUT
Write a program in Java to create a Magic Square of
size n*n (n is between 1 and 5).
ALGORITHMS
Algorithm for void main()
Step 1: START.
Step 2: Create a class object, obj.
Step 3: Call a function void accept().
Step 4: End.
Algorithm for void accept()
Step
Step
Step
Step
1:
2:
3:
4:
Accept the size of the matrix, n from user.
If n<1||n>5 then exit the program after printing a message.
Mnew int[n][n].
Call a function void compute().
Algorithm for void compute()
Step
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
6:
7:
Run a loop from i0 to n and a nested loop from j0 to n and then M[i][j]0.
If n%2!=0 then GOTO Step 3 else GOTO Step 6.
Run a while loop till k<=n*n and repeat Steps 4,5.
M[i][j]k , kk+1, ii-1, jj+1.
Create a Magic square matrix of even size.
k1.
Run a loop from i0 to n and a nested loop from j0 to n and then M[i][j]k and
kk+1.
Step 8: jn-1.
Step 9: Create a magic square matrix of odd size.
Step 10: Call a function void display().
Algorithm for void display()
Step 1: Run a for loop from i0 to n and repeat Step 2.
Step 2: Run a nested for loop from j0 to n and print M[i][j]+\t.
DOCUMENTATION
import [Link];
import [Link].*;
class MagicMatrix
{ int M[][];
int n;
MagicMatrix() { n=0; }
public void accept()
{ Scanner sc=new Scanner([Link]);
[Link]("Enter the size of the Matrix : ");
n=[Link]();
if(n<1||n>5)
{ [Link]("Enter a number between 1 to 5 ");
[Link](0);
}
M=new int[n][n];
compute();
}
public void compute()
{ int i,j,k,t;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
M[i][j] = 0;
if(n%2!=0)
{ i=0;
j = n/2;
k = 1;
while(k<=n*n)
{ M[i][j] = k++;
i--;
j++;
if(i<0 && j>n-1)
{ i = i+2;
j--; }
if(i<0)
i = n-1;
if(j>n-1)
j = 0;
if(M[i][j]>0)
{ i = i+2;
j--; } } }
else
{ k = 1;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
M[i][j] = k++;
j = n-1;
for(i=0; i<n/2; i++)
{ t = M[i][i];
M[i][i] = M[j][j];
M[j][j] = t;
t = M[i][j];
M[i][j] = M[j][i];
M[j][i] = t;
j--;
} }
display(); }
public void display()
{ [Link]("The Magic Matrix of size "+n+"*"+n+" is:");
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
[Link](M[i][j]+ "\t");
[Link]();
} }
public static void main(String args[])
{ MagicMatrix obj=new MagicMatrix();
[Link]();
} }
VARIABLE
NAME
DATATYPE
M[][]
int
int
an to store the elements of the Magic
Matrix.
stores the size of M[][].
int
a loop variable which signifies rows of M[][].
int
int
int
a loop variable which signifies columns of
M[][].
stores the temporary location of any
row/column .
a variable used to swap two numbers.
obj
MagicMatrix
INPUT
DESCRIPTION
an object to call non-static functions.
Enter the size of the Matrix : 3
OUTPUT
The Magic Matrix of size 3*3 is:
8
1
6
3
5
7
4
9
2
INPUT
Enter the size of the Matrix : 5
OUTPUT
The Magic Matrix of size 5*5 is:
17
24
1
8
15
23
5
7
14
16
4
6
13
20
22
10
12
19
21
3
11
18
25
2
9
INPUT
Enter the size of the Matrix : 8
OUTPUT
Enter a number between 1 to 5
Write a program to declare a square matrix A[][] of
order M*M (M must be greater than 2 and less than 10).
Rotate the matrix 90 degree clockwise. Find the sum of
the elements of the four corners of the matrix.
ALGORITHMS
Algorithm for void main()
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
Step 6:
Step 7:
Step 8:
Step 9:
Step 10:
START.
Read a number, size.
If size<2 || size>10 then print "THE MATRIX SIZE IS OUT OF RANGE".
else create a class object, obj.
Call a function void accept().
Call a function void displayOriginal().
Call a function void rotateMatrix().
Call a function void displayRotated().
Call a function void sumOfCorner().
End.
Algorithm for void accept()
Step 1: Run a for loop from i0 to M and a nested loop from j0 to M.
Step 2: A[i][j]=[Link]().
Algorithm for void displayOriginal()
Step
Step
Step
Step
1:
2:
3:
4:
print "ORIGINAL MATRIX".
Run a for loop from i0 to M and a nested loop from j0 to M.
print A[i][j]+ .
End loop.
Algorithm for void rotateMatrix()
Step
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
6:
7:
k0.
Run a for loop from i0 to M and repeat Steps 3,4 .
kM-1.
Run a for loop from j0 to M and repeat Steps 5,6 .
B[i][j]A[k][i]
if k>0, kk-1.
End loop.
Algorithm for void displayRotated()
Step
Step
Step
Step
1:
2:
3:
4:
print " MATRIX AFTER ROTATION".
Run a for loop from i0 to M and a nested loop from j0 to M.
print B[i][j]+ .
End loop.
Algorithm for void sumOfCorner()
Step 1: sumA[0][0]+A[0][M-1]+A[M-1][M-1]+A[M-1][0].
Step 2: print "Sum of corner elements= "+sum.
DOCUMENTATION
import [Link];
import [Link].*;
class MatrixRotation
{
int M;
int A[][],B[][];
MatrixRotation(int size)
{ M=size;
A=new int[M][M];
B=new int[M][M];
}
public void accept()
{ Scanner sc=new Scanner([Link]);
[Link]("Enter elements of the Matrix");
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
A[i][j]=[Link]();
}
public void displayOriginal()
{ [Link]("ORIGINAL MATRIX");
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
[Link](A[i][j]+" ");
[Link]();
}
}
public void rotateMatrix()
{ int k=0;
for(int i=0;i<M;i++)
{ k=M-1;
for(int j=0;j<M;j++)
{ B[i][j]=A[k][i];
if(k>0)
k--;
}
}
}
public void displayRotated()
{ [Link]("MATRIX AFTER ROTATION");
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
[Link](B[i][j]+" ");
[Link]();
}
}
public void sumOfCorner()
{ int sum=A[0][0]+A[0][M-1]+A[M-1][M-1]+A[M-1][0];
[Link]("Sum of corner elements= "+sum);
}
public static void main(String args[])
{ Scanner sc=new Scanner([Link]);
[Link]("Enter size of Matrix: ");
int size=[Link]();
if(size<2||size>10)
[Link]("THE MATRIX SIZE IS OUT OF RANGE");
else
{ MatrixRotation obj=new MatrixRotation(size);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
} }
VARIABLE
NAME
DATATYPE
DESCRIPTION
A[][]
int
B[][]
Int
int
int
an array to store the elements of the
square matrix.
an array to store the rotated elements of
the square matrix A[][]
stores the size of the square matrix A[]
[].
loop variable from 0 to M.
int
another loop variable from 0 to M.
int
sum
int
obj
MatrixRotatio
n
it stores the row index of the matrix B[]
[].
stores the summation of elements of
corner elements of the matrix.
an object to call non-static functions.
INPUT
Enter size of Matrix: 3
Enter elements of the Matrix
123456789
OUTPUT
ORIGINAL MATRIX
123
456
789
MATRIX AFTER ROTATION
741
852
963
Sum of corner elements= 20
INPUT
Enter size of Matrix: 11
OUTPUT
THE MATRIX SIZE IS OUT OF RANGE
Write a program in Java to check whether a given
number is a Smith Number or not.
ALGORITHMS
Algorithm for void main()
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
6:
START.
Read a number n.
if n>=0 then create a class object, obj.
Call a function void display().
else print "INVALID INPUT".
End.
Algorithm for void display()
Step 1: sumDsumDig(N).
Step 2: sumFsumPrimeFact(N).
Step 3: if sumD==sumF then print "SMITH NUMBER".
Step 4: else print "NOT A SMITH NUMBER".
Algorithm for int sumDig(int n)
Step
Step
Step
Step
1:
2:
3:
4:
s0.
Run a while loop till n!=0 and repeat Step 3.
ss+n%10. nn/10.
return s.
Algorithm for int sumPrimeFact(int n)
Step
Step
Step
Step
1:
2:
3:
4:
i2 , s0.
Run a while loop till n>1 and repeat Step 3.
if n%i=0 then ss+sumDig(i) , nn/I else ii+1.
return s.
DOCUMENTATION
import [Link];
import java .lang.*;
class SmithNumber
{
int N;
SmithNumber(int n)
{
N=n;
}
public void display()
{
int sumD=sumDig(N);
int sumF=sumPrimeFact(N);
if(sumD==sumF)
[Link]("SMITH NUMBER");
else
[Link]("NOT A SMITH NUMBER");
}
public int sumDig(int n)
{
int s=0;
while(n!=0)
{
s=s+(n%10);
n=n/10;
}
return s;
}
public int sumPrimeFact(int n)
{
int i=2,s=0;
while(n>1)
{
if(n%i==0)
{
s=s+sumDig(i);
n=n/i;
}
else
i++;
}
return s;
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number: ");
int n=[Link]();
if(n>=0)
{ SmithNumber obj=new SmithNumber(n);
[Link]();
}
else
[Link]("INVALID INPUT");
}
}
VARIABLE
NAME
DATATYPE
int
stores a number inputted by the user.
int
stores a number.
sumD
int
stores the summation of digits of N.
sumF
int
int
stores the summation of prime factors
of N.
stores the summation of digits of n.
int
a loop variable.
obj
SmithNumber
INPUT
Enter a number: 666
OUTPUT
SMITH NUMBER
INPUT
DESCRIPTION
an object to call non-static functions.
Enter a number: 4937775
OUTPUT
SMITH NUMBER
INPUT
Enter a number: 123
OUTPUT
NOT A SMITH NUMBER
INPUT
Enter a number: -10
OUTPUT
INVALID INPUT
Write a program in Java to check whether a given set of
elements form a Symmetric Matrix or not.
ALGORITHMS
Algorithm for void main()
Step
Step
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
6:
7:
8:
START.
Read a number, size.
Create a class object, obj.
Call a function void accept().
Call a function void display_Original().
Call a function void checkSymmetric().
Call a function void compute().
End.
Algorithm for void accept()
Step 1: If M>2&&M<10 then GOTO Step 2.
Step 2: Run a for loop from i0 to M and a nested loop from j0 to M and repeat Step 3.
Step 3: A[i][j][Link]().
Step 4: else print "THE MATRIX SIZE IS OUT OF RANGE" and exit the program.
Algorithm for void display_Original()
Step
Step
Step
Step
1:
2:
3:
4:
print "ORIGINAL MATRIX".
Run a for loop from i0 to M and a nested loop from j0 to M.
print A[i][j]+ .
End loop.
Algorithm for void checkSymmetric()
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
flag1.
Run a for loop from i0 to M and a nested loop from j0 to M and repeat Step 3.
if A[i][j]!=A[j][i] then flag0.
if flag=1 then print "THE GIVEN MATRIX IS SYMMETRIC".
else print "THE GIVEN MATRIX IS NOT SYMMETRIC".
Algorithm for void compute()
Step
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
5:
sumLD0, sumRD0.
Run a for loop from i0 to M and a nested loop from j0 to M and repeat Steps .
if i=j then sumLDsumLD+A[i][j].
if i+j=M then sumRDsumRD+A[i][j].
Print sumLD.
Print sumRD.
DOCUMENTATION
import [Link];
class Symmetric
{ int A[][];
int M;
Symmetric(int size)
{ M=size;
A=new int[M][M];
}
public void accept()
{ if(M>2&&M<10)
{ Scanner sc=new Scanner([Link]);
[Link]("Enter elements of the Matrix");
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
A[i][j]=[Link]();
}}
else
{ [Link]("THE MATRIX SIZE IS OUT OF RANGE");
[Link](0);
}
}
public void display_Original()
{ [Link]("\nORIGINAL MATRIX");
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
[Link](A[i][j]+" ");
[Link]();}
}
public void checkSymmetric()
{ int flag=1;
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
{ if(A[i][j]!=A[j][i])
{ flag=0;
break; } } }
if(flag==1)
[Link]("THE GIVEN MATRIX IS SYMMETRIC");
else
[Link]("THE GIVEN MATRIX IS NOT SYMMETRIC");
}
public void compute()
{ int sumLD=0,sumRD=0;
for(int i=0;i<M;i++)
{ for(int j=0;j<M;j++)
{ if(i==j)
sumLD+=A[i][j];
if((i+j)==M)
sumRD+=A[i][j]; } }
[Link]("The sum of the left diagonal= "+sumLD);
[Link]("The sum of the right diagonal= "+sumRD);
}
public static void main(String args[])
{ Scanner sc=new Scanner([Link]);
[Link]("Enter size of Matrix: ");
int size=[Link]();
Symmetric obj=new Symmetric(size);
[Link]();
obj.display_Original();
[Link]();
[Link]();
}
}
VARIABLE
NAME
DATATYPE
A[][]
int
int
int
an array to store the elements of the
square matrix.
stores the size of the square matrix A[]
[].
loop variable from 0 to M.
int
another loop variable from 0 to M.
flag
int
sumLD
int
sumRD
int
obj
Symmetric
indicator to show whether the matrix is
symmetric or not.
stores the summation of elements of left
diagonal of the matrix.
stores the summation of elements of
right diagonal of the matrix.
an object to call non-static functions.
INPUT
Enter size of Matrix: 3
Enter elements of the Matrix
123245356
OUTPUT
ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
DESCRIPTION
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal= 11
The sum of the right diagonal= 10
INPUT
Enter size of Matrix: 4
Enter elements of the Matrix
7892456385317642
OUTPUT
ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal= 17
The sum of the right diagonal= 20
INPUT
Enter size of Matrix: 10
OUTPUT
THE MATRIX SIZE IS OUT OF RANGE