0% found this document useful (0 votes)
45 views

Computer Project

The document describes an algorithm to check if a number is a magic number or not. It includes steps to: 1. Take input of a number 2. Calculate the sum of digits of the number 3. Check if the sum equals 1, and if so print that it is a magic number, otherwise print that it is not.

Uploaded by

Shrikant Kashyap
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)
45 views

Computer Project

The document describes an algorithm to check if a number is a magic number or not. It includes steps to: 1. Take input of a number 2. Calculate the sum of digits of the number 3. Check if the sum equals 1, and if so print that it is a magic number, otherwise print that it is not.

Uploaded by

Shrikant Kashyap
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/ 52

Design a program to enter a 4x4 matrix & print the

transpose of it.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Transpose
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function display()
Step 9: Printing the original matrix
Step 10: Printing the transpose of matrix by changing row
to
column & column to row
Step 11: Declaring the function main()
Step 12: Creating an object of class Transpose
Step 13: Calling the member the functions [input(), display()]
Step 14: STOP

Pratush Barthwal
XII SCIENCE B
Code
import java.util.*;
class Transpose
{
private int A[][]=new int[4][4];
public void input()
{
Scanner sc=new Scanner(System.in);
int i=0,j=0;

Pratush Barthwal
XII Science B
System.out.println("Enter Array Elements");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=sc.nextInt();
}
}
}
public void display()
{
int i=0,j=0;
System.out.println("Original Matrix:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("Transpose:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{

Pratush Barthwal
XII Science B
System.out.print(A[j][i]+"\t");
}
System.out.println();
}
}
public static void main(String args[])
{
Transpose tr=new Transpose();
tr.input();
tr.display();
}
}

Output

Input Output
Enter Array Elements Original Matrix:
1 1 2 3 4
2 5 6 7 8
3 9 10 11 12
4 13 14 15 16
5 Transpose:
6 1 5 9 13
7 2 6 10 14
8 3 7 11 15
9 4 8 12 16
Pratush Barthwal
XII Science B
10
11
12
13
14
15
16

Design a program to enter a 4x4 matrix & perform the


following:
✓ Sum of Left & Right Diagonal
✓ Sum of All Rows
✓ Sum of All Columns
✓ Difference of Left & Right Diagonal
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Matrix
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Pratush Barthwal
XII Science B
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function calc()
Step 9: Calculating the sum of left diagonal(i==j) & right
diagonal((i+j)==3)
Step 10: Printing the sum of left & right diagonal
Step 11: Calculating the sum of each row & printing it
Step 12: Calculating the sum of each column & printing it
Step 13: Checking which is greater sum of left diagonal or
sum of right diagonal
Step 14: Printing the difference
Step 15: Declaring the function display()
Step 16: Printing the original matrix
Step 17: Declaring the function main()
Step 18: Creating an object of class Matrix
Step 19: Calling the member the functions [input(),
display(), & calc()]
Step 20: STOP
Code
import java.util.*;
class Matrix
{
private int A[][]=new int[4][4];
public void input()
{
Scanner sc=new Scanner(System.in);
Pratush Barthwal
XII Science B
int i=0,j=0;
System.out.println("Enter Array Elements:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=sc.nextInt();
}
}
}
public void calc()
{
int sl=0,sr=0,i=0,j=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
{
sl=sl+A[i][j];
}
if((i+j)==3)
{
sr=sr+A[i][j];
}
}

