Você está na página 1de 57

COMPUTER SCIENCE

PRACTICAL

RIK HALDAR
CLASS XI-A
ROLL NO : 16

YEAR : 2018-2019
Question 1

Write a program to input a natural number and print whether it is a smith


number or not.

A smith number is a composite number whose sum of digits is equal to the


sum of the digits of it’s prime factors.

Example:

Number=666

Sum of digits=6+6+6=18

Sum of digits of prime factors: 2+3+3+ (3+7) = 18

Sum of digits=Sum of digits of prime factors

Hence, 666 is a smith number.


Algorithm
1. START

2. DEFINE A CLASS Smith

3. INPUT THE NUMBER TO BE CHECKED FROM THE USER AND STORE IT IN VARIABLE num

4. COPY THE NUMBER AND STORE IT IN ANOTHER VARIABLE n1

5. INITIALIZE THE VARIABLES d1 AND digsum

6. START A while LOOP AND RUN IT UNTIL n1=0, EVALUATING THE SUM OF THE DIGITS AND
STORING THEM IN VARIABLE digsum

7. COPY THE NUMBER TO VARIABLE n AND INITIALIZE THE VARIABLES d, sum=0, c, k

8. START A LOOP l=2 TO n

9. INITIALIZE A VARIABLE i=l

10. CHECK IF n%i=0 THEN

11. INITIALIZE VARIABLE k=1

12. START A LOOP for j=2 TILL I AND CHECK IF i%j=0 THEN

13. INCREMENT c BY 1

14. END OF INNER FOR LOOP

15. CHECK IF k=1 AND c=2 THEN

16. START A while LOOP AND RUN IT UNTIL i=0 EVALUATING SUM OF DIGITS OF i AND STORING
THEM IN sum

17. STORE THE VALUE OF (n/i) IN VARIABLE n

18. REINITIALIZE k=0 AND c=0

19. ELSE INCREMENT l BY 1

20. END OF if

21. END OF OUTER for LOOP

22. CHECK IF sum=digsum THEN

23. DISPLAY “SMITH NUMBER”

24. ELSE DISPLAY “NOT SMITH NUMBER”

25. END OS CLASS AND ALGORITHM


PROGRAM CODE
VARIABLE DESCRIPTION TABLE

Method Name Variable Datatype Function


sc Scanner Scans a line of text from
display() keyboard
num integer Stores the number
inputted
n1 integer Stores the copied
number
d1 integer Stores digits of the
number
digsum integer Stores sum of digits of
the number
n integer Stores copied number
d integer Stores digits of prime
factors
sum integer Stores sum of digits of
prime factors
c integer Counter variable
k integer Counter variable
i integer Stores prime factors of
the number
l, j integer Loop variables
INPUT AND OUTPUT

INPUT: Enter a number (composite)

666

Output: 666 is a smith number

INPUT: Enter a number (composite)

22

Output: 22 is a smith number

INPUT: Enter a number (composite)

24

Output: 24 is not a smith number

INPUT: Enter a number (composite)

4937775

Output: 4937775 is a smith number

INPUT: Enter a number (composite)

49

Output: 49 is not a smith number


Question 2

A positive natural number can be represented as follows:

N=5

1+1+1+1+1

1+1+1+2

1+2+2

1+1+3

1+4

2+3

Where each row represents a combination of positive natural numbers,


which add up to N. The combination 1+4 and 4+1 have the same meaning so
only one combination is taken. Write a program which inputs a natural
number between 2 to 20 and prints the possible combinations which when
added gives N.
Algorithm

1. START

2. DEFINE A CLASS NUMBER

3. INPUT THE NUMBER FROM THE USER AND STORE IT IN VARIABLE num

4. CHECK IF THE NUMBER IS WITHIN THE RANGE OF 2 TO 20 THEN

5. CALL THE RECURSIVE FUNCTION partitions (int, int, String) WITH PARAMETERs AS num, 1, “”

6. WITHIN THE RECURSIVE FUNCTION, CHECK IF N=0 THEN

7. STORE THE LAST INDEX OF THE CHARACTER ‘+’ IN THE VARIABLE pos

8. DISPLAY THE STRING sofar WITHIN THE INDEX POSITIONS OF 0 TO pos

9. END OF IF

10. ELSE START A for LOOP FROM max TO N

11. IN THE LOOP, RECALL THE FUNCTION partitions(int, int, String) WITH PARAMETERS AS N-i, i,
sofar + i + ””

