You are on page 1of 34

_________________________________________________________________________________

_________

DATE: 21/04/2021
Write a program in Java to input an integer, and check whether it is an increasing number, or a
decreasing number or a bouncy number, and display a suitable message accordingly.

 A bouncy number is one in which is a non-negative integer, and it is neither an increasing


number, nor a decreasing number. Example: 264458.

Algorithm
STEP1: START

STEP2:Accept a Value for n and a[][]

STEP3: if(n < 0)

System.out.println("Not an increasing number.");

System.out.println("Not a decreasing number.");

System.out.println("Not a bouncy number.");

return;

STEP4: if(isIncreasing(n))// Checks if it is a Increasing Number or Not.

System.out.println(n + " is an increasing number.");

STEP5: if(isDecreasing(n))// Checks if it is a Decreasing Number or Not.

System.out.println(n + " is a decreasing number.");

STEP6: if(!isIncreasing(n) && !isDecreasing(n))// Checks if it is Bouncy Number or Not.

System.out.println(n + " is a bouncy number.");

STEP7: int last = num % 10;

STEP8: while(num != 0)

int digit = num % 10;

if(digit > last){

flag = false;

break;

last = digit;

num=num/ 10;

}
return flag;

STEP9: while(num != 0)

int digit = num % 10;

if(digit < last){

flag = false;

break;

last = digit;

num /= 10;

return flag;

STEP10: End Of Program

SOURCE CODE
import java.util.*;//Declaration of Java Package
class Bouncy_Number//Declaration of Class Name
{
public static void main(String args[])//Declaration of Main Method
{
Scanner sc= new Scanner(System.in);//Activation of Scanner Syntax
System.out.print("N = ");//Inputing the Number
int n = sc.nextInt();
if(n < 0)
{
System.out.println("Not an increasing number.");
System.out.println("Not a decreasing number.");
System.out.println("Not a bouncy number.");
return;
}
if(isIncreasing(n))// Checks if it is a Increasing Number or Not.
System.out.println(n + " is an increasing number.");
if(isDecreasing(n))// Checks if it is a Decreasing Number or Not.
System.out.println(n + " is a decreasing number.");
if(!isIncreasing(n) && !isDecreasing(n))// Checks if it is Bouncy Number or Not.
System.out.println(n + " is a bouncy number.");
}
public static boolean isIncreasing(int num)
{
int last = num % 10;
boolean flag = true;
while(num != 0){
int digit = num % 10;
if(digit > last){
flag = false;
break;
}
last = digit;
num=num/ 10;
}
return flag;
}
public static boolean isDecreasing(int num)
{
int last = num % 10;
boolean flag = true;
while(num != 0)
{
int digit = num % 10;
if(digit < last){
flag = false;
break;
}
last = digit;
num /= 10;
}
return flag;
}
}

VARIABLE DESCRIPTION
SL.NO NAME OF THE DATA PURPOSE
VARIABLE TYPE
1 n int To store the value for n
2 last int To store the value of last digit of a
number
3 num int To store the value for num
4 flag boolean To store the value of flag
5 digit int To store the value of last digit of num

OUTPUT
Date:-22/04/2021
Write a program to input a arithmetic expression in string which contains only one operator
between two operands and prints the output in integer form. If the expression contains more
than one operator then display”Invalid Input”.

Algorithm
STEP 1 : START

STEP 2 : Declaration of java package

STEP 3 : Declaration of class name (sum)

STEP 4 : Declaration of main method

STEP 5 : Activating Scanner class (Scanner sc= new Scanner (System.in);

STEP 6: Initialization of Variables

STEP 7 : Length of the Input string

STEP 8 : Using for loop and then extracting the characters to check the input contains more than one
operator.

STEP 9 : If the input has more than 1 operator then it is Invalid input

STEP 10 : If it is not more than 1 operator then we extract the operator form the string.

STEP 11: We use substring to extract the first operand and second operand and convert it into Integer
type .

STEP 12 : if(t=='+')

p=aa+bb;

else if(t=='-')

p=aa-bb;

else if(t=='*')

p=aa*bb;

else if(t=='/')

p=aa/bb;

STEP 13: Print the result of the arithmetic expression

STEP 14: END OF PROGRAM

SOURCE CODE
import java.util.*;
class SUM
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int c=0,p=0,i;
char ch,t=' ';
System.out.println("ENTER THE EXPRESSION:");
String s=sc.nextLine();
int len=s.length();
for(i=0;i<len;i++)
{
ch=s.charAt(i);
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
c++;
}
}
if(c>1)
{
System.out.println("INVALID INPUT");
}
else
{
for(i=0;i<len;i++)
{
ch=s.charAt(i);
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
t=ch;
break;
}
}
String a=s.substring(0,i);
String b=s.substring(i+1);
int aa=Integer.parseInt(a);
int bb=Integer.parseInt(b);
if(t=='+')
p=aa+bb;
else if(t=='-')
p=aa-bb;
else if(t=='*')
p=aa*bb;
else if(t=='/')
p=aa/bb;
System.out.println(" THE RESULT OF THE GIVEN EXPRESSION="+p);
}
}
}

