You are on page 1of 98

HOLY ANGELS’ ISC SCHOOL

THIRUVANANTHAPURAM

COMPUTER SCIENCE PROJECT

SUBMITTED BY:

NAME: KRISHNANANA V

STANDARD: XII C

U.I.D NO:

0
CERTIFICATE

This is to certify that

Miss……………………………………………………………………………………

Examination UID
No……………………………………………………………………………………….

has completed the required number of programs in Computer


Science ,as per the syllabus, in the laboratory of this school.

Internal Examiner External Examiner

1
CONTENTS

SL NO DESCRIPTION OF PROGRAM PAGE NO


1 Number Programs 4
2 Number Programs 8
3 Number Programs 11
4 Number Programs 13
5 Number Programs 15
6 Number Programs 18
7 Number Programs 20
8 Number Programs 23
9 Arrays 25
10 Arrays 29
11 Arrays 33
12 Arrays 37
13 Arrays 42
14 Arrays 47
15 Arrays 51
16 Arrays 56
17 Strings 59
18 Strings 63
19 Strings 67
20 Strings 71

2
21 Strings 78
22 Strings 82
23 Strings 86
24 Merge Sort 91
25 Date program 95

3
1.NUMBER PROGRAMS

Write a program to check whether a number is a Prime Adam number


or not

A Prime Adam integer is a positive (without leading zeroes) which is a


prime as well as an Adam number

Prime number : A number which has only two factors , i.e 1 and the
number itself

Eg : 2,3,5,7,etc.

Adam number : The square of a number and the square of it’s reverse
and reverse to each other

Eg : if n=13 and reverse of n=31 , then

(13)^2=169

(31)^2=961

Which is reverse of 169

Thus, 13 is an Adam number

import java.util.*;
public class PrimeAdam
{
Scanner sc= new Scanner (System.in);
int m,n;
public boolean isPrime(int n)
{

4
int ct=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
ct++;
}
if(ct==2)
return true;
else
return false;
}
public boolean isAdam(int n)
{
int sq= n*n;
int rev=0;
while(n>0)
{
int d= n%10;
rev= rev*10+d;
n=n/10;
}
int sq1= rev*rev;
int rev1 =0;
while(sq1>0)
{
int d1= sq1%10;
rev1= rev1 * 10+d1;
sq1 = sq1/10;

5
}
if(rev1==sq)
return true;
else
return false;
}
public void check()
{
System.out.println("Enter the range");
m=sc.nextInt();
n=sc.nextInt();
int ct=0;
for(int i=m;i<=n;i++)
{
if(isPrime(i)== true && isAdam(i)==true)
{
ct++;
System.out.print(i+"");
}
}
System.out.println("Number of Primeadam numbers are"+ ct);
}
}

6
OUTPUT

Enter the range

53

11 13 31 Number of Primeadam numbers are 3

7
2.NUMBER PROGRAMS

Write a program to check whether a number is a goldbach number or


not

A number is said to be a Goldbach number, if the number can be


expressed as the addition of two odd prime number pairs

import java.util.*;
public class Goldbach
{
Scanner sc= new Scanner(System.in);
int sum=0,c=0;
public void input()
{
System.out.println("Enter a number");
int n=sc.nextInt();
if(n%2!=0)
System.out.println("It is not a Goldbach no");
else
{
for(int i=1;i<n;i+=2)
{
for(int j=1;j<n;j+=2)
{
sum= i+j;
if(sum==n)

8
{
if(isPrime(i)==1 && isPrime(j)==1)
{
c++;
}
}
if(c==0)
System.out.println("It is not Goldbach number");
else
System.out.println("It is a Goldbach number");
}
}
public int isPrime(int a)
{
int ct=0;
for(int i=1;i<=a;i++)
{
if(a%i==0)
ct++;
}
if(ct==2)
return 1;
else
return 0;
}
}

9
OUTPUT

Enter the element

It is a goldbatch no.

10
3.NUMBER PROGRAMS

Write a program to check whether a number is a kaprikar number or


not

A Kaprekar number is a number whose square when divided into two


parts and such that sum of parts is equal to the original number and
none of the parts has value 0.

import java.util.*;
public class Kaprekar
{
Scanner sc= new Scanner(System.in);
public void check()
{
System.out.println("Enter the number");
int n= sc.nextInt();
int sqrt= n*n;
int rem1=0, pow=1,flag=0,rem=0;
while(sqrt>0)
{
rem=sqrt%10;
sqrt=sqrt/10;
rem1= rem*pow + rem1;
pow=pow*10;
if((sqrt+rem1)==n)
{
flag=1;break;

11
}
}
if(flag==1)
System.out.println("It is a Kaprekar number");
else
System.out.println("It is not a Kaprekar number");
}
}

OUTPUT

Enter the number

It Is a kaprikar number

12
4.NUMBER PROGRAMS

Write a program to check whether a number is a Sublime number or


not

Sublime numbers are defined as positive numbers whose sum of


positive divisors and count of positive divisors are both perfect
numbers.

import java.util.*;
public class Sublime
{
Scanner sc= new Scanner(System.in);
int i,sum=0,n,ct=0;
public void input()
{
System.out.println("Enter the number");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
sum=sum +i;
ct++;
}
}
if(Perfect(sum)== true&& Perfect(ct)== true)

13
System.out.println("It is a Sublime number");
}
public boolean Perfect(int n1)
{
int s=0;
for(int i=1;i<n1;i++)
{
if(n1%i==0)
s=s+i;
}
if(s==n1)
return true;
else
return false;
}
public void main()
{
Sublime s= new Sublime();
s.input();
}
}

