Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

// program to count the frequeency of words in the entered two sentences.

Algorithm
1. Initialize the two strings st1, st2 using string tokenizer.
2. Condition checked whether the two tokens finished, hasMoreTokens().
3. Whenever a word is reached each time count c, c2 is increased of two strings.

import java.util.*;
class tokens
{
public static void main(String str,String str1)
{
int c=0,c1=0;
String str2,str3;
StringTokenizer st1= new StringTokenizer(str);
StringTokenizer st2= new StringTokenizer(str1);
System.out.println("The different tokens");

while(st1.hasMoreTokens())
{
str2=st1.nextToken();

System.out.println(str2);
c=c+1;
}
while(st2.hasMoreTokens())
{
str3=st2.nextToken();
System.out.println(str3);
c1=c1+1;
}
System.out.println("Number of tokens in first program="+c);
System.out.println("Number of tokens in second string="+c1);
}
}
// pgm to display the sorted array of 10 elements using the bubble sort
Algorithm
Run a nested for loop to traverse the input array using two variables i and j, such that 0 ≤ i < n-1
and 0 ≤ j < n-i-1
2.If arr[j] is greater than arr[j+1] then swap these adjacent elements, else move on
3.Print the sorted array

import java.util.*;
public class bubble
{
public static void main(String[] args)
{
int n=10, i, j, x=0;
int array[] = new int[10];
Scanner s = new Scanner(System.in);

System.out.print("Enter 10 Elements in Random Order: ");


for(i=0; i<n; i++)
{
array[i] = s.nextInt();
}

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


{
for(j=0; j<(9-i); j++)
{
if(array[j]<array[j+1])
{
x = array[j];
array[j] = array[j+1];
array[j+1] = x;
}
}
}
System.out.println("\nThe new sorted array is:");
for(i=0; i<n; i++)
System.out.print(array[i]+ " ");
}
}
3. Smith number
Algorithm
 Read or initialize a number from the user.
 Find the sum of its digits.
 Find the prime factors of the given number.
 Determine the sum of digits of its prime factors.
 Compare the sum of digits with the sum of digits of its prime factors. If they are equal, the
given number is a smith.

import java.util.*;
class SmithNumber
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,f=2,m,t,s1=0,s2=0,d;
System.out.println("Enter the 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;
}
n=n/f;
}
else
f++;
}
t=m;
while(t!=0)
{
d=t%10;
s2+=d;
t/=10;
}
if(s1==s2)
System.out.println("Smith number");
else
System.out.println("Not a smith number");
}
}

You might also like