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

YEAR: 2023-24

NAME: MONALISA TRIPATHY

CLASS:XII

ROLLNO:07

SCHOOL:BLESSED SACRAMENT HIGH SCHOOL

1 |P a g e
ACKNOWLEDGEMENT

I, Monalisa Tripathy of Std XII of Blessed Sacrament High School,


consider myself lucky that I got the privilege to complete and present
this project work which has overall interesting tasks to do. Thanks to
the Education board of ISC of this decade which gives the student a
golden opportunity to learn about the project.

I express my gratitude to my subject teacher (Mr. Tushar Sir) who had


taken a lot of burden to collect the required information to make this
project more effective and attractive , and also successful in giving a
fine shape to my project work.

INDEX
S. Pag Remark
Program Date
No e No
1. Write a program to display the entered string in alphabetical 4
order.
2. Write a program to input a string and replace all the vowels 6
with ‘*’ and consonants with ‘#’.
3. Write a program to find sum of each row and each column of 9
m*n array.
4. Write a program to convert decimal no. to binary no. . 12

2 |P a g e
5. Write a program to print a date from any day no. and year . 14
6. Write a program to search an element in the array using 17
binary search array size- n .
7. Write a program to print the elements of Fibonacci series. 19
8. Write a program to calculate GCD of two no.s using recursion. 21
9. Write a program to print the sum of the border elements of 2- 24
d array of size n*n.
10. Write a program to generate the sum of two double 27
dimensional array in a third dimensional array.
11. Write a program to input a natural number within 10,000 and 30
display it in words.
12. Write a program to input a sentence and a number between 1 33
to 10 and encode the sentence by advancing each character n
places.
13. Write a program to print all the dinominators required for an 36
amount entered by user.
14. Write a program to check if a number is Smith number or not. 40
15. Write a program to print unique digit number between a 44
given range.
16. Write a program to print the frequency of each elements in a 47
string.
17. Write a program to sort an array of size n using selection sort. 49
18. Write a program to check if a number is a magic number or 52
not.
19. Write a program to insert a given element at any position 54
inside a array.
20. Write a program to delete a element in array and bring right 56
one to left by one place.
21. Write a program to find out the no. of times a substring is 58
present in the main string.
22. Write a program to arrange a sentence in ascending order of 61
word’s length.
23. Write a program to print the number of vowels in each word 64
of a sentence.
24. Write a program to print the longest and shortest word of a 66
sentence.
25. Write a program to remove all the repeating characters from 69
a string.
Question 1:

Write a program to display the entered string in alphabetical order.

Algorithm:

Step 1: Start
Step 2: Import the Scanner class for user input.

3 |P a g e
Step 3: Define a class named "Qno1."
Step 4: Inside the main method:
a) Create a Scanner object for user input.
b) Display a prompt to enter a word.
c) Read the user's input into a String variable "s."
d) Convert the String "s" to a character array "c."
e) Create an instance of the "Qno1" class.
f) Call the "arrange" method with the character array "c."
g) Print the sorted string from the character array "c."
Step 5: Define the "arrange" method that takes a character array "t" as
input:
Step 6: Get the length of the array "t."
Step 7:Use nested loops to iterate over the elements in the array:
a) The outer loop iterates from 0 to length-1.
b) The inner loop iterates from 0 to length-1.
Step 8:Inside the inner loop, compare adjacent characters, and swap them
if they are out of order
Step 9:Stop

Solution:

importjava.util.Scanner;
public class Qno1
{
public static void main(String args[])
{
String s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
s=sc.nextLine();
char c[]=s.toCharArray();
Qno1 obj=new Qno1();
obj.arrange(c);
System.out.println(new String(c));
}
void arrange(char t[])
{
int l=t.length;

4 |P a g e
for(int i=0;i<l-1;i++)
{
for(int j=0;j<l-1;j++)
{
if(t[j]>t[j+1])
{
char tm=t[j];
t[j]=t[j+1];
t[j+1]=tm;
}
}
}
}
}
Output:

Question 2:

Write a program to input a string and replace all the vowels with ‘*’
and consonants with ‘#’.

Algorithm:
Step1:Start

5 |P a g e
Step 2: Define the main method as the program's entry point.
Step 3: Create an instance of the Qno2 class and assign it to the variable
obj.
Step 4: Create a Scanner object sc for user input.
Step 5: Display the message "Enter a string" to prompt the user.
Step 6: Read a string from the user and store it in a String variable s.
Step 7: Call the modify method of the Qno2 class, passing the string s as an
argument.
Step 8: Print the modified string obtained from the modify method.
Step 9: Define the modify method with a parameter s of type String.
Step 10:Initialize an empty string s1 to store the modified string.
Step 11: Iterate over each character ch in the input string s:
a. Check if the character ch is a letter using Character.isLetter(ch).
b. If it's a letter, check if it's not a vowel (i.e., not in "aeiouAEIOU").
c. If it's not a vowel, add "#" to the s1; otherwise, add "*".
d. If the character is not a letter, add it directly to ‘s1’.
Step 12: Return the modified string ‘s1’.
Step 13: Stop

Solution:

importjava.util.Scanner;
public class Qno2
{
public String modify(String s)
{
int l=s.length();
String s1="";
for(int i=0;i<l;i++)
{
charch=s.charAt(i);
if(Character.isLetter(ch))

6 |P a g e
{
if("aeiouAEIOU".indexOf(ch)<0)
s1=s1+"#";
else
s1=s1+"*";
}
else
s1=s1+ch;
}
return s1;
}
public static void main(String args[])
{
Qno2 obj=new Qno2();
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.next();
System.out.println("Output");
System.out.println(obj.modify(s));
}
}
Output:

7 |P a g e
Question 3:
Write a program to find sum of each row and each column of
m*narray.

Algorithm:

Start 1:Start
Step 2: Define the main method as the program's entry point.

8 |P a g e
Step 3: Declare variables for the array, row size (rows), column size (cols),
and row and column sums (sr and sco).
Step 4: Create a Scanner object sc for user input.
Step 5: Prompt the user to enter the row size and read it into the rows
variable.
Step 6: Prompt the user to enter the column size and read it into the cols
variable.
Step 7: Create a 2D integer array a with dimensions rows x cols.
Step 8: Display a message to instruct the user to enter the elements of the
array.
Step 9: Use nested loops to read and store the elements in the array a.
Step 10: Display the elements of the array by iterating over rows and
columns and printing each element.
Step 11: Calculate and display the sum of each row by iterating through
rows: a. Initialize the sr variable to zero. b. Use a nested loop to iterate
through the columns. c. Add each element of the current row to the sr
variable. d. Print the sum of the current row.
Step 12: Calculate and display the sum of each column by iterating through
columns: a. Initialize the sco variable to zero. b. Use a nested loop to iterate
through the rows. c. Add each element of the current column to the sco
variable. d. Print the sum of the current column.
Step13:Stop

Solution:
importjava.util.Scanner;
public class Qno3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[][];
introws,cols,sr,sco;
System.out.println("Enter row size:");
rows=sc.nextInt();
System.out.println("Enter column size:");
cols=sc.nextInt();
a=new int[rows][cols];
System.out.println("Enter the elements of array:");

9 |P a g e
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Array elements:");
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println();
for(int i=0;i<rows;i++)
{
sr=0;
for(int j=0;j<cols;j++)
{
sr=sr+a[i][j];
}
System.out.println("Sum of row"+(i+1)+":"+sr);
}
System.out.println();
for(int i=0;i<cols;i++)
{
sco=0;
for(int j=0;j<rows;j++)
{
sco=sco+a[j][i];
}
System.out.println("Sum of column"+(i+1)+":"+sco);
}
}

10 |P a g e
}

Output:

Question 4:
Write a program to convert decimal no. to binary no. .

Algorithm:

Step1:Start:
Step2:Define the main function for converting decimal to binary.

11 |P a g e
Step3:Input the decimal number you want to convert (let's call it
decimalNumber).
Step4:Call the recursive function convertToBinary with the
decimalNumber as the argument.
Step5:TheconvertToBinary function:
a. If decimalNumber is 0, return 0 as a base case.
b. Otherwise, calculate the remainder when decimalNumber is
divided by 2.
c. Call the convertToBinary function recursively with the result of
integer division of decimalNumber by 2.
d. Append the remainder from step 4b to the result from step 4c.
e. Return the binary representation obtained in step 4d.
Step6:Output the binary representation obtained from step 3.
Step7:Stop.

Solution:
public class Qno4
{
public String bin(int n)
{
if(n==1)
return n+" ";
else
return bin(n/2)+(n%2+ " ");
}
void main(int n)
{
System.out.println("The original no."+n);
System.out.println("Binary form:"+bin(n));

12 |P a g e
}
}
Output:

Question 5:
Write a program to print a date from any day no. and year .

Algorithm:
Step1:Start.
Step2:Define the main function for printing a date from a day number
and year.

13 |P a g e
Step3:Input the day number (let's call it dayNumber) and the year (let's
call it year) you want to use.
Step4:Check if the year is a leap year (use a leap year calculation
function if necessary).
Step5:Create an array or a list of the number of days in each month.
Count for leap years by adjusting the number of days in February.
Step6:Initialize variables for month (let's call it month) and day (let's
call it day). Set both to 1 initially.
Step7:WhiledayNumber is greater than or equal to the number of days
in the current month (i.e., dayNumber>= daysInMonth[month]), do the
following:
a. Subtract the number of days in the current month from
dayNumber.
b. Increment month by 1.
Step8:Set day to dayNumber (as it represents the day of the month).
Step9:Output the date in the format "day/month/year."
Step10:Stop