OUTPUT

Enter the number

12

I t Is a sublime number

14
5.NUMBER PROGRAMS

Write a program to check whether a number is a fascinating number


or not

Fascinating Number: When a number ( 3 digits or more ) is multiplied


by 2 and 3, and when both these products are concatenated with the
original number, then it results in all digits from 1 to 9 present exactly
once. There could be any number of zeros and are ignored.

import java.util.*;
public class Fascinating
{
Scanner sc= new Scanner(System.in);
public void number()
{
System.out.println("Enter a digit of 3 numbers or more");
int N= sc.nextInt();
int flag=0;
if(N<100)
{
System.out.println("INVALID");
System.exit(0);
}
int n1= N*2;
int n2= N*3;
String s1= Integer.toString(n1);

15
String s2= Integer.toString(n2);
String s3= Integer.toString(N);
String str= s3+s1+s2;
long n= Long.parseLong(str);
long m=n;
for(int i=1;i<=9;i++)
{
long sum=0,ctr=0;
while(m>0)
{
sum= m%10;
if(sum==1)
ctr++;
m=m/10;
}
if(ctr!=1)
flag++;
m=n;
}
if(flag==0)
{
System.out.println("It's a Fascinating number");
}
else
{
System.out.println("It's not a Fascinating number");
}
}

16
OUTPUT

Enter a number of 3 digits or more

192

It is a Fascinating number

17
6.NUMBER PROGRAMS

Write a program to check whether a number is a Keith number or not

An digit number x is called Keith number if it appears in a special


sequence (defined below) generated using its digits. The special
sequence has first n terms as digits of x and other terms are
recursively evaluated as sum of previous n terms.

import java.util.*;
public class Keith
{
public void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Input a number");
int n= sc.nextInt();
int n1=n;
String s= Integer.toString(n);
int d=s.length();
int arr[] = new int[n];
int i,sum=0;
for( i=d-1;i>=0;i--)
{
arr[i]=n1%10;
n1=n1/10;
}
i=d;
while(sum < n)

18
{
sum=0;
for(int j=1;j<= d;j++)
{
sum= sum+arr[i-j];
}
arr[i] = sum;
i++;
}
if(sum == n)
System.out.println("It is a Keith number");
else
System.out.println("It is not a Keith number");
}
}

OUTPUT

Enter a number

19

It is a Keith number

19
7.NUMBER PROGRAMS

Write a program to check whether a number is a prime-palindrome


number or not

A number which is prime as well a palindrome is said to be 'Prime


Palindrome' number
Prime number is a number that is greater than 1 and divided by 1 or
itself only.
A palindrome number is a number that is same after reverse

import java.util.*;

public class primepalindrome

Scanner sc =new Scanner(System.in);

public void input()

System.out.println("Enter the number");

int n=sc.nextInt();

if(isPrime(n)==true &&isPalin(n)==true)

System.out.println("It is a prime-palindrome no");

else

20
System.out.println("not a prime palindrome");

public boolean isPrime (int n)

int ct=0;

for(int i =1;i<=n;i++)

if(n%i==0)

ct++;

if(ct==2)

return true;

else

return false;

public boolean isPalin(int n)

int rev =0;int n1=n;

while(n>0)

21
int d=n%10;

rev=rev*10+d;

n=n/10;

if(rev==n1)

return true;

else

return false;

public void main()

primepalindrome ob=new primepalindrome();

ob.input();

OUTPUT

Enter the number

11

It is a prime-palindrome no

22
8.NUMBER PROGRAMS

Write a program to check whether a number is a Smith number or not

A Smith Number is a composite number whose sum of digits is equal


to the sum of digits in its prime factorization.

import java.util.*;
public class Smith
{
public void main()
{
Scanner sc= new Scanner (System.in);
int n,f=2,m,t,s1=0,s2=0,d;
System.out.println("Enter a number");
n= sc.nextInt();
m=n;
while(n>1)
{
if(n%f==0)
{
t=f;
while(t!=0)
{
d=t%10;
s1+=d;
t/=10;

23
}
n=n/f;
}
else
f++;
}
t=m;
while(t!=0)
{
d=t%10;
s2+=d;
t/=10;
}
if(s1==s2)
System.out.println("It is a Smith number");
else
System.out.println("It is not a Smith number");
}
}

OUTPUT

Enter a number

27

It is a smith number

24
9.ARRAY

Input an integer array of size m x n and print the left and right
diagonal elements

import java.util.*;

public class Leftandright

Scanner sc= new Scanner(System.in);

int m,n;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

n=sc.nextInt();

int a[][]=new int [m][n];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

25
{

a[i][j]=sc.nextInt();

System.out.println("Array in Matrix Form");

for(int i=0;i<m;i++)

for (int j=0;j<n;j++)

System.out.print(a[i][j]+ "");

System.out.println("");

System.out.println("left diagonal elements are");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

if(i==j)

26
System.out.print(a[i][j]+"");

System.out.println();

System.out.println("Right diagonal elements are");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

if(i+j==n-1)

System.out.print(a[i][j]+"");

System.out.println();

} } }

OUTPUT

Enter the size

Enter the elements

27
2

Array in Matrix Form

123

456

789

left diagonal elements are

Right diagonal elements are

28
10. ARRAY

Input an integer array of size m x m and print the boundary elements


along with the sum of boundary elements

import java.util.*;

public class Boundary

Scanner sc= new Scanner(System.in);

intm,sum;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

int a[][]=new int [m][m];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

29
a[i][j]=sc.nextInt();

System.out.println("Array in Matrix Form");

for(int i=0;i<m;i++)

for (int j=0;j<m;j++)

System.out.print(a[i][j]+ "");

System.out.println("");

System.out.println("Boundary elements are");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

if(i==0||i==m-1||j==0||j==m-1)

System.out.print(a[i][j]+"");

30
System.out.println("");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

if(i==0||i==m-1||j==0||j==m-1)

sum = sum+ a[i][j];

System.out.println(" SUM of Boundary elements are"+ sum);

OUTPUT

Enter the size

Enter the elements

31
2

Array in Matrix Form

123

456

789

Boundary elements are

123

46

789

SUM of Boundary elements are40

32
11.ARRAY

Input an integer array of size m x m and check whether it is symmetric


number or not

import java.util.*;

public class Symmetric

Scanner sc= new Scanner(System.in);

int m,sum;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

int a[][]=new int [m][m];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

33
a[i][j]=sc.nextInt();

System.out.println("Array in Matrix Form");

for(int i=0;i<m;i++)

for (int j=0;j<m;j++)

System.out.print(a[i][j]+ "");

System.out.println("");

int flag=0;

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

if(a[i][j]!=a[j][i])

flag++;

34
break;

if(flag==0)

System.out.println("It is a Symmetric matrix");

else

System.out.println("It is not a Symmetric matrix");

OUTPUT

Enter the size

Enter the elements

11

22

33

22

44

55

35
33

55

66

Array in Matrix Form

11 22 33

22 44 55

33 55 66

It is a Symmetric matrix

36
12.ARRAY

Input an integer array of size m x m and print the largest , second


largest , smallest ,second smallest

import java.util.*;

public class Lslssm

Scanner sc= new Scanner(System.in);

int m,n;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

int a[][]=new int [m][m];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

37
a[i][j]=sc.nextInt();

int b[]=new int[m*m];

int l=b.length;

int k=0;

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

b[k]=a[i][j];

k++;

int temp=0;

for(int i=0;i<l-1;i++)

for(int j=0;j<l-1-i;j++)

if (b[j]>b[j+1])