VARIABLE DESCRIPTION
SL.NO NAME OF THE DATA PURPOSE
VARIABLE TYPE
1 c int To store as a counter variable
2 p int To store the value as arithmetic
expression
3 i int To initiate the for loop .
4 ch char To store the extracted character
from the string
5 t char to store the operator of the
expression
6 s String to store the arithmetic expression
in string
7 len int to store the length of the string
8 aa int to store the integer form of first
operand
9 bb int to store the integer form of the
second operand

OUTPUT
Date:26/04/2021
A Prime-Adam integer is a positive integer (without leading zeroes) which is a prime as well as
an Adam number.Prime number: A number which has only two factors, i.e. 1 and the number
itself. Example: 2, 3, 5, 7, etc.Adam number: The square of a number and the square of its
reverse are reverse to each other. Example: If n = 13 and reverse of ‘n’ is 31, then, 13 2 = 169,
and 312 = 961 which is reverse of 169. Thus, 13 is an Adam number.Accept two positive
integers m and n, where m is less than n as user input. Display all Prime-Adam integers that
are in the range between m and n (both inclusive) and output them along with the frequency, in
the format given below:INPUT:
m=5
n = 100
OUTPUT:
The Prime-Adam integers are:
11, 13, 31
Frequency of Prime-Adam integers is: 3

Algorithm
STEP 1:Start

STEP 2:Declaration of java package(import.java.util.*;)

STEP 3:Declaration of class name(PrimeAdam)

STEP 4:Activation of Scanner Class

STEP 5:Initialization of Variable(int m, int n,int count)

STEP 6:Checking for Invalid Input

if(m >= n)
System.out.println("Invalid input.");
return;
STEP 7: Checking in the numbers are Prime Adam or not in the given range

System.out.println("The Prime-Adam integers are:");