12. END OF for LOOP

13. END OF ELSE

14. END OF RECURSIVE FUNCTION partitions (int, int, String)

15. END OF OUTER IF

16. ELSE DISPLAY “NUMBER OUT OF RANGE”

17. END OF CLASS

18. END OF ALGORITHM


PROGRAM CODE

VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


sc Scanner Scans a line of
main(String args[]) text from
keyboard
num integer Stores the
inputted number
N integer Copies the
partitions(int, int, number
max integer Counter
String)
sofar String Stores the
combinations
pos integer Stores last index
of plus character
i integer Loop variable
INPUT AND OUTPUT

Enter a number between 2 and 20

1+1+1+1+1

1+1+1+2

1+1+3

1+2+2

1+4

2+3

Enter a number between 2 and 20

1+1+1+1+1+1+1+1

1+1+1+1+1+1+2

1+1+1+1+1+3

1+1+1+1+2+2

1+1+1+1+4

1+1+1+2+3

1+1+1+5

1+1+2+2+2

1+1+2+4

1+1+3+3
1+1+6

1+2+2+3

1+2+5

1+3+4

1+7

2+2+2+2

2+2+4

2+3+3

2+6

3+5

4+4

Enter a number between 2 and 20

1+1+1

1+2

Enter a number between 2 and 20

21

Number out of range


Question 3

For adding two binary numbers, the following rules are applied:

0+0=0

0+1=1

1+ 0 = 1

1 + 1 = 10

Write a program to enter two decimal numbers greater than 0, convert them
to their binary equivalent numbers and add them to print their sum in the
binary form.
Algorithm
1. START

2. DEFINE A CLASS BIN_ADD

3. ENTER TWO NUMBERS IN DECIMAL FORM AND STORE THEM IN VARIABLES I AND j
RESPECTIVELY

4. COPY THE VARIABLES i AND j TO VARIABLES n1 AND n2

5. INITIALIZE TWO STRING VARIABLES N1 AND N2

6. START TWO while LOOPS AND RUN THEM TILL n1! = 0 AND n2! = 0

7. CONVERT THE DECIMAL NUMBERS TO THEIR BINARY FORM AND STORE THEM IN N1 AND N2
RESPECTIVELY

8. CONVERT THE STRING VARIABLES N1 AND N2 TO INTEGER VARIABLES AND STORE THEM IN
VARIABLES a AND b RESPECTIVELY

9. START ANOTHER while LOOP AND RUN IT UNTIL a! = 0 OR b! = 0

10. EXTRACT THE LAST DIGITS OF THE NUMBERS a AND b AND STORE THEM IN VARIABLES d1 AND
d2 RESPECTIVELY

11. CHECK IF d1=0 AND d2=0 THEN

12. STORE 0 IN THE VARIABLE d

13. ELSE CHECK IF d1=0 AND d2=1 THEN

14. STORE 1 IN THE VARIABLE d

15. ELSE CHECK IF d1=1 AND d2=0 THEN

16. STORE 1 IN THE VARIABLE d

17. ELSE STORE 10 IN THE VARIABLE d

18. CONCATENATE VARIABLE d TO STRING n AND STORE VALUE OF a/10 AND b/10 TO VARIABLES
a AND b RESPECTIVELY

19. END OF while LOOP

19. DISPLAY THE SUM OF THE NUMBERS IN BINARY FORM

20. END OF CLASS

21. END OF ALGORITHM


PROGRAM CODE
VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


sc Scanner Scans a line of text
display() from keyboard
i integer Stores first decimal
number
j integer Stores second
decimal number
n1 integer Stores copied number
n2 integer Stores copied number
N1 String Stores binary form of
number
N2 String Stores binary form of
number
d1 integer Stores the digits of
the number
d2 integer Stores the digits of
the number
a integer Stores the binary
form in integer
b integer Stores the binary
form in integer
n String Stores the sum of the
numbers
d integer Stores the binary
digits
INPUT AND OUTPUT

Enter 2 numbers in decimal form

The sum of the numbers 6 & 8 in binary form is : 1110

Enter 2 numbers in decimal form

24

32

The sum of the numbers 24 & 32 in binary form is : 111000

Enter 2 numbers in decimal form

The sum of the numbers 3 & 4 in binary form is : 111

Enter 2 numbers in decimal form