38
{

temp=b[j];

b[j]=b[j+1];

b[j+1]=temp;

k=0;

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

a[i][j]=b[k];

k++;

}}

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

System.out.print(a[i][j]+"");

39
}

System.out.println("");

System.out.println("Largest elements is" + a[m-1][m-1]);

System.out.println(" Second Largest elements is" + a[m-1][m-2]);

System.out.println("Smallest elements is" + a[0][0]);

System.out.println("Second Smallest elements is" + a[0][1]);

OUTPUT

Enter the size

Enter the elements

99

88

77

66

55

44

33

40
22

11

11 22 33

44 55 66

77 88 99

Largest elements is 99

Second Largest elements is 88

Smallest elements is 11

Second Smallest elements is 22

41
13. ARRAY

Input an integer array of size m x m and sort each row in ascending


order using any standard sorting technique

import java.util.*;

public class Ascending

Scanner sc= new Scanner(System.in);

int m,n;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

int a[][]=new int [m][m];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

42
a[i][j]=sc.nextInt();

System.out.println("Array is");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

System.out.print(a[i][j]+"");

System.out.println("");

int temp=0;

for(int i=0;i<m;i++)

for(int j=0 ; j<m ; j++)

for(int k=j+1;k<m;k++)

43
if (a[j][i]>a[k][i])

temp=a[j][i];

a[j][i]=a[i][k];

a[i][k]=temp;

System.out.println("Sorted array is");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

System.out.print(a[i][j]+"");

System.out.println("");

44
OUTPUT

Enter the size

Enter the elements

73

56

87

55

43

25

86

35

28

96

46

87

35

32

76

45

45
Array is

73 56 87 55

43 25 86 35

28 96 46 87

35 32 76 45

Sorted array is

55 25 43 45

87 35 46 56

28 73 87 76

35 32 96 86

46
14.ARRAY

Input an integer array of size m x n and replace all the corner elements
with “ * ” and all non-boundary elements with “ # “.

import java.util.*;

public class Corner

Scanner sc= new Scanner(System.in);

int m,n;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

n=sc.nextInt();

int a[][]=new int [m][n];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

47
{

a[i][j]=sc.nextInt();

System.out.println("Array in Matrix Form");

for(int i=0;i<m;i++)

for (int j=0;j<n;j++)

System.out.print(a[i][j]+ "");

System.out.println("");

System.out.println("Changed array is");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

if(i!=0&&j!=0&&i!=m-1&j!=n-1)

System.out.print("#" +"");

48
else if(i==0&&j==0||i==0&&j==n-1||i==m-1&&j==n-1||i==m-1&&j==0)

System.out.print("*" +"");

else

System.out.print(a[i][j] +"");

System.out.println("");

OUTPUT
Enter the size

Enter the elements

49
7

Array in Matrix Form

123

456

789

Changed array is

* 2*

4#6

* 8*

50
15.ARRAY

Input an integer array of size m x n and print the sum of each row and
column and rotate the matrix in 90 degree clockwise

import java.util.*;

public class Sumodrandc

Scanner sc= new Scanner(System.in);

int m,n;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

n=sc.nextInt();

int a[][]=new int [m][n];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

51
{

a[i][j]=sc.nextInt();

System.out.println("Array in Matrix Form");

for(int i=0;i<m;i++)

for (int j=0;j<n;j++)

System.out.print(a[i][j]+ "");

System.out.println("");

for(int i=0;i<m;i++)

int sumr=0,sumc=0;

for(int j=0;j<n;j++)

sumr= sumr+a[i][j];

sumc =sumc +a[j][i];

52
}

System.out.println("Sum of"+(i+1)+"row is"+sumr);

System.out.println("Sum of"+(i+1)+"coloumn is"+sumc);

System.out.println("ROTATING THE MATRIX 90");

for(int i=0;i<m;i++)

for(int j=m-1;j>=0;j--)

System.out.print(a[i][j]+ "");

System.out.println();

53
OUTPUT

Enter the size

Enter the elements

Array in Matrix Form

123

456

789

Sum of1row is 6

Sum of1coloumn is 12

54
Sum of2row is 15

Sum of2coloumn Is 15

Sum of3row is 24

Sum of3coloumn is 18

ROTATING THE MATRIX 90

321

654

987

55
16. ARRAY

Input an integer array of size m x m and print the decimal equivalent


of each row and display the result in appropriate format

import java.util.*;

public class binary

Scanner sc= new Scanner(System.in);

int m,dd;

public void input()

System.out.println("Enter the size");

m=sc.nextInt();

int a[][]=new int [m][m];

System.out.println("Enter the elements");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

56
{

a[i][j]=sc.nextInt();

System.out.println("Array in matrix form");

for(int i=0;i<m;i++)

for(int j=0;j<m;j++)

dd=dec_bin(a[i][j]);

System.out.print(dd+"");

System.out.println("");

public int dec_bin(int n)

if(n==0)

return 0;

else

57
return dec_bin(n/2)*10 +(n%2);

} }