Solution:
importjava.util.Scanner;
public class Q5 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String
arr[]={"JANUARY","FEBUARY","MARCH","APRIL","MAY","JUNE","JULY
","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
System.out.println("Enter the day number");
intdayNo=sc.nextInt();
System.out.println("Enter the year ");
int year=sc.nextInt();
if(year%4==0)
{
if(dayNo>=1 &&dayNo<=31)
System.out.println((dayNo-1+1)+" "+arr[0]+" "+year);
else if (dayNo>= 32 &&dayNo<= 60)
System.out.println((dayNo - 32+1) + " " + arr[1] + " " + year);
else if (dayNo>= 61 &&dayNo<= 91)
System.out.println((dayNo - 61+1) + " " + arr[2] + " " + year);

14 |P a g e
else if (dayNo>= 92 &&dayNo<= 121)
System.out.println((dayNo - 92+1) + " " + arr[3] + " " + year);
else if (dayNo>= 122 &&dayNo<= 152)
System.out.println((dayNo - 122+1) + " " + arr[4] + " " + year);
else if (dayNo>= 153 &&dayNo<= 182)
System.out.println((dayNo - 152+1) + " " + arr[5] + " " + year);
else if (dayNo>= 183 &&dayNo<= 213)
System.out.println((dayNo - 183+1) + " " + arr[6] + " " + year);
else if (dayNo>= 214 &&dayNo<= 244)
System.out.println((dayNo - 214+1) + " " + arr[7] + " " + year);
else if (dayNo>= 245 &&dayNo<= 274)
System.out.println((dayNo - 245+1) + " " + arr[8] + " " + year);
else if (dayNo>= 275 &&dayNo<= 305)
System.out.println((dayNo - 275+1) + " " + arr[9] + " " + year);
else if (dayNo>= 306 &&dayNo<= 335)
System.out.println((dayNo - 306+1) + " " + arr[10] + " " + year);
else if (dayNo>= 336 &&dayNo<= 366)
System.out.println((dayNo - 336+1) + " " + arr[11] + " " + year);
elseSystem.out.println("Invalid day number");

}
else
{
if(dayNo>=1 &&dayNo<=31)
System.out.println((dayNo-1+1)+" "+arr[0]+" "+year);
else if (dayNo>= 32 &&dayNo<= 59)
System.out.println((dayNo - 32+1) + " " + arr[1] + " " + year);
else if (dayNo>= 60 &&dayNo<= 90)
System.out.println((dayNo - 60+1) + " " + arr[2] + " " + year);
else if (dayNo>= 91 &&dayNo<= 120)
System.out.println((dayNo - 91+1) + " " + arr[3] + " " + year);
else if (dayNo>= 121 &&dayNo<= 151)
System.out.println((dayNo - 121+1) + " " + arr[4] + " " + year);
else if (dayNo>= 152 &&dayNo<= 181)
System.out.println((dayNo - 152+1) + " " + arr[5] + " " + year);

15 |P a g e
else if (dayNo>= 182 &&dayNo<= 212)
System.out.println((dayNo - 182+1) + " " + arr[6] + " " + year);
else if (dayNo>= 213 &&dayNo<= 243)
System.out.println((dayNo - 213+1) + " " + arr[7] + " " + year);
else if (dayNo>= 244 &&dayNo<= 273)
System.out.println((dayNo - 244+1) + " " + arr[8] + " " + year);
else if (dayNo>= 274 &&dayNo<= 304)
System.out.println((dayNo - 274+1) + " " + arr[9] + " " + year);
else if (dayNo>= 305 &&dayNo<= 334)
System.out.println((dayNo - 305+1) + " " + arr[10] + " " + year);
else if (dayNo>= 335 &&dayNo<= 365)
System.out.println((dayNo - 335+1) + " " + arr[11] + " " + year);
elseSystem.out.println("Invalid day number");
}

}
}
Output:

Question 6:
Write a program to search an element in the array using
binarysearch array size- n .

Algorithm:

Step1:Start: Define the main function for binary search.

