Ray Comp
Ray Comp
APPLICATION
PROJECT
PRACTICALS
(30
PROGRAMS)
DONE BY:
Rayyan Muhammad
XI-A
import java.util.*;
class Prime_Range
int c=0,t=0;
for(int i=1;i<=x;i++)
if(x%i==0)
return true;//True
else
}
public static void main (String args[])
int m,n;
m=sc.nextInt();
n=sc.nextInt();
int freq=0;
for(int i=m;i<=n;i++)
if(IsPrime(i)==true)
System.out.print(i+" ");
freq++;
System.out.println();
ALGORITHM:
OUTPUT:
100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
2. Prime_Palindrome
import java.util.*;
class Prime_Palindrome
int c=0,t=0;
for(int i=1;i<=x;i++)
if(x%i==0)
return true;//True
else
int c=x,d=0,rev=0;
while(c>0)
d=c%10;
rev=rev*10+d;
c=c/10;
if(rev==x)
return true;
else
return false;
int m,n;
m=sc.nextInt();
n=sc.nextInt();
int freq=0;
for(int i=m;i<=n;i++)
System.out.print(i+" ");
freq++;
System.out.println();
ALGORITHM:
OUTPUT:
1000
2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
3. Composite_Magic
Composite Number: Number which has more than 2 Factors ie, Not Prime
import java.util.*;
class Composite_Magic
int c=0,t=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
return true;//True
else
int c=x,d=0,sum=0;
while(c>0)
d=c%10;
sum+=d;//Sum of Digits
c=c/10;
if(sum>9)//Finding Eventual Sum of Digits. Finding the Sum of Digits of the Sum number again
return Magic(sum);
return sum;
int m,n;
m=sc.nextInt();
int freq=0;
for(int i=m;i<=n;i++)
System.out.print(i+" ");
freq++;
System.out.println();
ALGORITHM:
OUTPUT:
100
10 28 46 55 64 82 91 100
4. Emirp_Number
import java.util.*;
class Emirp_Number
{
Scanner sc=new Scanner(System.in);
int n;
n=sc.nextInt();
int c1=0,t1=0;
for(int i=1;i<=n;i++)
if(n%i==0)
t1=0;
else
t1=1;
int c=n,d=0,rev=0;
while(c>0)
d=c%10;
rev=rev*10+d;
c=c/10;
int c2=0,t2=0;
for(int j=1;j<=rev;j++)
if(rev%j==0)
c2++; //No. of Factors
t2=0;
else
t2=1;
if(t1==0 && t2==0)//Checking whether both the Numbers are Prime or not
System.out.println("Emirp Number");
else
ALGORITHM:
OUTPUT:
Enter a Number(n):
13
Reversed Number(rev): 31
Emirp Number
5. Kapreka_Number
Kapreka Number: Number whose square is divided into two parts in any conditions and parts are
added, the sum is equal to the number*/
import java.util.*;;
class Kapreka_Number
System.out.println("Enter a Number");
int n=sc.nextInt();
boolean IsKapreka=false;
int sq=n*n;
int c=sq;
int numdigits=0;
while(c>0)
numdigits++;
c=c/10;
for(int i=1;i<numdigits;i++)
int sum=quo+rem;
if(sum==n)
IsKapreka=true;
if(IsKapreka==true)
System.out.println("Kapreka Number");
else
ALGORITHM:
OUTPUT:
Enter a Number
297
Kapreka Number
6. Circular_Prime
/** A Circular Prime is a prime number that remains prime under cyclic shifts of its digits.
* When the leftmost digit is removed and replaced at the end of the remaining string of digits, the
generated number is still prime.
import java.util.*;
class Circular_Prime
int c=0;
for(int i=1;i<=x;i++)
if(x%i==0)
c++;
if(c==2)
return true;
else
return false;
String s1=Integer.toString(x);
String s2=s1.substring(1)+s1.charAt(0);
int a=Integer.parseInt(s2);
return a;
void IsCircularPrime(int x)
{
int a=x;
do
System.out.println(a);
if(isPrime(a) == false)
Prime = false;
break;
a=Shift(a);
while (a!=x);
if(Prime==true)
else if(Prime==false)
System.out.println("Enter a Number:");
int n=sc.nextInt();
cp.IsCircularPrime(n);
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class Circular_Prime
3. Define the isPrime method:
a. static boolean isPrime(int x)
b. Initialize int c to 0.
c. For i from 1 to x (inclusive):
i. If x is divisible by i, increment c.
d. If c is equal to 2, return true; otherwise, return false.
4. Define the Shift method:
a. static int Shift(int x)
b. Convert x to a string s1.
c. Create a new string s2 by taking the substring from the second character to the
end of s1 and appending the first character.
d. Parse s2 back to an integer and return it.
5. Define the IsCircularPrime method:
a. void IsCircularPrime(int x)
b. Initialize int a to x and boolean Prime to true.
c. Use a do-while loop until a becomes equal to x:
i. Print a.
ii. If isPrime(a) is false, set Prime to false and break out of the loop.
iii. Update a by shifting its digits using the Shift method.
d. If Prime is true, print "Circular Prime Number"; otherwise, print "Not a
Circular Prime Number".
6. Define the main method:
a. public static void main(String args[])
b. Create a Scanner object (sc).
c. Prompt the user to enter a number (n) and read the input.
d. Create an instance of the Circular_Prime class (cp) and call the
IsCircularPrime method with the entered number.
7. Close the Scanner object (sc).
OUTPUT:
Enter a Number:
1193
1193
1931
9311
3119
import java.util.*;
class Twin_Prime
int n1,n2;
n1=sc.nextInt();
n2=sc.nextInt();
int c1=0,t1=0;
for(int i=1;i<=n1;i++)
if(n1%i==0)
t1=0;
else
t1=1;
int c2=0,t2=0;
for(int j=1;j<=n2;j++)
if(n2%j==0)
t2=0;
else
t2=1;
if(t1==0 && t2==0)//Checking whether both the Numbers are Prime or not
System.out.println("Twin Prime");
else
else if(t1==1&&t2==1)
}
ALGORITHM:
OUTPUT:
Enter a Number(1):
137
Enter a Number(2):
139
Twin Prime
8. Goldbach Number
/** A Goldbach number is a positive even integer that can be expressed as sum of two odd primes.
* FInd and display all the odd prime pair whose sum is equal to N. */
import java.util.*;
class Goldbach
int c=0,t=0;
for(int i=1;i<=x;i++)
if(x%i==0)
return true;//True
else
int n=sc.nextInt();
if(n%2!=0)
System.out.println("Invalid Input");
else
}}}
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class Goldbach
3. Define the isPrime method:
a. static boolean IsPrime(int x)
b. Initialize int c to 0.
c. For i from 1 to x (inclusive):
i. If x is divisible by i, increment c.
d. If c is equal to 2, return true; otherwise, return false.
4. Define the main method:
a. public static void main(String args[])
b. Create a Scanner object (sc).
c. Prompt the user to enter a number (n) and read the input.
5. Check for Valid Input:
- If n is odd, print "Invalid Input" and exit the program.
6. Find and Display Prime Pairs:
a. Print "Pair of Prime Numbers: ".
b. Use nested loops to iterate over odd numbers from 3 to n (inclusive) and vary
the value of i.
c. For each i, iterate over values from i to n and vary the value of j.
d. Check if both i and j are prime using the IsPrime method.
e. If both are prime, check if the sum of i and j is equal to n.
f. If true, print the pair: "i + j = n".
7. Close the Scanner object (sc).
OUTPUT:
12
5 + 7 = 12
9. Combinations
/** A positive number can be expressed as a combination of consecutive number that adds up to the
number.
import java.util.*;
class Combinations
int n=sc.nextInt();
System.out.println("Combinations: ");
int sum=0,x=i;
x++;//Increment
System.out.println(comb);
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class Combinations
3. Define the main method:
a. public static void main(String args[])
b. Create a Scanner object (sc).
c. Prompt the user to enter a positive number (n) and read the input.
4. Display Combinations:
a. Print "Combinations: ".
b. Use a for loop to iterate from i=1 to n/2 (inclusive):
i. Initialize int sum to 0 and int x to i.
ii. Initialize an empty string comb to store combinations.
iii. Use a while loop to iterate while sum is less than n:
- Add x to sum.
- Append x + " + " to the comb string.
- Increment x.
iv. If sum is equal to n, print the combination string (comb).
5. Close the Scanner object (sc).
OUTPUT:
100
Combinations:
9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 +
18 + 19 + 20 + 21 + 22 +
10. Unique Digit Number
* Unique Digit Number: Positive Number whose digits are not repeated */
import java.util.*;
class Unique_Digit
int r1,r2,n1,n2,c=0;
n1=x;
n2=x;
while(n1>0)
while(n2>0)
c++;
if(c==1)
return true;
else
return false;
int m,n;
m=sc.nextInt();
n=sc.nextInt();
int freq=0;
for(int i=m;i<=n;i++)
if(IsUnique_Digit(i)==true)
System.out.print(i+" ");
freq++;
System.out.println();
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class Unique_Digit
3. Define the isUnique_Digit method:
a. static boolean IsUnique_Digit(int x)
b. Initialize int n1 and n2 to x.
c. Initialize int c to 0.
d. Use a while loop to iterate while n1 is greater than 0:
i. Extract the last digit of n1 and store it in r1.
ii. Use another while loop to iterate while n2 is greater than 0:
- Extract the last digit of n2 and store it in r2.
- If r1 is equal to r2, increment the counter c.
- Remove the last digit of n2.
iii. Remove the last digit of n1.
e. If c is equal to 1, return true; otherwise, return false.
4. Define the main method:
a. public static void main(String args[])
b. Create a Scanner object (sc).
c. Declare int m, n, and freq.
d. Prompt the user to enter the starting limit (m) and read the input.
e. Prompt the user to enter the ending limit (n) and read the input.
f. Initialize freq to 0.
5. Check and Display Unique Digit Numbers:
a. Print "Frequency of Unique Digit Numbers is: ".
b. Use a for loop to iterate from i=m to n (inclusive).
c. Inside the loop, check if IsUnique_Digit(i) is true:
i. Print the current value of i.
ii. Increment freq.
6. Display Frequency:
- Print the value of freq.
7. Close the Scanner object (sc).
OUTPUT:
50
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 23 24 25 26 27 28 29 30 31 32 34 35 36 37 38 39
40 41 42 43 45 46 47 48 49 50
ARRAYS
1. TwoD_Array
* Data Members:
* Member Methods:
import java.util.*;
class TwoD_Array
int m;//Rows
int n;//Columns
m=mm;
n=nn;
a=new int[m][n];
void fill()
a[i][j]=sc.nextInt();//Input
void display()
System.out.println("Array:(Matrix Form)");
{
for(int j=0;j<n;j++)//Loop for Columns(n)
System.out.print(a[i][j]+" ");//Output
System.out.println();
td.fill();
td.display();
ALGORITHM:
OUTPUT:
Enter Elements:
Array:(Matrix Form)
123
456
789
2. TwoD_Min_Max
/** Program to find Minimum and Maximum Element of a Two Dimensional Array */
import java.util.*;
class TwoD_Min_Max
{
Scanner sc=new Scanner(System.in);
int m=3,n=3;
a[i][j]=sc.nextInt();//Input
System.out.println("Array:(Matrix Form)");
System.out.print(a[i][j]+" ");//Output
System.out.println();
int min=a[0][0];
if(a[i][j]<min)
min=a[i][j];
}
int max=a[0][0];
if(a[i][j]>max)
max=a[i][j];
ALGORITHM:
Enter Elements:
Array:(Matrix Form)
123
456
789
Minimum Number 1
Maximum Number 9
3. TwoD_Symmetric
import java.util.*;
class TwoD_Symmetric
{
int m=3,n=3;
a[i][j]=sc.nextInt();//Input
System.out.println("Array:(Matrix Form)");
System.out.print(a[i][j]+" ");//Output
System.out.println();
int k=0;
if(a[i][j]==a[j][i])
k=1;
if(k==1)
System.out.println("Symmetric");
else
System.out.println("Not Symmetric");
ALGORITHM:
OUTPUT:
Enter Elements:
Array:(Matrix Form)
751
583
134
Symmetric
4. TwoD_TRC_Sums
/** Program to find Total Sum, Row Sums and Column Sums of a Two Dimensional Array */
import java.util.*;
class TwoD_TRC_Sums
int m=3,n=3;
a[i][j]=sc.nextInt();//Input
System.out.println("Array:(Matrix Form)");
System.out.print(a[i][j]+" ");//Output
System.out.println();
int totalsum=0;
int rowsum=0,colsum=0;
totalsum+=a[i][j];
rowsum+=a[i][j];
colsum+=a[j][i];
System.out.println();
System.out.println();
ALGORITHM:
OUTPUT:
Enter Elements:
Array:(Matrix Form)
123
456
789
/** Program to find Sum of Left and Right Diagonal Elements of a Two Dimensional Array */
import java.util.*;
class TwoD_Diagonal_Sums
a[i][j]=sc.nextInt();//Input
System.out.println("Array:(Matrix Form)");
System.out.print(a[i][j]+" ");//Output
System.out.println();
}
int t=m=n;
int ldsum=0,rdsum=0;
if(i==j)
ldsum+=a[i][j]; //Left Diagonal Sum Logic ie. When row no.==column no.
else if(i!=j)
rdsum+=a[i][t-(i+1)]; //Right agonal Sum Logic ie. When row increases and column
decreases simaltaneously
rdsum=rdsum/2;
ALGORITHM:
OUTPUT:
Enter Elements:
Array:(Matrix Form)
123
456
789
import java.util.*;
class TwoD_Row_Sort
int m=3,n=3;
a[i][j]=sc.nextInt();//Input
System.out.println("Array:(Matrix Form)");
System.out.print(a[i][j]+" ");//Output
System.out.println();
for(int i=0;i<m;i++)
for(int j=0;j<n-1;j++)
for(int k=0;k<n-j-1;k++)
if(a[i][k]>a[i][k+1])//Ascending Order
int t=a[i][k];
a[i][k]=a[i][k+1];
a[i][k+1]=t;
System.out.print(a[i][j]+" ");//Output
System.out.println();
}
}
ALGORITHM:
OUTPUT:
Enter Elements:
10
3
2
Array:(Matrix Form)
10 9 8
765
432
8 9 10
567
234
7. TwoD_Column_Sort
import java.util.*;
class TwoD_Column_Sort
int m=3,n=3;
a[i][j]=sc.nextInt();//Input
System.out.println("Array:(Matrix Form)");
for(int i=0;i<m;i++)//Loop for Rows(m)
System.out.print(a[i][j]+" ");//Output
System.out.println();
for(int i=0;i<m;i++)
for(int j=0;j<n-1;j++)
for(int k=0;k<n-j-1;k++)
int t=a[k][i];
a[k][i]=a[k+1][i];
a[k+1][i]=t;
System.out.print(a[i][j]+" ");//Output
}
System.out.println();
ALGORITHM:
OUTPUT:
Enter Elements:
1
2
Array:(Matrix Form)
937
612
924
612
924
937
8. TwoD_NB_B
/** Program to declare a square matrix A[][] of order (M × M) where 'M' must be greater than 3 and
less than 10.
Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:
(i)Sort the non-boundary elements in ascending order using any standard sorting technique and
rearrange them in the matrix.
(iii)Display the Original matrix, Rearranged matrix and only the Diagonal elements of the rearranged
matrix with their sum. */
import java.util.*;
class TwoD_NB_B
int m=sc.nextInt();
a[i][j]=sc.nextInt();//Input
System.out.print(a[i][j]+" ");//Output
System.out.println();
int k=0;
for(int i=1;i<m-1;i++)
for(int j=1;j<m-1;j++)
for(int i=0;i<k-1;i++)
for(int j=0;j<k-i-1;j++)
{
if(b[j]>b[j+1]) //Ascending Order Sort
int t=b[j];
b[j]=b[j+1];
b[j+1]=t;
k=0;
for(int i=1;i<m-1;i++)
for(int j=1;j<m-1;j++)
System.out.print(a[i][j]+" ");//Output
System.out.println();
int dsum=0;
if((i==j) || (i+j==m-1))
System.out.print(a[i][j]+"\t");
else
System.out.print("\t");
System.out.println();
ALGORITHM:
OUTPUT:
Enter Elements:
4
Original Array:(Matrix Form)
824
653
014
824
653
014
Diagonal Elements:
8 4
0 4
9. Mirror
import java.util.*;
class Mirror
int m=3;
a[i][j]=sc.nextInt();//Input
System.out.println("Array:(Matrix Form)");
System.out.print(a[i][j]+" ");//Output
System.out.println();
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
b[i][m-1-j]=a[i][j];
System.out.print(b[i][j]+" ");//Output
System.out.println();
ALGORITHM:
OUTPUT:
Enter Elements:
Array:(Matrix Form)
123
456
789
321
654
987
10. Denomination
/** Program to accept amount from user and display the break-up in descending order denominaion
along with total no. of notes */
import java.util.*;
class Denomination
int amt=sc.nextInt();
int c=amt;
int rev=0;
int d=0;
while(c>0)
d=c%10;
rev=rev*10+d;
c=c/10;
while(rev>0)
d=rev%10;
switch(d)
case 0:
System.out.println("ZERO");
break;
case 1:
System.out.println("ONE");
break;
case 2:
System.out.println("TWO");
break;
case 3:
System.out.println("THREE");
break;
case 4:
System.out.println("FOUR");
break;
case 5:
System.out.println("FIVE");
break;
case 6:
System.out.println("SIX");
break;
case 7:
System.out.println("SEVEN");
break;
case 8:
System.out.println("EIGHT");
break;
case 9:
System.out.println("NINE");
break;
rev=rev/10;
System.out.println("OUTPUT: ");
int total=0;
int t=0;
while(amt!=0)
rev=amt/den[t];
if(rev!=0)
{
System.out.println(den[t]+"x"+rev+"="+rev*den[t]);
total+=rev;
amt=amt%den[t];
t++;
ALGORITHM:
OUTPUT:
Enter Amount:
4568321
FOUR
FIVE
SIX
EIGHT
THREE
TWO
ONE
OUTPUT:
1000x4568=4568000
100x3=300
20x1=20
1x1=1
STRING
1. String_Tokenizer
/** Program to input a Sentence and check for Validity and Display the OG Sentence.
* Convert the Sentence into Uppercase and Display the New Sentence.
* Also store the words along with their lengths */
import java.util.*;
class String_Tokenizer
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
int w=st.countTokens();
while(st.hasMoreTokens())
String s1=st.nextToken();
int l1=s1.length();
System.out.println(s1+"\t"+l1);
else
System.out.println("Invalid Input");
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class String_Tokenizer
3. Declare class members:
a. Scanner sc
b. String s
c. int l
d. char ch
4. Input sentence:
a. Print "Enter a Sentence: "
b. Read a line from the user and store it in s
c. Get the length of the sentence and store it in l
d. Extract the last character of the sentence and store it in ch
5. Check sentence validity:
a. Check if the last character is '.', '?' or '!'
b. If false, print "Invalid Input" and terminate the program
6. Convert sentence to uppercase:
a. Convert the sentence to uppercase using s.toUpperCase() and store the result
back in s
7. Remove ending punctuation:
a. Remove the last character from the sentence using s.substring(0, l-1)
8. Tokenize the sentence:
a. Use StringTokenizer to tokenize the modified sentence
b. Count the number of tokens and store it in w
c. Print "Total Number of Words: " followed by the value of w
9. Display words and their lengths:
a. Print "Words are: "
b. Use a loop with StringTokenizer to iterate through each word
c. For each word, get its length and display the word along with its length
OUTPUT:
Enter a Sentence:
Hi hello Java!
Words are:
HI 2
HELLO 5
JAVA 4
2. VowelConsonant
/** Program to display the Frequency of Vowels and Consonants in each word in a sentence */
import java.util.*;
class VowelConsonant
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
int w=st.countTokens();
System.out.println("Words:"+"\t"+"Vowels"+"\t"+"Consonants");
while(st.hasMoreTokens())
String s1=st.nextToken();
int l1=s1.length();
int vc=0,cc=0;
for(int i=0;i<l1;i++)
char ch1=s1.charAt(i);
vc++;
cc++;
}
System.out.println(s1+"\t"+vc+"\t"+cc);
else
System.out.println("Invalid Input");
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class VowelConsonant
3. Declare class members:
a. Scanner sc
b. String s
c. int l
d. char ch
4. Input sentence:
a. Print "Enter a Sentence: "
b. Read a line from the user and store it in s
c. Get the length of the sentence and store it in l
d. Extract the last character of the sentence and store it in ch
5. Check sentence validity:
a. Check if the last character is '.', '?' or '!'
b. If false, print "Invalid Input" and terminate the program
6. Convert sentence to uppercase:
a. Convert the sentence to uppercase using s.toUpperCase() and store the result
back in s
7. Remove ending punctuation:
a. Remove the last character from the sentence using s.substring(0, l-1)
8. Tokenize the sentence:
a. Use StringTokenizer to tokenize the modified sentence
b. Count the number of tokens and store it in w
c. Print "Number of Words: " followed by the value of w
d. Print column headers: "Words", "Vowels", "Consonants"
9. Display frequency of vowels and consonants:
a. Use a loop with StringTokenizer to iterate through each word
b. For each word, initialize vc (vowel count) and cc (consonant count) to 0
c. Use a nested loop to iterate through each character of the word
d. For each character, check if it's a vowel (A, E, I, O, U) and increment vc,
otherwise increment cc
e. Print the word, vc, and cc for each word
OUTPUT:
Enter a Sentence:
it is a good day!
Number of Words: 5
IT 1 1
IS 1 1
A 1 0
GOOD 2 2
DAY 1 2
3. Alphabetical_Order
import java.util.*;
class Alphabetical_Order
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
String s1[]=s.split(" "); //Splits the sentence into words ie, through " "
int l1=s1.length;
for(int i=0;i<l1;i++)
for(int j=0;j<l1-i-1;j++)
if(s1[j].compareTo(s1[j+1])>0)//Bubble Sort
String t=s1[j];
s1[j]=s1[j+1];
s1[j+1]=t;
for(int i=0;i<l1;i++)
System.out.print(s1[i]+" ");
else
System.out.println("Invalid Input");
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class Alphabetical_Order
3. Declare class members:
a. Scanner sc
b. String s
c. int l
d. char ch
4. Input sentence:
a. Print "Enter a Sentence: "
b. Read a line from the user and store it in s
c. Get the length of the sentence and store it in l
d. Extract the last character of the sentence and store it in ch
5. Check sentence validity:
a. Check if the last character is '.', '?' or '!'
b. If false, print "Invalid Input" and terminate the program
6. Convert sentence to uppercase:
a. Convert the sentence to uppercase using s.toUpperCase() and store the result
back in s
7. Remove ending punctuation:
a. Remove the last character from the sentence using s.substring(0, l-1)
8. Split sentence into words:
a. Use String.split(" ") to split the modified sentence into an array of words,
stored in s1
9. Sort words alphabetically:
a. Use a nested loop with the Bubble Sort algorithm to arrange the words in
alphabetical order
b. Compare adjacent words using compareTo and swap them if necessary
10. Display rearranged sentence:
a. Print "Rearranged Sentence: " followed by the sorted words separated by spaces
OUTPUT:
Enter a Sentence:
Rearranged Sentence:
4. Ceaser_Cipher
/** Program to shift the characters of a string by 13 according to their postion in alphabets(26) */
import java.util.*;
class Ceaser_Cipher
String s=sc.nextLine();
int l=s.length();
s.toUpperCase();
String ss="";
for(int i=0;i<l;i++)
char ch=s.charAt(i);
if(ch<'N')
ss+=(char)(ch+13);
else
ss+=(char)(ch-13);
System.out.println(ss);
}
ALGORITHM:
OUTPUT:
Enter a Sentence:
I am a good boy!
V-T`-T-ZbbW-Ubl.
5. Unique_Word
Member methods:
boolean check(): checks and returns true if word starts and ends with a vowel.
void show(): Displays thw word starting and ending with a vowel by invoking boolean check() */
import java.util.*;
class Unique_Word
String word;
int len;
Unique_Word()
word="";
len=0;
}
Scanner sc=new Scanner(System.in);
void accept()
word=sc.next();
len=word.length();
boolean checkv()
char ch1=word.charAt(0);
char ch2=word.charAt(len-1);
if((ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U') &&
(ch2=='A'||ch2=='E'||ch2=='I'||ch2=='O'||ch2=='U'))
return true;
else
return false;
void display()
if(checkv()==true)
else
System.out.println("The Word "+word+" does not start and end with a vowel.");
uw.display();
ALGORITHM:
OUTPUT:
Enter a Word(Uppercase):
APPLE
/** Program to display all the words of sentence which starts and ends with a vowel along with their
frequencies. */
import java.util.*;
class Unique_Sentence
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
int w=st.countTokens();
while(st.hasMoreTokens())
String s1=st.nextToken();
int l1=s1.length();
int vc=0;
int flag=0;
char ch1=s1.charAt(0);
for(int i=0;i<l1;i++)
char chi=s1.charAt(i);
char ch2=s1.charAt(l1-1);
if((ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U') &&
(ch2=='A'||ch2=='E'||ch2=='I'||ch2=='O'||ch2=='U'))
flag=1;
if((chi=='A'||chi=='E'||chi=='I'||chi=='O'||chi=='U'))
vc++;
if(flag==1)
System.out.println(s1+"\t"+vc+" Vowels");
else
System.out.println("Invalid Input");
ALGORITHM:
1. Import java utilities: import java.util.*;
2. Define the class: class Unique_Sentence
3. Main method:
a. Create a Scanner object sc for input.
b. Print "Enter a Sentence: ".
c. Read a sentence from the user and store it in the variable s.
d. Get the last character (ch) of the sentence.
4. Check Sentence Validity:
- Check if ch is '.', '?', or '!'. If not, print "Invalid Input" and exit the
program.
5. Preprocess Sentence:
a. Convert the sentence to uppercase using s.toUpperCase().
b. Remove the last character using s.substring(0, l-1).
6. Tokenization:
- Tokenize the preprocessed sentence using StringTokenizer st = new
StringTokenizer(s).
7. Count Number of Words:
- Get the total number of words using st.countTokens() and store it in w.
- Print "Total Number of Words: " + w.
8. Word Processing Loop:
- Iterate through each word using a while loop while (st.hasMoreTokens()):
a. Get the next word using String s1 = st.nextToken().
b. Get the length of the word using int l1 = s1.length().
9. Word Processing:
a. Initialize variables vc (vowel count) and flag to 0.
b. Get the first character (ch1) of the word.
c. Iterate through each character in the word using a for loop:
i. Get the current character (chi) and the last character (ch2) of the word.
ii. Check if the word starts and ends with a vowel.
iii. If yes, set flag to 1 and count the vowels (vc).
d. If flag is 1, print "Number of Words starting and ending with Vowels:" and
display the word and its vowel count.
10. Output Invalid Input:
- If the sentence does not end with '.', '?', or '!', print "Invalid Input".
OUTPUT:
Enter a Sentence:
APPLE 2 Vowels
A 1 Vowels
AWA 2 Vowels
7. Palindrome_Sentence
/** Program to display only the palindrome words of a sentence along with their frequencies. */
import java.util.*;
class Palindrome_Sentence
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
int w=st.countTokens();
int pw=0;
while(st.hasMoreTokens())
String s1=st.nextToken();
int l1=s1.length();
String rev="";
int flag=0;
for(int i=l1-1;i>=0;i--)
char chr=s1.charAt(i);
rev+=chr;
if(s1.compareTo(rev)==0)
flag=1;
pw++;
if(flag==1)
System.out.println(rev);
else
System.out.println("Invalid Input");
ALGORITHM:
OUTPUT:
Enter a Sentence:
Palindrome Words:
MADAM
MALAYALAM
8. Insert_Word
import java.util.*;
class Insert_Word
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
String word=sc.next();
word=word.toUpperCase();
int pos=sc.nextInt();
String s1[]=s.split(" "); //Splits the sentence into words ie, through " "
int l1=s1.length;
String s2[]=new String [l1+1]; //New array with an extra block [] for insertion of a word
s2[pos-1]=word; //Assigning the word to be insrted in the required position in the new array
for(int i=0;i<=l1;i++)
else
System.out.println("Invalid Input");
}
ALGORITHM:
OUTPUT:
Enter a Sentence:
It is a day.
good
IT IS A GOOD DAY
9. Delete _Word
import java.util.*;
class Delete_Word
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
String word=sc.next();
word=word.toUpperCase();
int pos=sc.nextInt();
String s1[]=s.split(" "); //Splits the sentence into words ie, through " "
int l1=s1.length;
String ns="";
for(int i=0;i<pos-1;i++) //Loop for framing words before the position/delete word
for(int i=pos;i<l1;i++) //Loop for framing words after the position/delete word
ns=ns+" "+s1[i]; //Equating words after position/delete word by excluding the word in the
position
System.out.println(ns); //Output
else
System.out.println("Invalid Input");
}
}
ALGORITHM:
Enter a Sentence:
bad
HE IS A GOOD PERSON
10. Potential_Word
class Potential_Word
String s=sc.nextLine();
int l=s.length();
char ch=s.charAt(l-1);
if(ch=='.'||ch=='?'||ch=='!')
s=s.toUpperCase();
s=s.substring(0,l-1);
int w=st.countTokens();
while(st.hasMoreTokens())
String s1=st.nextToken();
int l1=s1.length();
int sum=0;
for(int i=0;i<l1;i++)
char chw=s1.charAt(i);
sum=sum+((int)(chw));
System.out.println(s1+"-"+sum);
}
}
else
System.out.println("Invalid Input");
ALGORITHM:
Enter a Sentence:
Potential of Words:
STUDENTS-634
ARE-216
STUDYING-631
IN-151
SCHOOL-456