0% found this document useful (0 votes)
31 views6 pages

Java Programs for ISC Class 12 Projects

The document contains three programming tasks: one for counting words and their frequencies in a string, another for checking if a number is a Smith number, and the last for converting a decimal number to binary, octal, and hexadecimal using recursion. Each task is accompanied by an algorithm and Java code implementation. The document provides detailed steps and code for each problem, demonstrating various programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Java Programs for ISC Class 12 Projects

The document contains three programming tasks: one for counting words and their frequencies in a string, another for checking if a number is a Smith number, and the last for converting a decimal number to binary, octal, and hexadecimal using recursion. Each task is accompanied by an algorithm and Java code implementation. The document provides detailed steps and code for each problem, demonstrating various programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

QUESTION 05: Write a program to input a string and find the total number of words and the

frequency of each word.

Algorithm:

1. Start.
2. Accept and store the sentence in STR
3. Split the words of the sentenced store each word in the string array A[]
4. COUNT=1
5. FLAG=0
6. Store the length of the array A[] in L
7. Repeat for I=0,1,2,..L
Repeat for J=I+1,.....L
If (A[I] is equal of A[J] and A[I] is not equal “*”)
A[J]=”*”
Increment of COUNT
End If
End loop J
If(A[I] = “*”)
Display A[I] and COUNT
A[I]= “*”
End If
COUNT =1
Increment of FLAG
end loop I
8. Display (“Total words”) and FLAG.
9. Stop.

Code:
import [Link].*; //importing package
class program_05{ //declaring class
String s,a[];
program_05(String x){
s=x;
a=[Link](" ");
}
void display(){ //displaying frequency of words
int count=1,f=0;
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
if(a[i].equals(a[j]) && a[i]!="*"){
a[j]="*";
count++;
}
}

9
if(a[i]!="*"){
[Link](a[i]+" "+count);
a[i]="*";
}
count=1;
f++;
}
[Link]("Total words="+f);
}
public static void main (String args[]){
Scanner scr=new Scanner([Link]);
[Link]("Enter a string:");
String str=[Link]();
program_05 ob=new program_05(str);
[Link]();
}
} //end of class

Output:

QUESTION 6: A smith number is a composite number, the sum of whose digits is the sum of the
digits of its prime factors obtained as a result of prime factorization (excluding 1). Check if a given
number is a smith number or not.

Algorithm:

1. Start.
2. Accept and store the number in NUM
3. [ function digitSum(n) to get the sum of the digits in the number]
while(n>0)
SUM=SUM+(n % 10)
n=n/10
end while loop
returns SUM