56

79

The sum of the numbers 56 & 79 in binary form is : 11110111

Enter 2 numbers in decimal form

The sum of the numbers 2 & 2 in binary form is : 100


Question 4

Input a word and print all possible anagrams of that word.

For example: TOP -> TOP

TPO

OTP

OPT

PTO

POT

An anagram can be printed in any order. It must be printed only once.

The number of letters of the word should not be greater than seven.
Algorithm

1. START

2. DEFINE A CLASS Permutation

3. ENTER A WORD AND STORE IT IN VARIABLE str AND STOTE THE LENGTH OF THE WORD IN n

4. CHECK IF LENGTH OF WORD IS GREATER THAN 7 THEN

5. DISPLAY “WORD OUT OF RANGE”

6. ELSE CREATE AN OBJECT permutation OF THE CLASS Permutation

7. CALL THE METHOD permute () THROUGH THE OBJECT permutation WITH PARAMETER LIST AS
str, 0, n-1

8. IN METHOD permute (), CHECK IF VARIABLE l=r THEN

9. DISPLAY STRING str

10. ELSE RUN A for LOOP FROM i=l TO r

11. CALL A RECURSIVE METHOD swap (String, int, int) WITH PARAMETER LIST AS str, l, i

12. IN RECURSIVE METHOD swap (String, int, int), DECLARE A CHARACTER VARIABLE temp

13. DECLARE A CHARACTER ARRAY AND STORE THE CHARACTERS OF THE STRING IN THE ARRAY

14. SWAP THE ELEMENTS OF THE ARRAY AT DIFFERENT INDEX POSITIONS TO FORM DIFFERENT
WORDS AND RETURN THEM THROUGH THE METHOD

15. END OF RECURSIVE METHOD swap (String, int, int)

16. STORE THE RETURNED STRING IN A LOCAL VARIABLE str AND RECALL THE METHOD permute
(String, int, int) WITH PARAMETER LIST str, l+1, r

17. END OF for LOOP

18. END OF RECURSIVE METHOD permute (String, int, int)

19. END OF CLASS

20. END OF ALGORITHM


PROGRAM CODE
VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


main(String args[]) sc Scanner Scans a line of text
from the keyboard
str String Stores the
inputted string
n integer Stores the length
of the string
permutation Permutation Stores memory
space for object
permute(String, int, l integer Local variable
int)
r integer Local variable
i integer Loop variable
swap(String, int, int) a String Stores the string
j integer Local variable
temp character Stores characters
of the string
charArray character Stores memory
space for
character array
INPUT AND OUTPUT

Enter word

TOP

All possible anagrams are:

TOP

TPO

OTP

OPT

POT

PTO

Enter word

RICK

All possible anagrams are:

RICK

RIKC

RCIK

RCKI

RKCI

RKIC
IRCK

IRKC

ICRK

ICKR

IKCR

IKRC

CIRK

CIKR

CRIK

CRKI

CKRI

CKIR

KICR

KIRC

KCIR

KCRI

KRCI

KRIC

Enter word

AMERICAN

Word out of range


Question 5

Write a program to input a number and check whether the number is a magic
number or not. A number is said to be magic if the eventual sum of the digits
of the number is equal to one.

For example: 28 = 2 + 8 = 10

10 = 1 + 0 = 1

Therefore 28 is a magic number.


Algorithm
1. START

2. DEFINE A CLASS MagicNum

3. ENTER A NUMBER AND STORE IT IN VARIABLE num

4. INITIALIZE THE VARIABLES d, s AND COPY THE NUMBER TO THE VARIABLE n

5. START A while LOOP AND RUN IT TILL s>9

6. REDECLARE s=n AND n=0

7. START A NESTED while LOOP AND RUN IT TILL n! = 0

8. EXTRACT THE DIGIT IF THE NUMBER AND STORE IT IN VARIABLE d, THEN ADD THE d TO
VARIABLE s

9. STORE THE VALUE OF n/10 IN THE VARIABLE n

10. END OF INNER while LOOP

11. END OF OUTER while LOOP

12. CHECK IF s=1 THEN

13. DISPLAY n IS MAGIC NUMBER

14. ELSE DISPLAY “NOT MAGIC NUMBER”

15. END OF CLASS

16. END OF ALGORITHM


PROGRAM CODE

VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


