Professional Documents
Culture Documents
Comp Project
Comp Project
_________
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.
Algorithm
STEP1: START
return;
STEP8: while(num != 0)
flag = false;
break;
last = digit;
num=num/ 10;
}
return flag;
STEP9: while(num != 0)
flag = false;
break;
last = digit;
num /= 10;
return flag;
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 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;
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
if(m >= n)
System.out.println("Invalid input.");
return;
STEP 7: Checking in the numbers are Prime Adam or not in the given range
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)
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
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
a[i][j] = in.nextInt();
System.out.println("INVALID INPUT");
return;
int decNum = 0;
SOURCE CODE
import java.util.*;
int m = in.nextInt();
int n = in.nextInt();
System.out.println("OUT OF RANGE");
return;
a[i][j] = in.nextInt();
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.
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 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)
return 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
SOURCE CODE
import java.util.Scanner;
class Mobius{
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
int m = mob(n);
int pf = 2;
int total = 0;
if(n == 1){
return 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){
if(total % 2 == 0)
return 1;
else
return -1;
return 0;
VARIABLE DESCRIPTION
OUTPUT
Date:28/04/2021
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 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:
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
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
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
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
SOURCE CODE
import java.io.*;
class magicmatrix
int n=Integer.parseInt(x.readLine());
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--;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
System.out.print(A[i][j]+"\t");
System.out.println();