16 |P a g e
Step2:Input the element you want to find (let's call it key).
Step3:Input the size of the sorted array (let's call it n).
Step4:Create an array (or use an existing one) of size n and populate it
with sorted elements.
Step5:Initialize variables for the start index (let's call it start) and the
end index (let's call it end) of the search range. Set start to 0 and end to
n - 1.
Step6:While start is less than or equal to end, do the following:
a. Calculate the middle index as (start + end) / 2 (let's call it mid).
b. Check if the element at the middle index (array[mid]) is equal
to key.
c. If they are equal, the element has been found, and you can
return mid as the index of the found element.
d. If key is less than array[mid], set end to mid - 1 (to search in
the left half of the array).
e. If key is greater than array[mid], set start to mid + 1 (to search
in the right half of the array).
Step7:If the loop exits without finding the element, return a value
(e.g., -1) to indicate that the element is not in the array.
Step8:Output the result, either the index of the found element or a
message indicating that the element is not in the array.
Step9:Stop

Solution:
importjava.util.Scanner;
public class Qno6
{
public static void binarySearch(intarr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at position: " + (mid+1));
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;

17 |P a g e
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements no. of array");
int n=sc.nextInt();
intarr[]=new int[n];
System.out.println("Enter elements");
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
System.out.println("Enter element to be searched");
int key = sc.nextInt();
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
Output:

Question 7:
Write a program to print the elements of Fibonacci series.
Algorithm:

Step 1: Start

18 |P a g e
Step 2: Read the value of n from the user, representing the number of
Fibonacci numbers to print.
Step 3: If n is less than or equal to 0, display an error message and
terminate.
Step 4: Initialize variables:
a to 0
b to 1
i to 1
Step 5: Display a message indicating that you are printing the Fibonacci
series of n numbers.
Step 6: While i is less than or equal to n, do the following:
a. Print a.
b. Update a and b by swapping their values (b = a + b, a = b - a).
c. Increment i by 1.
Step 7: End the loop.
Step 8: Display a newline to format the output.
Step 9: Stop

Solution:
importjava.util.Scanner;
public class Qno7
{
public static intfibRecursion(int count) {
if (count == 0) {
return 0;
}
if (count == 1 || count == 2) {
return 1;
}
returnfibRecursion(count - 1) + fibRecursion(count - 2);
}

19 |P a g e
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no. ");
intfib_len = sc.nextInt();
System.out.print("Fibonacci Series of " + fib_len + " numbers is: \
n");

for (int i = 0; i <fib_len; i++) {


System.out.print(fibRecursion(i) + " ");
}
}
}
Output:

Question 8:

Write a program to calculate GCD of two no.s using recursion.


Algorithm:

20 |P a g e
Step 1: Start
Step 2: Accept two positive integers, a and b, where a is greater than or
equal to b.
Step 3: Check if b is equal to 0. If it is, then:
Return a as the GCD.
Step 4: If b is not equal to 0, then:
Calculate the remainder of a divided by b using the modulo operation:
remainder = a % b.
Step 5: Recursively call the GCD function with the values b and remainder,
i.e., gcd(b, remainder).
Step 6: Repeat steps 3 to 5 with the new values of b and remainder until b
becomes 0. When b becomes 0, return a as the GCD.
Step 7: Stop

Solution:
public class Qno8
{
public static intCalGCD(inta,int b)
{
if(a==b)
{
return a;
}
else if(a==0)
{
return b;
}
else if(b==0)

21 |P a g e
{
return a;
}
else if(a>b)
{
returnCalGCD(a%b,b);
}
else
{
returnCalGCD(a,b%a);
}
}
public static void main(String args[])
{
int a=6;
int b=2;
System.out.println("First number:"+a);
System.out.println("Second number:"+b);
System.out.println("GCD="+(CalGCD(a,b)));
}
}
Output:

22 |P a g e
Question 9:
Write a program to print the sum of the border elements of 2-d
array of size n*n.
Algorithm:

23 |P a g e
Step 1: Start
Step 2: Initialize a variable sum to 0 to store the sum of border elements.
Step 3: Iterate through the 2D array using two nested loops. The outer loop
will iterate over rows, and the inner loop will iterate over columns.
Step 4: For each element in the 2D array, check if it's part of the border:
 If the current element is in the first row (i == 0) or the last row (i
== n-1) or the first column (j == 0) or the last column (j == n-1),
add the value of the current element to the sum.
Step 5: Continue this process until all elements in the 2D array are
checked.
Step 6: After the loops are complete, the variable sum will hold the sum of
the border elements.
Step 7: Print the value of sum, which is the sum of the border elements.
Step 8: Stop

Solution

importjava.util.Scanner;
public class Qno9
{
public static void main(String args[])
{
intarr[][],n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size");
n=sc.nextInt();
arr=new int[n][n];
System.out.println("Enter elements");
for(int i=0;i<n;i++)
{

24 |P a g e
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}
Bsum(arr,n);
}
static void Bsum(int a[][],int n1)
{
int sum=0;
for(int i=0;i<n1;i++)
{
for(int j=0;j<n1;j++)
{
if(i==j || (i+j)==n1-1)
{
sum=sum+a[i][j];
}
else if(i==0 ||j==0||i==n1-1||j==n1-1)
{
sum=sum+a[i][j];
}

25 |P a g e
}
}
System.out.println("Sum of border elements"+sum);
}
}
Output:

Question 10:
Write a program to generate the sum of two double
dimensionalarray in a third dimensional array.
Algorithm:

26 |P a g e
Step 1: Start
Step 2: Initialize three double-dimensional arrays: array1, array2, and
resultArray. Ensure that the dimensions of array1 and array2 are the same.
Step 3: Create a variable rows and set it to the number of rows in array1
(assuming both arrays have the same dimensions).
Step 4: Create a variable columns and set it to the number of columns in
array1.
Step 5: Initialize an empty array resultArray with dimensions rows x
columns.
Step6: Iterate through the rows using a loop with index i from 0 to rows-1.
Step 7: Inside the outer loop, iterate through the columns using a loop with
index j from 0 to columns-1.
Step 8: For each element at position (i, j) in array1 and array2, add the
corresponding elements.
Step 9: Store the result in the same position (i, j) in resultArray.
Step 10: After iterating through all elements, the resultArray will contain
the sum of the two input arrays.
Step 11: Print or use the resultArray as needed.
Step 12: Stop.

Solution

importjava.util.Scanner;
public class Qno10{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];
int b[][]=new int[4][4];
System.out.println("Enter array 'a' elements");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
27 |P a g e
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter array 'b' elements");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
b[i][j]=sc.nextInt();
}
}
int c[][]=new int[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(+c[i][j]+" ");
}
System.out.println();
}
}}
Output:

28 |P a g e
Question 11:
Write a program to input a natural number within 10,000 and
display it in words.
Algorithm:

29 |P a g e
Step 1: Start
Step 2: Initialize two arrays a1 and a2 with words for numbers 0-19 and
multiples of 10.
Step 3: Define a method three:
a. Check if n is valid (between 1 and 999).
b. If n is between 100 and 999:
i. Extract hundreds place digit (d).
ii. Print word for d followed by "hundred".
iii. Update n to remaining two digits.
iv. If n is not zero, call two for the remaining two digits.
c. If n is not between 100 and 999, call two for the entire number.
Step 4: Define a method two:
a. If n is between 20 and 99:
i. Extract tens place digit (t) and units place digit (u).
ii. If u is zero, print word for t.
iii. If u is not zero, print word for t followed by word for u.
b. If n is not between 20 and 99, print word for entire number.
Step 5: In the main method:
a. Prompt user for a number.
b. Read the input integer.
c. Call three with the input integer.
Step 6: Stop

Solution

importjava.util.Scanner;

public class Q11

static String
a1[]={"zero","one","two","three","four","five","six","seven","eight","
nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixtee
n","seventeen","eighteen","nineteen"};

30 |P a g e
static String
a2[]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","nin
ety"};

static void three(int n)

if(n>999||n<0)

System.out.println("Invalid");

else if(n>=100 && n<=999)

int d=n/100;

System.out.print(a1[d]+ " hundred ");

n=n%100;

if(n!=0)two(n);

else two(n);

static void two(int n)

if(n>=20 && n<=99)

int t=n/10;

int u=n%10;

31 |P a g e
if(u==0)

System.out.print(a2[t-2]);

else

System.out.print(a2[t-2]+" "+a1[u]);

else

System.out.print(a1[n]);

public static void main(String[]args)

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

three(n);

} }