br BufferedReader Reads a line of
main( String text from
args[] ) keyboard
num integer Stores the number
inputted
d integer Stores the digits of
the number
s integer Stores the sum of
the digits of the
number
n integer Stores a copy of
the number
INPUT AND OUTPUT
Enter a number

28

28 is a magic number

Enter a number

172

172 is a magic number

Enter a number

29

29 is not a magic number

Enter a number

676

676 is a magic number

Enter a number

518

518 is not a magic number

Enter a number

919

919 is a magic number

Enter a number

920

920 is not a magic number


Question 6

Write a program to input a line of text ( String ) and replace the duplicate the
characters in sequence by their single occurrence in the text. Note that the
sequence of spaces should also be replaced by single spaces and print the
text in the final form.

Example : Heee iiss ggooinnng ttooo ssee aa mmovvie

Final form : He is going to se a movie


Algorithm
1. START

2. DECLARE A CLASS Dupliminate

3. ENTER A TEXT AND STORE IT IN VARIABLE s

4. INITIALIZE THE STRING VARIABLE str = ”” AND INTEGER VARIABLE c=0

5. START A for LOOP FROM i=0 TO s.length ()

6. EXTRACT EACH CHARACTER FROM THE TEXT AND STORE IT IN VARIABLE ch

7. CHECK IF ch = ’ ‘ THEN

8. INITIALIZE c=0 AND ADD ch TO str

9. ELSE CHECK IF c=0 THEN

10. ADD A SPACE TO str

11. END OF NESTED if

12. INCREMENT c BY 1

13. END OS ELSE

14. REMOVE ALL SPACES DROM FRONT AND REAR END OF THE STRING AND THEN ADD A SPACE AT THE END OF
THE STRING

15. STORE THE LENGTH OF THE STRING IN len AND INITIALIZE wrd=” “

16. DECLARE THE VARIABLES newstr, ch, cha

17. START A for LOOP FROM i=0 TO len

18. EXTRACT CHARACTERS OF THE STRING AND STORE THEM IN ch

19. CHECK IF ch ! = ’ ‘ THEN

20. STORE THE LENGTH OF THE WORD IN VARIABLE l AND EXTRACT THE CHARACTERS OF THE WORD STORING
THEM IN VARIABLE cha

21. START A NESTED if AND CHECK IF ch ! = cha THEN

22. ADD ch TO VARIABLW wrd

23. END OF NESTED if AND OUTER if

24. ELSE ADD wrd TO VARIABLE newstr AND REINITIALIZE wrd=” “

25. END OF ELSE AND for LOOP

26. PRINT THE FINAL FORM OF THE STRING BY DISPLAYING VARIABLE newstr

27. END OF CLASS AND ALGORITHM


PROGRAM CODE
VARIABLE DESCRIPTION TABLE

METHOD NAME DATATYPE VARIABLE FUNCTION


sc Scanner Scans a line of text
display () from keyboard
s String Stores the inputted
text
str String Stores the original
text but with single
spaces
c integer Counter
i integer Loop variable
ch character Stores the characters
of the string
len integer Stores the length of
the string
wrd String Stores the words of
the string
newstr String Stores the final string
to be outputted
ch character Stores the characters
of the words of the
string
cha character Stores the characters
of the words of the
string
l integer Stores the length of
the word
INPUT AND OUTPUT
Enter sentence

Heee iiiss ggooiinngg ttooo ssee aa mmovvviiee

The new string is : He is going to se a movie

Enter sentence

III aammm aaa sssttuudddeenntt

The new string is : I am a student

Enter sentence

iiiii aaam aa cciitizzeenn ooff Innddiiaa

The new string is : i am a citizen of India

Enter sentence

MMYYY GGGRRAANNNDDFATTHHEERR IISSS 6699yrrs OOLLDD..

The new string is : MY GRANDFATHER IS 69yrs OLD.

Enter sentence

I lloovveee Coommppuutteerr Scciieencce

The new string is : I love Computer Science

Enter sentence

"TTiimmmeee aaannd TTiddee wwaaittss ffor nnonne

The new string is : "Time and Tide waits for none


Question 7

Write a program to create two 2D matrices and perform matrix


multiplication. Store the result of their multiplication in a new matrix and
display the original and the resultant matrices.

For example:

Matrix 1 Matrix 2 Resultant Matrix

1 2 3 9 8 7 30 24 18

4 5 6 6 5 4 84 69 54

