Professional Documents
Culture Documents
THIRUVANANTHAPURAM
SUBMITTED BY:
NAME: KRISHNANANA V
STANDARD: XII C
U.I.D NO:
0
CERTIFICATE
Miss……………………………………………………………………………………
Examination UID
No……………………………………………………………………………………….
1
CONTENTS
2
21 Strings 78
22 Strings 82
23 Strings 86
24 Merge Sort 91
25 Date program 95
3
1.NUMBER PROGRAMS
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
(13)^2=169
(31)^2=961
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
53
7
2.NUMBER PROGRAMS
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
It is a goldbatch no.
10
3.NUMBER PROGRAMS
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
It Is a kaprikar number
12
4.NUMBER PROGRAMS
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
12
I t Is a sublime number
14
5.NUMBER PROGRAMS
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
192
It is a Fascinating number
17
6.NUMBER PROGRAMS
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
import java.util.*;
int n=sc.nextInt();
if(isPrime(n)==true &&isPalin(n)==true)
else
20
System.out.println("not a prime palindrome");
int ct=0;
for(int i =1;i<=n;i++)
if(n%i==0)
ct++;
if(ct==2)
return true;
else
return false;
while(n>0)
21
int d=n%10;
rev=rev*10+d;
n=n/10;
if(rev==n1)
return true;
else
return false;
ob.input();
OUTPUT
11
It is a prime-palindrome no
22
8.NUMBER PROGRAMS
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.*;
int m,n;
m=sc.nextInt();
n=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
25
{
a[i][j]=sc.nextInt();
for(int i=0;i<m;i++)
System.out.print(a[i][j]+ "");
System.out.println("");
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();
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
27
2
123
456
789
28
10. ARRAY
import java.util.*;
intm,sum;
m=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
29
a[i][j]=sc.nextInt();
for(int i=0;i<m;i++)
System.out.print(a[i][j]+ "");
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)
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)
OUTPUT
31
2
123
456
789
123
46
789
32
11.ARRAY
import java.util.*;
int m,sum;
m=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
33
a[i][j]=sc.nextInt();
for(int i=0;i<m;i++)
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)
else
OUTPUT
11
22
33
22
44
55
35
33
55
66
11 22 33
22 44 55
33 55 66
It is a Symmetric matrix
36
12.ARRAY
import java.util.*;
int m,n;
m=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
37
a[i][j]=sc.nextInt();
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("");
OUTPUT
99
88
77
66
55
44
33
40
22
11
11 22 33
44 55 66
77 88 99
Largest elements is 99
Smallest elements is 11
41
13. ARRAY
import java.util.*;
int m,n;
m=sc.nextInt();
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 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;
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
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.*;
int m,n;
m=sc.nextInt();
n=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
47
{
a[i][j]=sc.nextInt();
for(int i=0;i<m;i++)
System.out.print(a[i][j]+ "");
System.out.println("");
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
49
7
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.*;
int m,n;
m=sc.nextInt();
n=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
51
{
a[i][j]=sc.nextInt();
for(int i=0;i<m;i++)
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];
52
}
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
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
321
654
987
55
16. ARRAY
import java.util.*;
int m,dd;
m=sc.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
56
{
a[i][j]=sc.nextInt();
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("");
if(n==0)
return 0;
else
57
return dec_bin(n/2)*10 +(n%2);
} }
OUTPUT
1 10 11
58
17.STRING
(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:
import java.util.*;
class RemoveWord
59
public void main ()
String s = sc.nextLine();
int l = s.length();
else
int c = str.countTokens();
60
int pos = sc.nextInt();
if(pos<1 || pos>c)
else
wrd = str.nextToken();
//if(w.equals(del)==true && i == x)
continue;
61
OUTPUT
Enter a sentence
blessing
Changed sentence is
62
18.STRING
Example
OUTPUT:
Length: 6
Rearranged Sentence:
import java.util.*;
String sent;
int l;
63
public void input()
sent=sc.nextLine();
l=sent.length();
int i=0;
while(st.hasMoreTokens())
String w=st.nextToken();
arr[i]=w;
i++;
String temp="";
64
for(i=0;i<words-1; i++)
if(arr[j].compareTo(arr[j+1])>0)
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
for( i=0;i<words;i++)
System.out.print(arr[i] +"");
ob.input();
65
}
OUTPUT
66
19.STRING
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
67
String sent; String str="",v="",c="";
int l, ct=0;
sent=sc.nextLine();
l=sent.length();
sent=sent.toUpperCase();
//int ct=st.countTokens();
while(st.hasMoreTokens())
String w= st.nextToken();
if(isVowel(w))
ct++;
v=v+w;
else
68
{
c=c+w;
c=c+"";
str=v+c;
int len=n.length();
char ch=n.charAt(0);
char ch1=n.charAt(len-1);
return true;
else
return false;
69
}
Q3 ob=new Q3();
ob.input();
}}
OUTPUT
70
20.STRING
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.
IS 2
IT 2
RAINING 2
import java.util.*;
String s= sc.nextLine();
int n=st.countTokens();
72
String s1= st.nextToken().trim();
System.out.println(s1);
System.out.println(s2);
int a=st1.countTokens();
int i=0;
while(st1.hasMoreTokens())
w1[i++]=st1.nextToken();
int b=st2.countTokens();
i=0;
while(st2.hasMoreTokens())
w2[i++]=st2.nextToken();
73
int freq[]= new int[a];
int x=0;
for( i=0;i<a;i++)
if (word1.equals(word))
com[x]=word;
freq[x]=2;
x++;
w1[i]="";
w2[j]="";
break;
74
}
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
}
if(x!=0)
for( i=0;i<a;i++)
if(com[i] !="")
else
else
System.out.println("INVALID");
76
} }
OUTPUT
IS IT RAINING
IS 2
IT 2
RAINING 2
77
21.STRING
Example 1
OUTPUT:
Intelligence Plus Character Is Education
78
Is 1 1
Education 5 4
import java.util.*;
public class Q5
String p="";
int v=0,c=0;
System.out.println("Enter Sentence");
String n=sc.nextLine();
n=n+"";
int l=n.length();
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 4 8
plus 1 3
character 3 6
is 1 1
education. 5 4
81
22.STRING
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
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
int p=0,s=0;
char a=0;
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
85
23.STRING
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.
(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
//Ask user to enter any sentence that ends with '.','?', and '!'
//read input
// compute length
int len=str.length();
87
if(".?!".indexOf(str.charAt(len-1))!=-1)
System.out.println(str);
str = str.trim();
String words="";
if(!(ch=='!'|| ch == '?'||ch=='.'||ch==''))
words= words+ch;
else{
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="";
else
System.out.println("INVALID INPUT");
int len=st.length();
String newString="";
newString=st.charAt(i) + newString;
return(newString);
89
OUTPUT
90
24.MERGE SORT
import java.util.*;
int c1=0,c2=0,c3=0;
for(int i=0;i<5;i++)
m1[i]=sc.nextInt();
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]);
i.show();
OUTPUT
93
3
Array required
94
25.DATEPROGRAM
import java.util.*;
class Date
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;
s=0;
s1=0;
d=sc.nextInt( );
m=sc.nextInt( );
95
System.out.println("Enter year : ");
y=sc.nextInt( );
d1=sc.nextInt( );
m1=sc.nextInt( );
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;
96
}}
OUTPUT
24
Enter month :
09
Enter year :
1960
08
Enter month :
12
Enter year :
1960
97