Output:

Question 12:

32 |P a g e
Write a program to input a sentence and a number between 1 to
10and encode the sentence by advancing each character n places.
Algorithm:

Step 1: Start
Step 2: Create a class
Step 3: Inside the class, define a method encode that takes a string s and an
integer n as parameters.
Step 4: Initialize an empty string ss to store the encoded sentence.
Step 5: Initialize a boolean variable f to true. This variable will be used to
check if the input string contains only letters.
Step 6: Use a loop to iterate through each character of the input string s:
a. Get the current character ch.
b. Check if the character is not a letter. If it's not, set f to false and
break out of the loop.
c. Calculate the new ASCII value a by adding n to the ASCII value of
the current character.
d. If the character is uppercase, adjust a to stay within the uppercase
ASCII range.
e. If the character is lowercase, adjust a to stay within the lowercase
ASCII range.
f. Append the character corresponding to the new ASCII value a to the
string ss.
Step 7: Print the original string s, the number of positions n, and the
encoded string ss.
Step 8: If f is false, print "Invalid String" as the input string contained non-
letter characters.
Step 9: Stop

Solution

public class Qno12


{
public void encode(String s, int n)
{
String ss="";inti;boolean f=true;

33 |P a g e
for(i=0;i<s.length();i++)
{
charch=s.charAt(i);
if(!Character.isLetter(ch))
{
f=false;break;
}
int a=ch+n;
if(Character.isUpperCase(ch))
{
if(a>90) a-=32;
}
if(Character.isLowerCase(ch))
{
if(a>122) a-=32;
}
ss+=(char)a;
}
System.out.println("Original string: "+s);
System.out.println("Number of position: "+n);
if(f)
System.out.println("Encode string: "+ss);

34 |P a g e
else
System.out.println("Invalid String");
}
}

Output:

Question 13:

35 |P a g e
Write a program to print all the denominators required for
anamount entered by user.
Algorithm:

Step 1: Start
Step 2: Initialize variables for different denominations (r500, r200, ..., r1)
and set them to 0. Also, initialize a variable count to 0.
Step 3: Prompt the user to enter the amount in rupees.
Step 4: Read the input amount from the user.
Step 5: Use a series of while loops to calculate and print the number of
each denomination required:
a. For each denomination, check if the remaining amount is greater
than or equal to the denomination value.
b. If true, calculate the number of notes or coins of that
denomination, update the remaining amount, and print the result.
c. Use break to exit the loop after processing each denomination.
Step 6: Repeat this process for denominations in decreasing order (from
500 to 1).
Step 7: Stop

Solution

importjava.util.Scanner;
public class Q13
{
public static void main(String[]args)
{
intamt, r500 = 0, r200 = 0, r100 = 0, r50 = 0, r20 = 0, r10 = 0, r5 =
0, r2 = 0, r1 = 0, count = 0;
Scanner sc = new Scanner(System.in);

System.out.print("Enter The Amount in Rupees : \n\n");

36 |P a g e
amt = sc.nextInt();
while (amt>= 500) {
r500 = amt / 500;
amt = amt % 500;
System.out.print("\n500 Rupees Notes : " + r500);
break;
}
while (amt>= 200) {
r200 = amt / 200;
amt = amt % 200;
System.out.print("\n200 Rupees Notes : " + r200);
break;
}
while (amt>= 100) {
r100 = amt / 100;
amt = amt % 100;
System.out.print("\n100 Rupees Notes : " + r100);
break;
}

while (amt>= 50) {


r50 = amt / 50;

37 |P a g e
amt = amt % 50;
System.out.print("\n50 Rupees Notes : " + r50);
break;
}

while (amt>= 20) {


r20 = amt / 20;
amt = amt % 20;
System.out.print("\n20 Rupees Notes : " + r20);
break;
}

while (amt>= 10) {


r10 = amt / 10;
amt = amt % 10;
System.out.print("\n10 Rupees Notes Or Coin : " + r10);
break;
}

while (amt>= 5) {
r5 = amt / 5;
amt = amt % 5;

38 |P a g e
System.out.print("\n5 Rupees Notes Or Coin : " + r5);
break;
}
while (amt>= 2) {
r2 = amt / 2;
amt = amt % 2;
System.out.print("\n2 Rupees Notes Or Coin : " + r2);
break;
}
while (amt>= 1) {
r1 = amt / 1;
amt = amt % 1;
System.out.print("\n1 Rupees Note Or Coin : " + r1);
break;
} }}
Output:

Question 14:

39 |P a g e
Write a program to check if a number is Smith number or not.
Algorithm:

Step 1: Start
Step 2: Prompt the user to enter a number.
Step 3: Read the input number.
Step 4: Check if the input number is less than or equal to 0. If true, print
that it's not a Smith number and end the algorithm.
Step 5: Initialize a boolean variable isComposite to false.
Step 6: Use a loop to check if the input number is composite:
a. Iterate from 2 to one less than the input number.
b. If the input number is divisible by any number in the range, set
isComposite to true and break out of the loop.
Step 7: Check if the number is composite (i.e., isComposite is true)
and not equal to 1.
Step 8: If the number is composite, calculate the sum of its digits
(sumDigits) using a loop.
Step 9: Initialize sumPrimeDigits to 0.
Step 10: Use a loop to find the prime factors of the number:
a. Iterate from 2 to the input number.
b. While the input number is divisible by the current factor, update
the input number and calculate the sum of its digits
(sumPrimeDigits).
Step 11: Check if the remaining input number (t) is greater than 2:
a. If true, calculate the sum of its digits (sumPrimeDigits).
Step 12: Compare the sum of prime digits (sumPrimeDigits) with the sum
of digits (sumDigits).
Step 13: If the sums are equal, print that the number is a Smith number.
Otherwise, print that it's not a Smith number.
Step 14: Stop

Solution

importjava.util.Scanner;

public class Q14

