0% found this document useful (0 votes)
10 views43 pages

Comp 11th Records (1) 20

The document contains a series of Java programs aimed at teaching various programming concepts, including checking for superspy numbers, Hamming numbers, binary and decimal conversions, and unique digit integers. Each program is accompanied by an algorithm, source code, and variable description tables. The document serves as a practical guide for students learning computer applications and programming in Java.

Uploaded by

sanjaykarthikg05
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)
10 views43 pages

Comp 11th Records (1) 20

The document contains a series of Java programs aimed at teaching various programming concepts, including checking for superspy numbers, Hamming numbers, binary and decimal conversions, and unique digit integers. Each program is accompanied by an algorithm, source code, and variable description tables. The document serves as a practical guide for students learning computer applications and programming in Java.

Uploaded by

sanjaykarthikg05
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/ 43

COMPUTER

COMPUTER APPLICATIONS
APPLICATIONS
RECORD
RECORD

2024
2024 -- 25
25

SANJAY
SANJAY K K
XI
XI A
A
Roll
Roll No.
No. 27
27
SANJAY K

683
Program 11
Program
AIM : Define a class to accept a number and check if
it is a superspy or not. In a Superspy : No. of digits =
Sum of digits
Algorithm : Accept a number. Split the digits and
add them. Count the number of digits. Check if they
are equal.

SOURCE CODE:
import java.util.*;
public class superspy_Rec1
{
public static void main ( String args[]){
Scanner sc = new Scanner(System.in);
int digits,num,counter=0,sum=0;
boolean superspy=false;
System.out.println("ENTER A NUMBER TO CHECK IF IT IS A
SUPER SPY OR NOT");
num=sc.nextInt(); // entering number
while(num>0){
counter++; //counting digits
digits = num%10;
sum = sum + digits; //finding sum of digits
num=num/10;
}
if(counter==sum) //checking with condition
superspy=true;
else
superspy=false;
superspy_Rec1 obj = new superspy_Rec1();
obj.check(superspy); //calling the method to display
}
public void check( boolean supspy ){
if (supspy == true )
System.out.println("The entered number is a Superspy");
else
System.out.println("The entered number is not Superspy");
}
}
1
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

2
Program 2
Program 2
AIM : To check a positive integer if it is a Haming number and
display the result in the format given below :
3600= 2x2x2x2x3x3x5x5
Program should display error if negative number is entered.
Haming numbers only prime factors are 2 , 3 or 5.

Algorithm : Accept a number. Check if the factors of


the number are prime.Check if the prime factors are
either 2,3 or 5 only. If so print the same format given.

SOURCE CODE:
import java.util.*;
public class HamingNumber_Rec2

{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
HamingNumber_Rec2 obj = new HamingNumber_Rec2();
int a = sc.nextInt(); //accepting a number
boolean ifprime;
System.out.println("Enter a positive integer to check Haming number");
if(a<0)
System.out.println("ERROR : The number is negative"); // error message
else{
System.out.print( a + " " + "=" +"");
for(int i = 1 ; i<=a ; i++)
{

if (a%i==0)
{
ifprime= obj.checkprime(i);

if( ifprime==true)
{
while(a%i==0){
if( i==2 ){
System.out.print(i + "x"); //checking if the prime factor is 2
a=a/2;
}
if(i==3){
System.out.print(i + "x"); //checking if the prime factor is 3
a=a/3;
}
if(i==5){
System.out.print(i + "x"); //checking if the prime factor is 5
a=a/5;}
}

}}
}
3
}
System.out.println("its a Haming number");
}

public boolean checkprime(int n){


{
boolean ans;
int count=0;
for(int i =1 ; i<=n ; i++)
{
if (n%i==0)
count++;
}
if(count==2)
return true; // checking if it is a prime number
else
return false;
}
}
}

INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

4
Program 3
Program 3
AIM : To convert a binary number to decimal and a decimal number
to a binary number
Algorithm: DECIMAL TO BINARY : Accept a number. Find the
reminders on dividing by 2 and concat it . Reverse the string and add
“1” to the start of string.
BINARY TO DECIMAL : Accept a number. Split the digit and multiply
with 2 raised to powers going till max digits and add them.

