0% found this document useful (0 votes)
9 views

Computer Project (1)

Uploaded by

gowreeshlall69
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Computer Project (1)

Uploaded by

gowreeshlall69
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Acknowledge

ment
I would like to express my sincere gratitude
to all these individuals for mentoring and
supporting me in completing this Project.

My Teacher Mrs. Jyotsna Pant , for providing


me with invaluable insights and direction.

Our esteemed principal Mrs.Neena Uniyal,


for fostering an environment of learning and
creativity within our school.

To my parents, their constant


encouragement, patience, and
understanding have been the pillars of my
success.

I am grateful to my friends who contributed


ideas and perspectives that enriched the
project.
1
Thank you everyone for shaping this project
and enhancing my learning experience.
Index
S.No Topic Page
No.
1. Program 4
1
2. Program 7
2
3. Program 10
3
4. Program 14
4
5. Program 17
5
6. Program 20
6
7. Program 22
7
2
8. Program 25
8
9. Program 28
9
10. Program 31
10
11. Program 33
11
12. Program 38
12
13. Program 39
15
14. Program 41
16
15. Program 42
17
16. Program 43
18
17. Program 45
20

3
18. Program 47
21
19. Program 49
22
20. Program 51
23
22. Program 53
24
23. Program 54
25(i)
24. Program 56
25(ii)
25. Program 58
27
26. Program 59
28
27. Bibliogra 61
phy

4
Program 1:

5
Output:

6
Algorithms:-
1. Input Reading:

 Read the range limits p and q from the user.

2. Initialization:

 Initialize variables:
o freq to count the number of Kaprekar numbers found.
o i to iterate through each number in the range.
o d, n, a, b, s for various calculations.

3. Iterate Through Range:

 Use a for loop to iterate through each


number i from p to q.

4. Calculate Number of Digits:

 Initialize n to i and d to 0.
 Use a while loop to count digits in i (n /= 10 and
increment d until n becomes 0).

5. Calculate Square and Split Digits:

 Compute s = i * i, the square of i.


 Calculate a as the last d digits of s (a = s %
(int)Math.pow(10, d)).
 Calculate b as the remaining digits of s (b = s /
(int)Math.pow(10, d)).

6. Check for Kaprekar Number:

 If a + b == i, then i is a Kaprekar number:


o Print i.
o Increment freq to count it.

7. Output Results:

 After the loop, print all Kaprekar numbers found.


 Print the total count (freq) of Kaprekar numbers found.

7
Program 2:

8
Output:

9
Algorithms:
1. Input Reading:

 The program reads an integer input n from the user, expecting


it to be less than 1000.

2. Range Check:

 If the input number n is 1000 or greater, the program


prints "OUT OF RANGE" and terminates.

3. Digit Extraction:

 Three variables a, b, and c are used to store the hundreds,


tens, and ones digits of the number n.
 a = n / 100 extracts the hundreds place.
 n = n % 100 reduces n to its last two digits for further
processing.
 b = n / 10 extracts the tens place.
 c = n % 10 extracts the ones place.

4. Mapping Numbers to Words:

 Arrays ones, teens, and tens are used to map digits and
combinations of digits to their word representations.
 For example, ones[0] is "one", teens[0] is "eleven",
and tens[0] is "ten".

5. Constructing the Output:

 Depending on the values of a, b, and c, appropriate strings


(h, t, o) are populated with the corresponding words.
 Special cases are handled, such as numbers like 11-19 and the
number 10.

6. Formatting the Result:

 The final result string result is constructed based on whether


the number is in the hundreds, tens, or ones place, and
formatted correctly with spaces and "and" where necessary.

7. Output:

10
 The final result is printed in uppercase
using result.toUpperCase().

Program 3:

11
12
13
14
Algorithms:
1. Input Dimensions of Matrices:

 Prompt the user to enter the number of rows (row1, row2) and
columns (col1, col2) for two matrices (A and B).

2. Requirement Check for Multiplication:

 Verify if the number of columns of matrix A (col1) is equal to


the number of rows of matrix B (row2). If not, print "Matrix
multiplication is not possible" and exit.

3. Matrix Initialization:

 Declare three matrices:


o a[][]: Matrix A with dimensions [row1][col1].
o b[][]: Matrix B with dimensions [row2][col2].
o c[][]: Resultant matrix C of multiplication with
dimensions [row1][col2].

4. Input Values of Matrices:

 Prompt the user to input values for matrix A and store them
in a[][].
 Prompt the user to input values for matrix B and store them
in b[][].

5. Matrix Multiplication:

 Perform matrix multiplication using nested for loops:


o Initialize each element c[i][j] of matrix C to zero.
o Use another loop (k) to calculate the dot product of
corresponding rows from A and columns from B,
updating c[i][j] accordingly.

6. Compute Transpose of Matrix C:

 Declare another matrix transpose[][] with dimensions [col2]


