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

Class 12 Java Programs ISC

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)
37 views6 pages

Class 12 Java Programs ISC

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
You are on page 1/ 6

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 java.util.*; //importing package
class program_05{ //declaring class
String s,a[];
program_05(String x){
s=x;
a=s.split(" ");
}
void display(){ //displaying frequency of words
int count=1,f=0;
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i].equals(a[j]) && a[i]!="*"){
a[j]="*";
count++;
}
}

9
if(a[i]!="*"){
System.out.println(a[i]+" "+count);
a[i]="*";
}
count=1;
f++;
}
System.out.println("Total words="+f);
}
public static void main (String args[]){
Scanner scr=new Scanner(System.in);
System.out.println("Enter a string:");
String str=scr.nextLine();
program_05 ob=new program_05(str);
ob.display();
}
} //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 java.util.*; //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))
System.out.println("yes, it's a smith number");
else
System.out.println("no, it’s not smith number");
}
public static void main(String[] args){
Scanner scr=new Scanner(System.in);
System.out.println("enter a num");
int num=scr.nextInt();
program_06 ob=new program_06(num);
ob.display();
}
} //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 java.util.*; //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(System.in);
System.out.println("Enter the decimal number:");
int n=scr.nextInt();
program_07 ob=new program_07();
System.out.println("Binary Equivalent: "+ob.binary(n));
System.out.println("Octal Equivalent: "+ob.octo(n));
System.out.println("HexaDecimal Equivalent: "+ob.hex(n));
}
}
Output:

14

You might also like