Computer Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 56

INDEX.

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

8 DECIMAL TO OCTAL NUMBER

9 CONVERTING THE CASE


10 DECODE THE MESSAGE
11 LUCKY NUMBER
12 PALINDROME

13 HAMMING NUMBER
14 MERGING TWO ARRAYS

15 BUBBLE SORT
16 EXCHANGE SELECTION SORT

17 REMOVAL OF DUPLICATE LETTER FROM A WORD/SENTENCE

18 REVERSING A SENTENCE AND ITS WORDS


19 REVERSING THE WORDS OF A SENTENCE

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( )

Scanner sc = new Scanner (System.in);


System.out.println ("enter no.");
String num = sc.nextLine(); //input string int I =
num.length(); int c=O; for (int

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");

boolean isPrime(int a) //checking prime

for (int i=2;i<a;i++)


if(a%i==0)
return false;

return true ;

variable Data type description


int Loop variable
num String Store number in string
a int Convert string into
integer
c int Counter variable
BlueJ: Terminal Window - nimo
Options
enter no.
113
113
131 311 numbers is circular prime enter
no. 1193 1 1 93
1931 9311 3119 numbers is circular
prime enter no. 29 29
92 numbers is not circular prime
Program 2
Encode a text as per Caesar cipher. Rotate each character through 13 places
of alphabets before printing the output.
Algorithm
l.start
2.input a series of text

3. run a loop from Oto length-I by 1


4. extract a character and check if it is a letter or not
5.check for its case

6. rotate the ascii code by 13 places after checking the initial value
7. convert ascii code to character

8. add character in the form of string


9. end of loop
10.disIay the string

Il. stop

import java.util.*;

class caesar

public static void main ()

Scanner sc = new Scanner (System.in);


System.out.println("enter"); //entering the string

String str = sc.nextLine();

int i,k;
i
String s= "';

for(i=O;i<str.Iength();i++)
char ch = str.charAt(i); //extracting character
if(Character.isLetter(ch))

if(Character.isUpperCase(ch)) //checking character

k=k-13;

else

k=k+13;

else

k=k-13;

else

k=k+13;

s=s+(char)k;

System.out.println(s); //printing the new string

variable datatype description


int Loop variable
int To store ascii code
S string To store output
Blue): Terminal Window - nimo
Options
I
enter hello.how
are you uryyb.ubj
ner Ibh

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

public static void main()

int c;
String word;
Scanner sc= new Scanner (System.in);

System.out.println("enter sentence");

String str = sc.nextLine(); //enter a string


StringTokenizer x = new StringTokenizer(str, '
c= x.countTokens(); int i ; char ch; char chl;
String n, // store all the vowels as string for
n=x.nextToken();

ch=n.charAt(0);

ch=n.charAt((n.length())-1);

if(k.indexOf(ch)>=0 && k.indexOf(ch1)>=O) // word satisfies the


condition

// grouping words

else

b=b+n+l"';

System.out.println(a+'"'+b);

Variable datatype description


str string To input sentence
string Tokenizer variable
string To group the words
b string To group the words

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

public static void main()

Scanner sc = new Scanner (System.in); System.out.println

("enter");

String str = sc.nextLine(); //INPUT


int I = str.length(); int

i,j,k,c=O,x=O; char ch;

int

for//loop of new int[26]; alphabets by 1

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++)

// display the output

Variable datatype description


int Array of size 26
int To store length of the
word
k int To store the index no
c int Couter variable
-

Program5

Write a program to print the maximum number in an array using recursive


technique.
Algorithm

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

public static int findMax(int all, int n)