[row1] to store the transpose of C.
 Use nested loops to swap rows and columns:
o Assign transpose[i][j] = c[j][i] to achieve the transpose.

7. Output Results:

15
 Print the original matrix C before transpose.
 Print the transposed matrix transpose.

Program 4:

16
17
Output:

18
Algorithms:
1. Class Declaration:

 Define a class piglatin.

2. Instance Variables:

 word: Stores the input sentence.


 newword: Stores the sentence converted to Pig Latin.
 index: Flag to check if a vowel is found.

3. Constructor:

 Initialize word and newword to empty strings ("").

4. Input Method (input):

 Use Scanner to get user input for a sentence.


 Convert the input sentence to uppercase to simplify vowel
checks.

5. Conversion Method (convert):

 Iterate through each character (ch) in word.


 Check if ch is one of the vowels (A, E, I, O, U).
 If a vowel is found:
o Construct newword by moving characters from the vowel
onwards to the end of the word, followed by characters
before the vowel, and append "AY".
o Set index to 1 to indicate a vowel was found and conversion
is done.
o Break out of the loop.

6. Output Method (output):

 If no vowel was found (index == 0):


o Simply append "AY" to word to form newword.
 Print the original format of the word (word).
 Print the word in Pig Latin format (newword).

7. Main Method (main):

 Create an instance of the piglatin class.


 Call input, convert, and output methods in sequence to perform
the conversion and display the results

19
Program 5:

20
Output:

21
Algorithms:
1. Class Declaration:

 Define a public class sortParagraph.

2. Instance Variables:

 None explicitly declared. All operations are performed within methods.

3. Method: countWords(String s):

 Input: Takes a string s (representing a sentence).


 Process: Uses StringTokenizer to split the sentence into words based on
delimiters (" .,?!").
 Output: Returns the count of words in the sentence.

4. Method: sort(String w[], int p[]):

 Inputs:
o w[]: Array of sentences.
o p[]: Array of integers representing the number of words in each
sentence.
 Process: Implements a nested loop bubble sort to sort sentences (w[])
based on their corresponding word counts (p[]).
 Output: Calls printResult(w, p) to print the sorted sentences along with
their word counts.

5. Method: printResult(String w[], int p[]):

 Inputs:
o w[]: Array of sentences.
o p[]: Array of integers representing the number of words in each
sentence.
 Process: Iterates through w[] and p[] arrays and prints each sentence
followed by its word count.

6. Main Method (public static void main(String args[])):

 Inputs:
o Uses Scanner to input a paragraph (pg).
 Process:
o Splits the input paragraph into sentences
using StringTokenizer based on sentence-ending punctuation
(“.?!”).
o Counts the number of sentences (count).
o If the number of sentences (count) is greater than 10, prints “invalid
entry”.
o Otherwise, initializes arrays sent[] for sentences and p[] for word
counts:

22
 Iterates through each sentence, trims leading and trailing
spaces, and counts words using countWords() method.
 Calls sort(sent, p) to sort sentences based on word counts
and prints the sorted result.

Program 6:

23
Output:

Algorithms:
1. Class Declaration:

 Define a class repeat.

2. Instance Variables:

 None explicitly declared. All operations are performed


within the main method.

3. Main Method (public static void main(String args[])):

 Inputs:
o Use Scanner to input a word (wrd).
 Process:
o Append a space to the end of wrd to simplify the loop
logic.
o Initialize finalwrd to an empty string to store the
resulting word after removing repeated characters.
o Get the length of the word (len).
o Iterate through each character (ch) in the word:
 Access the next character (ch2).
 Compare ch with ch2.
 If they are not equal, append ch to finalwrd.
o This effectively removes consecutive repeated
characters.
 Output:
o Print the resulting word (finalwrd) after removing
repeated consecutive characters.

24
Program 7

25
26
27
Output:

28
Program 8

29
Output:

30
Algorithms:
1. Class Declaration:

 Define a class bouncy.

2. Main Method (public static void main(String


args[])):

 Inputs:
o Use Scanner to input an integer (num).
 Process:
o Print the given number.
o Check if the number is less than 100. If so,
print "The number is not a bouncy number" and
exit.
o Initialize two boolean variables:
 increasing: Indicates if the digits of the
number are in increasing order.
 decreasing: Indicates if the digits of the
number are in decreasing order.
o Use a loop to iterate through each digit of the
number:
 Track the last digit (prev) and compare it
with the current digit (temp % 10).
 Update increasing to false if any digit is
greater than the previous one.
 Update decreasing to false if any digit is less
than the previous one.
 Reduce temp by removing the last digit
(temp /= 10).
o Print "The number is a bouncy number" if
neither increasing nor decreasing is true,
indicating that the number is neither strictly
increasing nor decreasing.
o Otherwise, print "The number is not a bouncy
number".

31
Program 9:

32
33
Output:

34
Algorithms:
1. Class Declaration:

 Define a public class deleteword1.