OUTPUT

Enter the size

Enter the elements

Array in matrix form

1 10 11

100 101 110

111 1000 1001

58
17.STRING

Write a program to accept a sentence which may be terminated by


either ‘.’ ‘?’ or ‘!’ only. Any other character may be ignored. The words
may be separated by more than one blank space and are in UPPER
CASE.

Perform the following tasks:

(a) Accept the sentence and reduce all the extra blank space between
two words to
a single blank space.
(b) Accept a word from the user which is part of the sentence along
with its
position number and delete the word and display the sentence.

INPUT:

A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.


WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6
OUTPUT: A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.

import java.util.*;

class RemoveWord

59
public void main ()

Scanner sc = new Scanner(System.in);

System.out.print("Enter a sentence \n");

String s = sc.nextLine();

int l = s.length();

char last = s.charAt(l-1);

if(last != '.'&& last != '?'&& last != '!')

System.out.println("Invalid Input. End a sentence with either '.', '?' or '!'


only");

else

StringTokenizer str = new StringTokenizer(s," .?!");

int c = str.countTokens();

String wrd="",newwrd = "";

System.out.print("Enter the word to delete\n");

String del = sc.next();

System.out.print("Enter the word position is the sentence\n");

60
int pos = sc.nextInt();

if(pos<1 || pos>c)

System.out.println("The word position entered is out of range");

else

for(int i=1; i<=c; i++)

wrd = str.nextToken();

//if(w.equals(del)==true && i == x)

if(wrd.equals(del)==true && i == pos)

continue;

newwrd = newwrd + wrd + "";

System.out.print("Changed sentence is \n"+newwrd.trim()+last);

61
OUTPUT

Enter a sentence

A morning is a blessing for the whole day.

Enter the word to delete

blessing

Enter the word position is the sentence

Changed sentence is

A morning is a for the whole day.

62
18.STRING

Write a program to accept a sentence as input. The words in the string


sentence is terminated by either “.”,”!” or “?”. Perform the following
tasks:

(i) Obtain the length of the sentence. (measured in words)

(ii) Arrange the sentence in alphabetical order of the words.

Example

INPUT: NECESSITY IS THE MOTHER OF INVENTION.

OUTPUT:

Length: 6

Rearranged Sentence:

INVENTION IS MOTHER NECESSITY OF THE

import java.util.*;

public class Sentence

Scanner sc= new Scanner(System.in);

String sent;

int l;

63
public void input()

System.out.println("Enter the sentence");

sent=sc.nextLine();

l=sent.length();

StringTokenizer st=new StringTokenizer(sent);

int words= st.countTokens();

System.out.println("Length of the sentence measured in


words"+words);

//copying each words into a single dimensional array

int i=0;

String arr[]= new String[words];

while(st.hasMoreTokens())

String w=st.nextToken();

arr[i]=w;

i++;

//Sorting the string array in ascending order using bubble sort

String temp="";

64
for(i=0;i<words-1; i++)

for(int j=0;j< words-1-i;j++)

if(arr[j].compareTo(arr[j+1])>0)

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

System.out.println("Sentence in alphabetical order is");

for( i=0;i<words;i++)

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

public void main()

Sentence ob= new Sentence();

ob.input();

65
}

OUTPUT

Enter the sentence

NECESSITY IS THE MOTHER OF INVENTION

Length of the sentence measured in words6

Sentence in alphabetical order is

INVENTION IS MOTHER NECESSITY OF THE

66
19.STRING

Write a program to accept a sentence which may be terminated by


either '.', '?' or '!' only. The words may be separated by more than one
blank space and are in UPPER CASE.

Perform the following tasks:

Find the number of words beginning and ending with a vowel.

Place the words which begin and end with a vowel at the beginning,
followed by the remaining words as they occur in the sentence.

Test your program with the sample data and some random data:

Example 1

INPUT:
ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.

OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3
ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL

import java.util.*;

public class Q3

Scanner sc= new Scanner(System.in);

67
String sent; String str="",v="",c="";

int l, ct=0;

public void input()

System.out.println("Enter the sentence");

sent=sc.nextLine();

l=sent.length();

sent=sent.toUpperCase();

StringTokenizer st=new StringTokenizer(sent);

//int ct=st.countTokens();

while(st.hasMoreTokens())

String w= st.nextToken();

if(isVowel(w))

ct++;

v=v+w;

v=v+""; //;putting space between words

else

68
{

c=c+w;

c=c+"";

str=v+c;

System.out.println("NUMBER OF WORDS BEGINNING AND ENDING


WITH VOWELS"+ct);

System.out.println("REARRANGED SENTENCE IS"+str);

public boolean isVowel(String n)

int len=n.length();

char ch=n.charAt(0);

char ch1=n.charAt(len-1);

if((ch=='A'|| ch=='E' || ch=='I' || ch=='O' || ch=='U') && (ch1=='A' ||


ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U'))

return true;

else

return false;

69
}

public void main()

Q3 ob=new Q3();

ob.input();

}}

OUTPUT

Enter the sentence

ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE.

NUMBER OF WORDS BEGINNING AND ENDING WITH VOWELS 3

REARRANGED SENTENCE IS ANAMIKA ARE ANYMORE AND SUSAN


NEVER GOING TO QUARREL.

70
20.STRING

Write a program to accept a paragraph containing TWO sentences


only. The sentences may be terminated by either '.', '?' or '!' only. Any
other characters may be ignored. The words may be separated by a
single blank space and are in UPPER CASE.

Perform the following tasks:

Check for the validity of the accepted paragraph for the number of
sentences and for the terminating character.

Separate the two sentences from the paragraph and find the common
words in the two sentences with their frequency of occurrence in the
paragraph.

Display both the sentences separately along with the common words
and their frequency, in the format given below:

Test your program with the sample data and some random data:

Example 1

INPUT:
IS IT RAINING? YOU MAY GET WET IF IT IS RAINING.

OUTPUT:
IS IT RAINING?

71
YOU MAY GET WET IF IT IS RAINING.

COMMON WORDS FREQUENCY

IS 2

IT 2

RAINING 2

import java.util.*;

public class Para

Scanner sc= new Scanner(System.in);

public void main()

System.out.println("Enter the paragraph");

String s= sc.nextLine();

// separate the two sentences

StringTokenizer st= new StringTokenizer(s, ".?!");

int n=st.countTokens();

if(n==2) //if there are only two sentences

72
String s1= st.nextToken().trim();

String s2= st.nextToken().trim();

System.out.println(s1);

System.out.println(s2);

// store words from sentence 1 to array 1

StringTokenizer st1= new StringTokenizer(s1);

int a=st1.countTokens();

String w1[]=new String[a];

int i=0;

while(st1.hasMoreTokens())

w1[i++]=st1.nextToken();

// store words from sentence 2 to array 2

StringTokenizer st2= new StringTokenizer(s2);

int b=st2.countTokens();

String w2[]=new String[b];

i=0;

while(st2.hasMoreTokens())

w2[i++]=st2.nextToken();

// find common words in both the sentences

String com[]= new String[a];

73
int freq[]= new int[a];

int x=0;

for( i=0;i<a;i++)

//take 1 word from 1st sentence word array

String word= w1[i];

for( int j=0;j<b;j++)

//take 1 word from 2nd sentence word array

String word1= w2[j];

// check if both the sentences has the same words

if (word1.equals(word))

com[x]=word;

freq[x]=2;

x++;

//also remove that word from both arrays

w1[i]="";

w2[j]="";

break;

74
}

// check if the common word is repeated in either sentences

i=0;

while(i<x)

//sentence1

for(int p=0;p<a;p++)

if(com[i].equals(w1[p]))

freq[i]++;

//sentence2

for(int p=0;p<a;p++)

if(com[i].equals(w2[p]))

freq[i]++;

i++;

75
}