SOURCE CODE:
iimport java.util.*;
public class BINARYDECIMAL_Rec3
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to convert from decimal to binary");
int n = sc.nextInt(); //number to convert from decimal to binary
String newnum = "";
while (n>1){
int rem = n%2;
newnum = newnum + rem; //splitting the reminders and concatting
n=n/2;}
String newstr="";
for (int i = newnum.length()-1 ; i>= 0 ; i--)
{
newstr = newstr + newnum.charAt(i); // reversing the string
}
System.out.println("1"+newstr); //adding 1 infront of the string
System.out.println("Enter a number to convert from binary to decimal");
int bin = sc.nextInt(); // number to convert from binary to decimal
BINARYDECIMAL_Rec3 obj = new BINARYDECIMAL_Rec3();
obj.decimal(bin); // calling the method
}
public void decimal(int d)
{
double num =0;
for(int i=0;d>0;i++)
{
num = num + (d%10) * Math.pow(2,i); //multiplying with 2 raised to power and
adding the number
d = d/10;
}
System.out.print(num);
}
} 5
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

6
Program 4
Program 4
AIM : Write a program to accept an even integer n and find all the
odd prime pairs.

Algorithm : Accept a number. Check if its factor is odd and Check if


the factor is prime and the sum of two odd prime factors is equal to
the number.Display the pair of odd prime numbers

SOURCE CODE:
import java.util.*;
public class Goldback_rec4
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
Goldback_rec4 obj = new Goldback_rec4();

if(a%2!=0 || a<0)
{
System.out.println(" Enter an even positive interger");
a = obj.input();
}
for(int i =2;i<a/2;i++)
{
if(obj.prime(i)==true&& obj.prime(a-i)==true) // checking if sum of two odd primes is that number
{
System.out.println("("+i+","+(a-i)+")");
}
}
}
public int input()
{
System.out.println("enter:");

Scanner sc = new Scanner (System.in);


int a = sc.nextInt(); //taking input
return a;
}
public boolean prime(int i)
{
for(int j =2;j<i;j++)
{
if(i%j==0)
return false; //checking prime
}
return true;
}
}

7
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

8
Program 5
Program 5
AIM : To check if it is a evil number or not. Evil number has even number of 1s in
its binary equivalent.

Algorithm: Accept a number and convert it to its binary form. Split the digits
from the binary number and check the frequency of 1 in that . If the frequency
leaves a reminder of 0 when divided by 2 (i.e. its even) then it is an evil number.

SOURCE CODE:
import java.util.Scanner;
public class Rec5_Evilnum
{
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = sc.nextInt(); //accepting a number
if (n < 0) {
System.out.println("Invalid Input"); //checking if positive
return;
}
int count = 0;
int pow = 0;
int binNum = 0;
while (n > 0) {
int d = n % 2;
if (d == 1)
count++; //counting the number of 1s appearing
binNum += (int)(d * Math.pow(10, pow)); //converting it to binary number
pow++;
n /= 2;
}
System.out.println("Binary number:" + binNum);
System.out.println("No. of 1's: " + count);
if (count % 2 == 0) //checking if number of 1s is even
System.out.println("It is an Evil Number");
else
System.out.println("It is Not an Evil Number");
}
}

9
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

10
Program 6
Program 6
AIM : Program to determine how many unique digit integers
are there in the range between m and n and output them.
Algorithm: Accept Two limits. Convert the number to string
and check if the all adjacent characters are repeating. If
repeating they are not unique else print the number