2. Main Method (public static void main(String args[])):

 Inputs:
o Use Scanner to input a sentence (str), a word to be
deleted (wrd), and its position (pos).
 Process:
o Initialize variables:
 i: Loop variable.
 count: To count words in the sentence.
 len: Length of the input sentence.
 pos: Position of the word to be deleted.
 ch: Current character being processed.
 last: Last character of the input sentence.
 str1: String to store the modified sentence.
 wrd: Word to be deleted.
 wrd1: Temporary string to store current word
being read.
o Check if the last character of the input sentence (last)
is a valid punctuation ('.', '?', '!'). If not, print "INVALID
INPUT." and exit.
o Convert the entire sentence (str) to uppercase to
handle case insensitivity.
o Prompt the user for the word to be deleted (wrd) and
its position (pos).
o Initialize a loop to iterate through each character (ch)
in the sentence:
 If ch is a space (' '), or punctuation ('.', '?', '!'), it
indicates the end of a word:
 Compare wrd1 (current word) with wrd (word
to be deleted) and check if count equals pos.
 If wrd1 matches wrd and count matches pos,
skip adding wrd1 to str1 (effectively deleting
the word).
 Otherwise, concatenate wrd1 followed
by ch to str1.
 Reset wrd1 and increment count.

35
 If ch is not a space or punctuation,
concatenate ch to wrd1 (build the current word).
o Print the modified sentence (str1).

Program 10:

Output:
36
Algorithms:
1. Class Declaration:

 Define a class happynumber.

2. Main Method (public static void main(String


args[])):

 Inputs:
o Use Scanner to input an integer (num), which is
the number to be checked for being a happy
number.
 Process:
o Initialize variables:
 num: Input number provided by the user.
 sum: Variable to store the sum of the
squares of digits.
 n: Temporary variable initialized to num.
o Print a prompt to enter a happy number.
o Do-While Loop:
 Start a do-while loop to repeatedly calculate
the sum of squares of digits (sum):
 Initialize sum to 0.
 While Loop:
 Iterate through each digit
of n until n becomes 0:
 Calculate the last digit
of n using n % 10 and store it
in d.
 Compute the square of d (d*d)
and add it to sum.
 Update n by removing the last
digit (n = n / 10).

37
Set n to sum after completing the inner
while loop.
 Continue this process until n is not
greater than 6.
o Result Evaluation:
 Check if sum equals 1 after exiting the loop:
 If true, print that num is a happy
number.
 Otherwise, print that num is not a happy
number.

38
Program 11:

39
40
Output:

41
Algorithms:
1. Class Definition:

 Define a class named ST.


 Declare private instance variables:
o inputString: Stores the user-inputted string.
o vowelsMatrix: 2D array to store vowels in a
square matrix.
o vowelsCount: Count of vowels found in the input
string.

2. Input Function (inputString):

 Create a Scanner object to read input from the user.


 Prompt the user to enter a string.
 Store the entered string in the inputString variable.

3. Find and Count Vowels (findAndCountVowels):

 Initialize vowelsCount to 0.
 Convert inputString to lowercase for case-insensitive
comparison.
 Traverse through each character of lowerCaseString.
 If the character is a vowel ('a', 'e', 'i', 'o', 'u'),
increment vowelsCount.

4. Adjust Vowels Count (findAndCountVowels):

 If vowelsCount is odd, increment it by 1 to ensure


even count of vowels.

5. Arrange Vowels in Matrix


(arrangeVowelsInMatrix):

 Calculate size as vowelsCount / 2, which determines


the dimension of the square matrix.
 Initialize vowelsMatrix as a size x size 2D character
array.
 Convert inputString to lowercase.

42
 Initialize index to 0 for accessing characters
in lowerCaseString.
 Use nested loops to fill vowelsMatrix row-major wise:
o Find the next vowel character in lowerCaseString.
o Store the vowel in vowelsMatrix[i][j].
o Break the loop once a vowel is found and stored.

6. Display Matrix (displayMatrix):

 Print a message indicating the vowels are arranged in


a square matrix.
 Traverse through vowelsMatrix and print each
character followed by a space.
 Print a newline after printing each row of the matrix.

7. Main Function (main):

 Create an instance of ST class (obj).


 Invoke inputString method to input the string.
 Invoke findAndCountVowels to count vowels.
 Invoke arrangeVowelsInMatrix to
populate vowelsMatrix.
 Invoke displayMatrix to print the arranged matrix of
vowels.

43
Program 12

44
Output:
Program 15
45
46
Output:

47
Program 16

Output:

48
Program 17

Output:

49
Program 18

50
Output:

51
Program 20

52
Output:

53
Program 21

54
Output

55
Program 22

56
Output:

57
Program 23

58
Output:

59
Program 24

Output:

60
Program 25(i)

61
Output:

62
Program 25(ii)

63
Output:

64
Program 27

Output:

65
Program 28

66
Output:

67

You might also like