7 8 9 3 2 1 138 114 90
Algorithm
1. START

2. DEFINE A CLASS Matrix_Mult

3. ENTER THE DIMENSIONS OF THE FIRST MATRIX AND STORE THEM IN VARIABLES m1 AND n1

4. ENTER THE DIMENSIONS OF THE SECOND MATRIX AND STORE THEM IN VARIABLES m2 AND n2

5. IF n1 ! = m2 THEN

6. DISPLAY “MATRIX MULTIPLICATION CANNOT BE PERFORMED”

7. ELSE START MATRIX MULTIPLICATION

8. DECLARE TWO ARRAYS a[][] AND b[][]

9. ENTER ELEMENTS IN THE FIRST ARRAY THEN ENTER ELEMENTS IN THE SECOND ARRAY

10. THE ARRAYS REPRESENT THE TWO MATRICES RESPECTIVELY

11. INITIALIZE VARIABLE pro=0 AND DECLARE AN ARRAY c[][] WITH THE DIMENSIONS m1 AND n2

12. START A for LOOP FROM i=0 TO c.length

13. START ANOTHER NESTED for LOOP FROM j=0 TO c [0].length

14. START A THIRD NESTED for LOOP FROM k=0 TO n1 OR m2

15. EVALUATE PRODUCT OF THE ELEMENTS OF THE MATRICES AND STORE IT IN VARIABLE pro

16. END OF INNERMOST NESTED for LOOP

17. STORE THE VALUE OF pro AT THE RESPECTIVE POSITION IN c[][] AND REINITIALIZE pro=0

18. END OF SECOND for LOOP

19. END OF OUTERMOST for LOOP

20. DISPLAY THE TWO MATRICES RESPECTIVELY USING for LOOPS

21. DISPLAY THE RESULTANT MATRIX USING for LOOPS

22. END OF ELSE

23. END OF CLASS

24. END OF ALGORITHM


PROGRAM CODE
VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


sc Scanner Scans a line of text
display() from keyboard
m1 integer Stores the row of
first matrix
n1 integer Stores the column
of first matrix
m2 integer Stores the row of
second matrix
n2 integer Stores the column
of second matrix
a integer Stores the memory
space for array one
b integer Stores the memory
space for array two
i integer Loop variable
j integer Loop variable
pro integer Stores the
multiplied number
c integer Stores the memory
space for array
three
k integer Loop variable
INPUT AND OUTPUT

Enter the dimensions of the first matrix

Enter the dimensions of the second matrix

Enter elements of first matrix

Enter elements of second matrix

6
Matrix 1:

1 2

3 4

5 6

Matrix 2:

1 2 3

4 5 6

New matrix is:

9 12 15

19 26 33

29 40 51

Enter the dimensions of the first matrix

Enter the dimensions of the second matrix

Enter elements of first matrix

4
5

Enter elements of second matrix

Matrix 1:

1 2 3

4 5 6

7 8 9

Matrix 2:

1 2 3

4 5 6

7 8 9
New matrix is:

30 36 42

66 81 96

102 126 150

Enter the dimensions of the first matrix

Enter the dimensions of the second matrix

The dimensions of the matrices are incorrect.

Matrix multiplication cannot be performed


Question 8

Write a program to which creates a circular square matrix and prints it in circular fashion
in the clockwise direction.

For example : n=3 n=4

1 2 3 1 2 3 4

8 9 4 12 13 14 5

7 6 5 11 16 15 6

10 9 8 7
Algorithm

1. START

2. DEFINE A CLASS Spiral

3. ENTER THE DIMENSION OF THE SQUARE MATRIX

4. DECLARE AN ARRAY a [] [] WITH DIMENSION n

5. INITIALIZE VARIABLES k=1, c1=0, c2=n-1, r1=0, r2=n-1

6. START A while LOOP AND RUN IT TILL k < = n * n

7. START FOUR NESTED for LOOPS

8. RUN THE FIRST LOOP FROM i=c1 TO c2, THE SECOND LOOP FROM i=r1+1 TO r2, THE
THIRD LOOP FROM i=c2-1 TO c1, THE LAST LOOP FROM i=r2-1 TO r1+1

9. IN EVERY for LOOP INCREMENT k BY 1 AND ASSIGN IT AT THE RESPECTIVE POSITION IN


ARRAY a [] []

10. INCREMENT c1 BY 1, DECREMENT c2 BY 1, INCREMENT r1 BY 1 AND DECREMENT r2 BY 1