SOURCE CODE:
import java.util.*;
public class Rec6_unique
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two values less than 30000 where m<n");
System.out.print("Enter m");
int m = sc.nextInt(); //acceoting limits
System.out.print("Enter n");
int n = sc.nextInt(); //accepting limits
int z=0;
System.out.print("The unique numbers are: ");
for (int i = m+1; i < n;i++){
String a = ""+i;
boolean unique = true;
for (int k = 0; k < a.length() - 1; k++) {
for (int j = k + 1; j < a.length(); j++) {
if (a.charAt(k) == a.charAt(j)) { // if a character is repeating then it is not
unique
unique = false;
}
}
}
if (unique) {
z++; // counting the number of unique numbers
System.out.print(i+", "); //printing the numbers
}
}
System.out.println();
System.out.println("The number of unique numbers are: " + z);
}
} 11
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

12
Program 7
Program 7
Binary Search

SOURCE CODE:
import java.util.*;
public class Record7
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
int lb=0,ub=9,mid=0,posi=0;
int a[] = new int[10];
int flag=0;
System.out.println("Enter sorted elements");
for(int i = 0; i<10 ; i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter Search element");
int search = sc.nextInt();
while(lb<=ub){
mid=(lb+ub)/2; // mid index
if (a[mid]<search)
{
lb=mid+1;
}
else if (a[mid]>search)
{
ub=mid-1;
}
else if (a[mid]==search) //checking condition
{
flag=1;
posi=mid+1;
break;
}
}
if(flag==1)
{
System.out.println("Search success and element present at position: " + posi);
}
else
System.out.println("Search unsuccessful");
}
} 13
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

14
Program 8
Program 8
Selection Sort

SOURCE CODE:
import java.util.*;
public class Record8
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
String a[] = new String[10];
System.out.println("Enter unsorted words");
String small,temp;
int posi;
for(int i = 0; i<10 ; i++)
{
a[i]=sc.next();
}
for(int i = 0;i<10 ; i++)
{
small=a[i];
posi=i;
for(int j = i+1;j<1;j++)
{
if(small.compareTo(a[j])>0) //checking condition
{
small=a[j];
posi=j;}}
temp =a[i]; //swapping
a[i] = a[posi];
a[posi]=temp;
} System.out.println("Array in ascending order is :");
for(int e = 0 ; e <10 ; e++)
{
System.out.println(a[e] + "\t");
}
}
} 15
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

16
Program 9
Program 9
SOURCE CODE:
import java.util.*;
public class Record_9
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.print("number of elements:");
int len = sc.nextInt();
int a[] = new int [len];
for(int i = 0;i<a.length;i++)
{
a[i] = sc.nextInt();
}
int b [] = new int[len];
int max=0,temp=0,counter=0;
for(int n = (a.length/2);n!=a.length;n=(counter%2==0)?(n-counter):
(n+counter))
{
max = 0;
for(int i = 0;i < a.length;i++)
{
if(max<a[i]) //checking condition
{
max = a[i];
temp = i;
}
}
b[n] = max; //swapping
a[temp] = 0;
counter++;
}
for(int i=0; i<b.length ; i++)
System.out.println(b[i]);
}
} 17
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

18
Program 10
Program 10
Kaprikar number

SOURCE CODE:
import java.util.*;
public class Rec10_Kaprikar{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int a = n*n;
int l;
String s = Integer.toString(a);
if(s.length()%2==0){
l=s.length()/2;
}
else{
l = s.length()/2 + 1;
}
int sum=0,rem;
while(a!=0){
rem = a%(int)(Math.pow(10,l));
sum = sum+rem; //summing
a=a/(int)(Math.pow(10,l));
}
System.out.println("Sum is: "+ sum);
if(n==sum){ //checking condition
System.out.println("It is a Kaprikar number");
}
else{
System.out.println("Not a Kaprikar number");
}
}
}
19
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

20
Program 11
Program 11
ISBN NUMBER
SOURCE CODE:
import java.util.*;
public class Record11_ISBN
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the ISBN: ");
long isbn = in.nextLong();
int sum = 0, count = 0, m = 10;
while (isbn != 0) {
int digits = (int)(isbn % 10);
count++;
sum =sum+ (digits * m); //sum of digits
m--;
isbn /= 10;
}
if (count != 10) {
System.out.println("Not an ISBN number");
}
else if (sum % 11 == 0) { //checking condition
System.out.println("ISBN Number");
}
else {
System.out.println("Not an ISBN NUMBER");
}
}
}

