java program file By-chirodeep Dutta
15 java program constructed under bluJ enviroment after execution of each
of the code and compling the code the program code are presented below
with the appropiate logic behind it.
BIODATA
NAME – CHIRODEEP DUTTA
CLASS – XI
SECTION – C
ROLL NUMBR – 8
SUBJECT – COMPUTER
PRACTICAL
YEAR – 2024-2025
GUIDED BY- SIR ARUN KUMAR
PATHAK
1. Write a program to input a number and check whether
it is a fascinating number or
not.
Fascinating Numbers: Some numbers of 3 digits or more
exhibit a very interesting property.
The property is such that, when the number is multiplied
by 2 and 3, and both these products are
concatenated with the original number, all digits from 1 to
9 are present exactly once, regardless
of the number of zeroes.
For eg;192
Consider the number 192,
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results : 192384576
It could be observed that ‘192384576’ consists of all digits
from 1 to 9 exactly once. Hence, it could
be concluded that 192 is a Fascinating Number.
Sample input: (i)219
(ii)123
Sample output: (i)Fascinating number
(ii)Not a fascinating Number
ALGORITHM-
Algorithm for Unique()
1. Start
2. Create an array A[ ] to store the digits
3. Create integer variables i and flag
4. Repeat step 5 to 7 till i<q.length()
5. Extract character from I index in string q
6. Convert the ascii value of the character into numeric value by subtracting 48 and
store it
in array A[ ]
7. Add 1 to i
8. Repeat steps 9 for 10 times
9. Check if frequency is 1.If not equal to 1, then go to step 10
10. Make the value of flag 1
11. Check if flag is 1.If 1,then return false else return true.
12. Stop
Algorithm for main()
1. Create an object in of Scanner Class
2. Create an object ob of class
3. Display suitable message and take input in variable n
4. Convert the number to string and store it in p
5. Check if the length of p is less than 3. If less,then print that the number must be
greater
than 99 and go to 8
6. Add the string converts of the number,numberx2 and numberx3 and store it in
string
variable s.
7. Perform unique() for s and give the returned value to if statement.If true then
print
Fascinating number else not fascinating.
8. Stop
import java.util.*;
class FascinatingNumber
{
boolean Unique(String q)
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit from '0' to '9'
int i, flag = 0;
char ch;
for(i=0; i<q.length(); i++)
{
ch = q.charAt(i);
A[ch-48]++;
}
for(i=1; i<10; i++)
{
//checking if every digit from '1' to '9' is present exactly once or not
if (A[i]!=1)
{
flag = 1; //flag is set to 1 if frequency is not 1
break;
}
}
if(flag == 1)
return false;
else
return true;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
FascinatingNumber ob = new FascinatingNumber();
System.out.println("Enter a number");
int n = in.nextInt();
String p = Integer.toString(n); //converting the number to String
if(p.length()<3)
System.out.println("Number should be of atleast 3 digits.");
else
{
String s = Integer.toString(n*1) + Integer.toString(n*2) + Integer.toString(n*3);
/* Joining the first, second and third multiple of the number
* by converting them to Strings and concatenating them*/
if(ob.Unique(s))
System.out.println(n+" is a Fascinating Number.");
else
System.out.println(n+" is not a Fascinating Number.");
}
}
}
NAME TYPE USAGE
n int To take input
i int To run the loop
ch char To extraxct the charecters
2.Program to check whether a number is a kaprikar
number or not
A positive whole number ‘n’ that has ‘d’ number of digits
is squared and split into two pieces, a
right-hand piece that has ‘d’ digits and a left-hand piece
that has remaining ‘d’ or ‘d-1’ digits.
If the sum of the two pieces is equal to the number, then
‘n’ is a Kaprekar number. The first few
Kaprekar numbers are: 9, 45, 297 ........
Example 1: 9
9
2 = 81, right-hand piece of 81 = 1 and left hand piece of
81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a
Kaprekar number.
Sample input:45
Sample output:Kaprikar number
Algorithm-
Algorithm for input
1. Start
2. Create an object in of Scanner class
3. Dispaly message to enter a number and take the input in n
4. Stop
Algorithm for check
1. Start
2. Declare an int variable sq =n x n, l=s.length() and k
3. If l%2==0 then split sq in two equal parts.Add them and store it in k
4. Else divide sq into unequal parts and store it in k
5. If k==n then return true else return false
6. Stop
Algorithm for main
1. Start
2. Create an object ob of class kaprikar
3. Call input of ob
4. Call check of ob and pass it to if statement
5. If check gives true then print kaprikar number else print not a kaprekar
number
6. Stop
import java.util.*;
class kaprikar
{
int n;
kaprikar()//Default Constructor
{
n=0;
}
void input()//To take input
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number");
n=in.nextInt();
}
boolean check()//To check kaprikar
{
int sq=n*n;//Squaring Number
String s=Integer.toString(sq);
int l=s.length();
int k;
if(l%2==0)
{
k=(sq%((int)Math.pow(10,l/2)))+sq/((int)Math.pow(10,l/2));//Splitting and adding
}
else
{
k=(sq%((int)Math.pow(10,(l/2)+1)))+(sq%((int)Math.pow(10,(l/2))));
}
if(k==n)//Checkin Kaprikar
return(true);
else
return(false);
}
public static void main(String Args[])
{
kaprikar ob=new kaprikar();
ob.input();
if(ob.check())
System.out.println("Kaprekar Number");
else
System.out.println("Not a Kaprekar Number");
}
}
3. Program to check whether a number is a bouncy
number or not
Increasing Number : Working from left-to-right if no digit
is exceeded by the digit to its left it is
called an increasing number; for example, 22344.
Decreasing Number : Similarly if no digit is exceeded by
the digit to its right it is called a
decreasing number; for example, 774410.
Bouncy Number : We shall call a positive integer that is
neither increasing nor decreasing a
“bouncy” number; for example, 155349. Clearly there
cannot be any bouncy numbers below 100.
Sample input:15349
Sample ouput:Bouncy number
Algorithm
Algorithm for isIncreasing
1. Start
2. Convert n to string and store it in a String variable s
3. Declare a char variable ch and int variable f=0
4. Int i=0 and repeat step 4 and 5 while i<s.length()-1
5. Find character at index i in s and store it in ch
6. If ch is greater than the value of character at i+1 then store the value of
f as 1 and
break
7. If f==1 then return false else return true
8. Stop
Algorithm for isDecreasing
1. Start
2. Convert n to string and store it in a String variable s
3. Declare a char variable ch and int variable f=0
4. Int i=0 and repeat step 4 and 5 while i<s.length()-1
5. Find character at index i in s and store it in ch
6. If ch is less than the value of character at i+1 then store the value of f
as 1 and break
7. If f==1 then return false else return true
8. Stop
Algorithm for isBouncy
1. Start
2. Pass n to isIncreasing.If it returns true then print Increasing number
3. Pass n to isDecreasing.If it returns true then print Decreasing number
4. If neither of 2 and 3 are executed then print that it is a bouncy number
5. Stop
Algorithm for main
1. Start
2. Create an object ob of class BouncyNumber
3. Create an object in of Scanner class
4. Display message to input a number and store the input in a variable int
n
5. Call isBouncy of ob by passing n
6. Stop
4. Program to check whether a number is Smith Number
or not. A smith number is a composite number, the sum of
whose digits is the sum of the digits of its prime factors
obtained as a result of prime factorisation (excluding 1).
The first few such numbers are 4, 22, 27, 58, 85, 94, 121
… Example: 666 Prime factors are 2, 3, 3 and 37 Sum of
the digits are (6+6+6) = 18 Sum of the digits of the
factors (2+3+3+(3+7) = 18 Sample input:666 Sample
output:Smith Number A
Algorithm-
Algorithm for sumDig
1. Start
2. Create a variable int s
3. Repeat steps 4 and 5 while n is greater than 0
4. Add n%10 to s
5. Divide n by 10 and store it in n
6. Return s
7. Stop
Algorithm for sumPrimeFact
1. Start
2. Create two int variables i=2 and sum=0
3. Repeat from step 4 to step 7 while n>1
4. If n%i==0 then perform steps 5 and 6 and then go to 8 else perform
step 7
5. Pass i to sumDig and add it to sum
6. Divide n by i and store it in n
7. Add 1 to i
8. Return sum
9. Stop
Algorithm for main
1. Start
2. Create an object ob of classs smith
3. Create an object in of Scanner Class
4. Print message to enter a number and take the input in a variable int n
5. Pass n to sumDig of ob an d store it in int a
6. Pass b to sumPrimeFact of ob and store it in int b
7. If a==b then print it is a smith number else print it is not a smith
number
8. Stop
import java.util.*;
class Smith
{
int sumDig(int n)//Sum of Digits
{ int
s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
int sumPrimeFact(int n)//Sum of primes
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(i); //Finding
Sum n=n/i;
}
else
i++;
}
return sum;
}
public static void main(String args[])
{
Smith ob=new Smith();
Scanner in=new
Scanner(System.in);
System.out.println("Enter a
Number"); int n=in.nextInt();
int a=ob.sumDig(n);// finding sum of digit
int b=ob.sumPrimeFact(n); //finding sum of prime
factors if(a==b)
System.out.print("It is a Smith
Number"); else
System.out.print("It is Not a Smith Number");
}
}
5.Program to check whether a number is an evil number
or not
An Evil number is a positive whole number which has
even number of 1’s in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which
contains even number of 1’s.
A few evil numbers are 3, 5, 6, 9….
Sample input:9
Sample
Output:Evil
number
1. Start
2. Declare two variables int r and string s
3. Create a character array dig with characters 0 and 1
4. While n>0 repeat steps 4,5 and 6
5. Store n%2 in r
6. Pass r as index to dig and add it to s
7. Divide n by 2 and store it in n
8. Return Stop
Algorithm for count
1. Start
2. Create two int variables c=0 and l=s.length()
3. Create a char variable ch
4. Int i=0 and repeat steps 4,5 and 6 while i<l
5. Store character at index i of s in ch
6. If ch==’1’ then add one to c
7. Add one to i
8. Return c
9. Stop
Algorithm for main
1. Start
2. Create an object ob of class EvilNumber
3. Create an object in of Scanner class
4. Display message to enter a number and take the input in int n
5. Pass n to Binary of ob and store it in String st
6. Pass st to count of ob and store it in int x
7. Check if x is divisible by x or not. If divisible then print Evil Number
else print not an evil number
8. Stop
import java.util.Scanner;
public class EvilNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = in.nextInt();
if (n < 0) {
System.out.println("Invalid Input");
return;
}
int count = 0;
int p = 0;
int binNum = 0;
while (n > 0) {
int d = n % 2;
if (d == 1)
count++;
binNum += (int)(d * Math.pow(10, p));
p++;
n /= 2;
}
System.out.println("Binary Equivalent: " + binNum);
System.out.println("No. of 1's: " + count);
if (count % 2 == 0)
System.out.println("Output: Evil Number");
else
System.out.println("Output: Not an Evil Number");
}
}
6.Program to check whether a number is a kaprikar
number or not A positive whole number ‘n’ that has ‘d’
number of digits is squared and split into two pieces, a
right-hand piece that has ‘d’ digits and a left-hand piece
that has remaining ‘d’ or ‘d-1’ digits. If the sum of the two
pieces is equal to the number, then ‘n’ is a Kaprekar
number. The first few Kaprekar numbers
are: 9, 45, 297 …….. Example 1: 9 9 2 = 81, right-hand
piece of 81 = 1 and left hand piece of 81 = 8 Sum = 1 + 8
= 9, i.e. equal to the number. Hence, 9 is a Kaprekar
number. Sample input:45 Sample output:Kaprikar number
Algorithm Algorithm for input 1. Start 2. Create an object in of Scanner class 3.
Dispaly message to enter a number and take the input in n 4. Stop Algorithm for
check 1. Start 2. Declare an int variable sq =n x n, l=s.length() and k 3. If l%2==0
then split sq in two equal parts.Add them and store it in k 4. Else divide sq into
unequal parts and store it in k 5. If k==n then return true else return false 6. Stop
Algorithm for main 1. Start 2. Create an object ob of class kaprikar 3. Call input of
ob 4. Call check of ob and pass it to if statement 5. If check gives true then print
kaprikar number else print not a kaprekar number 6. StoP
import java.util.ArrayList;
public class KaprekarNumber {
public static boolean isKaprekar(int num) {
long squared = (long) num * num;
1. String squaredStr = String.valueOf(squared);
2. for (int i = 1; i < squaredStr.length(); i++) {
3. String firstPart = squaredStr.substring(0, i);
4. String secondPart = squaredStr.substring(i);
5. int first = (firstPart.isEmpty()) ? 0 : Integer.parseInt(firstPart);
6. int second = (secondPart.isEmpty()) ? 0 : Integer.parseInt(secondPart);
7. if (first + second == num) {
8. return true;
9. }
10. }
11. return false;
12. }
13. public static ArrayList<Integer> findKaprekarNumbers(int start, int end) {
14. ArrayList<Integer> kaprekarNumbers = new ArrayList<>();
15. for (int i = start; i <= end; i++) {
16. if (isKaprekar(i)) {
17. kaprekarNumbers.add(i);
18. }
19. }
20. return kaprekarNumbers;
21. }
22. public static void main(String[] args) {
23. int startRange = 1;
24. int endRange = 1000;
25. ArrayList<Integer> kaprekarNumbers = findKaprekarNumbers(startRange, end
Range);
26. System.out.println("Kaprekar Numbers between " + startRange + " and " + en
dRange + ":");
27. for (int number : kaprekarNumbers) {
28. System.out.println(number);
29. }
30. }
31.}
7. string is said to be a palindrome if it is the same if we
start reading it from left to right or right to left. In this
article, we will learn how to check if a string is a
palindrome in Java.
So let us consider a string “str”, now the task is just to
find out with its reverse string is the same as it is.
Example of Palindrome:
Input: str = “abba”
Output: Yes
Input: str = “geeks”
Output: No
Java Program to implement
// Basic Approach to check if
// string is a Palindrome
import java.io.*;
// Driver Class
class GFG {
// main function
public static boolean isPalindrome(String str)
{
// Initializing an empty string to store the reverse
// of the original str
String rev = "";
// Initializing a new boolean variable for the
// answer
boolean ans = false;
for (int i = str.length() - 1; i >= 0; i--) {
rev = rev + str.charAt(i);
}
// Checking if both the strings are equal
if (str.equals(rev)) {
ans = true;
}
return ans;
}
public static void main(String[] args)
{
// Input string
String str = "geeks";
// Convert the string to lowercase
str = str.toLowerCase();
boolean A = isPalindrome(str);
System.out.println(A);
}
}