for(int i = m; i <= n; i++){
if(isPrime(i)){
int rev = reverse(i);
int s1 = i * i;
int s2 = rev * rev;
if(reverse(s1) == s2){
if(count == 0)
System.out.print(i);
else
System.out.print(", " + i);
count++;
STEP 8:Printing the Fr4equency if there are any.

if(count == 0)
System.out.println("NIL");
else
System.out.println();
System.out.println("Frequency of Prime-Adam integers is: " + count);
}
STEP 9:Declaration of boolean isPrime(int n)

STEP 10:Checking if the number is Prime or not


int f = 0;
for(int i = 1; i <= n; i++){
if(n % i == 0)
f++;
STEP 11: Declaration of int reverse(int n)
int rev = 0;
for(int i = n; i > 0; i /= 10)
rev = rev * 10 + i % 10;
return rev;
STEP 12:End of the Program
SOURCE CODE
import java.util.*;
class PrimeAdam{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.print("m = ");
int m = sc.nextInt();
System.out.print("n = ");
int n = sc.nextInt();
if(m >= n)
{
System.out.println("Invalid input.");
return;
}
int count = 0;
System.out.println("The Prime-Adam integers are:");
for(int i = m; i <= n; i++){
if(isPrime(i)){
int rev = reverse(i);
int s1 = i * i;
int s2 = rev * rev;
if(reverse(s1) == s2){
if(count == 0)
System.out.print(i);
else
System.out.print(", " + i);
count++;
}
}
}
if(count == 0)
System.out.println("NIL");
else
System.out.println();
System.out.println("Frequency of Prime-Adam integers is: " + count);
}
public static boolean isPrime(int n){
int f = 0;
for(int i = 1; i <= n; i++){
if(n % i == 0)
f++;
}
return f == 2;
}
public static int reverse(int n){
int rev = 0;
for(int i = n; i > 0; i /= 10)
rev = rev * 10 + i % 10;
return rev;
}
}
VARIABLE DESCRIPTION
SL.NO NAME OF THE DATA PURPOSE
VARIABLE TYPE
1 m int To input the Lower
range Value
2 n int To input the Upper
range Value
3 count int To use it as a
counter for
frequency
4 i int To initiate for loop
5 rev int To store the reverse
of the number
6 s1 int To store the square
the number
7 s2 int To store the square
of the reverse
number
8 f int To use it as a
counter for prime
checking
9 n int To store the value of
the number

OUTPUT

Date:27/04/2021
WRITE A PROGRAM TO DECLARE A MATRIX A[ ][ ]OF ORDER (M X N) WHER ‘M’ IS THE
NUMBER OF ROWS AND ‘N’ IS THE NUMBER OF COLUMNS SUCH THAT THE VALUE OF ‘M’
MUST BE GREATER THAN 0 AND LESS THAN 10 AND THE VALUE OF N MUST BE GREATER
THAN 2 AND OLESS THAN 6. ALLOW THE USER TO INPUT DIGITS (0-7) ONLY AT EACH
LOCTAION,SUCH THAT EACH ROW REPRESENTS AN OCTAL NUMBER.

Algorithm
STEP 1:Start

STEP 2:Declaration of java package(import.java.util.*;)

STEP 3:Declaration of class name(OctalMatrix)

STEP 4:Activation of Scanner Class

STEP 5:Initialization of Variable(int m, int n,int a[][],int decNum, int i, int j)

STEP 6:Checking for Out of Range Input for m and n

if (m <= 0 || m >= 10 || n <= 2 || n >= 6)

System.out.println("OUT OF RANGE")

return;

STEP 7:Enter elements of rows in the matrix and check the validity of the of the filled matrix

for (int i = 0; i < m; i++) {

System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");

for (int j = 0; j < n; j++) {

a[i][j] = in.nextInt();

if (a[i][j] < 0 || a[i][j] > 7) {

System.out.println("INVALID INPUT");

return;

STEP 8:Converting the Rows into octal equivalent

for (int i = 0; i < m; i++) {

int decNum = 0;

for (int j = 0; j < n; j++) {

decNum += a[i][j] * Math.pow(8, n - j - 1 );

System.out.print(a[i][j] + " ");

STEP 9: Printing statement for the required problem.


STEP 10:End of the program.

SOURCE CODE
import java.util.*;

public class OctalMatrix

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the number of rows (M): ");

int m = in.nextInt();

System.out.print("Enter the number of columns (N): ");

int n = in.nextInt();

if (m <= 0 || m >= 10 || n <= 2 || n >= 6) {

System.out.println("OUT OF RANGE");

return;

int a[][] = new int[m][n];

for (int i = 0; i < m; i++) {

System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");

for (int j = 0; j < n; j++) {

a[i][j] = in.nextInt();

if (a[i][j] < 0 || a[i][j] > 7) {

System.out.println("INVALID INPUT");

return;

VARIABLE DESCRIPTION
SL. NO. NAME OF THE DATA TYPE PURPOSE
VARIABLE
1. m int To store the value for m and the
number of rows.

2. n int To store the value for n and the number


of columns.

3. i int To store the value for i and helps in


iteration

4. i int To store the value for i and and helps in


iteration

OUTPUT

DATE:01/05/2021
A MOBIUS function M(N) returns the value -1 or 0 or 1 for a natural number (N) by the following
conditions are defined:
When,
M(N) = 1 if N = 1
M(N) = 0 if any prime factor of N is contained in N more than once.
M(N) = (-1)P if N is the product of ‘P’ distinct prime factors.
Write a program to accept a positive natural number (N) and display the MOBIUS result with
proper message.

Algorithm
Start

STEP 1:Declaration of Java package(import java.Scanner;)

STEP 2:Declaration of class name Mobius

STEP 3:Declaration of main method(public static void main())

STEP 4:Initialization of variable (int m,n,pf,total,distinct,count)

STEP 5:Enter the value for variable n and call the function mob()

STEP 6:In the function mob() check if the number has prime factors or not.. the required calculation
for the function as follow:

if(n == 1)

System.out.println("NO PRIME FACTORS");

return 1;

while(n > 1){

int count = 0;

while(n % pf == 0){

n /= pf;

System.out.print(pf);

count++;

total++;

if(count > 1)

distinct = false;

if(n > 1)

System.out.print(" x ");

pf++;

STEP 7:Finding the total number of disntinct prime factors of the number

if(total % 2 == 0)

return 1;

else

return -1;
STEP 8:if duplicate prime factors then it will return 0

STEP 9:End of program

SOURCE CODE
import java.util.Scanner;

class Mobius{

public static void main(String args[]){

Scanner in = new Scanner(System.in);

System.out.print("N = ");

int n = Integer.parseInt(in.nextLine());

int m = mob(n);

System.out.println("M(" + n + ") = " + m);

public static int mob(int n){

int pf = 2;

int total = 0;

boolean distinct = true;

if(n == 1){

System.out.println("NO PRIME FACTORS");

return 1;

while(n > 1){

int count = 0;

while(n % pf == 0){

n /= pf;

System.out.print(pf);

count++;

total++;

if(count > 1)

distinct = false;

if(n > 1)

System.out.print(" x ");

pf++;

}
System.out.println();

if(distinct){

System.out.println("NUMBER OF DISTINCT PRIME FACTORS = " + total);

if(total % 2 == 0)

return 1;

else

return -1;

System.out.println("DUPLICATE PRIME FACTORS");

return 0;

VARIABLE DESCRIPTION

SL. NO. NAME OF THE DATA TYPE PURPOSE


VARIABLE

1. m int To store the value of m and calls


the function mob( )

2. n int To store the value for n

3. pf int To store the value for pf

4. total int To store the value and used as


increment variable.

5. count int To store the value of count and


used as counters

6. distinct boolean To check truth or falsity

OUTPUT
Date:28/04/2021

Write a program to create a square matrix of type integer of


size ‘n’, where the value of ‘n’ is input by the user. Now fill the
matrix with natural numbers in circular/spiral fashion in
clockwise order, starting from index [0, 0], and display the
generated matrix.

Example:
INPUT:
N=5
OUTPUT:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Algorithm
STEP 1:Start

STEP 2:Declaration of java package(import.java.util.*;)

STEP 3:Declaration of class name(Circular_Matrix)

STEP 4:Declaration of main method

STEP 5:Activation of Scanner Class

STEP 6:Initialization of Variable( int n,int A[][],int k=1, int c1=0, int c2=n-1, int r1=0, int r2=n-1)
STEP 7:required calculation for circular matrix

while(k<=n*n)
{
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--;
}
STEP 7: Printing of circular matrix:

System.out.println("The Circular Matrix is:");


for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
STEP 8:End of program

SOURCE CODE
import java.util.*;
class Circular_Matrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int n = sc.nextInt();
int A[][] = new int[n][n];
int k=1, c1=0, c2=n-1, r1=0, r2=n-1;
while(k<=n*n)
{
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--;
}
System.out.println("The Circular Matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION
SL. NO. NAME OF THE DATE TYPE PURPOSE
VARIABLE

1. n int To store the number of


elements

2. a[][] int To store the array in double


dimension form

3. k int To store the variable of k and


helps in iteration

4. i int To store the variable of i and


helps in iteration

5. g int To store the value of j and


helps in iteration

6. rl boolean To store the value for the first


row.

7. n2 int To store the value of n – l rows.

8. c1 int To store the value of the first


columns

9. c2 int To store the value of n – l


columns
Date:12/05/2021
Given two positive numbers M and N, such that M is between
100 and 10000 and N is less than 100. Find the smallest
integer that is greater than M and whose digits add up to N. For
example, if M = 100 and N = 11, then the smallest integer
greater than 100 whose digits add up to 11 is 119.

Write a program to accept the numbers M and N from the user


and print the smallest required number whose sum of all its
digits is equal to N. Also, print the total number of digits present
in the required number. The program should check for the
validity of the inputs and display an appropriate message for an
invalid input.

Test your program with the sample data and some random
data:

Example 1:
INPUT:
M = 100
N = 11
OUTPUT:
The required number = 119
Total number of digits = 3

Example 2:
INPUT:
M = 1500
N = 25
OUTPUT:
The required number = 1699
Total number of digits = 4

Algorithm
STEP 1:Start

STEP 2:Declaration of java package(import java. io.*;)

STEP 3:Declaration of class name(Smallest)

STEP 4:Declaration of main method

STEP 5:Activation of Scanner Class


STEP 6:Initialization of Variable( int n,int m, int sum, int value , int c )

STEP 7:Accepting the value for m

STEP 8:Accepting the value for n

STEP9: Declaration of main method


Under main method:
if n < 100 and m >= 100 and m <= 10000:
sum = 0
value = m
repeat while sum != n:
value = value + 1
sum = sumofdigits(value)
print "the required number = " + value
count = countdigits(value)
print "total number of digits = " + count
STEP 10: ELSE:
Invalid input
Step 11:declaration of method(sumOfDigits(int n))
int sum = 0;
for(int i = n; i != 0; i /= 10)
sum += i % 10;
return sum;
STEP 12: declaration of method(countDigits(int n))
int c = 0;
for(int i = n; i != 0; i /= 10)
c++;
return c;
STEP 13: End of the program

SOURCE CODE

import java.io.*;
class Smallest
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("M = ");
int m = Math.abs(Integer.parseInt(br.readLine()));
System.out.print("N = ");
int n = Math.abs(Integer.parseInt(br.readLine()));
if(n < 100 && m >= 100 && m <= 10000)
{
int sum = 0;
int value = m;
do
{
value++;
sum = sumOfDigits(value);
}
while(sum != n);
System.out.println("The required number = " + value);
int count = countDigits(value);
System.out.println("Total number of digits = " + count);
}
else
System.out.println("INVALID INPUT");
}
public static int sumOfDigits(int n){
int sum = 0;
for(int i = n; i != 0; i /= 10)
sum += i % 10;
return sum;
}
public static int countDigits(int n){
int c = 0;
for(int i = n; i != 0; i /= 10)
c++;
return c;
}
}
VARIABLE DESCRIPTION
SL. NO. NAME OF THE DATE TYPE PURPOSE
VARIABLE

1. m int To store the higher range

2. n int To store the lower range

3. sum int To store the sum of digits

4. value int Copy of the higher range and


increment variable

5. i int To store the value of i and helps


in iteration

6. int To store the value of c and used


as counter.
Date:29/04/2021

A square matrix is said to be magic square,if the sum of each row, each column and
each diagonal is same.Write a program to enter an integer number N. Create a
magic square of size N*N.finally, print the elements of the matrix As magic square.
Enter the size of the matrix
3
The magic matrix of size 3×3 is:
816
357
492

Algorithm
START

STEP 1 : Declaration of Java Package (import Java.util.*)


STEP 2 : Declaration of Class (magicmatrix)
STEP 3 : Declaration of main method
STEP 4 : Initialization of variable (p, j,i, k, n, t)
STEP 5 : Accepting the number of rows only of a magic square.
STEP 6 : Declaration of array A[] [] int a [] [] = new int [n] [n]
for(i=0;i<n;i++)
STEP 7 : {
for(j=0;j<n;j++)
{
A[i][j]=0;
}
}
while(k<=n*n)
STEP 8 {
:
A[i][j]=k++;
i--;
j++;
if(i<0&&j>n-1)
{ i=i+2;
j--;
}
if(i<0)
i=n-1;
STEP 9 if(j>n-1)
:
j=0;
if(A[i][j]>0)
{
i=i+2; j--;
}

STEP 10 : Initialization of double dimensional array


STEP 11 : Calculation required for magic square
STEP 12 : Display the output magic square
STEP 13 : End of the program

SOURCE CODE

import java.io.*;

class magicmatrix

public static void main (String args[]) throws IOException

InputStreamReader read =new InputStreamReader (System.in);

BufferedReader x= new BufferedReader(read);

System.out.println("Enter the size of the matrix");

int n=Integer.parseInt(x.readLine());

int A[][]=new int[n][n];

int i,j,k,t;

for(i=0;i<n;i++)

{
for(j=0;j<n;j++)

A[i][j]=0;

if(n%2!=0)

i=0;

j=n/2;

k=1;

while(k<=n*n)

A[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(A[i][j]>0)

i=i+2;

j--;

else

k=1;

for(i=0;i<n;i++)

for(j=0;j<n;j++)

A[i][j]=k++;

j=n-1;

for(i=0;i<n/2;i++)

{
t=A[i][j];

A[i][i]=A[j][j];

A[j][j]=t;

t=A[i][j];

A[i][j]=A[j][i];

A[j][i]=t;

j--;

System.out.println("The magic matrix of size "+n+"x"+n+" is:");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

System.out.print(A[i][j]+"\t");

System.out.println();

You might also like