0% found this document useful (0 votes)
33 views4 pages

Board Programes

The document contains code snippets for 3 different Java programs: 1. A program to count the frequency of words in two input strings. It initializes the strings, tokenizes them, and counts the tokens. 2. A program to sort an array of 10 integers using bubble sort. It takes input, performs bubble sort, and prints the sorted array. 3. A program to check if a number is a Smith number by calculating the sums of digits of the number and its prime factors.

Uploaded by

ashley mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Board Programes

The document contains code snippets for 3 different Java programs: 1. A program to count the frequency of words in two input strings. It initializes the strings, tokenizes them, and counts the tokens. 2. A program to sort an array of 10 integers using bubble sort. It takes input, performs bubble sort, and prints the sorted array. 3. A program to check if a number is a Smith number by calculating the sums of digits of the number and its prime factors.

Uploaded by

ashley mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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