// Display the words and frequency

if(x!=0)

System.out.println("COMMON WORDS \t FREQUENCY");

for( i=0;i<a;i++)

//if word is there in array

if(com[i] !="")

System.out.println(com[i] + "\t\t"+ freq[i]);

else

System.out.println("NO COMMON WORDS");

//if there are more than two sentences

else

System.out.println("INVALID");

76
} }

OUTPUT

Enter the paragraph

IS IT RAINING? YOU MAY GET WET IF IT IS RAINING.

IS IT RAINING

YOU MAY GET WET IF IT IS RAINING

COMMON WORDS FREQUENCY

IS 2

IT 2

RAINING 2

77
21.STRING

Write a program to accept a sentence which may be terminated by


either ‘.’ or ‘?’ only. The words are to be separated by a single blank
space. Print an error message if the input does not terminate with ‘.’
or ‘?’. You can assume that no word in the sentence exceeds 15
characters, so that you get a proper formatted output.

Perform the following tasks:


(i) Convert the first letter of each word to uppercase.
(ii) Find the number of vowels and consonants in each word and
display them with proper headings along with the words.

Test your program with the following inputs.

Example 1

INPUT: Intelligence plus character is education.

OUTPUT:
Intelligence Plus Character Is Education

Word Vowels Consonants