10
4. [function prime(n) to check for prime]
COUNT=0
for i=2,3,....(n/2)
if(n % i =0)
increment of COUNT
End for loop i
if (COUNT =0)
returns true
else
returns false
5. [function factorSum(n) To find the sum of prime factors]
i=2
PrimeSum=0
while (n>1)
if(n % i=0)
PrimeSum=PrimeSum+ (Call function 3(i))
n=n/10
end If
else
do
increment of i
end do while(! prime(i))
end else
end while loop
returns PrimeSum
6. [Displaying the output]
if(digitSum(NUM)=factorSum(NUM)
print “THE NUMBER IS A SMITH NUMBER”
else
print “THE NUMBER IS NOT A SMITH NUMBER”
7. Stop.

Code:
import [Link].*; //importing package
public class program_06 { //declaring class name
int n,psum,sum;
program_06(int x){
n=x;
}
int digitSum(int a){ //returns the sum of the digits
sum=0;
while(a>0){
sum+=(a%10);
a=a/10;

11
}
return sum;
}
boolean prime(int y){ //checks for prime number
int c=0;
for(int i=2;i<y/2;i++) {
if(y%i==0)
c++;
}
if(c==0)
return true;
else
return false;
}
int factorSum(int b){ //finds the sum of its prime factors
int i=2;
psum=0;
while(b>1){
if(b%i==0){
psum+=digitSum(i);
b=b/i;
}
else{
do{
i++;
}while(!prime(i));
}
}
return psum;
}
void display (){
if(digitSum(n)==factorSum(n))
[Link]("yes, it's a smith number");
else
[Link]("no, it’s not smith number");
}
public static void main(String[] args){
Scanner scr=new Scanner([Link]);
[Link]("enter a num");
int num=[Link]();
program_06 ob=new program_06(num);
[Link]();
}
} //end of class

12
Output:

QUESTION 07: Write a program to convert a decimal number to binary octal and hexadecimal
systems using Recursion Technique.

Algorithm:

1. Start
2. Accept and store the decimal number in NUM.
3. [instance variable]
H={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}
String BIN=null, OCT=null, HEXA=null
4. [recursive function binary( n) to convert into binary equivalent]
if(n>0)
BIN=(n%2)+BIN
binary(n/2)
end if
returns BIN
5. [recursive function octal( n) to convert into octal equivalent]
if(n>0)
OCT=(n%8)+OCT
octal(n/8)
end if
returns OCT
6. [recursive function hexadecimal( n) to convert into hexadecimal equivalent]
if(n>0)
HEXA=H[n%16]+HEXA
hexadecimal(n/16)
end if
returns HEXA
7. call
8. Stop.

Code:
import [Link].*; //importing package

13
class program_07{ //declaring class name
String bin="",oct="",hexa="";
char h[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
String binary(int n){ //converts into binary equivalent
if(n>0){
bin=(n%2)+bin;
binary(n/2);
}
return bin;
}
String octo(int n){ //converts to octal equivalent
if(n>0){
oct=(n%8)+oct;
octo(n/8);
}
return oct;
}
String hex(int n){ //converts to hexadecimal equivalent
if(n>0){
hexa=h[(n%16)]+hexa;
hex(n/16);
}
return hexa;
}
public static void main(String args[]){
Scanner scr=new Scanner([Link]);
[Link]("Enter the decimal number:");
int n=[Link]();
program_07 ob=new program_07();
[Link]("Binary Equivalent: "+[Link](n));
[Link]("Octal Equivalent: "+[Link](n));
[Link]("HexaDecimal Equivalent: "+[Link](n));
}
}
Output:

14

Common questions

Powered by AI

The algorithm tracks word repetitions by maintaining a word list and marking repeated words with a placeholder ('*'). It first splits the input string into words and stores them in an array. The nested loops compare each word to the ones following it. If a word reappears, it is marked with '*', and a counter is incremented. Subsequently, when processing that word, if it is '*', it is skipped, ensuring each word is processed only once .

Misclassification can occur due to incorrect prime factorization or digit sum calculations. Errors in handling non-prime factors, omitting relevant factors, or calculating digit sums could lead to incorrect results. Ensuring accurate prime checks and using robust arithmetic operations can mitigate these issues. Implementing comprehensive test cases can help validate correctness .

To verify if a number is a Smith number, the algorithm calculates the sum of the digits of the number and matches it against the sum of the digits of its prime factors. The process involves computing the digit sum using the digitSum function and the sum of digits of its prime factors using the factorSum function, which involves prime factorization without using the number 1. If these two sums are equal, the number is classified as a Smith number .

In the hexadecimal conversion algorithm, the recursive function repeatedly divides the decimal number by 16, using the remainder to index into a character array that stores hexadecimal symbols ('0'-'9', 'A'-'F'). The indexed character is prepended to the result string. This process continues until the quotient is zero, building the hexadecimal representation in reverse order through recursive calls .

Recursion suits number system conversions due to its ease in handling repeated, similar operations like division and remainder collection, naturally modeling the stepwise conversion processes. However, potential drawbacks include high memory usage, deeper call stacks, and possible performance issues with very large numbers due to repeated function calls, which iterative approaches may handle more efficiently .

The recursive approach for converting a decimal number to binary involves repeatedly dividing the number by 2 and collecting the remainder. In each recursive call, the remainder of the division by 2 is stored and the function calls itself with the quotient. This process continues until the number is reduced to zero, at which point the accumulated remainders represent the binary form when read in reverse order .

The method involves dividing the number by successive integers starting from 2 to find the smallest prime factor repeatedly, incrementing as necessary. Each time a factor is found, its digit sum is added to a cumulative total. The challenge lies in ensuring the proper order of factorization and accurately calculating and comparing digit sums. Efficiency can be problematic with large numbers due to the intensive computation required for factorization and sum calculations .

The placeholder '*' is used in the word frequency algorithm to mark words that have already been counted, preventing them from being recounted. It serves as an indicator that a word has been processed and should be ignored in subsequent comparisons. This facilitates accurate frequency counting by ensuring each unique word's occurrence is registered only once .

String arrays are crucial as they facilitate the breakdown of the input sentence into individual words, enabling easy traversal, comparison, and frequency analysis. By storing each word in an array, the algorithm can efficiently iterate through the elements, compare them, and maintain a count of occurrences, assisting in the organized processing of the string .

The key differences lie in the base used during division and the symbols for remainders. The binary conversion divides by 2, using remainders 0 and 1. Octal conversion divides by 8, using remainders 0-7, and hexadecimal conversion divides by 16, using a character array for symbols 0-9 and A-F. Though structurally similar, each system's specific base influences the recursive division process and remainder handling .

You might also like