21
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

22
Program 12
Program 12
Day number and Nth Day

SOURCE CODE:
import java.util.*;
public class Record12
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the day: ");
int day = sc.nextInt();
System.out.print("Enter the year: ");
int year = sc.nextInt();
System.out.print("Enter the time span in days: ");
int n = sc.nextInt();
String a[]= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "July",
"Aug","Sep", "Oct", "Nov", "Dec"};
int m[]= {31,28,31,30,31,30,31,31,30,31,30,31};
int index = 0;
int dnew= day + n;
if (year%4==0){
m[1] = 29;
}
for(int i = 0; day>m[i]; i++){
day = day - m[i]; //calculating date
index++;
}
System.out.println(day +" " + a[index] + " " + year);
int month = index;
for (int j = 0; dnew>m[j]; j++){
dnew = dnew - m[j]; //calculating new date
month++;
}
System.out.println(dnew +" " + a[month] + " " + year);
}
} 23
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

24
Program 13
Program 13
Prime Palindrome Number
SOURCE CODE:
import java.util.*;
public class PrimePalindrome
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter two limits m and n whre m<n");
int m = sc.nextInt();
int n = sc.nextInt();
int counter1 = 0;
int count=0;
for (int i = m; i <= n; i++) {
int num = i, revNum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
revNum = revNum * 10 + digit; //reversing
}
if (revNum == i) {
boolean Primecheck = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) { //checking prime
Primecheck = false;
break;
}
}
if (Primecheck==true) {
System.out.print(i + " ");
count++;
counter1++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
System.out.println();
System.out.println("No. of prime palindromes are: "+ counter1);
}
} 25
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

26
Program 14
Program 14
AIM : To calculate the number of days elapsed between two given dates

SOURCE CODE:
import java.util.*;
public class record14
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first date (dd mm yyyy): ");
int day1 = sc.nextInt();
int month1 = sc.nextInt();
int year1 = sc.nextInt();
System.out.println("Enter the second date (dd mm yyyy): ");
int day2 = sc.nextInt();
int month2 = sc.nextInt();
int year2 = sc.nextInt();
String a[]= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug","Sep", "Oct", "Nov", "Dec"};
int m[]= {31,28,31,30,31,30,31,31,30,31,30,31}; //days
if (year1%4==0){ //leap year check
m[1] = 29;
}
day1 = m[month1 - 1] - day1;
for(int i = month1; i < 12 ; i++){
day1 = day1 + m[i];
}
if (year2%4!=0){ // leap year check
m[1] = 28;
}
for(int j = 0; j < month2 - 1; j++){
day2 = day2 + m[j]; //finding total days
}
int sum = day1 + day2;
for (int i = year1+1; i <year2; i++) { // no. of days between the two years
sum += (i%4 == 0) ? 366 : 365;
}
System.out.println("Number of days = "+sum);
}
}
27
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

28
Program 15
Program 15
AIM : To Display all Vampire Numbers that are in the range between m and n both
inclusive where m and n are the lower and upper limits respectively.