Intelligence 5 7
Plus 1 3
Character 3 6

78
Is 1 1
Education 5 4

import java.util.*;

public class Q5

public void main()

String p="";

int v=0,c=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Sentence");

String n=sc.nextLine();

n=n+"";

System.out.println("Word \t vowel \tconsonant");

int l=n.length();

if(n.charAt(l-2) != '.'&& n.charAt(l-2) != '?')

System.out.println("Invalid Input. End a sentence with either '.' or '?'");

79
else

for(int i=0;i<l;i++)

char ch=n.charAt(i);

if(ch!='')

p=p+ch;

else

int k=p.length();

for(int j=0;j<k;j++)

char ch1=p.charAt(j);

if(ch1=='.')

break;

if(ch1=='a'||ch1=='e'||ch1=='i'||ch1=='o'||ch1=='u')

v++;

80
else

c++;

System.out.println(p+"\t"+v+"\t"+c);

p="";

v=0;

c=0;

}}}}}

OUTPUT

Enter Sentence

Intelligence plus character is education.

Word vowel consonant

Intelligence 4 8

plus 1 3

character 3 6

is 1 1

education. 5 4

81
22.STRING

Caesar Cipher is an encryption technique which is implemented as


ROT13 ('rotate by 13 places'). It is a simple letter substitution cipher
that replaces a letter with the letter 13 places after it in the alphabets,
with the other characters remaining unchanged.

ROT13

E F K
A B C D G H I/ L M/
/ / J/j /
/a /b /c /d /g /h i /l m
e f k

↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕

P S V X Y
N O Q R T/ U W
/ / / / / Z/z
/n /o /q /r t /u /w
p s v x y

Write a program to accept a plain text of length L, where L must be


greater than 3 and less than 100.

Encrypt the text if valid as per the Caesar Cipher.

Test your program with the sample data and some random data.

Example 1

INPUT:
Hello! How are you?

82
OUTPUT:
The cipher text is:
Uryyb! Ubj ner lbh?

import java.util.*;

class CipherCaesar

public void main()

int p=0,s=0;

char a=0;

Scanner sc=new Scanner(System.in);

System.out.println("enter n");

String n=sc.nextLine();

n=n+"";

int l=n.length();

if(l<3&&l>100)

System.out.println("invalid");

83
else

for(int i=0;i<l;i++)

char ch=n.charAt(i);

if(ch>='A'&&ch<='M')

p=ch+13;

a=(char)p;

System.out.print(a);

else if(ch>='N'&&ch<='Z')

s=ch-13;

a=(char)s;

System.out.print(a);

if(ch>='a'&&ch<='m')

84
{

p=ch+13;

a=(char)p;

System.out.print(a);

else if(ch>='n'&&ch<='z')

{ s=ch-13;

a=(char)s;

System.out.print(a);

else

System.out.print(ch);

} }}}}

OUTPUT

Enter the sentence

Hello! How are you?

UHryyb! UHbj ner lbh?

85
23.STRING

Write a program to accept a sentence which may be terminated by


either ‘.’, ‘?’ or ‘!’ only. The words are to be separated by a single blank
space and are in uppercase.

Perform the following tasks:

(a) Check for the validity of the accepted sentence.

(b) Convert the non-palindrome words of the sentence into


palindrome words by concatenating the word by its reverse (excluding
the last character).

Example:

The reverse of the word HELP would be LEH (omitting the last
alphabet) and by concatenating both, the new palindrome word is
HELPLEH. Thus, the word HELP becomes HELPLEH.

Note: The words which end with repeated alphabets, for example ABB
would become ABBA and not ABBBA and XAZZZ becomes XAZZZAX.