11. END OF while LOOP

12. PRINT THE MATRIX a [] [] YSING for LOOPS, RUNNING THEM FROM 0 TO n

13. END OF FOR LOOP

14. END OF CLASS

15. END OF ALGORITHM


PROGRAM CODE

VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


sc Scanner Scans a line of text
display () from keyboard
n integer Stores the dimension
of the array
a integer Stores memory space
for array
k integer Creates the elements
of the array
c1 integer Stores the column
position
c2 integer Stores the column
position
r1 integer Stores the row
position
r2 integer Stores the row
position
i, j integer Loop variables
INPUT AND OUTPUT

Enter the dimension of the matrix

Spiral Matrix in clockwise manner is:

1 2 3

8 9 4

7 6 5

Enter the dimension of the matrix

Spiral Matrix in clockwise manner is:

1 2 3 4

12 13 14 5

11 16 15 6

10 9 8 7
Question 9

Create a single dimensional array with integer values. Check the elements for
primeness using recursive method i.e check if the elements in the array are
prime or not. After checking, print only the elements in the array, which are
prime.
Algorithm
1. START

2. DEFINE A CLASS Primeness

3. ENTER THE SIZE OF THE ARRAY AND STORE IT IN VARIABLE len

4. DECLARE AN ARRAY a []

5. ENTER THE ELEMENTS OF THE ARRAY AND STORE IT IN ARRAY a []

6. DISPLAY THE ARRAY a [] BY PRINTING THE ELEMENTS

7. DECLARE VARIABLES i, c AND INITIALIZE VARIABLE k=0

8. START A for LOOP AND RUN IT FROM j=0 TO len-1

9. INITIALIZE VARIABLE i=1 AND n=a [j]

10. CALL RECURSIVE METHOD Prime ( int ) AND STORE THE VALUE IN VARIABLE c

11. CHECK IF c=2 THEN

12. PRINT THE VARIABLE n AND INCREMENT k BY 1

13. END OF if

14. END OF for LOOP

15. CHECK IF k=0 THEN

16. DISPLAY “NO PRIME NUMBERS ARE PRESENT”

17. END OF IF

18. IN RECURSIVE METHOD Prime (int), CHECK IF i=1 AND n% i=0 THEN

19. RETURN 1 ALONG WITH CALLING THE METHOD Prime (int) BY INCREMENTING i BY 1

20. ELSE CHECK IF i<=n AND n%i=0 THEN

21. RETURN 1 ALONG WITH CALLING THE METHOD Prime (int) BY INCREMENTING i BY 1

22. ELSE CHECK IF i<=n AND n % i ! =0 THEN

23. RETURN 0 ALONG WITH CALLING THE METHOD Prime (int) BY INCREMENTING i BY 1

24. ELSE RETURN 0

25. END OF RECURSION

26. END OF CLASS AND ALGORITHM


PROGRAM CODE
VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


n integer Stores the elements
main ( String args of the array at a
time
[] )
sc Scanner Scans a line of text
from keyboard
len integer Stores the length of
the array
a integer Stores the memory
space for the array
i integer Counter
k integer Counter

c integer Counter
j integer Loop variable

Prime ( int ) i integer Local variable


INPUT AND OUTPUT

Enter the size of the array

Enter the elements of the array

11

13

The array is: 1 3 5 7 9 11 13

Elements which are prime are : 3 5 7 11 13

Enter the size of the array

Enter the elements of the array


49

37

41

377

The array is: 49 37 41 377

Elements which are prime are : 37 41

Enter the size of the array

Enter the elements of the array

The array is: 4 6 8

Elements which are prime are :

No prime numbers are present


Question 10

Write a program to create a text file and input and store n number of
sentences in it. The name of the text file is “sentence.txt”. Read the text
file and print the number of words and vowels in each sentence in the
given format :

Sentence : ……..

No of words No of vowels

…… …….
Algorithm
1. START

2. DEFINE A CLASS FileString

3. STORE THE NAME OF THE FILE IN THE STATIC VARIABLE filename

4. START A METHOD input () TO STORE THE SENTENCES IN THE FILE

5. START A try BLOCK

6. CREATE A FILEWRITER, BUFFEREDWRITER AND PRINTWRITER OBJECT AND CONNECT THEM TO


FORM A STREAM