SOURCE CODE:
import java.util.*;
public class record15
{
public static void main(String args[])
{ record15 obj = new record15();
Scanner sc=new Scanner(System.in);
System.out.print("Enter the lower limit: ");
int m = sc.nextInt();
System.out.print("Enter the upper limit: ");
int n = sc.nextInt();
int count = 0;
System.out.println("The Vampire Numbers are: ");
while (m<=n){
boolean composite = false; //checking for composite number
for (int i = 2; i < m; i++){
if (m%i==0){
composite = true;
break;
}
}
if (!composite){
m++;
continue; //moving to next iteration if prime
}
int m1 = m;
int num = 0; //number of digits
//check for even number of digits
while(m1>0){
m1 = m1/10;
num++;
}
if (num%2!=0){
m++;
continue; //moving to next iteration if odd
}
for (int i = (int) Math.pow(10, num / 2 - 1); i < (int) Math.pow(10, num / 2); i++) {

29
if (m % i == 0) {
int j = m / i;
int numj =0;
int jcopy = j; //taking a copy in order to not alter j value
//finding the number of digits in the factors
while(jcopy>0){
jcopy = jcopy/10;
numj++;
}
// Check if both factors have the same number of digits
if (numj == num / 2) {
// Check if the digits of the factors match the digits of the number
if (obj.containsSameDigits(m, i, j)) {
System.out.println(m); //m is a vampire number
count++; //incrementing frequency
break; //discontinuing the loop in case of repetitions
}
}
}
}
m++; //increment to next iteration
}
System.out.println("Frequency = "+ count);
}
public boolean containsSameDigits(int number, int factor1, int factor2) {
int count[] = new int[10]; // Array to count occurrences of digits 0-9
// Count digits in the number
while (number > 0) {
count[number % 10]++;
number /= 10;
}
// Decrement counts for factor1
while (factor1 > 0) {
count[factor1 % 10]--;
factor1 /= 10;
}
// Decrement counts for factor2
while (factor2 > 0) {
count[factor2 % 10]--;
factor2 /= 10;
}
// Check if all counts are zero
for (int c : count) {
if (c != 0) {
return false;
}
}
return true;
}
}
30
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

30
Program 16
Program 16
AIM : Write a program to accept a sentence , create a new sentence by replacing each
consonant with a previous letter , if the previous letter is a vowel then replace it with
next letter.

SOURCE CODE:
import java.util.*;
public class Record16
{
public static void main(String args[])
{
String str;Scanner sc = new Scanner(System.in);str =
sc.nextLine();
String vowels = "aeiouAEIOU";
for(int i=0 ; i<str.length() ; i++)
{ if(str.charAt(i)==' ')
System.out.print(" "); //if space prints space
else if(vowels.indexOf(str.charAt(i))>=0) //checks for vowel
System.out.print(str.charAt(i));
else if(vowels.indexOf(str.charAt(i))==-1)
{ //if not a vowel
if(vowels.indexOf((char)(((int)str.charAt(i))-1))>=0)
//checks if the previous letter is a vowel
System.out.print((char)(((int)str.charAt(i))+1));
else
System.out.print((char)(((int)str.charAt(i))-1));
}
}
}
}

31
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

32
Program 17
Program 17
AIM : Place the words which begin and end with a vowel at the beginning followed by
the remaining words as they occur in the sentence.

SOURCE CODE:
import java.util.*;
public class record17
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a sentence ending with '.' or '?' or '!'");
String str = sc.nextLine();
char end = str.charAt(str.length()-1);
String vowels="aeiouAEIOU";
int count=0;
if(end == '.' || end == '?' ||end == '!') //checking for terminating condition
{
str=str.substring(0,str.length()-1);
str=str.toUpperCase();
String a[] = str.split(" "); // splitting the words in the string and storing it in the array
for(int i = 0 ; i<a.length ; i++)
{
if(vowels.indexOf(a[i].charAt(a[i].length()-1))>=0 && vowels.indexOf(a[i].charAt(0))>=0)
{
count++;
System.out.print(a[i] + " "); //prints the words which start and end with a vowel
}
}

for(int i = 0 ; i<a.length ; i++)


{
if(vowels.indexOf(a[i].charAt(a[i].length()-1))>=0 && vowels.indexOf(a[i].charAt(0))>=0)
{
System.out.print(""); //prints nothing if the word starts and ends with a vowel
}
else
System.out.print(a[i]+" ");} // printing the other words which do not start and end with a vowel
normally
System.out.println();
System.out.println("Count=" + count);
}
else
System.out.println("Enter with '.' or '?' or '!' in the end");
}}
33
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

34
Program 18
Program 18
AIM : To define a class Mystring which reads a given string and
returns ascii code for the character at position index , and displays
the longest word in the string