[Palindrome word: Spells same from either side. Example: DAD,


MADAM etc.]

(c) Display the original sentence along with the converted sentence.

Test your program for the following data and some random data:

Example 1

86
INPUT:
THE BIRD IS FLYING.

OUTPUT:
THE BIRD IS FLYING.
THEHT BIRDRIB ISI FLYINGNIYLF

import java.util.*;

//create a class

public class MakePalindrome

public void main()

//Create Scanner class object

Scanner sc = new Scanner(System.in);

//Ask user to enter any sentence that ends with '.','?', and '!'

System.out.println("ENTER ANY SENTENCE");

//read input

String str = sc.nextLine();

// compute length

int len=str.length();

//check if a string ends with '.', '?', and '!'

87
if(".?!".indexOf(str.charAt(len-1))!=-1)

//print the original string

System.out.println(str);

str = str.trim();

String words="";

for(int i=0; i< len;i++){

char ch= str.charAt(i);

//check if a space or ending character is found

if(!(ch=='!'|| ch == '?'||ch=='.'||ch==''))

words= words+ch;

else{

//for pallindrome print only word

if(words.equals(reverse(words)))

System.out.print(words+"");

//else print the reversed word with original word excluding last word

else

88
System.out.print(words+
(reverse(words).substring(1,reverse(words).length())+""));

words="";

//if string do not end with ., ? or ! . print INVALID INPUT

else

System.out.println("INVALID INPUT");

public String reverse(String st)

int len=st.length();

String newString="";

//reverse string adding character from the beginning

for(int i=0;i< len;i++)

newString=st.charAt(i) + newString;

return(newString);

89
OUTPUT

ENTER ANY SENTENCE

THE BIRD IS FLYING.

THE BIRD IS FLYING.

THEHT BIRDRIB ISI FLYINGNIYLF

90
24.MERGE SORT

import java.util.*;

public class Mergesort

Scanner sc= new Scanner(System.in);

int c1=0,c2=0,c3=0;

public void show()

int m1[]=new int[5];

int m2[]=new int[5];

int m3[]=new int[10];

System.out.println("enter the first array");

for(int i=0;i<5;i++)

m1[i]=sc.nextInt();

System.out.println("enter the second array");

for(int i=0;i<5;i++)

91
m2[i]= sc.nextInt();

while((c1<=4)&&(c2<=4))

if (m1[c1]<m2[c2])

m3[c3++]=m1[c1++];

else

m3[c3++]=m2[c2++];

if(c1==5)

while(c2<=4)

m3[c3++]=m2[c2++];

else if(c2==5)

while(c1<=4)

92
{

m3[c3++]=m1[c1++];

System.out.println("array required");

for(int i=0;i<10;i++)

System.out.println(m3[i]);

public void main()

Mergesort i=new Mergesort();

i.show();

OUTPUT

Enter the first array

93
3

Enter the second array

Array required

94
25.DATEPROGRAM

Write a program to input two valid dates, each comprising of day (2


digit) month (2 digit) and year (4 digit) and calculate the day elapsed
between the two dates.

import java.util.*;

class Date

public void main()

int a[ ]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int d,m,y,d1,m1,y1,s,s1,i,x;

Scanner sc =new Scanner(System.in);

s=0;

s1=0;

System.out.println("Enter First Date day : ");

d=sc.nextInt( );

System.out.println("Enter month : ");

m=sc.nextInt( );

95
System.out.println("Enter year : ");

y=sc.nextInt( );

System.out.println("Enter Second Date day : ");

d1=sc.nextInt( );

System.out.println("Enter month : ");

m1=sc.nextInt( );

System.out.println("Enter year : ");

y1=sc.nextInt( );

for(i=1;i<m;i++)

s=s+a[i];

if(y%4==0) s=s+1;

s=s+d;

for(i=0;i<m1;i++)

s1=s1+a[i];

if(y1%4==0) s1=s1+1;

s1=s1+d1;

x=s1-s;

System.out.print("Total number of days between : " + d+"/"+m+"/"+y);

System.out.print(" and " + d1+"/"+m1+"/"+y1);

System.out.print(" is " + x);

96
}}

OUTPUT

Enter First Date day :

24

Enter month :

09

Enter year :

1960

Enter Second Date day :

08

Enter month :

12

Enter year :

1960

Total number of days between : 24/9/1960 and 8/12/1960 is 75

97

You might also like