7. ENTER THE NUMBER OF SENTENCES TO BE INPUTTED AND STOTE IT IN VARIABLE n

8. START A for LOOP FROM i=0 TO n, ENTER THE SENTENCES, STORE IT IN VARIABLE str AND COPY
THEM TO THE FILE

9. END OF for LOOP

10. CLOSE THE FILE OBJECT

11. END OF TRY BLOCK

12. KEEP A catch BLOCK TO CATCH ANY EXCEPTION IF PRESENT

13. END OF METHOD input ()

14. START ANOTHER METHOD display () TO DISPLAY THE SENTENCES IN GIVEN FORMAT

15. CREATE A FILEREADER AND BUFFEREDREADER OBJECT, CONNECT THEM TO FORM A STREAM

16. DECLARE VARIABLR text AND INITIALIZE VARIABLE i=0

17. START A while LOOP AND RUN IT TILL FILE IS NOT EMPTY

18. CHECK IF i ! =0 THEN

19. DISPLAY THE SENTENCE AT THE RESPECTIVE POSITION

20. END OF IF

21. INITIALIZE vwl=0, c=0, wrd=””

22. REMOVE ALL SPACES FROM FRONT AND BACK OF THE TEXT AND ADD A SPACE AT THE END

23. STATR A for LOOP FROM j=0 TILL j LESS THAN THE LENGTH OF THE TEXT

24. EXTRACT EACH CHARACTER FROM THE TEXT AND STORE IT IN VARIABLE ch

25. CHECK IF ch IS A VOWEL THEN


26. INCREMENT VARIABLE vwl BY 1

27. END OF IF

28. CHECK IF ch ! = ‘ ‘ THEN

29. ADD ch TO VARIABLE wrd

30. END OF IF

31. ELSE INCREMENT c BY 1 AND REINITIALIZE wrd=””

32. END OF for LOOP

33. CHECK IF i>0 THEN

34. DISPLAY THE NUMBER OF WORDS AND NUMBER OF VOWELS IN GIVEN FORMAT

35. END OF IF

36. INCREMENT i BY 1, INITIALIZE c=0 AND vwl=0

37. END OF while LOOP

38. CLOSE THE FILE STREAM OBJECT

39. END OF METHOD display ()

40. DECLARE THE main ( String args [] ) METHOD AND CREATE AN OBJECT OF THE GIVEN CLASS

41. CALL THE METHODS input () AND display () THROUGH THE CREATED OBJECT

42. END OF main ( String args [] ) METHOD

43. END OF CLASS

44. END OF ALGORITHM


PROGRAM CODE
VARIABLE DESCRIPTION TABLE

METHOD NAME VARIABLE DATATYPE FUNCTION


fw FileWriter Stores memory
input () space for
FileWriter object
bw BufferedWriter Stores memory
space for
BufferedWriter
object
outfile PrintWriter Stores memory
space for
PrintWriter object
n integer Stores the number
of sentences
inputted
i integer Loop variable

str String Stores the string

display () file FileReader Stores memory


space for
FileReader object
fileinput BufferedReader Stores memory
space for
BufferedReader
object
text String Stores the string
copied from file
i integer Counter
vwl integer Counter
c integer Counter
wrd String Stores each word
of the string
j integer Loop variable
ch character Stores each
character
ob FileString Stores memory
main (String args[] ) space for
FileString object
filename String Stores the name
of the file
sc Scanner Scans a line of text
from keyboard
INPUT AND OUTPUT

Enter the number of sentences to be entered

Enter sentence 1:My name is Rik

Enter sentence 2:I love Computer Science

Enter sentence 3:I am in class 11

Sentence 1:My name is Rik

No of Words No of vowels

4 4

Sentence 2:I love Computer Science

No of Words No of vowels

4 9

Sentence 3:I am in class 11

No of Words No of vowels

5 4
Enter the number of sentences to be entered

Enter sentence 0:Enter sentence 1:I am having DiNnEr

Enter sentence 2:Tommorrow, I have school.

Enter sentence 3:It is 8 o clock.

Enter sentence 4:I will sleep now!

Sentence 1:I am having DiNnEr

No of Words No of vowels

4 6

Sentence 2:Tommorrow, I have school.

No of Words No of vowels

4 8

Sentence 3:It is 8 o clock.

No of Words No of vowels

5 4

Sentence 4:I will sleep now!

No of Words No of vowels

4 5

Você também pode gostar