Class 11 Project - Computer Science
Class 11 Project - Computer Science
NT
I would like to take this opportunity to extend my sincere
gratitude and appreciation to my computer teacher Kuntal sir
for providing guidance and support throughout the process of
completing my computer project for school.
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
Sq int to store square of
the number num.
sqRev int To store square of
the reverse of num.
Rev int To store the
reverse of num.
revSquare int To store the
reverse of sq.
OUTPUT:
PROGRAM 2-
Twisted Prime Number
ALGORITHM:
Input:num
isPrime(n):Returns true if n is prime.
Start:
reverse_numReverse(num)
ifisPrime(num) AND isPrime(reverse_num)
Output “Twisted Prime”
Else
Output “Not a Twisted Prime”
End
SOLUTION:
import java.util.*;
class TwistedPrime
{
int Reverse(int n)
{
int r,rev=0;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
return rev;
}
boolean isPrime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
TwistedPrime ob=new TwistedPrime();
System.out.println(“Enter a number”);
int num=sc.nextInt();
int reverse_num=ob.Reverse(num);
if(ob.isPrime(num) &&ob.isPrime(reverse_num))
System.out.println(“Twisted Prime”);
else
System.out.println(“Not a Twisted Prime”);
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
num int To input a number.
reverse_num int To reverse the
number num.
i int To run the loop
from 1 to num.
c int Counter variable to
check for prime.
OUTPUT:
PROGRAM 3-
Smith Number
ALGORITHM:
Input: num
PrimeFactors(n):Returns the prime factors of n.
Start:
if(isPrime(num))
Output “Not a Smith Number”
else
digit_sumSumOfDigits(num)
factorsPrimeFactors(num)
factors_sumSumOfDigitsOfFactors(factors)
if(digit_sum==factors_sum)
Output “Smith Number”
Else
Output “Not a Smith Number”
End
SOLUTION:
import java.util.*;
class Smith
{
int SumOfDigits(int n)
{
int s=0,r;
for(;n>0;n=n/10)
{
r=n%10;
s+=r;
}
return s;
}
boolean isPrime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
int SumOfDigitsOfFactors(int n)
{
int i=2,s=0;
while(n>1)
{
if(n%i==0)
{
s+=SumOfDigits(i);
n=n/i;
}
else
{
do
{
i++;
}
while(!isPrime(i));
}
}
return s;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
Smith ob=new Smith();
System.out.println("Enter a number");
int num=sc.nextInt();
int digits_sum,factors_sum;
if(ob.isPrime(num))
System.out.println("Not a Smith Number");
else
{
digits_sum=ob.SumOfDigits(num);
factors_sum=ob.SumOfDigitsOfFactors(num);
if(digits_sum==factors_sum)
System.out.println("Smith Number");
else
System.out.println("Not a Smith Number");
}
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
num int To take a number
as input.
digits_sum int To find the sum of
digits of num.
factors_sum int To find the sum of
digits of factors.
i int To run the loop
from 1 to num.
c int Counter variable to
check for prime.
OUTPUT:
PROGRAM 4-
Harshad Number
ALGORITHM:
Input:num
Start:
digit_sumSumOfDigits(num)
if num MOD digit_sum == 0
Output “Harshad Number”
else
Output “Not a Harshad Number”
End
SOLUTION:
import java.util.*;
class Harshad
{
int SumOfDigits(int n)
{
int s=0,r;
for(;n>0;n=n/10)
{
r=n%10;
s+=r;
}
return s;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
Harshad ob=new Harshad();
System.out.println("Enter a number");
int num=sc.nextInt();
int digit_sum=ob.SumOfDigits(num);
if(num%digit_sum==0)
System.out.println("Harshad Number");
else
System.out.println("Not a Harshad Number");
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
num int to accept the
number as input.
digit_sum int To store the sum
of digits.
s int To store sum of
digits in function.
OUTPUT:
PROGRAM 5-
Dudeney Number
ALGORITHM:
Input:num
Start:
cube_rootMath.cbrt(num)
if((int)Math.pow(cube_root,3)==num)
if(cube_root==SumOfDigits(num))
Output “Dudeney Number”
else
Output “Not a Dudeney Number”
else
Output “Not a Dudeney Number”
End
SOLUTION:
import java.util.*;
class Dudeney
{
int SumOfDigits(int n)
{
int s=0;
while(n>0)
{
s+=n%10;
n=n/10;
}
return s;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
Dudeney ob=new Dudeney();
System.out.println("Enter a number");
int num=sc.nextInt();
double cube_root=Math.cbrt(num);
if((int)Math.pow(cube_root,3)==num)
{
if(cube_root==ob.SumOfDigits(num))
System.out.println("Dudeney Number");
else
System.out.println("Not a Dudeney Number");
}
else
System.out.println("Not a Dudeney Number");
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
num int to accept the
number as input.
cube_root double To find the cube
root of num.
s int To store sum of
digits in function.
OUTPUT:
PROGRAM 6-
Longest And Shortest Word
ALGORITHM:
Input:sentence
Start:
Wordssplit sentence into words.
Longest””
ShortestFirstWord(words)
If Length(word)>Length(longest)
longest=word
If Length(word)<Length(shortest)
shortest=word
Output “Longest Word:” ,longest
Output “Shortest Word:” ,shortest
End
SOLUTION:
import java.util.*;
class LongAndShort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String s,longest="",shortest="",word="";
int len,i;
char ch;
System.out.println("Enter a Sentence");
s=sc.nextLine();
s+=" ";
len=s.length();
shortest=s.substring(0,s.indexOf(' '));
for(i=0;i<len;i++)
{
ch=s.charAt(i);
if(ch!=' ')
word+=ch;
else
{
if(word.length()>longest.length())
longest=word;
if(word.length()<shortest.length())
shortest=word;
word="";
}
}
System.out.println("Longest Word: "+longest);
System.out.println("Shortest Word: "+shortest);
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
s String To accept a
sentence as input.
longest String To find the longest
word.
shortest String To find the
shortest word.
word String To extract each
word from s.
len int To find length of
string s.
OUTPUT:
PROGRAM 7-
Decode String with Opposite
Case
ALGORITHM:
Input: string
Start:
Decoded””
for char in string
if case is uppercase
decoded=decoded+ToLowercase(char)
else if char is lowercase
decoded=decoded+ToUpperCase(char)
else
decoded=decoded+char
Output decoded
End
SOLUTION:
import java.util.*;
class Convert_Case
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,len;
String string,decoded="";
char ch;
System.out.println("Enter your string");
string=sc.nextLine();
len=string.length();
for(i=0;i<len;i++)
{
ch=string.charAt(i);
if(ch>='a' && ch<='z')
decoded+=Character.toUpperCase(ch);
else if(ch>='A' && ch<='Z')
decoded+=Character.toLowerCase(ch);
else
decoded+=ch;
}
System.out.println("The new string after converting the case of
each letter:");
System.out.println(decoded);
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
string String To accept a
sentence as input.
decoded String To convert the
case of each
character.
ch char To extract each
character.
len int To store length of
string.
OUTPUT:
PROGRAM 8-
Binary Search
ALGORITHM:
Input:sorted_array,key
Start:
low0
high(Length(sorted_array)-1
while low<=high
mid(low+high) DIV 2
if sorted_array[mid]==key
Output “Key found at index”,midExit
else if sorted_array[mid]<key
lowmid+1
else
highmid-1
Output “Key Not Found”
End
SOLUTION:
import java.util.*;
class Binary_Search
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of array");
int k=sc.nextInt(),i,flag=0,low,mid=0,high,key;
int sorted_array[]=new int[k];
System.out.println("Enter elements in array");
low=0;high=(k-1);
for(i=0;i<k;i++)
sorted_array[i]=sc.nextInt();
System.out.println("Enter number to be searched");
key=sc.nextInt();
for(;low<=high;)
{
mid=(low+high)/2;
if(sorted_array[mid]==key)
{
flag=1;
break;
}
else if(sorted_array[mid]>key)
high=mid-1;
else
low=mid+1;
}
if(flag==1)
System.out.println(key+" found at "+mid);
else
System.out.println(key+" not found");
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
sorted_array int To accept array of
k size.
key int To accept number
to be searched.
flag int Counter variable.
k int To accept size of
array.
OUTPUT:
PROGRAM 9-
Insertion Sort
ALGORITHM:
Input: array
Start:
for i1 to length(array)-1
keyarray[i]
ji-1
while j>=0 AND array[j]>key
array[j+1]array[j]
jj-1
array[j+1]key
Output array
End
SOLUTION:
import java.util.*;
class Insertion_Sort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[1000];
System.out.println("Enter size of the array");
int n=sc.nextInt();
int i,pos,val;
System.out.println("Enter the elements in array");
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Before Insertion");
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
System.out.println();
System.out.println("Enter position and value simultaneously");
pos=sc.nextInt();
val=sc.nextInt();
for(i=n-1;i>=pos;i--)
arr[i+1]=arr[i];
arr[pos]=val;
System.out.println("After Insertion");
for(i=0;i<n+1;i++)
System.out.print(arr[i]+" ");
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
arr int An array to accept
numbers as input.
n int To accept size of
array as input.
pos int To accept the
position where
number is to be
inserted.
val int To accept the
number to be
inserted.
i int To run the loop.
OUTPUT:
PROGRAM 10-
Deletion Sort
ALGORITHM:
Input: array,key
Start:
indexFindindex(array,key)
if index==-1
Output “Key not found”
else
for iindex to Length(array)-2
array[i]array[i+1]
Remove last element of array
Output array
End
SOLUTION:
import java.util.*;
class Deletion_Sort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int arr[]=new int[1000];
System.out.println("Enter size of the array:");
int n=sc.nextInt();
int i,pos;
System.out.println("Enter the elements in array:");
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Before Deletion:");
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
System.out.println();
System.out.println("Enter position from where number is to be
deleted:");
pos=sc.nextInt();
for(i=pos+1;i<n;i++)
arr[i-1]=arr[i];
n--;
System.out.println("After Deletion:");
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
}
VARIABLE DESCRIPTION
TABLE
Variable Data Type Description
arr int An array to accept
numbers as input.
n int To accept size of
array as input.
pos int To accept the
position where
number is to be
deleted.
i int To run the loop.
OUTPUT:
CONCLUSION
In conclusion, this Java programming project provided a
valuable learning experience by applying core Java concepts
like classes, objects, methods, and data structures to create a
functional application, demonstrating the versatility of Java
for real-world scenarios.
Java offers the real possibility the most programs can be
written in a type-safe language. However, for Java to be
broadly useful, it needs to have expressive power than it
does at present.