You are on page 1of 167

Program 1

Write a program in java to find a pascal triangle

ALGORITHM
Step 1: Start
Step 2: Input the number of rows, 'n' for Pascal's Triangle
Step 3: Initialize a variable 'number' to 1
Step 4: Loop 'i' from 0 to 'n- 1'
Step 5: Loop 'j' from 0 to 'n- i - 1'
Step 6: Print spaces for formatting
Step 7: Loop 'j' from 0 to 'i'
Step 8: Print the current value of 'number' with appropriate formatting
Step 9: Update 'number' by multiplying it by (i - j) / (j + 1)
Step 10: Move to the next line to start the next row
Step 11: End of the outer loop
Step 12: End

PROGRAM CODE
VARIABLE DESCRIPTION TABLE
No. Name Type Method Description
1 br BufferedReader pascalw() BufferedReader object
2 n int pascalw() Input value
3 pas int[] pascalw() Matrix storing pascal numbers
4 i int pascalw() Loop variable
5 j int pascalw() Loop variable
OUTPUT

PROGRAM 2

To Display Entered Number in Words


ALGORITHM
Step 1: Start

Step 2: Input an integer number, 'num'

Step 3: Define three arrays: units, tens, and teens for storing word representations of numbers

units[1] = "One", units[2] = "Two", ..., units[9] = "Nine"

tens[1] = "Ten", tens[2] = "Twenty", ..., tens[9] = "Ninety"

teens[11] = "Eleven", teens[12] = "Twelve", ..., teens[19] = "Nineteen"

Step 4: Initialize an empty string 'wordRepresentation'

Step 5: If 'num' is 0, set 'wordRepresentation' to "Zero"

Step 6: If 'num' is greater than or equal to 100, do the following:

Step 7: Add 'units[num / 100]' followed by "Hundred" to 'wordRepresentation'

Step 8: Remove the hundreds place from 'num' (e.g., num = num % 100)

Step 9: If 'num' is not 0, add "and" to 'wordRepresentation'

Step 10: If 'num' is greater than or equal to 20, do the following:

Step 11: Add 'tens[num / 10]' to 'wordRepresentation'

Step 12: Remove the tens place from 'num' (e.g., num = num % 10)
Step 13: If 'num' is not 0, add a hyphen "-" to 'wordRepresentation'

Step 14: If 'num' is greater than 0 and less than 10, add 'units[num]' to 'wordRepresentation'

Step 15: If 'num' is greater than 10 and less than 20, add 'teens[num]' to 'wordRepresentation'

Step 16: 'wordRepresentation' as the word representation of the input number

Step 17: End

PROGRAM CODE

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 z int main() amt%10
3 g int main() amt/10
4 x String[] main() String array storing no.in words
5 x1 String[] main() String array storing no.in words
6 x2 String[] main() String array storing no.in words
7 amt int main() input number

OUTPUT

PROGRAM 3
To Display A.P. Series and Its Sum
ALGORITHM
Step 1: Start
Step 2: Input the first term 'a', common difference 'd', and the number of terms 'n' in the A.P.
Step 3: Initialize a variable 'sum' to 0 to store the sum of the A.P.
Step 4: Print "Arithmetic Progression Series:"
Step 5: Initialize a loop variable 'i' to 1
Step 6: While 'i' is less than or equal to 'n', do the following:
Step 7: Calculate the 'i'-th term of the A.P. as 'term = a + (i - 1) * d'
Step 8: Add 'term' to 'sum'
Step 9: Print 'term' as part of the A.P. series
Step 10: Increment 'i' by 1
Step 11: End of the loop
Step 12: Print "Sum of A.P.:" followed by the value of 'sum'
Step 13: End

PROGRAM CODE
VARIABLE DESCRIPTION TABLE
No. Name Type Method Description
1 a int - 1st term
2 d int - common difference
3 n int Sum(), showSeries(), nTHTerm() total terms
4 i int showSeries() loop variable

OUTPUT
PROGRAM 5
To Calculate Factorial Using Recursion
ALGORITHM
Step 1: Start

Step 2: Input an integer 'n' for which you want to calculate the factorial

Step 3: Create a recursive function 'calculateFactorial' that takes an integer 'n' as a parameter

Step 4: If 'n' is 0 or 1, return 1 (base case)

Step 5: Otherwise, return 'n' multiplied by the result of the 'calculateFactorial' function called with
'n - 1'

Step 6: Calculate the factorial by calling 'calculateFactorial(n)' and store the result in 'factorialResult'

Step 7: Print 'factorialResult' as the factorial of 'n'

Step 8: End

PROGRAM CODE
VARIABLE DESCRIPTION TABLE
No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 n int main() input number
3 obj Factorial main() Factorial object
4 f long main() variable storing factorial
5 n int fact() parameter in recursive function fact()

OUTPUT
PROGRAM 6
To Display Fibonacci Series Using
Recursion
ALGORITHM
Step 1: Start

Step 2: Input the number of terms 'n' for the Fibonacci series

Step 3: Create a recursive function 'fibonacci' that takes an integer 'n' as a parameter

Step 4: If 'n' is 0, return 0 (base case)

Step 5: If 'n' is 1, return 1 (base case)

Step 6: Otherwise, return the sum of 'fibonacci(n - 1)' and 'fibonacci(n - 2)'

Step 7: Initialize a loop variable 'i' from 0 to 'n - 1'

Step 8: Calculate the 'i'-th Fibonacci term using 'fibonacci(i)' and store it in 'fibTerm'

Step 9: Print 'fibTerm' followed by a space

Step 10: End of the loop

Step 11: End


PROGRAM CODE

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 obj Fibonacci main() Fibonacci object
3 n int main() input number
4 i int main() loop variable for Fibonacci element
5 f int main() Fibonacci element
6 n int fib() recursive function fib() parameter

OUTPUT
PROGRAM 7
To Calculate GCD Using
Recursion
ALGORITHM
Step 1: Start
Step 2: Input two positive integers 'a' and 'b'
Step 3: Create a recursive function 'calculateGCD' that takes two integers 'a' and 'b' as parameters
Step 4: If 'b' is 0, return 'a' as the GCD (base case)
Step 5: Otherwise, return the result of 'calculateGCD(b, a % b)' (recursive case)
Step 6: Calculate the GCD by calling 'calculateGCD(a, b)' and store the result in 'gcdResult'
Step 7: Print 'gcdResult' as the GCD of 'a' and 'b'
Step 8: End

PROGRAM CODE

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 p int main() ,calc() input number
3 q int main() ,calc() input number
4 obj GCD main() GCD object
5 g int main() variable storing the GCD
OUTPUT

PROGRAM 8
To Display Spiral Matrix.
ALGORITHM
Step 1: Start
Step 2: Input the number of rows 'm' and the number of columns 'n' for the matrix
Step 3: Initialize an 'm x n' matrix 'spiralMatrix' to store the result
Step 4: Initialize variables 'rowStart', 'rowEnd', 'colStart', and 'colEnd' to define the boundaries of the
current spiral
Step 5: Initialize a variable 'value' to 1, which will be used to populate the matrix
Step 6: While 'rowStart' is less than or equal to 'rowEnd' and 'colStart' is less than or equal to 'colEnd',
do the following:
Step 7: Loop from left to right along the top row within the boundaries (from 'colStart' to 'colEnd')
Step 8: Set 'spiralMatrix[rowStart][i]' to 'value'
Step 9: Increment 'value' by 1
Step 10: Increment 'rowStart' by 1
Step 11: Loop from top to bottom along the rightmost column within the boundaries (from
'rowStart' to 'rowEnd')
Step 12: Set 'spiralMatrix[i][colEnd]' to 'value'
Step 13: Increment 'value' by 1
Step 14: Decrement 'colEnd' by 1
Step 15: If 'rowStart' is less than or equal to 'rowEnd', do the following:
Step 16: Loop from right to left along the bottom row within the boundaries (from 'colEnd' to
'colStart')
Step 17: Set 'spiralMatrix[rowEnd][i]' to 'value'
Step 18: Increment 'value' by 1
Step 19: Decrement 'rowEnd' by 1
Step 20: If 'colStart' is less than or equal to 'colEnd', do the following:
Step 21: Loop from bottom to top along the leftmost column within the boundaries (from
'rowEnd' to 'rowStart')
Step 22: Set 'spiralMatrix[i][colStart]' to 'value'
Step 23: Increment 'value' by 1
Step 24: Increment 'colStart' by 1
Step 25: Print the 'spiralMatrix' as the spiral matrix
Step 26: End

PROGRAM CODE
ISCComputerScienceProject

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 a int[][] main() spiral matrix
3 r int main() l/2
4 c int main() r-1
5 k1 int main() stores 2
6 k2 int main() stores 3
7 p int main() loop gate
8 co int main() coloumn index
9 re int main() row index
10 l int main() dimensions of thr matrix
11 ri int main() right side matrix loop variable
12 le int main() left side matrix loop variable
13 dw int main() down side matrix loop variable
14 up int main() up side matrix loop variable
15 y int main() loop variable to print matrix
16 yy int main() loop variable to print matrix