Pratush Barthwal
XII Science B
}
System.out.println("Sum of left diagonal:"+sl);
System.out.println("Sum of right diagonal:"+sr);
int srow=0,scol=0;
for(i=0;i<4;i++)
{
srow=0;
scol=0;
for(j=0;j<4;j++)
{
srow=srow+A[i][j];
scol=scol+A[j][i];
}
System.out.println("Sum of elements of
coloumn"+(i+1)+":"+scol);
System.out.println("Sum of elements of row
"+(i+1)+":"+srow);
}
System.out.print("Difference of left and right
diagonal:");
if(sr>sl)
{
System.out.println(sr-sl);
}
else if(sl>sr)
{

Pratush Barthwal
XII Science B
System.out.println(sl-sr);
}
else if(sl==sr)
{
System.out.println("0");
}
}
public void display()
{
int i=0,j=0;
System.out.println("Original Matrix:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String args[])
{
Matrix mx=new Matrix();
mx.input();
mx.display();
mx.calc();

Pratush Barthwal
XII Science B
}
}

Output

Input Output
Enter Array Elements Original Matrix:
1 1 2 3 4
2 5 6 7 8
3 9 10 11 12
4 13 14 15 16
5 Sum of left diagonal:34
6 Sum of right diagonal:34
7 Sum of elements of coloumn
8 1:28
9 Sum of elements of row 1:10
10 Sum of elements of coloumn
11 2:32
12 Sum of elements of row 2:26
13 Sum of elements of coloumn
14 3:36
15 Sum of elements of row 3:42
16 Sum of elements of coloumn
4:40
Sum of elements of row 4:58

Pratush Barthwal
XII Science B
Difference of left and right
diagonal:0

Design a program to enter a 4x4 matrix. Also find & print


the Saddle Point if present.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Matrix
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function display()
Step 9: Printing the original matrix
Step 10: Declaring the function calc()
Step 11: Finding the minimum element of a row
Step 12: Saving the row number of minimum element
Step 13: Finding the maximum element in the column
containing the minimum element
Step 14: Saving the column number of maximum element

Pratush Barthwal
XII Science B
Step 15: Checking if the maximum and minimum element
are equal or not
Step 16: If equal printing the element with position and
updating the indicator(‘f’)
Step 17: Checking if f==0
Step 18: If f=0 printing “No Saddle Point”
Step 19: Declaring the function main()
Step 20: Creating an object of class Matrix
Step 21: Calling the member the functions [input(),
display() & calc()]
Step 22: STOP

Code
import java.util.*;
class Saddle_Point
{
private int A[][]=new int[4][4];
public void input()
{
Scanner sc=new Scanner(System.in);
int j=0,i=0;
System.out.print("Enter Array Elements:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
Pratush Barthwal
XII Science B
A[i][j]=sc.nextInt();
}
}
}
public void display()
{
int i=0,j=0;
System.out.println("Original Matrix:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
public void calc()
{
int i=0,j=0,max=0,min=0;
int t=0,f=0,r=0;
for(i=0;i<4;i++)
{
j=0;
min=A[i][j];
for(j=0;j<4;j++)

Pratush Barthwal
XII Science B
{
if(min>A[i][j])
{
min=A[i][j];
t=j;
}
}
max=A[i][t];
for(j=0;j<4;j++)
{
if(max<A[j][t])
{
max=A[j][t];
r=j;
}
}
if(max==min)
{
f=1;
System.out.println("Saddle Point:"+max);
System.out.println("Row:"+(r+1)+"
Coloumn:"+(t+1));
break;
}
}
if(f==0)

Pratush Barthwal
XII Science B
{
System.out.println("No Saddle Point");
}
}
public static void main(String args[])
{
Saddle_Point sp=new Saddle_Point();
sp.input();
sp.display();
sp.calc();
}
}

Output

Input Output
Enter Array Elements: Original Matrix:
1 1 2 3 4
2 5 6 7 8
3 9 10 11 12
4 13 14 15 16
5 Saddle Point:13
6 Row:4 Coloumn:1
7
8
9
Pratush Barthwal
XII Science B
10
11
12
13
14
15
16

Design a program to enter a number and check if it is a


magic number or not.
A magic number is a number whose eventual sum
of digits equals 1
Algorithm
Step 1: START
Pratush Barthwal
XII Science B
Step 2: Importing java.util package
Step 3: Declaring class Magic
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a number
Step 8: Declaring the function sumDigit()
Step 9: Calculating the sum of digits of parameterised
number
Step 10: Declaring the function calc()
Step 11: Finding the sum of digits of number entered
Step 12: Calculating the sum of digits while the sum is
greater than 9
Step 13: Checking if the final sum of digits is 1 or not
Step 14: If it is 1 printing “Magic Number”
Step 15: Else printing “Not A Magic Number”
Step 16: Declaring the function main()
Step 17: Creating an object of class Magic
Step 18: Calling the member the functions [input(), calc()]
Step 19: STOP

Pratush Barthwal
XII Science B
Code
import java.util.*;
class Magic
{
private int n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
}
public int sumDigit(int n)
{
int k=0,s=0;
for(k=n;k>0;k=k/10)
{
s=s+(k%10);
}
return s;
}
public void calc()
{
int a=sumDigit(n);
while(a>9)
{
a=sumDigit(a);
}

Pratush Barthwal
XII Science B
if(a==1)
{
System.out.println("Magic Number");
}
else
{
System.out.println("Not A Magic Number");
}
}
public static void main(String args[])
{
Magic mg=new Magic();
mg.input();
mg.calc();
}
}

Output

Input Output
1234 Magic Number

Input Output

Pratush Barthwal
XII Science B
643 Not A Magic Number

Design a program to enter a number & check if it is a


Special Number or not.
A special number is a number for which the sum
of factorial of its digits is equal to the number itself.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Special
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a number
Step 8: Declaring the function factorial(int a)

Pratush Barthwal
XII Science B
Step 9: Calculating the factorial of parameterised digit
Step 10: Declaring the function calc()
Step 11: Calculating the sum of factorial of digits by calling
the function factorial()
Step 12: Checking if the total sum is equal to the number
itself
Step 13: If equal printing “Special Number”
Step 14: If unequal printing “Not A Special Number”
Step 15: Declaring the function main()
Step 16: Creating an object of class Special
Step 17: Calling the member the functions [input(), calc()]
Step 18: STOP

Code
import java.util.*;
class Special
{
private int n;
public void input()
{

Pratush Barthwal
XII Science B
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
}
public void calc()
{
int f=0,k=0,s=0,p=1;
for(k=n;k>0;k=k/10)
{
s=s+(k%10);
p=p*(k%10);
}
f=p+s;
if(f==n)
{
System.out.println("Special Number");
}
else
{
System.out.println("Not A Special Number");
}
}
public static void main(String args[])
{
Special sp=new Special();
sp.input();
sp.calc();

Pratush Barthwal
XII Science B
}
}

Output

Input Output
19 Special Number

Input Output
87 Not A Special Number

Pratush Barthwal
XII Science B
Design a program to enter an ISBN number & check if it is
a valid ISBN or not.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Book
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of an ISBN number
Step 8: Declaring the function calc()
Step 9: Calculating the length of the entered string
Step 10: Checking if the length is equal to 10 or not
Step 11: If not printing “Invalid ISBN”
Step 12: If equal then finding each character of the ISBN
Step 13: Finding the sum according to:
[Example-ISBN=ABCDEFGHIJ
Sum=(Ax1)+(Bx2)+(Cx3)+….+(Jx10)]
Step 14: Printing the sum
Pratush Barthwal
XII Science B
Step 15: Checking if the sum is dividible by 11 or not
Step 16: If it is divisible then printing “Valid ISBN”
Step 17: If not, printing “Invalid ISBN”
Step 18: Declaring the function main()
Step 19: Creating an object of class Book
Step 20: Calling the member the functions [input(), calc()]
Step 21: STOP

Code
import java.util.*;
class Book
{
private String ISBN;
public void input()
{
Scanner sc=new Scanner(System.in);
ISBN=sc.nextLine();
}
public void calc()
{
int l=0,s=0,i=0,a=0,j=10;

Pratush Barthwal
XII Science B
char ch=' ';
l=ISBN.length();
if(l==10)
{
for(i=0;i<l;i++)
{
ch=ISBN.charAt(i);
if(ch!='X'||ch!='x')
{
a=Integer.valueOf(ch);
a=a-48;
}
else
a=10;
s=s+(a*j);
j--;
}
System.out.println("Sum="+s);
if(s%11==0)
{
System.out.println("LEAVES NO REAMIANDER-
VALID
ISBN CODE");
}
else
{

Pratush Barthwal
XII Science B
System.out.println("LEAVES REAMIANDER-INVALID
ISBN
CODE");
}
}
else
{
System.out.println("INVALID INPUT");
}
}
public static void main(String args[])
{
Book bk=new Book();
bk.input();
bk.calc();
}
}

Output

Input Output
0590353403 Sum=187
LEAVES NO REAMIANDER-VALID ISBN CODE

Pratush Barthwal
XII Science B
Input Output
345354254X Sum=248
LEAVES REAMIANDER-INVALID ISBN CODE

Design a program to enter a number & check if it is a


Special Number or not.
A special number is a number for which the sum
of product of its digits and the sum of its digits is equal
to the number itself.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Special
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a number
Step 8: Declaring the function calc()
Step 9: Calculating the sum of digits and the product of
digits
Step 10: Calculating the sum of sum of digits and the
product of digits

Pratush Barthwal
XII Science B
Step 11: Checking if the total sum is equal to the number
itself
Step 12: If equal printing “Special Number”
Step 13: If unequal printing “Not A Special Number”
Step 14: Declaring the function main()
Step 15: Creating an object of class Special
Step 16: Calling the member the functions [input(), calc()]
Step 17: STOP

Code
import java.util.*;
class Special
{
private int n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
}
public void calc()
{

Pratush Barthwal
XII Science B
int f=0,k=0,s=0,p=1;
for(k=n;k>0;k=k/10)
{
s=s+(k%10);
p=p*(k%10);
}
f=p+s;
if(f==n)
{
System.out.println("Special Number");
}
else
{
System.out.println("Not A Special Number");
}
}
public static void main(String args[])
{
Special sp=new Special();
sp.input();
sp.calc();
}
}

Output

Pratush Barthwal
XII Science B
Input Output
19 Special Number

Input Output
87 Not A Special Number

Input Output
99 Special Number

Pratush Barthwal
XII Science B
Design a program to enter a number & check whether it is
a happy number or not.
A happy number is a number for which the
eventual sum of the square of digits is equal to 1
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Magic
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a number
Step 8: Declaring the function sumDigit()
Step 9: Calculating the sum of square of digits of
parameterised number
Step 10: Declaring the function calc()
Step 11: Finding the sum of digits of number entered
Step 12: Calculating the sum of digits while the sum is
greater than 9
Step 13: Checking if the final sum of digits is 1 or not
Step 14: If it is 1 printing “Magic Number”
Pratush Barthwal
XII Science B
Step 15: Else printing “Not A Magic Number”
Step 16: Declaring the function main()
Step 17: Creating an object of class Magic
Step 18: Calling the member the functions [input(), calc()]
Step 19: STOP

Code
import java.util.*;
class Happy
{
private int n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
}
public int sumDigit(int n)
{
int k=0,s=0;
for(k=n;k>0;k=k/10)
{

Pratush Barthwal
XII Science B
s=s+((k%10)*(k%10));
}
return s;
}
public void calc()
{
int a=sumDigit(n);
while(a>9)
{
a=sumDigit(a);
}
if(a==1)
{
System.out.println("Happy Number");
}
else
{
System.out.println("Not A Happy Number");
}
}
public static void main(String args[])
{
Happy mg=new Happy();
mg.input();
mg.calc();
}

Pratush Barthwal
XII Science B
}

Output

Input Output
13 Happy Number

Input Output
35 Not A Happy Number

Input Output
79 Happy Number

Pratush Barthwal
XII Science B
Design a program to enter a string & check if it is a Special
& Palindrome String or not.
A special string
A palindrome is a string which is same when read
backwards and forwards.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Special
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a string
Step 8:
Step 9:
Step 10:
Step 11:
Step 12:
Step 13:
Step 14:
Step 15:
Step 16:
Step 17: STOP

Pratush Barthwal
XII Science B
Code

Output

Input Output
Special & Palindrome String

Input Output

Pratush Barthwal
XII Science B
Neither a Special &
Palindrome String

Input Output
Special & Palindrome String

Design a program to enter a Sentence & change the first


letter of each word to Upper Case.
Algorithm
Step 1: START
Pratush Barthwal
XII Science B
Step 2: Importing java.util package
Step 3: Declaring class UpperCase
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of the sentence
Step 8: Declaring the function upper()
Step 9: Adding an extra space at the start of the sentence
Step 10: Calculating the length of the string
Step 11: Adding an extra space at the end of the sentence
Step 12: Extracting characters at i and i+1
Step 13: Checking if both characters are spaces
Step 14: Continuing the loop if they are spaces
Step 15: Else checking if character at i is a space or not
Step 16: If it is a space changing character at i+1 to upper
case
Step 17: Adding the character at i+1 to the new string
Step 18: Trimming the new string
Step 19: Declaring the function display()
Step 20: Printing the new sentence
Step 21: Declaring the function main()
Step 22: Creating an object of class UpperCase
Step 23: Calling the member the functions [input(), upper() &
display()]
Step 24: STOP

Pratush Barthwal
XII Science B
Code
import java.util.*;
class UpperCase
{
private String n=" ",s=" ";
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence:");
s=sc.nextLine();
}
public void upper()
{
int l=0,i=0,a=0;
char ch=' ',ch1=' ';
s=" "+s;
l=s.length();
s=s+" ";
for(i=0;i<l;i++)
{
a=i+1;
ch=s.charAt(i);
ch1=s.charAt(a);
if(ch==' '&&ch1==' ')
Pratush Barthwal
XII Science B
{
continue;
}
else
{
if(ch==' ')
{
ch1=Character.toUpperCase(ch1);
}
n=n+ch1;
}
}
n=n.trim();
}
public void display()
{
System.out.println(n);
}
public static void main(String args[])
{
UpperCase uc=new UpperCase();
uc.input();
uc.upper();
uc.display();
}
}

Pratush Barthwal
XII Science B
Output

Input Output
Enter A Sentence: My Name Is Sankalp
my name is sankalp Shrivastava
shrivastava

Input Output
Enter A Sentence: Harry Potter Is Great But
harry potter is great but Percy Jackson Isn't Bad
percy jackson isn't bad

Design a program to enter a string & sort it in ascending


order using the Selection Sort Technique.
Algorithm
Step 1: START
Step 2: Importing java.util package

Pratush Barthwal
XII Science B
Step 3: Declaring class Sele_Sort
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a string
Step 8: Calling the function store()
Step 9: Declaring the function store()
Step 10: Calculating the length of the string
Step 11: Updating the length of the array
Step 12: Storing the characters of the string in the array
Step 13: Declaring the function sort()
Step 14: Finding the minimum element
Step 15: Converting the element and minimum to lower
case
Step 16: Swapping the A[i] with A[min]
Step 17: Declaring the function print()
Step 18: Printing the original matrix
Step 19: Calling the function sort()
Step 20: Printing the original matrix
Step 21: Printing the sorted matrix
Step 22: Declaring the function main()
Step 23: Creating an object of class Sele_Sort
Step 24: Calling the member the functions [input(), print()]
Step 25: STOP

Pratush Barthwal
XII Science B
Code
import java.util.*;
class Sele_Sort
{
private String n;
private int l;
private char A[];
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextLine();
store();
}
public void store()
{
l=n.length();
A=new char[l];
int i=0;
char ch=' ';
for(i=0;i<l;i++)
{
ch=n.charAt(i);
A[i]=ch;
}
}
public void sort()

Pratush Barthwal
XII Science B
{
int i=0,j=0,min=0;
char temp=' ',ch=' ',ch1=' ';
for(i=0;i<l-1;i++)
{
min=i;
for(j=i+1;j<l;j++)
{
ch=Character.toLowerCase(A[j]);
ch1=Character.toLowerCase(A[min]);
if(ch<ch1)
{
min=j;
}
}
temp=A[min];
A[min]=A[i];
A[i]=temp;
}
}
public void print()
{
int i=0;
System.out.println("Original String:");
for(i=0;i<l;i++)
{

Pratush Barthwal
XII Science B
System.out.print(A[i]);
}
System.out.println();
sort();
System.out.println("Sorted String:");
for(i=0;i<l;i++)
{
System.out.print(A[i]);
}
}
public static void main(String args[])
{
Sele_Sort ss=new Sele_Sort();
ss.input();
ss.print();
}
}

Output

Input Output

Pratush Barthwal
XII Science B
Sankalp Shrivastava Original String:
Sankalp Shrivastava
Sorted String:
aaaaahiklnprSsStvv

Input Output
Harry Potter is the Original String:
greatest. Harry Potter is the greatest.
Sorted String:
.aaeeeegHhioPrrrrssttttty

Pratush Barthwal
XII Science B
Design a program to enter a string & search for a
character in the string using the Linear Search Technique.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Lin_Search
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of the string
Step 8: Changing the case of the string to Upper Case
Step 9: Taking input of the character to be searched
Step 10: Changing the case of the character to Upper Case
Step 11: Calling the function store()
Step 12: Declaring the function store()
Step 13: Calculating the length of string
Step 14: Updating the size of array to length ‘l’ of string
Step 15: Storing the characters of the string in the array
Step 11: Calling the function search()
Pratush Barthwal
XII Science B
Step 16: Declaring the function search()
Step 17: Comparing the character to be searched with each
element of the array
Step 18: If found printing “Character Found” along with its
position
Step 19: Declaring the function main()
Step 20: Creating an object of class Lin_Search
Step 21: Calling the member the functions [input()]
Step 22: STOP

Code
import java.util.*;
class Lin_Search
{
private String s;
private char A[],n;
private int l;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter A String: ");
s=sc.nextLine();
Pratush Barthwal
XII Science B
s=s.toUpperCase();
System.out.print("Enter the character to be searched:
");
n=sc.nextLine().charAt(0);
n=Character.toUpperCase(n);
store();
}
public void store()
{
l=s.length();
A=new char[l];
int i=0;
char ch=' ';
for(i=0;i<l;i++)
{
ch=s.charAt(i);
A[i]=ch;
}
search();
}
public void search()
{
int i=0,f=0;
for(i=0;i<l;i++)
{
if(n==A[i])

Pratush Barthwal
XII Science B
{
System.out.print("Yes, Character found at
"+(i+1));
f=1;
break;
}
}
if(f!=1)
{
System.out.println("Character not found");
}
}
public static void main(String args[])
{
Lin_Search ls=new Lin_Search();
ls.input();
}
}

Output

Input Output
Enter A String: Sankalp Yes, Character found at 16
Shrivastava
Enter the character to be
searched: t
Pratush Barthwal
XII Science B
Input Output
Enter A String: Harry Potter Yes, Character found at 5
Enter the character to be
searched: Y

Pratush Barthwal
XII Science B

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy