Professional Documents
Culture Documents
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
Step 3: Define three arrays: units, tens, and teens for storing word representations of numbers
Step 8: Remove the hundreds place from 'num' (e.g., num = num % 100)
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'
PROGRAM CODE
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 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 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 6: Otherwise, return the sum of 'fibonacci(n - 1)' and 'fibonacci(n - 2)'
Step 8: Calculate the 'i'-th Fibonacci term using 'fibonacci(i)' and store it in 'fibTerm'
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
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
out put
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
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;
{for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
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 5: Loop through the elements of 'arr' from the beginning to the end:
PROGRAM CODE
out put
PROGRAM 11
To Search an Array Using
Binary Search
ALGORITHM
STEP 1 - START
STEP 7 - m = (l+u)/2
STEP 13 - END
PROGRAM CODE
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 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 11: Otherwise, find the index of 'c' in 'characters' and increment the corresponding
'frequency' value by 1
PROGRAM CODE
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
ALGORITHM
STEP 1 - START
STEP 3 - StringBuffer sb = s
STEP 4 - sb.reverse
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
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.
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.
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
4. Extract the last digit of the number and assign it to the variable `lastDigit`.
7. Extract the rightmost digit of the number and assign it to the variable `digit`.
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.
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.
10. Print "The number is not a bouncy number" if it is not a bouncy number.
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.
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`.
8. Increment `i` by 1.
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 3: Create a function 'getPrimeFactors' that takes an integer 'n' as input and returns a list of its prime factors
Step 5: Initialize a variable 'sumOfPrimeFactors' to 0 to store the sum of digits in the prime factors
Step 7: Loop through the prime factors and calculate the sum of digits for each factor and add it to
'sumOfPrimeFactors'
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:
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:
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.
Algorithm
Step 1: Start
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 6: If 'number' is a single digit (1 to 9), print "The number is 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 4: Loop through odd numbers 'x' from 3 to N/2 (since the smallest prime pair is 3 and N should be even)
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 4: Start a loop 'i' from 3 to the square root of n with a step of 2
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.
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
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 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
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
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
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
Step 3:Input a number from the user and store it in variable n1.
Step 6:END
Step 3:END
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.
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
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
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
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
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
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
Step 1:START
Step 2:Input the number of terms from the user and store it in variable n. Step 3:END
Step 1:START
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+”,”);
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
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
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
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
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
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
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 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;
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
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
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
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
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.
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
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
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
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
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
Member Functions:
Member Functions:
Void main()
ALGORITHM
STEP 1 DECLARE AN INSTANCE VARIABLE N OF TYPE INT, INITIALIZED TO 0
SET N = 0
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
IF N IS DIVISIBLE BY I, INCREMENT F
IF N IS EQUAL TO 1, RETURN N
INITIALIZE SUM TO N + 1
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
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