public static void main(String args[]) {

40 |P a g e
Scanner in = new Scanner(System.in);

System.out.print("Enter number: ");

int n = in.nextInt();

if (n <= 0) {

System.out.println(n + " is not a Smith Number.");

return;

booleanisComposite = false;

for (int i = 2; i < n; i++) {

if (n % i == 0) {

isComposite = true;

break;

if (isComposite&& n != 1) {

intsumDigits = 0;

int t = n;

while (t != 0) {

41 |P a g e
int d = t % 10;

sumDigits += d;

t /= 10;

intsumPrimeDigits = 0;

t = n;

for(int i = 2; i < t; i++) {

while(t % i == 0) {

t /= i;

int temp = i;

while (temp != 0) {

int d = temp % 10;

sumPrimeDigits += d;

temp /= 10;

if(t > 2) {

while (t != 0) {

42 |P a g e
int d = t % 10;

sumPrimeDigits += d;

t /= 10;

if (sumPrimeDigits == sumDigits)

System.out.println(n + " is a Smith Number.");

else

System.out.println(n + " is not a Smith Number.");

}else {

System.out.println(n + " is not a Smith Number.");

}}}

Output:

Question 15:

43 |P a g e
Write a program to print unique digit number between a given
range.
Algorithm:

Step 1: Start
Step 2: Declare variables start, end, and an array arr.
Step 3: Initialize a constructor that takes two parameters s and e and
initializes the start and end variables.
Step 4: Define a method check that takes an integer n as input and checks
the count of repeated digits in the number:
a. Convert the integer n to a string s.
b. Convert the string to a character array c.
c. Iterate through the array and count the occurrences of each digit.
Step 5: Define a method check2 that iterates through the numbers in the
range [start, end] and prints those that have no repeated digits.
Step 6: In the main method:
a. Prompt the user to enter the start and end values.
b. Read the input values.
c. Create an object of the class with the provided start and end values.
d. Call the check2 method to print unique digit numbers.
Step 7: Stop

Solution

importjava.util.Scanner;

public class Qno15

intstart,end;

intarr[];

Qno15(ints,int e)

start=s;

end=e;

44 |P a g e
}

int check(int n) {

int count=0;

String s=String.valueOf(n);

char c[]=s.toCharArray();

int l=c.length;

for(int i=0;i<l-1;i++)

for(int j=i+1;j<l;j++)

if (c[i]==c[j])

count++;

return count;

void check2()

for(int i=start;i<=end;i++)

if(check(i)==0)

45 |P a g e
System.out.println(i);

public static void main(String args[])

intstart,end;

Scanner sc=new Scanner(System.in);

System.out.println("Enter a start");

start=sc.nextInt();

System.out.println("Enter a end");

end=sc.nextInt();

Qno15 obj=new Qno15(start,end);

obj.check2(); } }

Output:

Question 16:
Write a program to print the frequency of each elements in a string.

46 |P a g e
Algorithm:

Step 1: Start
Step 2: Prompt the user to enter a string.
Step 3: Read the input string.
Step 4: Initialize an empty string s1 to store unique characters
encountered.
Step 5: Iterate through each character (ch) in the input string:
a. Initialize a variable c to 0 to count the frequency of the current character.
b. Iterate through each character (ch1) in the input string to count the
occurrences of ch.
c. If the current character is equal to ch1, increment the count c.
d. If the character ch is not present in the string s1, print the character and
its frequency, and update s1 with the current character.
Step 6: End

Solution
importjava.util.Scanner;
public class Qno16
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String s=sc.nextLine();
String s1=" ";
for(int i=0;i<s.length();i++)
{
charch=s.charAt(i);
int c=0;

47 |P a g e
for(int j=0;j<s.length();j++)
{
char ch1=s.charAt(j);
if(ch==ch1)
{
c++;
}
}
if(s1.indexOf(ch)==-1)
{
s1=s1+ch;
System.out.println(ch+"\t"+c);
}
}
}
}
Output:

Question 17:

48 |P a g e
Write a program to sort an array of size n using selection sort.

Algorithm:

Step 1: Start
Step 2: Start with the first number in the list.
Step 3: Go through the entire list to find the smallest number.
Step 4: Compare each number with the first one.
Step 5: If you find a smaller number, remember its position.
Step 6: Swap the smallest number found with the first number.
Step 7: This puts the smallest number in the first position.
Step 8: Move to the next position in the list as the starting point for the
unsorted part.
Step 9: The part before this position is now sorted.
Step 10: Repeat steps 3-4 until the entire list is sorted.
Step 11: Display the sorted array.
Step 12: Stop

Solution

importjava.util.Scanner;
public class Qno17
{
void sort(intarr[])
{
int n = arr.length;
for (int i = 0; i < n - 1; i++) {

intmin_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] <arr[min_idx])
min_idx = j;

49 |P a g e
}

int temp = arr[min_idx];


arr[min_idx] = arr[i];
arr[i] = temp;
}
}
voidprintArray(intarr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Qno17 ob = new Qno17();
System.out.println("Enter the elements no. of array");
int n=sc.nextInt();
intarr[]=new int[n];
System.out.println("Enter elements");

50 |P a g e
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
Output:

51 |P a g e
Question 18:
Write a program to check if a number is a magic number or not.
Algorithm:

Step 1: Start
Step 2: Take an input integer num.
Step 3: Set n equal to num.
Step 4: Initialize sum to 0.
Step 5: Repeat Until n Becomes a Single Digit:
o While n is greater than 9, repeat the following steps:
 Set sum to 0.
 While n is not equal to 0, do the following:
 Get the last digit of n (remainder when divided by
10) and store it in d.
 Update n to be the result of integer division of n by
10.
 Add d to sum.
 Set n to the value of sum.
Step 6: Check if n is Equal to 1:
o If n is equal to 1, then the original number num is a magic
number.
 Print "It's a magic no."
o Otherwise,
 Print "It's not a magic no."
Step 7: Stop

Solution

importjava.util.Scanner;
public class Qno18{
publicstatic void main(String args[]) {
Scanner sc=new Scanner(System.in);
int sum=0;
System.out.println("Enter a no.");
intnum=sc.nextInt();

52 |P a g e
int n=num;
int d;
while(n>9)
{
while(n!=0)
{
d=n%10;
n=n/10;
sum+=d;
}
n=sum;
sum=0 }
if(n==1)
System.out.println("It's a magic no.");
else
System.out.println("It's not a magic no."); } }
Output:

53 |P a g e
Question 19:
Write a program to insert a given element at any position inside
aarray.
Algorithm:

Step 1: Start
Step 2: Take the number of elements n for the array.
Step 3: Create an array a[] of size n+1.
Step 4: Input the elements into the array.
Step 5: Take the position pos where the element is to be inserted.
Step 6: Take the element x to be inserted.
Step 7: Starting from the end of the array, shift elements to the right from
the specified position to Step 8: make room for the new element.
Step 9: Insert the element x at the specified position.
Step 10: Print the array a[] after the insertion
Step 11: Stop

Solution

importjava.util.Scanner;
public class Qno1 {
public static void main(String[] args) {
int n, pos, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n+1];
System.out.println("Enter all the elements:");

for(int i = 0; i < n; i++) {


a[i] = s.nextInt(); }

54 |P a g e
System.out.print("Enter the position where you want to insert
element:");
pos = s.nextInt();
System.out.print("Enter the element you want to insert:");
x = s.nextInt();
for(int i = (n-1); i >= (pos-1); i--){
a[i+1] = a[i]; }
a[pos-1] = x;
System.out.print("After inserting:");
for(int i = 0; i < n; i++)
{
System.out.print(a[i]); }
System.out.print(a[n]); } }
Output:

Question 20:
Write a program to delete a element in array and bring right one
toleft by one place.
Algorithm:

Step 1: Start
Step 2: Create an array arr[] of size n.
Step 3: Input elements into the array.

55 |P a g e
Step 4: Input the element key to be removed.
Step 5: Initialize an index variable index to 0.
Step 6: Iterate through each element in the array.
Step 7: If the current element is not equal to the specified key, store it in
the array at the index position and increment index.
Step 8: Return a new array containing the elements up to the index.
Step 9: Print the array after removing the specified element.
Step 10:Stop

Solution

importjava.util.Arrays;
importjava.util.Scanner;
class Qno20 {
public static int[] removeElements(int[] arr, int key)
{
int index = 0;
for (int i=0; i<arr.length; i++)
if (arr[i] != key)
arr[index++] = arr[i];
returnArrays.copyOf(arr, index);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System .in);
int[] array =new int[5];
System.out.println("Enter the elements");

56 |P a g e
for(int i=0;i<5;i++){
array[i] = sc.nextInt();
}
System.out.println("Enter the element,that should be deleted");
int key = sc.nextInt();;
array = removeElements(array, key);
System.out.println(Arrays.toString(array));
}
}
Output:

Question 21:
Write a program to find out the no. of times a substring is present
in the main string.
Algorithm:

Step 1:Start
Step 2: Accept the main string and the substring as inputs.

57 |P a g e
Step 3: Set a variable count to 0 to keep track of the number of
occurrences.
Step 4: Set start_index to 0, representing the starting index for
searching in the main string.
Step 5: Start a loop that continues until the end of the main string is
reached.
Step 6: Use the find method to locate the substring in the main string
starting from the current start_index.
Step 7: If the substring is found, Increment the count by 1.
Step 8: Update the start_index to the index where the last
occurrence was found plus 1.
Step 9: If the substring is not found (i.e., find returns -1), exit the
loop.
Step 10: After the loop, print or return the value of count as the
result.
Step 11: Stop
Solution

importjava.util.*;
public class Qno21
{
public static booleanisEmpty(String s) {
return s == null || s.length() == 0;
}
public static intcountMatches(String text, String str)
{
if (isEmpty(text) || isEmpty(str)) {
return 0;
}

58 |P a g e
int index = 0, count = 0;
while (true)
{
index = text.indexOf(str, index);
if (index != -1)
{
count ++;
index += str.length();
}
else {
break;
}
}

return count;
}

public static void main(String[] args)


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");

59 |P a g e
String text = sc.nextLine();
System.out.println("Enter a substring");
String str =sc.nextLine();

int count = countMatches(text, str);


System.out.println("Number of times the substring
appeared:"+count);
}
}
Output:

Question 22:
Write a program to arrange a sentence in ascending order of
word’s length.
Algorithm:

Step1:Start
Step2:Accept the sentence as input.

60 |P a g e
Step3:Split the sentence into words.
Step4:Compare the lengths of adjacent words and swap them if
necessary.
Step5:Continue this process until the list is sorted.
Step6:Initialize an empty string to store the sorted sentence.
Step7:Iterate through the sorted list of words.
Step8:Concatenate each word with a space and append it to the
result string.
Step9:Print or return the sorted sentence.
Step10:Stop

Solution

importjava.util.Scanner;
class Q22
{
void sort(String arr[])
{

for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++)
{
if(arr[i].length()<arr[j].length())
{
String temp=arr[i];

61 |P a g e
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}

public static void main(String[]args)


{int c=0;
Q22 ob=new Q22();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Sentence");
String sen=sc.nextLine();

Scanner ob1=new Scanner(sen);


while(ob1.hasNext())
{
ob1.next();
c++;
}ob1.close();
String arr[]=new String[c];
Scanner ob2=new Scanner(sen);

62 |P a g e
int i=0;
while(ob2.hasNext())
{
String word=ob2.next();
arr[i++]=word;
}ob.sort(arr);
for(i=0;i<c;i++)
System.out.println(arr[i]);
}
}
Output:

Question 23:

Write a program to print the number of vowels in each word of


asentence.
Algorithm:

Step1:Start
Step2:Accept the sentence as input.

63 |P a g e
Step3:Split the sentence into words.
Step4:Initialize a variable to store the count of vowels in each word.
Step5:Iterate through each word in the list.
Step6:For each word, iterate through its characters and count the
vowels (a, e, i, o, u).
Step7:Print or store the count for each word.
Step8:Print or return the counts of vowels in each word.
Step9:Stop
Solution

importjava.util.Scanner;
public class Qno23
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
str=str.toLowerCase();
int c=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||
str.charAt(i)=='o'||str.charAt(i)=='u')
{

64 |P a g e
c++;
}
}
System.out.println("Total no. of vowels in string:"+c);
}
}
Output:

Question 24:
Write a program to print the longest and shortest word of
asentence.
Algorithm:

Step1:Start
Step2:Accept the sentence as input.
Step3:Split the sentence into words.

65 |P a g e
Step4:Initialize variables to store the longest and shortest words.
Step5:Iterate through each word in the list.
Step6:Compare the length of each word with the lengths of the
current longest and shortest words.
Step7:Update the variables accordingly.
Step8:Print or return the longest and shortest words.
Step9:Stop
Solution

importjava.util.Scanner;
public class Qno24
{
public static void main(String srgd[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string:");
String st=sc.nextLine();
findMethod(st);
}
static public void findMethod(String s) {
String str = s + " ";
charch = ' ';
intlen = str.length(), l = 0;
int min = len, max = 0;
String shortest_word = "", longest_word = "", word = "";
for (int i = 0; i <len; i++) {

66 |P a g e
ch = str.charAt(i);
if (ch != ' ') {
word += ch;
}
else {
l = word.length();
if (l < min) {
min = l;
shortest_word = word;
}
if (l > max) {
max = l;
longest_word = word;
}
word = "";
}
}
System.out.println("Shortest word = " + shortest_word + " with
length " + min);
System.out.println("Longest word = " + longest_word + " with
length " + max);
}
}

67 |P a g e
Output:

Question 25:
Write a program to remove all the repeating characters from
astring.
Algorithm:

Step1:Start
Step2:Accept the string as input.
Step3:Create an empty string to store the result (initially empty).

68 |P a g e
Step4:Create a set to keep track of characters that have been
encountered.
Step5:Iterate through each character in the input string.
Step6:For each character, check if it is already in the set.
Step7:If the character is not in the set, add it to both the result
string and the set.
Step8:If the character is already in the set, skip it (as it is a
repeating character).
Step9:Print or return the result string with repeating characters
removed.
Step10:Stop

Solution

importjava.util.Scanner;
public class Qno25
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1;
System.out.println("Enter a string");
str1=sc.nextLine();
String ns=" ";
int l=str1.length();
for(int i=0;i<l;i++)
{

69 |P a g e
charch=str1.charAt(i);
if(ns.indexOf(ch)==-1)
ns=ns+ch;
}
System.out.println("After removing repeated charcters:"+ns);
}
}
Output:

70 |P a g e

You might also like