if(n 1)
return a[Ol; return Math.max(a[n-l],
findMax(a, n-l));

public static void main(String argsfl)

Scanner sc = new Scanner(System.in); //enter the size


System.out.println("enter

size"); int n=sc.nextlnt(); int

a[l=new int In];

System.out.println("enter"); //entering the elements in array


for(int

System.out.println(findMaxRec(a, n)); // calling the function

variable datatype description


n int To store size of an array
Int array
int Loop variable
Blue': Terrninal 'Nindow — nit-no
Options

enter size
Program

6
7
8
8 enter
size
6
en te r
23
865
53
67
08
865
Program 6

Design a class circle to calculate area of circle accepting points in another


class points.

Circle class inherits points. Display the output.


Algorithm

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

public static void main()

circle c = new circle();


c.caldistance();

c.diaplay();

class points
double xl,x2,y1,y2; points()

void acceptpoint()

Scanner sc = new Scanner (System.in); //entering the points


System.out.println("enter points"); xl=sc.nextDouble();

yl=sc.nextDouble(); x2=sc.nextDoubIe(); y2=sc.nextDouble();

void display()

System.out.println(xl+' ,

class circle extends points //subclass


Program

double radius; double area; void

calcdistance()

double a = Math.pow((x2-xI),2); double b =


radius = Math.sqrt(a+b);
void calcarea()

//calculating area
acceptpoint(); calcdistance(); area

3.14*radius*radius;

void display()

calcarea();

super.display(); //printing output

System.out.println("Radius:"+radius+"\nAREA:"+area);

Variable datatype description


Double To accept points
Double To accept points
Double To accept points
double To accept points
Double To calculate area
radius double To store radius

a,b double To store the value

Blue): Terminal Vhndow - nirno


enter points
2
3
4
5
2.0,3 .ø
4.0, 5.0
RADIUS :2.82842712474661903
AREA :25.120000000000005
Program 7
Design a class emirp() to check if a number is prime backward and
forward .use n,rev and f as data members and a function isPrime(int) to check
for prime using recursive technique.
Algorithm

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

import java. util.*;

class emirp

int n,rev,f;
emirp(int nn)

n=nn;

int isPrime(int x)

return I;
else if
return O;

else

return isPrime(x+1); //checking for prime no.

void isemirp()

int x=n; while(xl=0)

// storing reverse in rev


x=x/10;

int a = isPrime(f); //check if reverse is prime as well n=rev; int b = isPrime(f);

System.out.println ("no.is emirp"); // display output else

System.out.println ("no.is not emirp");

public static void main()

Scanner sc = new Scanner (System.in);


System.out.println ("enter the no.");
int n sc.nextlnt(); emirp ob= new emirp(n); ob.isemirp();
Variable datatype description
n int To accept the number
rev Int To store reverse number
int Check whether no.is
prime
-

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

N, oct to store decimal and octal number

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;

void getnum(int nn)

n=nn;

void deci_oct()

int nl = n; int r=O,s;

while(n1!=O) //convert decimal to binary

r=r*10+s; nI=n1/8;

oct —O; while(r!=O) //convert binary to decimal

int p = r%10; oct=oct* 10+p; r=r/10;

void show ()
deci_oct();
System.out.println("decimal:"+n+"\n octal:"+oct);

public static void main()


decioct d = new decioct();
Scanner sc = new Scanner(System.in);
System.out.println("enter number"); //entering the number

int nn =sc.nextlnt();
d.getnum(nn);

d.show();

Variable datatype description


n int To make copy of no.
oct Int To store octal no.
nn int To accept input from
user
nimo
Program 9

Design a class change() to perform string related operations. The details of the
class are given below: Class name change
Data members

Str,newstr to input and store the new string

Len store the length of the word


Member functions:

Change() default constructor Void inputword()

to accept the word


Char caseconvert(char ch) convert one case t the other
Void rechange(int) change its case using changecase().display

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()

Scanner sc = new Scanner (System.in);

System.out.println("enter sentence"); //entering the string

String str= sc.nextLine();

char caseconvert(char ch)

if(Character.isUpperCase(ch)) //checking if it is in uppercase

ch=(char)(ch+32); return ch;

return ch;

void rechange(int a) //rechanging the string

return; else

rechange(a-l); newstr=newstr+caseconvert(str.charAt(a-I)); public void

main()
change x = new change (); inputword(); len = str.length(); rechange(len);

System.out.println(newstr);

//orienting the new string


variable datatype description
str string To accept input from
user
newstr string To store new string
len int To store length
Blue): Terminal Window - nimo
Options

enter sentence tOggLE cAsE TesT


ToGG1e Case tESt
Program

Program 10
Decode the message.

Input : 122660523756622156
122660523756622156 651226657325066121
Output: AzB9 2By

Write a program to decode such messages


Algorithm

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

import java.util.*; class decrypt

public static void main()

Scanner sc = new Scanner (System.in);


System.out.println("enter");
String str = //input
sc.nextLine(); String
char ch; int
k=(),l=str.length();
for(int

rev=rev+(str.charAt(i))+"" //form reverse


for (int
ch=rev.charAt(i); //extract character int b= Integer.parselnt(ch+"");
//form a number char a=(char)k; if((k==32) I I (Character.isLetter(a)) I I

s=s+(char)k; //forming string k=o;

System.out.println(s);

variable datatype Description


rev String To store reverse
str String To accept from user
ch char To extract character
BlueJ: Terminal Window - nimo
Options

enter
122660523756622156
AzB9 2BY
Program11

Enter a number and check whether it is an lucky number or not using


recursion.
Algorithm

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

public static int c = 2;


Program

static boolean isLucky(int n) //checking if it is lucky no.or


not

if (c > n)

return true;

if (n O)
return false;
int nex= n- (n /
c);

return isLucky(nex); // recursion


public static void main (String[] args)

Scanner sc= new Scanner (System.in);

System.out.println("enter the no."); //entering the no.


Program 12
Check weather a string is palindrome or not using recursion
Algorithm

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.

import java. util.Scanner;

public class palindrome

static boolean checkPaIindrome(String str, int s, int e)

//method to check palindrome

if (s e) return true; if ((str.charAt(s))

(str.charAt(e))) return false; if (s < e +1) return

checkPalindrome(str, s + 1, e - 1); return true;


Program

static boolean isPalindrome(String str)


int n = str.length();

if (n O) //method to check whether it is palindrome or not

return true; return checkPalindrome(str, O, n - 1); // recursive technique

public static void main(String args[])

Scanner sc = new Scanner(System.in);


System .out.println("Enter the String :"); //entering the string

String str = sc.nextLine();


if (ispalindrome(str))
System.out.println(str+" is palindrome");

Else //printing the output


System.out.println(str+ " is not a palindrome");

Variable datatype Description


str String To accept string from
user
n Int To store length of string
int To store index no.
Enter the String
be IOW below is not a palindrome
Program 13

Check whether a no. is hamming no. or not


Algorithm

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

import java. util.Scanner; public


class HammingNumber if(flag)
//entering a number

public static void main(String args[])

Scanner scan = new Scanner(System.in);


System.out.print("Enter a number : ");
int num = scan.nextlnt(); boolean flag =
true; for(int i = 2; i<=num; i++)
// checking if it is prime or not

if(num%i==O&&isprime(i))
System.out.println(num+" is a Hamming number");

else

System.out.println(num+" is Not a Hamming number");

//displaying the
output
static boolean isPrime(int num)

boolean flag = true; for(int i = 2; i<=num/2; i++)

if(num%i==O)

flag = false; break;

variable datatype description


flag Boolean Flag variable
num Int To input the no.
int Loop variable
return flag;
//check whether it is prime or not
Enter • ntner : 2' 20 is •
Enter •
13 is "0t •
Program 14

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.

3 . Take the input from the user using for loop.


4. Take another array of size [m+nl.
5 . Declare two variables i and j (loop variable) and k. Now initialize k with O.

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

import java. util. * ;


class merge

public static void main()

Scanner sc = new Scanner (System.in);


System.out.println("Enter the size of first array."); // entering the size of an array
int m = sc.nextlnt();
System.out.println("Enter the size of second array."); //entering the size of
array int n = sc.nextlnt(); int at] = new int[m]; int = new intln]; int co = new
int[m+nl; int
System.out.println("Enter " elements in the array."); // entering the elements

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");

BlueJ: Terminal Window - Cll


Options

Enter the size of first array.


2
Enter the size of second
array.
3
Enter 2 elements in the array.
2
Enter 3 elements in the array.
3
4
5
Merged Array
1 2 3 4 5
// displaying the output

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

import java.util. *; class


bubblesort public static
void main()

Scanner sc = new Scanner (System.in); int


i,j,t,n;
System.out.println ("Enter the size of the array."); //entering the size n =
sc.nextlnt(); int = new int In);
System.out.println("Enter "+n+" numbers"); // entering the elements

a[iJ = sc.nextlnt(); for (i=O;i<n-I;i++) for(j=0;j<n-1-i;j++)

if (a[j] < a[j+l])

t=a[j]; alj] = a[j+l];


//comparing and shifting it to the right
System.out.println("Sorted array."); //printing the sorted array for
System.out.print(a[i]+"\t");
Variable datatype description
Int Loop variable
Int Loop variable
int Array to be arranged
BlueJ•. Terminal Window - Cll
Options

Enter the size of the array.


5 Enter 5 numbers
2
3
4

5
So rted array .
5 4 3 2 1
Program 16

Write a program to input n numbers. Sort these elements in ascending


order using exchange selection sort technique.
Algorithm
1. start
2. enter the size and elements of the array
3. start a loop
4. divide the list in two parts sorted and unsorted
5. now find the minimum element in the array
6. shift it to the first place
7. repeat the same process with the second element until the array is sorted
8. end the loop
9. print the array
10. stop

importjava.util.*;
class selsort

public static void main()


int
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array. // enter the size of the element
n=sc.nextlnt(); int a[]= new int [n];
System.outprintln("Enter the elements in the array."); //enter the elements of the array

scnextlnt();

min = a[i]; pos=i;

//comparing and arranging the array


min = a[jl; pos=j;

System.outprintln("sorted array'); //printing the array

variable datatype description


Int Arra to be sorted
Int Loop variable
int Loop variable

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()

Scanner sc= new Scanner(System.in);


System.out.printIn("Enter a word.");
String s= sc.nextLine(); //entering a word int l=s.length(); int c;
String org=s, for(int

i=O;i<(I-1);i++)

sl=s.substring(O,i+1); c=O;
for(int j=i+l; j<l;j++)

if(s.charAt(i)==s.charAt(j)) //comparing the character


continue;

sl=s1+s.charAt(j); // forming new string

s=sl;

I-=c;
System.out.println("Original String: "*Org); //printing the new string
System.out.println("String after removing duplicates:

Variable datatype description


String To accept string from user
s
c int Counter variable
int To find length
BlueJ: Terminal Window - Cl 1
Options

Enter a word. happy


Original String: happy
String after removing
hap
duplicates: y
Program 18

Write a program in to reverse a sentence as well its words.


Algorithm
1. Start
2. Enter a string from the user.
3. Extract each letter one by one.
4. Join the character to the word to make a reverse word.
5. Join the word to the sentence to make the reverse sentence.
6. Print the new sentence.
7. stop

import java. util.*; class revsen

Public static void main ()

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

nst= wd+' ' nst; //forming new string wd="";

String nstr= nst.trim()+st.charAt(l-1);


System.out.println("reversed sentence \n"+nstr); //printing new string

variable datatype description


st String To enter string from the
user
Int To find length
nst string To store new word

BlueJ: Terminal Window - Cll


Options

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 ()

Scanner sc= new Scanner (System.in);


System.out.println ("Enter a sentence.");
// entering a sentence
sent=sc.nextLine(); int size= sent.length(); void exfirstlast()

int i,c, WI;


String wd="", nwd—
StringTokenizer s= new StringTokenizer (sent,
c=s.countTokens(); for

wd=s.nextToken(); //extracting the character wl=wd.length(); if (wl>l)


nwd=wd.charAt(wI-I)+wd.substring(1,wI-I)+wd.charAt(0);

else

nwd=wd;

//storing reverse string


rev=rev.trim(); rev= rev +

void display

System.out.println ("Orignal Sentence. \n"+sent);


System.out.println ("Reversed Sentence. //printing the string

void main () //main method


Exchange 0b = new Exchange (); ob.readsentence(); ob.exfirstlast(); ob.display();

variable datatype description


sent string To input string from the
user
Int To store length
rev string To store reverse string

BlueJ: Terminal Window - Cll


Options

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

import java.util.*; class matrixsum

public static void main()

int i,j,sa=O,sld=O,srd=0,sbd=O,n; System.out.println("Enter size."); Scanner sc = new


Scanner (System.in); n=sc.nextlnt();
int
System.out.println("Enter the elements.
//entering the elements in array sld=sld //sum of left diagonal
if(i+j==n-l)

// sum of right diagonal


sbd=sbd + // sum of boundary

System.out.println();

System.out.print("Sum of all elements-I' of left diagonal="+sld+"\nSum of rigid


diagonal="+srd+"\nSum of all boundary elements="+sbd); //print the sum

Variable datatype description


sld Int to store the sum of left
diagonal
sbd Int To store the sum of
boundary elements
srd int To store the sum of right
diagonal

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()

Scanner sc = new Scanner(System.in);


System.out.println("Enter the number to search"); //entering the no. to be
searched int num; num = sc.nextlnt(); int c = -1; into arr = for (int i
= 0; i < arr.length; i++)
if (num arr[i]) //comparing each no. with search no.

break;

if (c O)

System.out.println(num+" is present at index = "+ c)

else

System.out.println(num+" is not present.");

variable datatype Description


c int Counter variable
num int No.to be searched
arro Array from which no.
must be found
BlueJ: Terminal Window -
Cll
Options

Enter the to search


number
10 index = 0
Program

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.

import java. util.Scanner; public


class SpecialNumberExampleI

public static void main(String args[])

int num, number, last_digit, sum_Of_Fact = 0; Scanner


sc = new Scanner(System.in);
System.out.print("Enter a number: //entering a no. number
= sc.nextlnt(); num = number; while (number > O)

last_digit = number % 10; int


fact-I; for(int i=l; i<=last_digit;
i++)

fact=fact*i; //checking it is a special no. or not

sum Of Fact = fact; number =


number / 10;
if(num==sum_Of_Fact)

System.out.println(num+ is a special number."); //printing the output

else

System.out.println(num+ " is not a special number.");

Variable datatype description


num Int Accept no. from user
last_digit Int To store last digit
sum Of Fact int To store sum of factorial
BlueJ: Terminal Window - nimo
Options

Enter a number: 145


145 is a special number.
Enter a number: 34
34 is not a special number.
Program 23
Write a program to initialise a 2d array of size 3*4 with integer elements and
display it in matrix form.

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 ()

int i,j; int a //elements of an array


for (i=O;i&lt;3;i++)

for (j=O;j&lt;4;j++)
System.out.print(a[i][j] + " //printing the elements in matrix form

System.out.println();

variable Datatype description


Int Loop variable
Int Loop variable
int Arrav from which the
a elements are to be
printed
BlueJ: Terminal V/indow —
nimo Options

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

import java. util. *;


class binary

public static void main()

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.");

// entering the elemnts


for (i=O;i<n-I;i++)

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");

System.out.println(); the element to be searched in the


array."); s = sc.nextlnt(); start = O;end = n-l; while(start<=end)
mid = (start+end)/2; //dividing the array into two parts
if (s==a[mid])

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.");

variable datatype description


afl Int Array to sorted and from
which no.is to be searched
Int Loop variable
int Loop variable
Blue}. Terminal Window - Cll
Options

Enter the number of elements in the array.


5
Enter the elements in the array in unsorted manner.
2
3
4
5
Sorted array
2 3 4 5
Enter the element to be searched in the array.
4
Element 4 found .
Program 25
Write a program_to enter a_sentence and arrange the words in ascending order
of length.
Algorithm
1. Start.
2. Enter a sentence.
3. Store sentence in str.
4. Create StringTokenizer for str.
5. Count tokens in c.
6. Store tokens from str in an array.
7. Check the condition in array using nested loop.
8. After checking, store the new sentence in new string variable nst.
9. Print nst.
10. End.

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++)

String nst=""; for(i=O;i<c;i++)

nst=nst+ +W[i];

nst=nst.trim()+str.charAt(str.length()-I);
System.out.println(nst); //orienting the new string

variable datatype description


Int Loop variable
Int Loop variable
c int Counter variable

BlueJ: Terminal Window - Cll


Options

enter a sentence
It was a nice evening. a
It was nice evening.

You might also like