out put

ISC Comput er Science Project

PROGRAM 9
To Display Magic Square
ALGORITHM
Step 1: Start
Step 2: Input an odd integer 'n' (e.g., 3, 5, 7) to determine the size of the magic square
Step 3: Create an 'n x n' matrix 'magicSquare' to store the magic square
Step 4: Initialize variables 'row' and 'col' to 0, the starting position
Step 5: Initialize a variable 'num' to 1, which will be used to populate the square
Step 6: Calculate the total number of cells in the square as 'n * n'
Step 7: Loop from 'num' to 'num' + 'totalCells' (inclusive)
Step 8: Set 'magicSquare[row][col]' to 'num'
Step 9: Increment 'num' by 1
Step 10: Calculate the next position by moving one row up and one column to the right:
- New 'row' = (row - 1 + n) % n
- New 'col' = (col + 1) % n
- If the cell is already filled, move one row down instead:
- If 'magicSquare[newRow][newCol]' is filled, set 'newRow' = (row + 1) % n
Step 11: Print 'magicSquare' as the magic square
Step 12: End

PROGRAM CODE
/*A Magic Square is a square whose sum of diagonal elements, row elements and coloumn
elements is the same*/ import java.io.*; class MagicSquare

{public static void main(String args[])throws Exception //main function

{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter the dimension of magical square="); int n =


Integer.parseInt(br.readLine()); //accepting dimensions int arr[][]=new int[n]
[n],c=n/2-1,r=1,num; for(num=1;num<=n*n;num++) //loop for finding magic
square elements
{r--;

c++; if(r==-1)
r=n-1; if(c>n-
1) c=0;

if(arr[r][c]!=0)
{r=r+2; c--;

arr[r][c]=num;
if(r==0&&c==0)
{r=n-1; c=1;

arr[r][c]=++num;

if(c==n-1&&r==0)

arr[++r][c]=++num;

System.out.println(); for(r=0;r<n;r++) //loop


displaying magic square

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

System.out.print(arr[r][c]+" ");

System.out.println();

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 n int main() input dimensions
3 arr int[][] main() magic square matrix
4 num int main() loop variable for magic square
5 r int main() row
6 c int main() coloumn

OUTPUT
PROGRAM 10
To Search an Array Using
Linear Search

ALGORITHM
Step 1: Start

Step 2: Input the array 'arr' and the element 'searchElement' to be found

Step 3: Initialize a variable 'found' to false

Step 4: Initialize a variable 'index' to -1

Step 5: Loop through the elements of 'arr' from the beginning to the end:

Step 6: If the current element is equal to 'searchElement', do the following:

Step 7: Set 'found' to true

Step 8: Set 'index' to the current index

Step 9: Break out of the loop

Step 10: Move to the next element

Step 11: If 'found' is true, print "'searchElement' found at index 'index'"

Otherwise, print "'searchElement' not found in the array"


Step 12: End

PROGRAM CODE

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader - BufferedReader object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int LinearSearch() parameter in constructor
6 v int search(), main() search element
7 flag int search() flag
8 obj LinearSearch main() LinearSearch object

out put
PROGRAM 11
To Search an Array Using
Binary Search

ALGORITHM
STEP 1 - START

STEP 2 - INPUT a[]

STEP 3 - FROM i=0 to i<n REPEAT STEP 4

STEP 4 - PRINT a[i]+" "

STEP 5 - flag=-1 , l=0, u=n-1

STEP 6 - IF(l<=u && flag=-1) REPEAT STEP 7 AND Step 8

STEP 7 - m = (l+u)/2

STEP 8 - IF (a[m] == v) THEN flag =m OTHERWISE GOTO STEP 9

STEP 9 - IF (a[m] < v) THEN l = m+1 OTHERWISE u =m-1

STEP 10 - IF (flag=-1) THEN GOTO STEP 11 OTHERWISE GOTO STEP 12

STEP 11 - PRINT “ not found”

STEP 12 - PRINT v+" found at position - "+flag

STEP 13 - END
PROGRAM CODE

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader - BufferedReader object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int BinarySearch() parameter in constructor
6 v int search(), main() search element
7 flag int search() flag
8 l int search() lower limit
9 u int search() upper limit
10 m int search() middle index
11 obj BinarySearch main() BinarySearch object

output
PROGRAM 12
To Sort an Srray Using
Selection Sort

ALGORITHM
Step 1: Start
Step 2: Input the array 'arr' to be sorted
Step 3: Get the length of the array 'n'
Step 4: Loop 'i' from 0 to 'n - 1'
Step 5: Assume the current element 'arr[i]' as the minimum
Step 6: Loop 'j' from 'i + 1' to 'n - 1'
Step 7: If 'arr[j]' is less than the current minimum 'arr[i]', do the following:
Step 8: Update the minimum to 'arr[j]'
Step 9: Swap 'arr[i]' with the minimum element found
Step 10: End of the outer loop
Step 11: The array is now sorted
Step 12: End

PROGRAM CODE
VARIABLE DESCRIPTION TABLE
No. Name Type Method Description
1 br BufferedReader input() BufferedReader object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int SelectionSort() parameter in constructor
6 j int sort() sort index
7 temp int sort() temporary storage
8 min int sort() minimum value
9 x SelectionSort main() SelectioSort object
OUTPUT
PROGRAM 13
To Sort an Array Using
Bubble Sort

ALGORITHM
Step 1: Start
Step 2: Input the array 'arr' to be sorted
Step 3: Get the length of the array 'n'
Step 4: Loop 'i' from 0 to 'n - 1'
Step 5: Loop 'j' from 0 to 'n - 1 - i'
Step 6: If 'arr[j]' is greater than 'arr[j+1]', do the following:
Step 7: Swap 'arr[j]' and 'arr[j+1]'
Step 8: End of the outer loop
Step 9: The array is now sorted in ascending order
Step 10: End

PROGRAM CODE
VARIABLE DESCRIPTION TABLE
No. Name Type Method Description
1 br BufferedReader input BufferedReader object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int BubbleSort() parameter in constructor
6 j int sort() sort index
7 temp int sort() temporary storage
8 x SelectionSort main() SelectionSort object

OUTPUT
PROGRAM 14
To Convert a Decimal no. Into its Binary
Equivalent

ALGORITHM
Step 1: Start

Step 2: Input a string 'inputString'

Step 3: Create a character array 'characters' to store unique characters from 'inputString'

Step 4: Initialize an integer array 'frequency' with the same length as 'characters' to store character
frequencies

Step 5: Initialize variables 'count' and 'length' to 0

Step 6: Loop through each character 'c' in 'inputString':

Step 7: If 'c' is not in 'characters', do the following:

Step 8: Add 'c' to 'characters'

Step 9: Set 'frequency[count]' to 1

Step 10: Increment 'count' by 1

Step 11: Otherwise, find the index of 'c' in 'characters' and increment the corresponding
'frequency' value by 1

Step 12: Set 'length' to 'count'


Step 13: Print the frequency of each unique character in 'inputString':

Step 14: Loop 'i' from 0 to 'length':

Step 15: Print "Character: 'characters[i]', Frequency: 'frequency[i]'"

Step 16: End

PROGRAM CODE

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader BufferedReader object

2 n int - array length


3 i int - loop variable
4 a[] int[] - array storing binary no.
5 nn int Dec2Bin() parameter in constructor
6 no int main(), dectobin() input number
7 temp int dectobin() temporary storage
8 c int dectobin() counter
9 obj Dec2Bin main() Dec2Bin object
out put

PROGRAM 15
To Display Date From Entered Day
Number
ALGORITHM
Step 1: Start
Step 2: Input the day number 'dayNumber' (e.g., 1 for January 1st, 365 for December 31st)
Step 3: Input the year 'year' (optional, if you want to consider leap years)
Step 4: Initialize an array 'daysInMonth' containing the number of days in each month
- daysInMonth[1] = 31, daysInMonth[2] = 28 (or 29 for leap years), daysInMonth[3] = 31, ...
Step 5: Initialize 'month' to 1
Step 6: While 'dayNumber' is greater than the number of days in the current month, do the following:
Step 7: Subtract the number of days in the current month from 'dayNumber'
Step 8: Increment 'month' by 1
Step 9: Update the number of days in the current month to the next month's value
Step 10: If 'month' is 2 and 'year' is a leap year, update 'daysInMonth[2]' to 29
Step 11: The 'dayNumber' now represents the day within the current month
Step 12: Print the date in the format 'day/month/year'
Step 13: End
PROGRAM CODE

No. Name Type Method Description


1 br BufferedReader - BufferedReader object
2 n int calc(), main() Day number
3 yr int calc(), main() year
4 a int[] calc() array storing day
5 m int[] calc() array storing month
6 t int calc() array index
7 s int calc() array index
8 d int calc() n+a[--s]+t
9 obj Day2Date main() Day2Date object
OUTPUT
PROGRAM 16
To Create a Star Pattern From Entered
String
PROGRAM CODE

No. Name Type Method Description


1 br BufferedReader main() BufferedReader object
2 s String main() input string
3 i int main() loop variable for printing the pattern
4 sp int main() Math.abs((l/2)-i)
5 j int main() loop variable for printing the pattern
6 k int main() loop variable for printing the pattern
7 l int main() length of string
OUTPUT
PROGRAM 17
To Check if Entered String is
Palindrome or Not

ALGORITHM
STEP 1 - START

STEP 2 - INPUT string s

STEP 3 - StringBuffer sb = s

STEP 4 - sb.reverse

STEP 5 - String rev = sb

STEP 6 - IF rev = s GOTO STEP 7 OTHERWISE GOTO STEP 8

STEP 7 - PRINT " Palindrome"

STEP 8 - PRINT " Not Palindrome"

STEP 9 – END

PROGRAM CODE
VARIABLE DESCRIPTION TABLE
No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 s String main() input string
3 sb StringBuffer main() StringBuffer object of s
4 rev String main() revese string
OUTPUT
PROGRAM 18
To Display a Frequency of
Each Character in Entered
String

ALGORITHM
Step 1: Start
Step 2: Input a string 'inputString'
Step 3: Remove spaces and convert 'inputString' to lowercase for case-insensitive comparison
Step 4: Create a character frequency map 'charFrequencyMap'
Step 5: Loop through each character 'c' in 'inputString':
Step 6: If 'c' is a letter (alphabet character):
Step 7: If 'c' is not a key in 'charFrequencyMap', add it with a value of 1
Step 8: If 'c' is already a key in 'charFrequencyMap', increment its value by 1
Step 9: Print the frequency of each unique character in 'inputString':
Step 10: Loop through the keys in 'charFrequencyMap':
Step 11: Print "Character: 'key', Frequency: 'value'"
Step 12: End
PROGRAM CODE

VARIABLE DESCRIPTION TABLE


No. Name Type Method Description
1 br BufferedReader main() BufferedReader object
2 i int - loop variable
3 a1 int - instance variable
4 l int - length of string
5 p int - instance variable
6 freq int - frequency of characters
7 ii int count() loop variable
8 a char count() character at index i
9 b char count() character at index ii
10 str String main() input string
11 x Frequency main() Frequency object

48 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 21
Write a Program in Java 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.

Algorithm

1. Start the program.

2. Read an integer input from the user.

3. Check if the number is fascinating:

1.
Concatenate the number, number multiplied by 2, and number multiplied by 3 into a single number.

49 | I S C C o m p u t e r S c i e n c e P r o j e c t
2. Count the frequency of each digit in the concatenated number.

3. Iterate through the digit counts from 1 to 9 and check if each digit count is equal to 1.

4. If any digit count is not equal to 1, return false.

5. If all digit counts are equal to 1, return true.

4. Print "Number is a fascinating number" if step 3 returns true.

5. Print "Number is not a fascinating number" if step 3 returns false.

6. End the program.

Solution

50 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output

51 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 22
Write a program in java to accept a number. Check and display whether it is a Bouncy
number or not.

Algorithm

1. Start the program.

2. Read an integer input from the user.

3. Initialize two boolean variables: `increasing` and `decreasing` to false.

4. Extract the last digit of the number and assign it to the variable `lastDigit`.

5. Remove the last digit from the number.

6. Repeat steps 7-10 until the number is greater than 0:

7. Extract the rightmost digit of the number and assign it to the variable `digit`.

8. Compare `digit` with `lastDigit`:

52 | I S C C o m p u t e r S c i e n c e P r o j e c t
- If `digit` is greater than `lastDigit`, set `increasing` to true.

- If `digit` is less than `lastDigit`, set `decreasing` to true.

9. Assign the value of `digit` to `lastDigit`.

10. Remove the rightmost digit from the number.

7. If both `increasing` and `decreasing` are true, the number is a bouncy number.

8. If either `increasing` or `decreasing` is true, but not both, the number is not a bouncy number.

9. Print "The number is a bouncy number" if it is a bouncy number.

10. Print "The number is not a bouncy number" if it is not a bouncy number.

11. End the program.

Solution

53 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output

54 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 23
.Write a program in Java to display all the triangular numbers from 3 to n, taking the
value of n as an input.

Algorithm:
1. Start the program.

2. Read an integer input `n`.

3. Print "Triangular numbers from 3 to n:".

4. Initialize a variable `i` with a value of 3.

5. Repeat steps
6-8 until `i` is less than or equal to `n`:
55 | I S C C o m p u t e r S c i e n c e P r o j e c t
6. Calculate the triangular number using the formula: `(i * (i + 1)) / 2`.

7. Print the calculated triangular number.

8. Increment `i` by 1.

9. End the program.

Solution

56 | I S C C o m p u t e r S c i e n c e P r o j e c t
output

57 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 24
Write a program in Java to enter a number and check whether it is a Smith
number or not.

Algorithm
Step 1: Start

Step 2: Input an integer 'number'

Step 3: Create a function 'getPrimeFactors' that takes an integer 'n' as input and returns a list of its prime factors

Step 4: Initialize a variable 'originalSum' to 0 to store the sum of digits in 'number'

Step 5: Initialize a variable 'sumOfPrimeFactors' to 0 to store the sum of digits in the prime factors

Step 6: Calculate the list of prime factors using 'getPrimeFactors'

Step 7: Loop through the prime factors and calculate the sum of digits for each factor and add it to
'sumOfPrimeFactors'

Step 8: Calculate the sum of digits in 'number' and store it in 'originalSum'

Step 9: If 'originalSum' is equal to 'sumOfPrimeFactors', print "'number' is a Smith number"

Otherwise,
print "'number' is not a Smith number"

58 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 10: End

Solution

59 | I S C C o m p u t e r S c i e n c e P r o j e c t
60 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output

61 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 25
.A unique-digit integer is a positive integer (without leading zeros) with no duplicates
digits. For example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are
not.
Given two positive integers m and n, where m < n, write a program to determine how
many unique-digit integers are there in the range between m and n (both inclusive) and
output them. The input contains two positive integers m and n. Assume m < 30000 and n
< 30000. You are to output the number of unique-digit integers in the specified range
along with their values in the format specified below:

Sample Input:

m = 100

n = 120

Sample Output:

The Unique-Digit integers are:

102, 103, 104,


105, 106, 107, 108, 109, 120.

62 | I S C C o m p u t e r S c i e n c e P r o j e c t
Frequency of unique-digit integers is : 9

Sample Input:

m = 2500

n = 2550

Sample Output:

The Unique-Digit integers are:

2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2517, 2530, 2519, 2530, 2531, 2534,
2536, 2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548, 2549.

Frequency of unique-digit integers is: 28.

Algorithm
Step 1: Start

Step 2: Input a list of numbers 'numbers'

Step 3: Create an empty HashMap 'numberCount'

Step 4: Loop through each number 'num' in 'numbers':

Step 5: If 'num' is not a key in 'numberCount', add it as a key with a value of 1

Step 6: If 'num' is already a key in 'numberCount', increment its value by 1

Step 7: Loop through the keys in 'numberCount':

Step 8: If the corresponding value is 1, print "The unique number is 'key'"

Step 9: End

Solution

63 | I S C C o m p u t e r S c i e n c e P r o j e c t
64 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output

65 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 26

Accept two positive integers 'm' and 'n', where m is less than n. Display the number of composite
magic integers that are in the range between m and n (both inclusive) and output them along with
frequency, in the format specified below:

Sample Input:
m=10 n=100
Output: The composite magic numbers are 10,28,46,55,64,82,91,100
Frequency of composite magic numbers: 8

Sample Input:
m=120 n=90
Output: Invalid input

Algorithm
66 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 1: Start

Step 2: Input a positive integer 'number'

Step 3: Repeat the following until 'number' is a single digit (1 to 9):

Step 4: Calculate the sum of the digits in 'number'

Step 5: Set 'number' to the sum calculated in Step 4

Step 6: If 'number' is a single digit (1 to 9), print "The number is a magic number."

Otherwise, print "The number is not a magic number."

Step 7: End

Solution

67 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output

68 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 27

69 | I S C C o m p u t e r S c i e n c e P r o j e c t
.Write a program to accept an even integer 'N' where N > 9 and N < 50. Find all the odd prime pairs
whose sum is equal to the number 'N'.

Algorithm
Step 1: Start

Step 2: Input an even integer 'N' such that N > 9 and N < 50

Step 3: Create a function 'isPrime' that checks if a number is prime

Step 4: Loop through odd numbers 'x' from 3 to N/2 (since the smallest prime pair is 3 and N should be even)

Step 5: Calculate 'y' as N - x

Step 6: Check if 'x' and 'y' are both prime using the 'isPrime' function

Step 7: If both 'x' and 'y' are prime, print the prime pair 'x' and 'y'

Step 8: End

Function isPrime(n):

Step 1: If n is less than 2, return false

Step 2: If n is equal to 2, return true

Step 3: If n is divisible by 2, return false

Step 4: Start a loop 'i' from 3 to the square root of n with a step of 2

Step 5: If n is divisible by 'i', return false

Step 6: If the loop completes, return true

Solution

70 | I S C C o m p u t e r S c i e n c e P r o j e c t
71 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output

72 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 28
Write a program to input a number. Use a function int Armstrong(int n) to accept the number.
The function returns 1, if the number is Armstrong, otherwise zero(0).

Solution

Output

73 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 29
Palindrome Number in Java: Write a program to accept a number from the user and check whether
it is a Palindrome number or not. A number is a Palindrome which when reads in reverse order is
same as in the right order.

ALGORITHM
Step 1: Start
Step 2: Input a positive integer 'num'
Step 3: Create a variable 'originalNum' to store the original number 'num'
Step 4: Create a variable 'reverseNum' and initialize it to 0
Step 5: Repeat the following until 'num' becomes 0:
Step 6: Calculate the last digit of 'num' and store it in 'lastDigit'
Step 7: Add 'lastDigit' to 'reverseNum' after multiplying 'reverseNum' by 10
Step 8: Remove the last digit from 'num'
Step 9: If 'originalNum' is equal to 'reverseNum', print "The number is a palindrome."
Otherwise, print "The number is not a palindrome."
Step 10: End

Solution

74 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description

Output

75 | I S C C o m p u t e r S c i e n c e P r o j e c t
}

PROGRAM 31
76 | I S C C o m p u t e r S c i e n c e P r o j e c t
A class Rearrange has been defined to modify a word by bringing all the vowels in the word at the beginning followed by the
consonants.
Example:
ORIGINAL becomes OIIARGNL
Some of the members of the class are given below:
Class name: Rearrange
Data Member/instance variable:
wrd: to store a word
newwrd: to store the rearranged word
Member functions/methods:
Rearrange(): default constructor
void readword(): to accept the word in UPPER case
vow freq_vow_con(): finds the frequency of vowels and consonants in
the word and displays them with an appropriate message
void arrange(): rearranges the word by bringing the vowels at the
beginning followed by consonants
void display(): displays the original word along with the rearranged
word
Specify the class Rearrange, giving the details of the constructor(), void
readword(), void freq _vow_con(), void arrange() and void display().
Define the main() function to create an object and call the functions
accordingly to enable the task.

77 | I S C C o m p u t e r S c i e n c e P r o j e c t
78 | I S C C o m p u t e r S c i e n c e P r o j e c t
Alogorithms
Algorithms for void main()
Step 1: START
Step 2: Create object of class and store it in a reference variable ob
Rearrange ob=new Rearrange();
Step 3: Call method readword.
Step 4: Call method freq_vow_con.
Step 5: Call method arrange.
Step 6: Call method display;
Step 7: END.

Algorithm for void freq_vow_con()


Step 1: START
Step 2: Declare variables int iß0 vß0 cß0 Char ch.
Step 3: Run a loop from iß0 till i<wrd.length() and repeat from Step 4 to Step 6
Step 4: chßwrd.charAt(i);
Step 5: Check if ch is a vowel
Step 6: if ch is a vowel then increment v by 1
vßv+1;
else increment c by 1
cßc +1
Step 7: Display v and c
Step 8:END

Algorithm for void arrange()


Step 1: START
Step 2: Declare variables int iß0 String vpß”” String cpß”” Char ch.
Step 3: Run a loop from iß0 till i<wrd.length() and repeat from Step 4 to Step 6
Step 4: chßwrd.charAt(i);
Step 5:
Check if ch is a vowel
79 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 6: if ch is a vowel then concatenate ch to variable vp
vpßvp+ch;
else concatenate ch to cp
cpßcp+ch;
Step 7: Concatenate vp and cp to variable newwrd
Newwrdßvp+cp;
Step 8:END

Algorithm for void display()


Step 1:START
Step 2:Display variable wrd and newwrd
System.out.println(“Original word “+wrd);
System.out.println(“New word “+newwrd);
Step 3:END

80 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table
Name of Datatype Purpose
variable
wrd To store a word input from the user
String
To store new word after
newwrd String rearrangement

i
int Loop variable to count from 0 till
the length of word

int To store the frequency of vowels in


v wrd.
c To store the frequency of consonants
int in
wrd.
ch char To temporarily store each character of
wrd

Output

81 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 32
Design a class Perfect to check if a given number is a perfect number or not. [A number is said to be perfect if sum of the
factors of the number excluding itself is equal to the original number].
Example: 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding
itself)
Some of the members of the class are given below:
Class name: Perfect
Data members/instance variables:
num: to store the number
Methods/Member functions:
Perfect (int nn): parameterized constructor to initialize the data
member num=nn .
int sum_of_factors(int i): returns the sum of the factors of the
number(num), excluding itself, using a recursive technique
void check(): checks whether the given number is perfect by invoking
the function sum_of_factors() and displays the result with an
appropriate message.
Specify the class Perfect giving details of the constructor(), int
sum_of_factors(int) and void check(). Define a main() function to create
an object and call the functions accordingly to enable the task.

82 | I S C C o m p u t e r S c i e n c e P r o j e c t
83 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms
Algorithm for main method
Step 1: START
Step 2: Declare variable int nß0.
Step 3: Input a number from the user and store it n.
Step 4: Create an object of class Perfect and pass the value of n in its constructor.
Perfect ob=new Perfect(n);
Step 5: Call method check
ob.check();

Step 6: END
Algorithm for void check()

Step 1: START
Step 2: Pass the value ‘1’ in method sum_of_factors() and check what it is returning.
If sum_of_factors returns the same value as num then display num is a Perfect
number else display num is not a perfect number.
If(num==sum_of_factors(1))
System.out.println(num+” is a Perfect number”);
else
System.out.println(num+” is not a Perfect number”);
Step 3:END
Algorithm for int sum_of_factors(int i)

Step 1:START
Step 2:if the value of I is greater than num/2 then return 0.
If(i>num/2) return 0

Step 3:If the reamainder on dividing num by i is 0 then return i+sum_of_factors(i+1) else

return sum_of_factors(i+1).

84 | I S C C o m p u t e r S c i e n c e P r o j e c t
If(num%i==0)
Return i+sum_of_factors(i+1);
else
return sum_of_factors(i+1);
Step 3:END

85 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table
Name of Datatype Purpose
variable
To store a number entered by the user.
int
num

i int
To temporarily store the factors of num one
by one.
n int To take input of a number from the
user

Output

86 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 33
A disarium number is a number in which the sum of the digits to the power of their respective
position is equal to the number itself.
Example: 135 = 1^1 + 3^2 + 5^3 Hence, 135 is
a disarium number.
Design a class Disarium to check if a given number is a disarium number or not. Some of the
members of the class are given below:
Class name: Disarium
Data members/instance variables:
int num: stores the number
int size: stores the size of the number
Methods/Member functions:
Disarium (int nn): parameterized constructor to initialize the data members n = nn and size = 0
void countDigit(): counts the total number of digits and assigns it to size
int sumofDigits (int n, int p): returns the sum of the digits of the number(n) to the power of their
respective positions (p) using recursive technique
void check(): checks whether the number is a disarium number and displays the result with an
appropriate message
Specify the class Disarium giving the details of the constructor! ), void countDigit(), int
sumofDigits(int, int) and void check(). Define the mainO function to create an object and call the
functions accordingly to enable the task.

87 | I S C C o m p u t e r S c i e n c e P r o j e c t
88 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithm for main method
Step 1: START

Step 2:Input a number from the user and store it in a variable ‘n’

Step 3:Create an object of the class Disarium and store it in a reference variable ob
Disarium ob=new Disarium();

Step 4:Call the methods countDigit() and check() using ‘ob’


Step 5:END

Algorithm for void countDigit()


Step 1:START

Step 2: Declare variables n and c.

Step 3:Initialize n with the value of num nßnum


Step 4:Run a loop till n>0 and repeat step 5 to 6
Step 5:Increment c by 1 cßc+1;

Step 6:Divide n by 10 nßn/10.

Step 7:Initialize size with the value of c sizeßc


Step 8:END

Algorithm for int sumofDigit(int n,int p)


Step 1:START

Step 2:if the value of p is 0 then return 0 else return (int)Math.pow(n%10,p)+sumofDigit(n/10,p-1).


if(p==0) return 0;

else return (int)Math.pow(n%10,p)+sumofDigit(n/10,p-1);


Step 3:END

Algorithm for void check()


Step 1:START

Step 2:Pass the value of num and size to method sumofDigits and if it returns the same value as num then
print num is a Disarium number else print num is not a disarium number

if(num==sumofDigit(num,size))

89 | I S C C o m p u t e r S c i e n c e P r o j e c t
System.out.println(num+” is a Disarium number”);
else

System.out.println(num+” is not a Disarium number”);


Step 3:END

90 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table
Name of Name of Name of
varible variable
variable
num int To store a number entered by the user
size int To store the number of digits in num
n int To input a number from the user and temporarily
store it
c int To count and temporarily store the number of digits
in num.

Output

91 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 34
A class Palin has been defined to check whether a positive number is a Palindrome number or not.
The number ‘N’ is palindrome if the original number and it’s
reverse are the same.
Some of the members of the class are given below:
Class name: Palin
Data members/instance variables:
num: integer to store the number
revnum: integer to store the reverse of the number
Methods/Member functions:
Palin(): constructor to initialize data members with legal initial
values
void accept(): to accept the number
int reverse(int y): reverses the parameterized argument ‘y’
and stores it in revenue using a recursive technique
void check(): checks whether the number is a Palindrome by
invoking the function reverse() and display the result with
an appropriate message
Specify the class Palin giving the details of the constructor (),
void accept(), int reverse(int) and void check(). Define the
main() function to create an object and call the functions
accordingly to enable the task.

92 | I S C C o m p u t e r S c i e n c e P r o j e c t
93 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithm for main method
Step 1:START
Step 2: Create an object of class Palin and store it in a reference variable ‘ob’ Palin ob=new Palin();
Step 3:Call methods accept() and check() using ‘ob’. ob.accept();
ob.check();
Step 4:END

Algorithm for void accept()


Step 1:START
Step 2:Input a number from the user and store it in a variable num. Step 3:END.

Algorithm for int reverse(int y)


Step 1:START
Step 2:Check If y is 0 then return the value of revnum else initialize revnumßrevnum*10+y%10. and call
method reverse and pass the value y/10
If(y==0)
return revnum else{
revnum=revnum*10+y%10; return
reverse(y/10);
}
Step 3:END

Algorithm for void check()


Step 1:START
Step 2:Pass the value of num to method reverse and store the value it returns to variable revnum.
Revnum=reverse(num);
Step 3:if the value of revnum is same as the value of num then print num is a palindromw number else display
num is not a palindrome number.
Step 4:END

94 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table
Name of Datatype Purpose
variable

num int To store a number entered by the


user.

revnum int To store the reverse of variable num.

y int Local variable to temporarily store the


value of num.

Output

95 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 35
Design a class ArmNum to check if a given number is an Armstrong number or not. [A number is said
to be Armstrong if sum of its digits raised to the power of length of the number is equal to the number]
Example:
371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus, 371, 1634 and 54748 are all examples of Armstrong numbers. Some of the members
of the class are given below:
Class name: ArmNum
Data members/instance variables:
n: to store the number
1: to store the length of the number
Methods/Member functions:
ArmNum (int nn): parameterized constructor to initialize the data member n = nn int sum_pow(int i):
returns the sum of each digit raised to the power of the length of the number using recursive
technique eg., 34 will return 32 + 42 (as the length of the number is 2)
void isArmstrong(): checks whether the given number is an Armstrong number by invoking the
function sum_pow () and displays the result with an appropriate message.
Specify the class ArmNum giving details of the constructor( ), int sum_pow(int) and void
isArmstrong( ). Define a main() function to create an object and call the functions accordingly to
enable the task.

96 | I S C C o m p u t e r S c i e n c e P r o j e c t
97 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithm for Question 5

Algorithm for main method


Step 1:START

Step 2:Declare variable int n1ß0

Step 3:Input a number from the user and store it in variable n1.

Step 4:Create an object of class ArmNum and pass n1 in its constructor


ArmNum ob=new ArmNum(n1)

Step 5:Call method isArmstrong() using object ob.


ob.isArmstrong().

Step 6:END

Algorithm for int sum_pow(int i)


Step 1:START

Step 2:if the value of I is 0 then return 0 else return Math.pow((i%10),l)+sum_pow(i/10).


if(i==0) return 0
else
return (int)Math.pow((i%10),l+sum_pow(i/10).

Step 3:END

Algorithm for void isArmstrong()


Step 1:START

Step 2:Declare variables int cnßn,cß0.

Step 3:Run a loop till cn>0 and repeat from step 4 to step 5
Step 4:increment c by 1
cßc+1

Step 5:cnßcn/10.

Step 6:Initialize l with the value of c.


lßc

98 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 7:Pass the value of n to method sum_pow and if it returns the same value as n then print n is an
Armstrong number else print n is not an Armstrong number.
If(sum_pow(n)==n)
System.out.println(n+” is an Armstrong number”);
else
System.out.println(n+” is not an Armstrong number”);

Step 8:END

99 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of Datatype Purpose


variable
n1 int To enter a number from the
user.
n int To store a number entered by
the
user
l int To store the number of digits in
n.
cn int Local variable to store the value
of
n.
c int Local variable to count the
number
of digits in n.

Output

100 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 36
A class Capital has been defined to check whether a sentence has words beginning with a capital letter or not.
Some of the members of the class are given below:
Class name: Capital
Data member/instance variable:
sent: to store a sentence
freq: stores the frequency of words beginning with a capital letter
Member functions/methods:
Capital () : default constructor
void input (): to accept the sentence
boolean isCap(String w): checks and returns true if the word
begins with a capital letter, otherwise returns false
void display(): displays the sentence along with the frequency of
the words beginning with a capital letter
Specify the class Capital, giving the details of the constructor( ),
void input( ), boolean isCap(String) and void display( ). Define
the main( ) function to create an object and call the functions
accordingly to enable the task.

101 | I S C C o m p u t e r S c i e n c e P r o j e c t
102 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms
Algorithm for main method

Step 1:START
Step 2:Create an object of class Capital
Capital ob=new Capital();
Step 3:Call methods input and display
Step 4:END

Algorithm for void input()

Step 1:START
Step 2:Input a sentence from the user and store it in variable sent.
Step 3: Add an extra space to variable sent
sentßsent+” “;
Step 4:END

Algorithm for Boolean isCap(String w)

Step 1:START
Step 2:If character at first index of w is uppercase then return true else return false
If(Character.isUpperCase(w.charAt(0))
Return true
Return false
Step 4:END

Algorithm for void display()

Step 1:START
Step 2:Declare variables String wß””,char ch,int iß0,fß0
Step 3:Run a
loop from i=0 till i<sent.length() and repeat from step 4 to Step 4:Extract
each character from w and store it in ch
103 | I S C C o m p u t e r S c i e n c e P r o j e c t
chßsent.charAt(i);
Step 5:Check if character extracted is blank space or not
Step 6:if charcter extracted is blank space then call isCap(w) and if it returns true then increment f by 1 else
concatenate w with ch
Step 7:wß””
If(ch==’ ‘){
If(isCap(w)==true)
f=f+1;
w=””
}
else
w=w+ch.
Step 8:Display the orginal sentence
System.out.println(“The original sentence is “+sent);
Step 9:Display the frequency of words beginning with capital letters
System.out.println(“The Frequency of words beginning with capital letters is “+f);
Step 4:END

104 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of variable Datatype Purpose


sent String To input and store a
sentence from the user.
freq int To store the number of
words beginning with
capital letters.

w String To temporarily store


each word of the
sentence.

ch char To store each word of


the sentence one by
one.

f int To count the number of


words beginning with
uppercase.

i Int Loop variable to count


from 0 till the length of
the string.

Output

105 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 37
A sequence of Fibonacci strings is generated as follows:
S0 = “a”, SF = “b”, Sn = S(n-1) + S(n-2) where ‘+’ denotes concatenation. Thus the sequence is:
a, b, ba, bab, babba, babbabab,……. n terms.
Design a class FiboString to generate Fibonacci strings. Some of the members of the class are given
below:
Class name: FiboString
Data members/instance variables:
x: to store the first string
y: to store the second string
z: to store the concatenation of the previous two strings n: to store
the number of terms
Member functions/methods:
FiboString(): constructor to assign x=”a”, y=”b” and z=”ba” void accept(): to
accept the number of terms ‘n’
void generate(): to generate and print the Fibonacci strings. The sum of (‘+’ ie concatenation) first
two strings is the third string. Eg. “a” is first string, “b” is second string then the third will be “ba”,
and fourth will be “bab” and so on.
Specify the class FiboString, giving details of the constructor(), void accept() and void generate().
Define the main() function to create an object and call the functions accordingly to enable the task.

106 | I S C C o m p u t e r S c i e n c e P r o j e c t
107 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms
Algorithm for main method
Step 1:START

Step 2:Create an object of class FiboString and call methods accept and generate FiboString ob=new
FiboString();
ob.accept();
ob.generate();

Step 3:END

Algorithm for void accept()

Step 1:START

Step 2:Input the number of terms from the user and store it in variable n. Step 3:END

Algorithm for void generate()

Step 1:START

Step 2:Declare variables int iß0 Step


3:Display the value of x and y

System.out.print(x+”,”+y);

Step 4:Run a for loop from i=1 till i<=n-2 and repeat from steps 5 to step 6 Step 5:Display the value
of z

System.out.print(z+”,”);

Step 6:Initialize xßy ,yßz and zßy+x Step 7:END

108 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of variable Datatype Purpose


n int To store the number of
terms of fibonnaci
series.

x String To store the first term of


the series
y String To store the second term
of the series
z String To store each term of the
series upto the nth term.
i int Loop variable to ocunt
from 1 till n-2

Output

109 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 38
A class SeriesSum is designed to calculate the sum of the following series:
Sum=x^2/1!+x^4/3!+x^6/5!+…x^n/(n−1)!
Some of the members of the class are given below:
Class name: SeriesSum
Data members/instance variables:
x: to store an integer number n: to
store the number of terms
sum: double variable to store the sum of the series Member
functions:
SeriesSum(int xx, int nn): constructor to assign x=xx and n=nn
double find fact(int m): to return the factorial of m using the recursive technique. double find
power(int x, int y): to return x raised to the power of y using the recursive technique.
void calculate(): to calculate the sum of the series by invoking the recursive functions
respectively
void display(): to display the sum of the series
(a) Specify the class SeriesSum, giving details of the constructor(int, int), double find fact(int),
double find power(int, int), void calculate() and void display(). Define the main() function to create an
object and call the functions accordingly to enable the task.

110 | I S C C o m p u t e r S c i e n c e P r o j e c t
Solution of question 8

111 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms

Algorithm for main method

Step 1:START
Step 2:Declare variables int xx,nn.

Step 3:Take the input of x and n from the user and store it in xx and nn.
Step 4:Create an object of class SeriesSum and call methods calculate() and display()
SeriesSum ob=new SeriesSum(xx,nn);
ob.calculate();
ob.display();
Step 5:END
Algorithm for find_fact(int m)

Step 1:START
Step 2:If value of m is 0 or 1 then return 1 else return m*find_fact(m-1)
if(m==1)
return 1
else
return m*find_fact(m-1);
Step 3:END
Algorithm for find_power(int x1,int y1)

Step 1:START

Step 2:if value of y1 is 1 then return x1 otherwise return x1*find_power(x1,y1-1);


If(y1==1)
return x1
else
return x1*find_power(x1,y1-1);
Step 3:END
Algorithm for
void calculate()

112 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 1:START
Step 2:declare int iß0
Step 3:Run a loop from i=0 till i<=n and repeat from step 4 to step
Step 4:sumßsum+find_power(x1,i)/find_fact(i-1);

Step 4:END.

113 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of the variable Datatype Purpose


nn int Local variable to input
the number of terms
from the user.

xx int Local variable to input


the value of x from the
user.

n int To store the number of


terms.
x int To store the value of x.
sum double To store the sum of the
series.

Output

114 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 39
A class ConsChange has been defined with the following details: Class name: ConsChange
Data members/instance variables:
word: stores the word len: stores the length of the word
Member functions/methods:
ConsChange(): default constructor
void readword(): accepts the word in lowercase
void shiftcons(): shifts all the consonants of the word at the beginning
followed by the vowels (e.g. spoon becomes spnoo)
void changeword(): changes the case of all occurring consonants of the
shifted word to uppercase, for e.g. (spnoo becomes SPNoo)
void show(): displays the original word, shifted word and the changed
word
Specify the class ConsChange giving the details of the constructor ),
void readword ( ), void shiftcons (), void changeword() and void
show(). Define the main() function to create an object and call the
functions accordingly to enable the task.

115 | I S C C o m p u t e r S c i e n c e P r o j e c t
116 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms
Algorithm for main method
Step 1:START

Step 2:Create an object of class ConsChange and call methods readword() and show()
ConsChange ob=new ConsChange();

ob.readword();
ob.show();

Step 3:END

Algorithm for void readword()


Step 1:START

Step 2:Enter the word from the user in lowercase and store it in variable word.
Step 3:Store the length of the word in variable len

Len=word.length();
Step 4:END

Algorithm for void shiftcons()


Step 1:START

Step 2:Declare variables int I,char ch ,String vpß””,cpß””.


Step 3:Run a for loop from i=0 till i<len and repeat from step 4 to step 6.
Step 4:Extract each character from word from ith index and store it in ch
ch=word.charAt(i);

Step 5:Check if ch is a vowel or not.If ch is a vowel then concatenate ch with vp otherwise concatenate ch with cp.

Step 6:Increment i by 1
ißi+1;

Step 7:initialize wordßcp+vp.


Step 8:END

Algorithm for void changeword()


Step 1:START

Step 2:Declare variables int I,char ch ,String wßword; and initialize wordß””.
Step 3:Run a for loop from i=0 till i<len and repeat from step 4 to step\

Step 4:Extract each character from word from ith index and store it in ch
ch=word.charAt(i);

Step 5:Check if ch is a vowel or not.If ch is a vowel then concatenate ch with word otherwise change ch to uppercase
and then concatenate it with word

117 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 6:Increment i by 1
ißi+1;

Step 7:initialize wordßcp+vp.


Step 8:END

Algorithm for void show()


Step 1:START

Step 2:Display the original word

System.out.println(“Original word “+word);


Step 3:Call method shiftcons();

Step 4: :Display the shifted word

System.out.println(“Shifted word “+word);


Step 5:Call method changeword();

Step 6: :Display the Changed word

System.out.println(“Changed word “+word);


Step 7:END.

118 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of variable Datatype Purpose


word String To store the word
entered by the user.
len int To store the length of
word.
i int Loop variable to count
from 0 till the end of the
length of string.

ch char To temporarily store


each character of word.
vp String To store all the vowels of
word.
cp String To store all the
consonants of word.

Output

119 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 40
A happy number is a number in which the eventual sum of the square of the digits of the number is equal to 1.
Example:
28 = (2)2 + (8)2 = 4 + 64 = 68
68 = (6)2 + (8)2 = 36 + 64 = 100
100 = (1)2 + (0)2 + (0)2 = 1 + 0 + 0 = 1
Hence, 28 is a happy number.
Example:
12 = (1)2 + (2)2 = 1 + 4 = 5
Hence, 12 is not a happy number.
Design a class Happy to check if a given number is a happy number.
Some of the members of the class are given below:
Class name: Happy
Data Members/instance variables:
n: stores the number Member functions
Happy( ): constructor to assign 0 to n.
void getnum (int nn): to assign the parameter value to the number n =
nn.
int sum_sq_digits (int x): returns the sum of the square of the digits of
the number x, using the recursive technique
void is happy (): checks if the given number is a happy number by
calling the function sum_sq_digits(int) and displays an
appropriate message.
Specify the class Happy giving details of the constructor (), void getnum
(int), int

120 | I S C C o m p u t e r S c i e n c e P r o j e c t
sum_sq_digits (int) and void ishappy (). Also define a main ( s) function
to create an object and call the methods to check for happy number.

121 | I S C C o m p u t e r S c i e n c e P r o j e c t
122 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms
Algorithm for main method
Step 1:START
Step 2:Input a number from the keyboard and store it in a variable num.
Step 3:Create an object of class Happy and call methods getnum() and isHappy()
Happy ob=new Happy();
ob.getnum(num);
ob.isHappy();
Step 4:END

Algorithm for void getnum(int nn)


Step 1:START
Step 2:Initialize nßnn;
Step 3:END.

Algorithm for int sum_sq_digits(int x)


Step 1:START
Step 2:Check if x is 0.If x is 0 then return 0 otherwise return x%10*x
%10+sum_sq_digits(x/10).
if(x==0)
return 0;
return x%10*x%10+sum_sq_digits(x/10).
Step 3:END.

Algorithm for void getnum(int nn)


Step 1:START
Step 2:Declare variable and initialize it with the value of n
int sumßn;
Step 3:Run a loop till sum>9 and repeat step 4.
Step
4:Initialize sumßsum_sq_digits(sum);

123 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 5:Check if the value of sum is 1.If the value of sum is 1 then display n is a magic
number .
if(sum==1)
System.out.println(n+” is a magic number”);
else
System.out.println(n+” is a magic number”);
Step 3:END.

124 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of the variable Datatype Purpose


num int To input a number from
the user.
n int To store the number
entered by the user.
sum int To store the sum of
square of each digit.

Output

125 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 41
Input a sentence from the user and count the number of times, the words “an” and “and” are present in the sentence.
Design a class Frequency using the description given below:
Class name: Frequency
Data members/variables:
text: stores the sentence
countand: to store the frequency of the word “and”
countan: to store the frequency of the word “an”
len: stores the length of the string
Member functions/methods:
Frequency(): constructor to initialize the instance variables
void accept(String n): to assign n to text, where the value of the
parameter n should be in lower case.
void checkandfreq(): to count the frequency of “and”
void checkanfreq(): to count the frequency of “an”
void display(): to display the number of “and” and “an” with
appropriate messages.
Specify the class Frequency giving details of the constructor(), void
accepts(String), void checkandfreq(), void checkanfreq() and void
display(). Also, define the main() function to create an object and call
methods accordingly to enable the task.

126 | I S C C o m p u t e r S c i e n c e P r o j e c t
127 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms

Algorithms for main method


Step 1:START
Step 2:Input a string from the user and store it in a variable s.
Step 3:Create an object of class Frequency and call all the methods
Frequency ob=new Frequency();
Ob.accept(s);
Ob.checkandfreq();
Ob.checkanfreq();
Ob.display();
Step 4:END

Algorithm for void checkandfreq()


Step 1:START
Step 2:Declare variables int iß0,char ch,String s1ß””.
Step 3:Run a loop from i=0 till i<text.length() and repeat from step 4 to step
Step 4:Extract each character from text and store it in variable ch.
ch=text.charAt(i);
Step 5:Check if ch is a blank space or not.If ch is a black space then check if String s1 is equal to and
or not.If s1 is and then increase the value of countand by1 otherwise concatenate ch with s1
if(ch==’ ‘){
if(s1.trim().equals(“and”))
countand++;
s1=””;
}else
s1=s1+ch;
Step 6:END.

Algorithm for void checkandfreq()


Step 1:START
Step 2:Declare variables int iß0,char ch,String s1ß””.
Step 3:Run a loop from i=0 till i<text.length() and repeat from step 4 to step
Step 4:Extract each character from text and store it in variable ch.
ch=text.charAt(i);

128 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 5:Check if ch is a blank space or not.If ch is a black space then check if String s1 is equal to “an”
or not.If s1 is then increase the value of countan by1 otherwise concatenate ch with s1
if(ch==’ ‘){
if(s1.trim().equals(“an”))
countan++;
s1=””;
}else
s1=s1+ch;
Step 6:END.

Algorithm for void display()


Step 1:START
Step 2:Display the frequerncy of “and” and “an”.
System.out.println(“Frequency of and is “+countand);
System.out.println(“Frequency of an is “+countan);
Step 3:END.

129 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of the variable Datatype Purpose


s String To input a string from
the user.
text String To store the string
entered by the user.
countand int To store the number of
“and” in sentence text.
countan int To store the number of
“and” in sentence text.
ch char To store each character
of text one by one
i int Loop variable to count
from 0 to less than the
length of text.

Output

130 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 42
Input a word in uppercase and check for the position of the first occurring vowel and perform the
following operation.
(i) Words that begin with a vowel are concatenated with
“Y”. For example, EUROPE becomes EUROPEY.
(ii) Words that contain a vowel in-between should have the first part
from the position of the vowel till the end, followed by the part of the
string from beginning till the position of the vowel and is concatenated
by “C”.
For example, PROJECT becomes OJECTPRC.
(iii) Words which do not contain a vowel are concatenated with
“N”. For example, SKY becomes SKYN.
Design a class Rearrange using the description of the data members and member functions given
below:
Class name: Rearrange
Data Members/instance variables:
Txt: to store a word
Cxt: to store the rearranged word len: to
store the length of the word Member
functions:
Rearrange (): constructor to initialize the instance variables void readword ():
to accept the word input in UPPER CASE
void convert (): converts the word into its changed form and stores it in string Cxt
void display(): displays the original and the changed word
Specify the class Rearrange giving the details of the constructor (), void readword ( ), void convert
() and void display (). Define a main () function to create an object and call the function accordingly
to enable the task.

131 | I S C C o m p u t e r S c i e n c e P r o j e c t
Solution of question 12

132 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithms
Algorithm for main method

Step 1:START
Step 2:Create an object of class Rearrange and call methods readword() ,convert() and
display().
Rearrange ob=new Rearrange();
ob.readword();
ob.convert();
ob.display();
Step 3:END

Algorithm for void convert()

Step 1:START
Step 2:Declare and initialize variables int i,pß-1,char ch,String fpß””,spß””.
Step 3:Run a loop from i=0 till i<len and repeat step 4 to step
Step 4:Extract each character of txt and store it in variable ch.
ch=txt.charAt(i);
Step 5:Check if ch is a vowel or not.If ch is a vowel set pßi and break the loop
Step 6:Check the value of p.
if(p==0)
cxt=txt+”Y”;
else if(p>0&&p<len-1){
fp=txt.substring(p);
sp=txt.substring(0,p);
cxt=fp+sp+”C”;
}
else
cxt=txt+”N”.
Step 7:END

Algorithm for void display()

133 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 1:START
Step 2:Display txt and cxt
System.out.println(“Original word “+txt);
System.out.println(“Changed word “+txt);
Step 3:END

134 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description table

Name of the variable Datatype Purpose


txt String To store a word from the
user.
len int To store the length of the
word.
cxt String To store the new word
after rearranging
ch char To store each character
of txt one by one.
i int Loop variable to count
from 0 till len-1.
fp String To store the first
rearranged part
sp String To store the second
rearranged part

Output

135 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 43
Take a number n and print the sum of factorials upto
the number using inheritance
ALGORITHM
Step 1: Start
Step 2: Input a positive integer 'n'
Step 3: Initialize a variable 'sum' to 0 to store the sum of the series
Step 4: Initialize a variable 'factorial' to 1
Step 5: Loop 'i' from 1 to 'n':
Step 6: Calculate 'factorial' as the factorial of 'i'
Step 7: Add 'factorial' to 'sum'
Step 8: Print 'sum' as the sum of the series
Step 9: End

Solution

136 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description

Output

137 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 44
Design a class to accept a day number between 1 and 366, year(in 4- digits) from
the user, to generate and display corresponding date. Also accept M(1 <= M <=
100) from the user to compute and display the future date corresponding to M
days after the generated date. Display an error message if the value of a day
number, year and M are not within the limit or not according to the conditions
specified . Paste your program with the following data.
INPUT: Day number
= 255 Year = 2018
Date after N days
= 22 OUTPUT:
Date = 12th September,2018
Date after 22 days = 4th October, 2018

ALGORITHM
Step 1: Start
Step 2: Check in which month the input days belong to, and accordingly print the
day accompanied by month using nested if in the function leap_year()
Step 3: Check in which month the input days belong to, and accordingly print the
day accompanied by month using nested if in the function normal_year()

138 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 4: Start of main()
Step 5: Declare d, d1, M, year, c, y
Step 6: Take the 3 inputs- year, day number and number of further
days. Step 7: Count the number of digits in the year input by the
user.
Step 8: Check the validity of inputs inserted by the
user Step 9: Create an object for the class
Step 10: Check the validity of leap year
Step 11: If the year is a leap year, then call the function
leap_year() Step 12: If it is not a leap year, then call the
function normal_year() Step 13: End of main
Step 14: End.

139 | I S C C o m p u t e r S c i e n c e P r o j e c t
Solution

140 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable description
Output

141 | I S C C o m p u t e r S c i e n c e P r o j e c t
142 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 45
Base Class: Number

Data Members: int n

Member Functions:

Number() – non-parameterized constructor

Void accept() – to accept the number

Void display() – to display the number

Int sof() – which returns the sum of factors recursively

Int nof() – which returns the number of factors recursively

Derived Class 1: Prime

Data Members: boolean res

Member Functions:

Prime() – non-parameterized constructor

Void display() – to display the number

Void main()

ALGORITHM
STEP 1 DECLARE AN INSTANCE VARIABLE N OF TYPE INT, INITIALIZED TO 0

STEP 2 DEFINE A CONSTRUCTOR NUMBER()

SET N = 0

STEP 3 DEFINE A METHOD ACCEPT()

CREATE A SCANNER OBJECT SC TO READ INPUT FROM THE CONSOLE

PRINT "ENTER A NUMBER"

READ AN INTEGER FROM THE USER USING SC AND STORE IT IN N

STEP 4 DEFINE A METHOD DISPLAY()

PRINT N

143 | I S C C o m p u t e r S c i e n c e P r o j e c t
DEFINE A METHOD NOF() TO CALCULATE THE NUMBER OF FACTORS OF N

IF N IS EQUAL TO 1, RETURN 1

INITIALIZE F TO 2 (SINCE EVERY NUMBER HAS AT LEAST 2 FACTORS, 1 AND ITSELF)

USE A LOOP TO ITERATE FROM I = 2 TO N/2

IF N IS DIVISIBLE BY I, INCREMENT F

RETURN THE VALUE OF F

STEP 5 DEFINE A METHOD SOF() TO CALCULATE THE SUM OF FACTORS OF N

IF N IS EQUAL TO 1, RETURN N

INITIALIZE SUM TO N + 1

USE A LOOP TO ITERATE FROM I = 2 TO N/2

IF N IS DIVISIBLE BY I, ADD I TO SUM

RETURN THE VALUE OF SUM

STEP 6 STOP

144 | I S C C o m p u t e r S c i e n c e P r o j e c t
145 | I S C C o m p u t e r S c i e n c e P r o j e c t
146 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 46
Write a program to declare a square matrix A[][] of order (M x M)
where ‘M’ is the number of rows and number of columns such that
‘M’ should be greater than 2 and less than 10.Accept the value of M
as user to input.Display an appropriate message for an invalid
input.Allow the user to input integers into the matrix. Perform the
following tasks:
(a)Display the original/input matrix.
(b)Check if the given matrix is symmetric or not.
A square matrix is said to be symmetric ,if the element of the ith row
and jth column is equal to the element of the jth row and ith column.
(c)Find the sum of the elements of the left diagonal and the sum of the
elements of the right diagonal of the matrix and display them.
Test your program for the following sample data and some random
data.
EXAMPLE 1:
INPUT: M=3
123
245
356
OUTPUT: ORIGINAL MATRIX:
123
245
356
THE GIVEN MATRIX IS SYMMETRIC.
THE SUM OF THE LEFT DIAGONAL=11
THE SUM OF THE RIGHT DIAGONAL=10
EXAMPLE 2:
INPUT: M=14
OUTPUT: SIZE IS OUT OF RANGE

Algorithm:
Step 1: Start

147 | I S C C o m p u t e r S c i e n c e P r o j e c t
Step 2: Declare I, j, n, ld, rd, f
Step 3: Take an input
Step 4: Check if n>2 and n<10
Step 5: Initialize f=0, ld=0, rd=0
Step 6: Declare two 2-D arrays of size n*n
Step 7: Take the input of elements of array
Step 8: Display the input elements in matrix form
Step 9: Run two for loops i and j, both less than n
Step 10: If i=j, add ld to the element and store it in the same.
Step 11: If i+j=n-1, add the element to rd and store it in the same.
Step 12: b[j][i]=a[i][j]
Step 13: Again run two for loops, i and j, both till n
Step 14: if any element of both the arrays is unequal, the f=1 and break
Step 15: Check if f=0, the print THE GIVEN MATRIX IS SYMMETRIC.
Step 16: Else print THE MATRIX IS UNSYMMETRIC.
Step 17: Print THE SUM OF LEFT DIAGONAL=, and the value of ld
Step 18: Print THE SUM OF RIGHT DIAGONAL=, and the value of rd
Step 19: if n<2 || n>10, then print SIZE IS OUT OF RANGE.
Step 20: End

148 | I S C C o m p u t e r S c i e n c e P r o j e c t
149 | I S C C o m p u t e r S c i e n c e P r o j e c t
150 | I S C C o m p u t e r S c i e n c e P r o j e c t
151 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 47
Write a program to accept a sentence as input. The words in the string
are to be separated by a blank. Each word must be in uppercase. The
sentence is terminated by either “.”, “!”, “?”
Perform the following tasks:
(a)Obtain the length of the sentence (measured in words).
(b)Arrange the sentence in alphabetical order of words.
Test your program with the following data values and also some
random values.
EXAMPLE 1:
INPUT: NECESSITY IS THE MOTHER OF INVENTION.
OUPUT: LENGTH: 6
REARRANGED SENTENCE:
INVENTION IS MOTHER NECESSITY OF THE.

Algorithm:
Step 1: Start
Step 2: Declare st1, st, a[], ch, p, i, j, k, l, f
Step 3: Take an input
Step 4: Convert the string input by the user into uppercase and count the length of
the string
Step 5: Extract the last character and check the validity of the input.
Step 6: Declare the array a[]
Step 7: Declare f and k to 0
Step 8: Run a for loop, i=0, i<l, i++
Step 9: Extract each character in ch and if it is not = ‘ ‘, then st1=st1+ch
Step 10: If the above condition is false, then increment the value of f, a[k]=st1,
increment the value of k and empty the variable st1
Step 11: Print LENGTH: , and the value stored in f
Step 12: Sort the elements in ascending order using bubble sort technique
Step 13: Print REARRANGED SENTENCE:
Step 14: Run a for loop i=0, i<k i++
Step 15: Print the array
Step :16: End.

152 | I S C C o m p u t e r S c i e n c e P r o j e c t
153 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output:

154 | I S C C o m p u t e r S c i e n c e P r o j e c t
155 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 48
Write a program using text file concept for the following:
(a)Create a text file “data.txt” to input and store N sentences.
(b)Read the above text file to read sentences and print the sentence
and number of words in each sentence.
Print the output in two columns.

Algorithm:
Step 1: Start
Step 2: Create a text file "data.txt"
Step 3: Create a FileWriter stream type object fw
Step 4: Link fw with a BufferedWriter object bw
Step 5: Now link the BufferedWriter object with a PrintWriter object outFile
Step 6: DECLARE N and take its value from the user
Step 7: Now write N sentences from the program with PrintWriter object
Step 8: Close the stream chain by using close() with the stream objects
Step 9: Create a FileReader stream type object file
Step 10: Link the FileReader object with a BufferedReader object fileInput
Step 11: Now read the text using readLine() from fileInput and store in the variable
text
Step 12: Count the number of words in text and store in c
Step 13: Display text and c
Step 14: Close the file
Step 15: End
Code:

156 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output:

157 | I S C C o m p u t e r S c i e n c e P r o j e c t
158 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 49
The computer department of the agency of international Espionage is
trying to decode intercepted messages .The Agencies have determined
that the enemy and quotes messages by first converting all characters
to their ASCII values and then reversing the string. For example,
consider A-z ( the underscore is just to highlight the space). The
ASCII values of A, <space>, z are 6532122; then reverse this to get
2212356 as the coded language.
Write a program which reads a coded message and decode it. The
coded message will not exceed 200 characters it will contain only
alphabets (A....Z and a....z) and spaces. ASCII values of A.....Z are
65.....90 and those of a.....z are 97.....122).
Test your program for the following data and some random data.
EXAMPLE:
INPUT: Encoded message
2312179862310199501872 379231018117927
OUTPUT: The Decoded Message is:
Have A Nice Day

159 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithm:
Step 1: Start
Step 2: Declare I, k, l, str1, str2, str3, str4, ch, chr
Step 3: Take an input
Step 4: Calculate the length of the input
Step 5: Run a for loop and reverse the string input by user
Step 6: Declare i=0
Step 7: Run a while loop from i till n
Step 8: if ch>1, then extract the even indexes and convert it to character data type
Step 9: If the above condition is false then extract the odd indexes and convert it to
character data type
Step 10: str4= str4+chr
Step 11: Print The original encoded message:
Step 12: Print the value of str1
Step 13: Print The decoded message.
Step 14: Print the value of str4
Step 15: End
Code:

160 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output:

161 | I S C C o m p u t e r S c i e n c e P r o j e c t
162 | I S C C o m p u t e r S c i e n c e P r o j e c t
Program 50
:
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.

Example 2: 45
45^2 = 2025, right-hand piece of 2025 = 25 and left-hand piece of
2025=20. Sum=25+20=45, i.e. equal to the number.

Example 3: 297
297^2=88209, right hand piece of 88209=209 and left hand piece of
88209=88. Sum=209+88=297, i.e. equal to the number.
Given two positive integers p and q, where p<q.Write a program to
determine how many Kaprekar numbers are there in the range
between p and q (both inclusive) and output them.
The input contains two positive integers p and q. Assume p<5000 and
q<5000. You are to output the number of Kaprekar numbers in the
specified range along with their values in the format specified below:

EXAMPLE:
INPUT: p=1 , q=1000
OUTPUT: THE KAPREKAR NUMBERS ARE:-
1,9,45,55,99,297, 703,999
FREQUENCY OF KAPREKAR NUMBERS IS: 8

163 | I S C C o m p u t e r S c i e n c e P r o j e c t
Algorithm:
Step 1: Start
Step 2: Declare N, x, n, sq, m, I, n1, lp, rp, f, d, c
Step 3: Initialize c and x to their default values
Step 4: Take the inputs of upper and lower limits
Step 5: Check the validity of the inputs
Step 6: Run a for loop, i=N, i<=m
Step 7: Initialise n to I;
Step 8: sq= n*n
Step 9: Initialise n1 and c to sq and 0 respectively
Step 10: Run a while loop and count the number of digits in n1
Step 11: check if c is even than lp =c/2, else lp=c/2 +1
Step 12: Make the square of right piece and left piece and add and store
them f
Step 13: Check if f and n are equal then increment x and print i
Step 14: If the limits are invalid then print INVALID INPUT.
Step 15: End.

164 | I S C C o m p u t e r S c i e n c e P r o j e c t
165 | I S C C o m p u t e r S c i e n c e P r o j e c t
166 | I S C C o m p u t e r S c i e n c e P r o j e c t
ACKNOWLEDGEMENT

I would like to express my special thanks of gratitude to my teacher as well as our


principal who gave me the golden opportunity to do this wonderful project on the
topic (Write the topic name), which also helped me in doing a lot of Research and i
came to know about so many new things I am really thankful to them.

Secondly i would also like to thank my parents and friends who helped me a lot in
finalizing this project within the limited time frame.

167 | I S C C o m p u t e r S c i e n c e P r o j e c t

You might also like