SOURCE CODE:
import java.util.*;
public class Mystring{
int len ;
String str;
Mystring() //initializing using constructor
{
str="";
len=0;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
Mystring obj = new Mystring();
System.out.println("Enter a string");
obj.readstring();
int index=sc.nextInt(); //accepting index
System.out.println("The ascii is "+ obj.code(index));
obj.word();
}
public void readstring()
{
Scanner sc = new Scanner(System.in);
str = sc.nextLine(); //accepting sentence
}
public int code (int index)
{
int ans , ind=index;
ans=(int)(str.charAt(ind));
return ans; //returns the ascii of the character in the given index
}
public void word()
{ String longword="";
String a[] = str.split(" ");
int longest = a[0].length();
for(int i = 0 ;i<a.length-1; i++)
{
int l1 = a[i+1].length();
String l1word = a[i+1];
if(l1>longest)
{
longest = l1;
longword = a[i+1];
}
}
System.out.println("The longest word is :"); //displays the lonngest word
System.out.println(longword);
}
} 35
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

36
Program 19
Program 19
AIM : To decode the words according to their potential and
arrange according to ascending order.

SOURCE CODE:
import java.util.*;
public class Record_19{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine(); //accepting sentence
for(int i=0;i<str.length();i++)
{
if (str.charAt(str.length()-1) == '.'||str.charAt(str.length()-1) == ','||str.charAt(str.length()-1) == '!'||str.charAt(str.length()-1) == '?')
{
break; //checking for terminating condition
}
else
System.exit(0);
}
String str2=str.substring(0,str.length()-1).toUpperCase();
String words[]=str2.split(" ");
int l=words.length;
int sum=0;
int potential[]=new int[l];
for(int i=0;i<l;i++)
{
int a=words[i].length();
for(int j=0;j<a;j++)
{
char d=words[i].charAt(j);
sum=sum+((int)d-64); //sum of potential of each word
}
potential[i]=sum; //stores potential of word in a separate array
sum=0;
}
int temp=0;
for(int z=0;z<l;z++) //sorting potential and the words
{
for(int y=0;y<l-1-z;y++)
{
if(potential[y]>potential[y+1])
{temp=potential[y];
potential[y]=potential[y+1];
potential[y+1]=temp;

String x=words[y];
words[y]=words[y+1];
words[y+1]=x;
}
}
}
for(int n=0;n<l;n++) //displaying the words and the potential
{
System.out.println(words[n]+"="+potential[n]);
}
for(int s=0;s<l;s++)
{
System.out.print(words[s]+" ");
}
}
}
37
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

38
Program 20
Program 20
AIM : To Accept a number and check if the number is a sphenic number or not.
A sphenic number is a positive integer which has exactly 3 prime factors

SOURCE CODE:
import java.util.*;
public class sphenic_rec20
{
int n , num , count=0 , prod=1, numcopy=0;
boolean prime=false , flag= false , factor= false ;
public static void main (String args[])
{ int n , num , count=0 , prod=1, numcopy;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
num = sc.nextInt(); //accepting a number
n=num;
numcopy=num;
sphenic_rec20 obj = new sphenic_rec20();
if(obj.check(num)==true)
{
System.out.println("It is a sphenic number");
}
else{
System.out.println("It is NOT a sphenic number");}
}
public boolean check ( int n )
{ sphenic_rec20 obj = new sphenic_rec20();
numcopy=n;
for(int i = 2 ; i<n ; i++)
{
if(n%i==0) //checking for factor
{
if(obj.isPrime(i) == true )
{
count++; //counting the number of prime factors
prod=prod*i; //finding product of prime factors
System.out.print(i + " ");
}
}
}
System.out.println();
System.out.println("Product of prime factors =" +prod);
System.out.println("No. of prime factors="+count);
if(count==3 && prod==numcopy)
return true; //returns true if the given condition is true
else
return false;
}
public boolean isPrime(int num) {
for (int i = 2; i < num; i++) { //checking for prime number
if (num % i == 0) {
return false;
}
}
return true;
}

} 39
INPUT / OUTPUT:

VARIABLE DESCRIPTION TABLE:

40

You might also like