Computer Project
Computer Project
Computer Project
S.NO PROGRAM
1 CIRCULAR PRIME
2 CAESAR CIPHER
3 BEGIN AND END WITH VOWEL
4 FREQUENCY
5 MAXIMUM NUMBER
6 AREA OF CIRCLE
7 EMIRP NUYMBER
13 HAMMING NUMBER
14 MERGING TWO ARRAYS
15 BUBBLE SORT
16 EXCHANGE SELECTION SORT
20 MATRIX SUM
21 LINEAR SEARCH
22 SPECIAL NUMBER
23 PRINTING ARRAY IN MATRIX FORM
24 BINARY SEARCH
25
Program 1
A circular prime is a number that remains prime under cyclic shifts of digits.
Design a java program to find if a number is circular prime or not .
Algorithm
1. Start
2. Input number
3. Run a loop from O to length-I by 1
4. Print the number
5. Manipulate the no.by shifting the first digit to the last //circular shift
6. Check if the no. is prime or not via a function isPrime()
7. C++ if not
8. End of loop
9. check flag and display the output
10. stop
import java.util.*;
class
circularprime
public void
main( )
System.out.println(num); num=num.substring(1,l)
+num.charAt(0); //circular shift int a =
Integer.parselnt(num); if(isPrime(a)==false)
C++;
if (c==0)
System.out.println("numbers is circular
prime"); else
System.out.println("numbers is not circular prime");
return true ;
6. rotate the ascii code by 13 places after checking the initial value
7. convert ascii code to character
Il. stop
import java.util.*;
class caesar
int i,k;
i
String s= "';
for(i=O;i<str.Iength();i++)
char ch = str.charAt(i); //extracting character
if(Character.isLetter(ch))
k=k-13;
else
k=k+13;
else
k=k-13;
else
k=k+13;
s=s+(char)k;
Program 3
Enter a sentence and print all the words that begin and end with a vowel,
followed by the other word as when they occur.
Algorithm
1. start
2. enter string
3. tokenize the string via string tokenizer 4. run a loop from 0 to no.of tokens by 1
5. obtain the next token
6. check if it satisfies the condition
7. group the vowel words and the non vowel ones separately
8. end of loop
9. join the strings ad print the new string
10. stop import java.util.*; import java. util.StringTokenizer;
class be
int c;
String word;
Scanner sc= new Scanner (System.in);
System.out.println("enter sentence");
ch=n.charAt(0);
ch=n.charAt((n.length())-1);
// grouping words
else
b=b+n+l"';
System.out.println(a+'"'+b);
enter
AISHA AND SUZAN ARE NEVER GOING TO QUARREL ANYMORE
AISHA ARE ANYMORE AND SUZAN NEVER GOING TO QUARREL
4
Enter a sentence and store the alphabets on the basis of their frequency in
alphabetical order.
Example input : tomoot
Output : mtooo
Algorithm
I. start
2. input word in uppercase
3. start a loop from A to Z by 1 character
4. within the loop, run another loop to check the frequency of the 'l' letter
5. store the frequency in a 26-sized array
6. end of loop
7. print each letter from the array whose frequency is greater than O
8. alphabetical order will automatically be maintained
9. output will appear to be in string format
10. stop import java.util.*; class ARRSTR
("enter");
int
for
k=str.charAt(j); //frequency of each alphabet if(k==ch)
Program
a [x++]=c;
c=0; //store frequency in an array
for (i=O;i<26;i++)
Program5
1. start
2. enter the size and elements of array
3. call function findmax
4. if size-I then return a[0]
5. return max element using Math.max() method
6. again call function findmax() but parameters will be a, n-l
7. stop
import java.util.*;
class max
if(n 1)
return a[Ol; return Math.max(a[n-l],
findMax(a, n-l));
enter size
Program
6
7
8
8 enter
size
6
en te r
23
865
53
67
08
865
Program 6
1. start
2. create a class calculate to act as main class to make another functions
3. create base class points
4. input the coordinates of two different points in this class
5. create a display function also
6. create a derived class circle
7. calculate radius using distance formula b/w the two points
8. calculate area
9. print the coordinates
10. print area and radius
11. stop
import java.util.*; class
calculate
c.diaplay();
class points
double xl,x2,y1,y2; points()
void acceptpoint()
void display()
System.out.println(xl+' ,
calcdistance()
//calculating area
acceptpoint(); calcdistance(); area
3.14*radius*radius;
void display()
calcarea();
System.out.println("Radius:"+radius+"\nAREA:"+area);
1. start
2. input a number
3. check if it is prime or not using recursive technique in isPrime(int n)
4. calculate reverse of the input number
5. check weather it is prime or not
6. compare and check if both the numbers are prime
7. give valid output
8. if both are prime print emirp
9. else print not emirp
10. stop
class emirp
int n,rev,f;
emirp(int nn)
n=nn;
int isPrime(int x)
return I;
else if
return O;
else
void isemirp()
Program 8
A class decioct has been defined to convert a decimal number into its
equivalent octal no. use main(). Some of the member of the class are given
below:
Class name : deci oct
Data members
Member functions
Decioct() constructor to data members
Void getnum(int nn) assign nn to n
Void deci_oct() calculates the octal equivalent of n amd stores it in
oct using the recursive technique
Void show() displays the decimal no. 'n' and its octal
Algorithm
I. start
2. input number
3. call function to change decimal to octal
4. in function, change decimal to binary to octal
5. return octal number
6. call the print function
7. display the output
8. stop import class decioct
int n; int
Oct;
decioct()
n=O; oct=O;
n=nn;
void deci_oct()
r=r*10+s; nI=n1/8;
void show ()
deci_oct();
System.out.println("decimal:"+n+"\n octal:"+oct);
int nn =sc.nextlnt();
d.getnum(nn);
d.show();
Design a class change() to perform string related operations. The details of the
class are given below: Class name change
Data members
newstr.
Algorithm
I. start
2. input word
3. loop from O to length-I by I
4. send each character
5. get back word from the function
6. add each character to string
7. use recursive technique in the function
8. end of loop
9. print the word
10. stop
importjava.util.*; class
change
string str;
string newstr;
int len;
change()
str='"';
newstr="";
len=0;
void inputwords()
return ch;
return; else
main()
change x = new change (); inputword(); len = str.length(); rechange(len);
System.out.println(newstr);
Program 10
Decode the message.
Input : 122660523756622156
122660523756622156 651226657325066121
Output: AzB9 2By
1. start
2. input code to be decrypt
3. store the reverse of the entered code
4. start a loop from O to length-I by 1
5. add up the ascii codes one by one
6. check if an ascii code matches that of a letter or digit or space
7. add up that character in a separate string and continue
8. end of loop
9. print the string
10. text is decrypted
11. stop
System.out.println(s);
enter
122660523756622156
AzB9 2BY
Program11
1. start
2. initialize c=2
3. makea method to check no. is lucky or not
4. if
5. return true
6. otherwise return false
7. nex=n-(n/c);
8. increase the counter variable
9. again call method islucky() using recursive
10. make main method
11. input the no.
12. stop import java.util.*; class lucky
if (c > n)
return true;
if (n O)
return false;
int nex= n- (n /
c);
1. Start
2. Declare a string variable.
3. Ask the user to initialize the string.
4. Call a function to check whether the string is palindrome or not.
5. If a string is empty, then it is a palindrome.
6. If the string is not empty, then call a recursive function.
7. If there is only one character, then it is a palindrome.
If the first and last characters do not match, then it is not a palindrome.
9. If there are multiple characters, check if the middle substring is also palindrome or
not using the recursive function.
10. Print the result.
11. Stop.
1. Start
2. Enter the number
3. make a method to check prim is prime()
4. Check weather the no-is prime and divisible by 2,3 and 5
5. If false then break flag =
6. If flag is true
false;
7. print hamming no.
8. else print not a hamming no. break;
9. stop
if(num%i==O&&isprime(i))
System.out.println(num+" is a Hamming number");
else
//displaying the
output
static boolean isPrime(int num)
if(num%i==O)
Write a program to input two array of size m and n. Now merge both
these array and display the elements of the merged array.
Algorithm —
1. Start
2 . Ask the user to input two array of size m and n respectively.
6 . Now fill the third array upto m meanwhile keep on updating the value of k in
the loop so that the further insertion can be done from the next index number.
7 . Now fill the third array from index number 1k] which was incremented in the
previous loop using the second array.
8 . Now print the merged array.
10. stop
a[i] = sc.nextlnt();
System.out.println("Enter " " elements in the array."); //entering the elements
b[il = sc.nextlnt();
cliJ = a[iJ; // merging the arrays
c[k) = b[il;
System.out.println("Merged Array");
Description
Array of size n
Array of size m
Array of size m+n
Counter variablr
Program 15
Write a program to an array. Now sort the elements of the array and display
the sorted array. Algorithm
1. Start
2. Enter the size and elements of the array
3. Start a loop
4. Compare 1st and its the adjacent element and move the larger one to the rightmost
5. Now compare the 2nd no. with its adjacent on right side and move the larger one to the
right in the second position .
6. Follow the same until the elements are arranged.
7. End of loop
8. Print the array
9. stop
5
So rted array .
5 4 3 2 1
Program 16
importjava.util.*;
class selsort
scnextlnt();
Program 17
Write a program to enter a sentence/word and remove the consecutive
duplicate letters.
Algorithm
1. start
2. enter the string
3. find length
4. start a loop
5. take out substring from O to i+l
6. now check if character at i is equal to character a j
7. if true than c++ break the loop
8. else form a new word
9, end the loop
10. print the new string
Il. stop
import java.util.*; class
RemoveDuplicates public static
void remove()
i=O;i<(I-1);i++)
sl=s.substring(O,i+1); c=O;
for(int j=i+l; j<l;j++)
s=sl;
I-=c;
System.out.println("Original String: "*Org); //printing the new string
System.out.println("String after removing duplicates:
Scanner sc= new Scanner (System.in); char ch; String st, wd="",
System.out.println ("Enter a sentence."); //entering a sentence st=sc.nextLine();
int 1= st. length(),i; for (i=0;
if
ch = st.charAt(i); ch !
=)
wd= ch+wd; //reversing the string
else
Enter a sentence.
Book is good.
reversed
sentence doog si
kooB.
Program 19
Write a program in java using the following.
Data Members:
sent -to store
sentence. rev -to store
new sentence. size -to
input length.
Member Functions: Exchange () void
readsentence () —to accept
sentence.
void exfirstlast() —extract each word and interchange the first and last
alphabet of word to form new sentence.
void display () —display original and new
sentence. Write main ()
Algorithm
1. Start
2. Enter sentence
3. call method exfirstlast()
4. use stringtokenizer to extract each word
5. start a loop
6. Interchange the first and last word
7. Form a new word
8. End a loop
9. Print the string
10. Make main() method
Il. Invoke all the
functions 12. stop
import java.util.*;
class Exchange
String
sent, rev;
int size;
Exchange
()
Scanner sc= new Scanner (System.in); sent=""•
rev=""• size=O;
void readsentence ()
else
nwd=wd;
void display
Enter a sentence.
It is a warm day.
Orignal Sentence. It
is a warm day.
Reversed Sentence.
tl si a marw yad.
Program
Program 20
Write a program to create a square matric of size n. Input numbers, then
calculate and display.
a) sum of all elements
b) sum of left diagonal
c) sum of right diagonal
d) sum of boundary elements
e) display the array in matrix form.
Algorithm
1. start
2. enter the elements of the array
3. start a loop
4. check if
5. then
6. else if i+j==n-l
then 7.
8. if or j==n-l
9.
10. end of loop
11. print the sum
12. stop
System.out.println();
Enter size
3 Enter the elements
2
3
4
5
Program
6
7
8
9
2 3
4 5 6
7 8 9
Sum
Sum left diagonal = 15 Sum Of rigid diagonal
—1 5
Sum Of 1 bound a e
Program 21
Write a program to input a number and search the number in the array using
linear search.
Algorithm :
1. start
2. Traverse the array.
3. Match the key element with array element.
4. If key element is found, return the index position of the array element.
5. If key element is not found, return -1.
6. stop
import java.util.Scanner;
public class LinearSearch
public static void main()
break;
if (c O)
else
10 is present at
Program 22
A special number is a number in which the sum of the factorial of digit of a
number is equal to number itself.
Algorithm :
1. Start.
2. initialize a number (N).
3. Split the given number (N) into digits if the number has more than one digit.
4. Find the factorial of all digits.
5. Sum up the factorial and store it in a variable (s).
6. Compare the sum with the given number (N).
7. If the sum is equal to the number itself, the number (N) is a special number, else not.
8. End.
else
Algorithm
1. Start
2. Enter the elements
3. Start a loop
4. Using nested loop
5. Printvthe elements
6. End the loop 7. stop
Program
class xyz
void main ()
for (j=O;j<4;j++)
System.out.print(a[i][j] + " //printing the elements in matrix form
System.out.println();
3617
19 7 6
24 6 8
Program 24
Write a program to input the elements in the array, sort them and check
whether the number inputted by the user is present or not using binary
search technique.
Algorithm
1. start
2. enter the array
3. sort the array
4. divide the array into 2 parts
5. check whether the element at mid is greater than search no then start = mid-I
6. else it is smaller then start = mid+l
7. otherwise c++
8. if print no. found
9. else no. not found
10. stop
int n,aO,i,j,start,t,mid,end,c=O,s;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the array.");
n = sc.nextlnt(); //entering the size of an array a = new int[n];
System.out.println("Enter the elements in the array in unsorted manner.");
for(j=0;j<n-1-i;j++)
t = a[jl;
a[jl =
a[j+l];
a[j+l) = t; //sorting the array
System.out,printIn("Sorted array"); for
System.out.print(a[i]+"\t");
break;
else if (s>a[mid])
start = mid+l; //searching for the no.
else if (s<a[mid])
end = start-I;
if (c>o)
System.out.printIn("EIement "+s+" found.");
Else //printing the output
System.out.println("Element "+s+" not found.");
import java.util.*;
class sizeArrange
void main()
int i,j,c;
String temp;
Scanner sc= new Scanner(System.in);
System.out.println("enter a sentence"); //entering the sentence
String str= sc.nextLine();
StringTokenizer st-new StringTokenizer(str," .?!");
c=st.countTokens(); String String[c];
for(i=O;i<c;i++)
//extracting a word and comparing it
w[il=st.nextToken();
for(i=0;i<c-I;i++)
for(j=0;j<c-1-i;j++)
nst=nst+ +W[i];
nst=nst.trim()+str.charAt(str.length()-I);
System.out.println(nst); //orienting the new string
enter a sentence
It was a nice evening. a
It was nice evening.