0% found this document useful (0 votes)
5 views70 pages

Computer Project

The document outlines several programming tasks involving mathematical concepts such as prime numbers, sorting, and packing cartons. Each task includes a description, example inputs and outputs, and a step-by-step algorithm for implementation. The document also provides Java code snippets for each task, detailing variable usage and logic flow.

Uploaded by

ig.souldetonator
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)
5 views70 pages

Computer Project

The document outlines several programming tasks involving mathematical concepts such as prime numbers, sorting, and packing cartons. Each task includes a description, example inputs and outputs, and a step-by-step algorithm for implementation. The document also provides Java code snippets for each task, detailing variable usage and logic flow.

Uploaded by

ig.souldetonator
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/ 70

Q1) A prime- Adam integer is a positive integer (without leading zeros) which is a prime as

well as an Adam-number.
[2020]
Adam number: The Square of a number and the square of its reverse are reverse to each
other.
Ex: if n=13 and reverse of n=31, then
(13)2 =169
(31)2=961 which is reverse of 169
Thus, 13, is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all
prime-Adam integers that are in range between m and n (both inclusive) and output them
along with the frequency, in the following format below:
Ex: INPUT: m=5
n=100
OUTPUT: THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY IS: 3
Step 1: Start
Step 2: Getting the upper limit and lower limit to extract the PRIME-ADAM numbers.
Step 3: Checking if the lower limit is greater than the upper limit then displays Invalid Input.
Step 4: Running a loop from lower limit to upper limit to find the PRIME-ADAM numbers between them.
Step 5: Checking if the loop control variable satisfies the conditions of the PRIME-ADAM number.
Step 6: Checking if the reverse of the original number squared is equal to the reverse of the original number squared
and the original number is prime.
Step 7: make a reverse function to return the reverse of the number entered.
Step 8: make a prime function to check and return if the number is prime or not.
Step 9: Print the PRIME-ADAM integers and update the counter variable to keep the count of numbers found.
Step 10: Print the frequency of PRIME-ADAM integers between upper and lower limit.
Step 11: End

import java.util.*; //importing util package to use scanner class


public class que1
{
static int rev(int n) //function to find reverse
{
int r=0; //to store reverse of the number
while(n!=0) //while loop to find reverse
{
r=r*10+(n%10); //storing reverse
n/=10; //updating the number
}
return r; //returning statement
}
static boolean prime(int n) //prime function
UID: 7925885
1
{
for(int i=2;i<=n/2;i++)
if(n%i==0)
return false; //returning statement
return true; //returning statement
}
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner
System.out.println("enter lower and upper limit");
int m=sc.nextInt(); //lower limit
//upper limit
int n = sc.nextInt();
if(m>n) //invalid condition
System.out.println("INVALID INPUT");
else{
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
int c=0; //for frequency
for(int i=m;i<n;i++)
if(rev((int)Math.pow(i,2))==(int)Math.pow(rev(i),2) && prime(i)) //checking
adam prime number
{
System.out.print(i+"\t");c++; //updating counter and printing statement
}
System.out.println();
System.out.println("FREQUENCY IS:"+c); //showing frequency
}
}
}

UID: 7925885
2
VARIABLE DESCRIPTION

Int n (functions) A variable to store original numbers

Int r To store the reverse of the number

Int i Loop control variable and checking variable for Prime


Adam numbers

Int m Stores lower limit

Int n Stores upper limit

Int c For the count of occurrence of Prime Adam numbers

UID: 7925885
3
Q2) Write a program to declare a 1 D array a[] and a square matrix b[][] of size N, where N
>2 and N< 10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
[2019]
Sort the elements of the single dimensional array in ascending order using any standard
sorting technique and display the sorted elements.
Fill the square matrix b[][] in the following format.
If that array a[]= {5,2,1,8} the after sorting a[] ={1,2,5,8}
Then the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
Display the filled matrix in the above format.

Step 1: Start
Step 2: input the size of Array.
Step 3: check if the size of Array is valid or not.
Step 4: create an array of the required size.
Step 5: input the values from the user.
Step 6: sorting the array by using bubble sort technique.
Step 7: print the sorted array.
Step 8: Create a double dimension array.
Step 9: Filling the Array using nested loops and required variables.
Step 10: Displaying the new double dimension array.
Step 11: End

import java.util.*; //for using scanner class


class que2
{
public static void main()
{
Scanner sc = new Scanner(System.in); //creating object of scanner
System.out.println("Enter array size");
int N=sc.nextInt(); //size of array
if(N<=2 || N>=10) //invalid condition
System.out.println("INVALID INPUT");
else
{
int a[] = new int[N]; //creating array
System.out.println("Enter array elements");
for(int i=0;i<N;i++)
a[i]=sc.nextInt(); //inputting array elements
for(int i=0;i<N;i++) //sorting array using bubble sort technique
for(int j=0;j<N-i-1;j++)
if(a[j]>a[j+1])
UID: 7925885
4
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
System.out.println("Sorted Array :");
for(int i=0;i<N;i++)
System.out.print(a[i]+" "); //printing sorted elements
System.out.println();
int b[][] = new int[N][N]; //creating new double dimension array
int x=0; //for entering elements from the opposite direction
for(int i=N-1;i>=0;i--) //filling the array
{
int j;
for(j=0;j<=i;j++)
b[x][j]=a[j];
int m=0;
for(int k=N-j+1;k<N;k++)
b[i][k]=a[m++];
x++;
}
System.out.println("New Array :"); //displaying new array
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
System.out.print(b[i][j]+" ");
System.out.println();
}
}
}

UID: 7925885
5
VARIABLES DESCRIPTION

Int N To store the size of the array

Int a[] Array to store the elements

Int i Outer loop control variable

Int j Inner loop control variable

Int b[][] Double dimension array to store elements

Int x Variable to keep the index number for storing numbers in a


specific pattern

Int j Variable to keep the index number for storing numbers in a


specific pattern

UID: 7925885
6
Q3) A company manufactures packing cartons in four sizes i.e. cartons in accommodate 6
boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes
to be packed (N) by user (maximum up to 1000 boxes) and display the break-up of the
cartons used in descending order of the capacity (i.e. preference should be given to
highest capacity available,
Ex1:
INPUT: N=726
OUTPUT: 48 * 15 =720
6 *1 = 6
Remaining boxes: =0
Total number of boxes: =726
Total number of cartons: =16
Ex 2:
INPUT: N=140
OUTPUT: 48 * 2 = 96
24 *1 = 24
12*1 = 12
6*1 =6
Remaining boxes 2 * 1 = 2
Total number of boxes =140
Total number of cartons = 6

Step 1: Start
Step 2: input the number of boxes.
Step 3: checking if the number is valid or not and printing relevant messages.
Step 4: create a variable to the total number of cartons required.
Step 5: create an array to keep the limit of each carton.
Step 6: run a loop four times.
Step 7: storing the number of cartons required.
Step 8: keep a count of the total number of cartons used.
Step 9: check if that carton is required or not and print the relevant details.
Step 10: print the number of remaining boxes, total boxes and total Cartons required.
Step 11: End

import java.util.*; //for using scanner class


class que3
{
public static void main()
{
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("Enter number of boxes");
int N=sc.nextInt(); //number of boxes
System.out.println();
if(N>1000) //invalid condition
System.out.println("INVALID INPUT");
else
{

UID: 7925885
7
int s=0;int n=N; //for total cartons required and copy of no. of original boxes
int a[]={48,24,12,6}; //array to keep limit of boxes
for(int i=0;i<4;i++) //loop running for elements
{
int s1=N/a[i]; //storing the no. of cartons required
s+=s1; //total cartons
if(s1!=0) //checking if the carton is required or not
System.out.println(a[i]+"*"+s1+"="+(a[i]*s1)); //printing statement
N=N%a[i]; //updating the value of no. of boxes
}
System.out.println("Remaining Boxes : 2*"+ (N/2)+"="+(N)); //remaining boxes
System.out.println("Total Boxes : "+n); //total boxes
System.out.println("Total Cartons : "+(s+(N/2))); //total cartons
}
}
}
VARIABLE DESCRIPTION

Int N Original number of boxes entered by user

Int s Total number of cartons

Int n Copy of number of original boxes

Int a[] Array to store limit of each carton

Int i Loop control variable

Int s1 To store the number of cartons required per size

UID: 7925885
8
Q4) A Goldbach number is a positive even integer that can be expressed as the sum of two
odd primes.(All even integers greater than 4 are Goldbach numbers.) [2018]
Ex: 6=3 +3
10=3 + 7
10= 5 + 5
Write a program to accept an even integer ‘N’ where N>9 and N<50. Find all the odd prime
pairs whose sum is equal to the number ‘N’.
Ex: INPUT: N=30
OUTPUT: PRIME PAIRS ARE: 7, 23
11, 19
13, 17
Step 1: Start
Step 2: input a number.
Step 3: checking if the number is valid or not and printing relevant details.
Step 4: running a loop from 2 to number divided by 2 to print the prime pairs.
Step 5: make a prime function.
Step 6: check if the number entered in the function is prime or not and return the value.
Step 7: in the loop of the main function check if the loop control variable and the difference between the number
and variable is prime or not.
Step 8: printing the number in a specific manner.
Step 9: End

import java.util.*; //using scanner class


public class que4
{
static boolean prime(int n) //checking prime by using functions
{
for(int i=2;i<n/2;i++)
if(n%i==0)
return false; //returning statements
return true; //returning statements
}
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter the number");
int n=sc.nextInt(); //input number
if(n%2==1||(n<9&&n>50)) //invalid conditions
{
System.out.println("INVALID INPUT");
System.exit(0); //exiting the program
}
System.out.println("PRIME PAIRS ARE: ");
for(int i=2;i<n/2;i++) //loop for printing the prime pairs
{
if(prime(i) && prime(n-i)) //checking condition
UID: 7925885
9
System.out.println(i+","+(n-i)); //printing statements
}
}
}

VARIABLE DESCRIPTION

Int n To store the original number

Int i Loop control variable

UID: 7925885
10
Q5) Give two positive numbers M and N, such that M is between 100 and 1000 and N is less
than 100. Find the smallest integer that is greater than M and whose digits add up to N. For
example if M=10 and N= 11, then the smallest integer greater than 100 whose digits add up
to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest
required number whose sum of all its digits is equal to N. Also print the total number of
digits present in the required number. The program should check for validity of the inputs
and display an appropriate message for an invalid input.
[2015]

EX: Input: M=100


N=11
Output: The required number is =119
Total number of digits=3

Input: M=1500
N=25
Output: The required number is= 1699
Total number of digits=4

Input: M=99
N=11
Output: Invalid Input

Step 1: Start
Step 2: input the lower limit.
Step 3: input the upper limit.
Step 4: checking if both the limits are valid or not.
Step 5: make a copy of the lower limit.
Step 6: running a while loop until the sum of digits is equal to the upper limit.
Step 7: make a function to find and return the sum of digits of the entered number.
Step 8: define the base case and the recursive case of the function.
Step 9: print the required number.
Step 10: print the total no. of digits of the number.
Step 11: End

import java.util.*; //for using scanner class


public class que5
{
static int sod(int n) //recursive technique to find sum of the digits
{
if(n==0) //base case
return 0;
else //recursive case
return n%10+sod(n/10);
}
public static void main()
{

UID: 7925885
11
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("Enter the value of M and N");
int m = sc.nextInt(); //lower limit
int n = sc.nextInt(); //upper limit
if((m>10000||m<100)&&n>100) //invalid condition
System.out.println("invalid input");
else
{
int a=m; //copy of lower limit
while(sod(a)!=n) //while loop to check the condition
a++; //updating the value of a
System.out.println("Required number is= "+a); //printing required details
System.out.println("Total no. of digits= "+(a+"").length());
}
}
}
VARIABLE DESCRIPTION

Int n(function) Parameter to find the sum of digits

Int m Lower limit

Int n Upper limit

Int a Copy of the lower limit to operate on a

UID: 7925885
12
Q6) Design a program to accept a day number (between 1 and 366), year (in 4 digits) from
the user to generate and display the corresponding date. Also accept ‘N’ (1<=N<=100) from
the user to compute and display the future date corresponding to ‘N’ days after the
generated date. Display an error message if the value of the day number, year and N are
within the limit or not according to the condition specified.
[2019]
Ex: INPUT: DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYAS): 45
OUTPUT: 26 TH SEPTEMBER, 2018
DATE AFTER 45 DAYS: 9 TH FEBRUARY, 2019

Step 1: Start
Step 2: input the number of days.
Step 3: input the year and number of days ahead.
Step 4: checking the validation of the input values.
Step 5: create a static and void function.
Step 6: create an array to store the name of the months as well as the maximum days in a month.
Step 7: check if the number of days is more than a year to convert them into a year.
Step 8: running a loop to find the date and month of the entered day.
Step 9: printing the date, month and year in specific manner.
Step 10: calling the function first with the original date.
Step 11: calling the function with the number of days ahead.
Step 12: End

import java.util.*; //for using scanner class


public class que6
{
static void convert(int days,int y) //for converting no. of days to date
{
int s=0;int c=0; //for day and month
int a[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //array to store the dates in a
specific month
String
b[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMB
ER","OCTOBER","NOVEMBER"
,"DECEMBER"}; //for storing the name of months
while(days>366) //if the no. of days are more than a year convert them into years
{
days -= 366;
y++;
}
if(y%4==0 && y%100!=0 || y%400==0) //if the year is leap year update the days of
february
a[1]=29;
for(int i = 0;i<12;i++) //loop to find the date and month
UID: 7925885
13
{
s+=a[i];
if(s>=days)
{
c=i;
break;
}
}
String s1=""; //to store the suffix of date
if(a[c]-(s-days)==11||a[c]-(s-days)==12||a[c]-(s-days)==13) //to store 'th' for
11,12,13
s1="TH";
else if((a[c]-(s-days))%10==1) //to store 'ST' for 1
s1="ST";
else if((a[c]-(s-days))%10==2) //to store 'ND' for 2
s1="ND";
else if((a[c]-(s-days))%10==3) //to store 'RD' for 3
s1="RD";
else //at the end if none of the special case matches store 'TH'
s1="TH";
System.out.println("date:\n"+(a[c]-(s-days))+s1+" "+b[c]+" "+(y)); //printing the date
in required form
}
public static void main()
{
Scanner sc= new Scanner(System.in); //object of scanner class
System.out.println("Enter the day no.");
int d = sc.nextInt(); //store no. of days
System.out.println("Year"); //to store the year for reference
int y = sc.nextInt();
System.out.println("Date after 'N' days");
int N = sc.nextInt(); //no. of days ahead
System.out.println();
if((d>366||d<1)||((y+"").length()!=4)||(N<1||N>100)) //invalidating statements
System.out.println("invalid input");
else
{
convert(d,y); //calling the function for initial date
System.out.println("after "+N+" days:"); //date after 'N' days
convert(d+N,y); //again calling the function
}
}
UID: 7925885
14
}

VARIABLE DESCRIPTION

Int d Original number of days

Int y Number of years

Int N Number of days ahead

Int s To store the day

Int c To store month

Int a[] To store the maximum number of days in a month

String b[] To store the name of months

String s1 To store the suffix of the date

UID: 7925885
15
Q7) Write a program to declare a square matrix A[][] of order (M×M) where M must be
greater than 3 and less than 10. Allow the user to input positive integers into this matrix.
Perform following tasks on the matrix.
[2016]
Sort the non boundary elements in ascending order using any standard sorting technique
and rearrange them in matrix.
Calculate the sum of both the diagonals.
Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Ex: M=4
Input:
9 2 1 2
8 13 8 4
15 6 3 11
7 12 23 8
Output:
Original matrix:
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8

Rearranged matrix
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8

Diagonal elements
9 5
3 6
8 13
7 8
Sum of the diagonal elements=59

Step 1: Start
Step 2: enter the number of rows and columns in the square matrix.
Step 3: check if the number of rows is valid or not.
Step 4: create a square matrix.
Step 5: input the elements to fill the matrix.
Step 6: create a temporary array to store required elements.
Step 7: sorting the temporary array using bubble sort technique.
Step 8: updating the rearranged matrix in the original matrix.
Step 9: print the new matrix.
Step 10: print the diagonal elements and evaluate their sum.
Step 11: print the relevant details.
Step 12: End.

import java.util.*; //importing the package of scanner class


UID: 7925885
16
public class que7
{
public static void main()
{
Scanner sc = new Scanner(System.in); //object of scanner created
System.out.println("enter no. row/columns ");
int M = sc.nextInt();
if(M<3||M>10) //validating condition
System.out.println("invalid input");
else
{
int A[][] = new int[M][M]; //array created
System.out.println("Enter array elements");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
A[i][j]=sc.nextInt(); //array input
}
System.out.println("Original Array :");
for(int i=0;i<M;i++) //array printing
{
for(int j=0;j<M;j++)
System.out.print(A[i][j]+" ");
System.out.println();
}
int k=0;
int elem[] = new int[(M-2)*(M-2)]; //array to store non-border elements
for(int i=1;i<M-1;i++)
{
for(int j=1;j<M-1;j++)
elem[k++]=A[i][j]; //non-border elements stored
}
int t=0; //temporary variable for swapping
for(int i=0;i<elem.length;i++)
{
for(int j=0;j<elem.length-i-1;j++)
if(elem[j]>elem[j+1]) //ascending order
{
t=elem[j];
elem[j]=elem[j+1];
elem[j+1]=t;
}
UID: 7925885
17
}
k=0;
for(int i=1;i<M-1;i++)
{
for(int j=1;j<M-1;j++) //sorted elements returned in original array
A[i][j]=elem[k++];
}
System.out.println("Rearranged Array :");
for(int i=0;i<M;i++) //array printing
{
for(int j=0;j<M;j++)
System.out.print(A[i][j]+" ");
System.out.println();
}
int s=0; //sum of diagonal elements
System.out.println("Diagonal Elements :");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
if((i==j)||(i+j==M-1)) //diagonal elements
{
System.out.print(A[i][j]+" ");
s+=A[i][j]; //adding to sum
}
else
System.out.print(" ");
System.out.println();
}
System.out.println("Sum of the diagonal elements="+s); //printing the sum of
diagonal elements
}
}}

VARIABLE DESCRIPTION

Int M Number of rows and columns

Int a[][] Double dimension array to store elements

Int i Outer loop control variable

Int j Inner loop control variable

UID: 7925885
18
Int temp[] Array to store non boundary elements

Int c Counter for index number of array

Int s Sum of diagonal elements

UID: 7925885
19
Q8) Write a program to accept a sentence which may be terminated by either ‘.’ ,’?’, or ‘!’
only. The words are separated by a single blank space and are in UPPERCASE. Perform the
following task: [2019]
Check for the validity of the accepted sentence
Convert the non-palindrome words of the sentence into palindrome words by
concatenating the word by its reverse (excluding the last character).

Ex: The reverse of the word HELP would be LEH (omitting the last alphabet) and by
concatenating both, the new palindrome word is HELPLEH. Thus, the word HELP becomes
HELPLEH.

NOTE: The words which end with repeated alphabets, for ex. ABB would becomes ABBA
and not ABBBA and XAZZZ becomes XAZZZAX
Display the original sentence along with the converted sentence.

Ex: INPUT: THE BIRD IS FLYING


OUTPUT: THEHT BIRDRIB ISI FLYINGNIYLF

Step 1: Start
Step 2: accept a sentence from the user.
Step 3: check the validity of the sentence.
Step 4: splitting the words in an array.
Step 5: create an empty string.
Step 6: obtaining the last index where the last character was present in the original string.
Step 7: add the original word to the new string.
Step 8: start storing the reversed word from the obtained index.
Step 9: add a blank space in the new string.
Step 10: print the new string.
Step 11: End

import java.util.*; //package of scanner class imported


public class que8
{
public static void main()
{
Scanner sc= new Scanner(System.in); //object of scanner created
System.out.println("enter a sentence ending with only .?! in uppercase");
String s = sc.nextLine().trim().toUpperCase(); //working on the input string
if(".?!".indexOf(s.charAt(s.length()-1))==-1) //validating statements
System.out.println("invalid sentence");
else
{
String sa[] = (s.substring(0,s.length()-1)).split(" "); //removing the last punctuation
String s1=""; //new string to store the word
for(int i=0;i<sa.length;i++)
{ int j; //index of last character
for(j=sa[i].length()-1;j>=0;j--)
if(sa[i].charAt(j)!=sa[i].charAt(sa[i].length()-1))
UID: 7925885
20
break;
s1+=sa[i];
for(int k=j;k>=0;k--) //loop to store the reversed string
s1+=sa[i].charAt(k);
s1+=" ";
}
System.out.println(s1); //printing the final string
}
}
}
VARIABLE DESCRIPTION

String s To store the original sentence

String sa[] To store the words

String s1 To store new words

Int i Outer loop control variable

Int j Inner loop control variable

Int k For reversing the string

UID: 7925885
21
Q9) The names of the teams participating in a competition should be displayed on a banner
vertically to accommodate as many teams as possible in a single banner. Design a program
to accept the names of N teams, where 2<N<9 and display them in a vertical order, side by
side with a horizontal tab (i.e. eight spaces).

Ex 1:
Input: N=3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote

Output:
E R C
m o o
u a y
s d o
t
R e
o
l
s

ex 2:
Input : N=10
Output: Invalid Input

Step 1: Start
Step 2: enter the size of the array.
Step 3: get an input for the first team using scanner.
Step 4: create an array of size entered by the user.
Step 5: input the names of the teams.
Step 6: obtain the length of the longest team’s name.
Step 7: run an outer loop for accessing the array.
Step 8: run an inner loop to access the team’s name.
Step 9: print if the length of the team’s name is smaller than the loop variable.
Step 10: print each character of the team’s name.
Step 11: change the line to maintain the format of printing.
Step 12: End

import java.util.*; //importing the package of scanner class


public class que9
{
public static void main()
{
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("enter the value of N");
int N = sc.nextInt();
sc.nextLine();
if(N<2||N>9) //validating condition
System.out.println("invalid input");
UID: 7925885
22
else
{
String a[]=new String[N];int lmax=0;
for(int i=0;i<N;i++) //loop to input the names of the teams
{
System.out.print("team "+(i+1)+":");
a[i]=sc.nextLine();
if(lmax<a[i].length()) //finding length of longest word
lmax=a[i].length();
}
for(int i=0;i<lmax;i++) //printing the team's name in specific manner
{
for(int j=0;j<N;j++)
if(i>=a[j].length())
System.out.print("\t");
else
System.out.print(a[j].charAt(i)+"\t");
System.out.println();
}
}
}
}
VARIABLE DESCRIPTION

Int N Number of teams

Int a[] Array to store team’s names

Int lmax To store the length of longest word

Int i Outer loop control variable

Int j Inner loop control variable

UID: 7925885
23
Q10) Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the
number of rows and the number of columns such that M must be greater than 2 and less
than 20. Allow the user to input integers into this matrix. Display appropriate error
message for an invalid input. Perform the following tasks:
(a) Display the input matrix.
(b) Create a mirror image matrix.
(c) Display the mirror image matrix.
Test your program with the sample data and some random data:

Example 1
INPUT : M = 3
4 16 12
8 2 14
413
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
413
MIRROR IMAGE MATRIX
12 16 4
14 2 8
316

Example 2
INPUT : M = 22
OUTPUT : SIZE OUT OF RANGE

Step 1: Start
Step 2: enter the number of rows or columns for the square matrix.
Step 3: check the valid condition after that create an array.
Step 4: enter the elements of each array.
Step 5: print the original matrix.
Step 6: create an array for mirror image.
Step 7: fill the array.
Step 8: print the mirror image of the original array.
Step 9: End

import java.util.*; //importing the package


public class que10
{
public static void main()
{
System.out.println("enter the no. of rows and columns");
Scanner sc = new Scanner(System.in); //object of scanner class
int M=sc.nextInt();
if(M<2||M>20){ //validating condition
System.out.println("invalid input");
System.exit(0);
}
UID: 7925885
24
int a[][]=new int[M][M];
System.out.println("enter "+(M*M)+" elements");
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
a[i][j]=sc.nextInt(); //input the matrix elements
System.out.println("original matrix:");
for(int i=0;i<M;i++){ //printing original matrix
for(int j=0;j<M;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
int b[][]=new int[M][M];
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
b[i][j]=a[i][M-j-1]; //storing elements for mirror image
System.out.println("mirror image matrix:");
for(int i=0;i<M;i++){ //printing mirror image of matrix
for(int j=0;j<M;j++)
System.out.print(b[i][j]+"\t");
System.out.println();
}
} VARIABLE DESCRIPTION
}
Int M Size of array

Int a[][] Array to store original numbers

Int i Outer loop control variable

Int j Inner loop control variable

Int b[][] To store the mirror image of the array

UID: 7925885
25
Q11) Write a program to accept a sentence which may be terminated by either '.' or '?' only.
The words are to be separated by a single blank space. Print an error message if the input
does not terminate with '.' or '?'. You can assume that no word in the sentence exceeds 15
characters, so that you get a proper formatted output.
Perform the following tasks:

(i) Convert the first letter of each word to uppercase.


(ii) Find the number of vowels and consonants in each word and display them with proper
headings along with the words.
Test your program with the following inputs.

INPUT : God is great.


OUTPUT :
God Is Great
Word Vowels Consonants
God 1 2
Is 1 1
Great 2 3

Step 1: Start
Step 2: enter a sentence.
Step 3: check the validation of the entered sentence.
Step 4: remove the punctuation at last.
Step 5: split the words of the sentence and store them into an array.
Step 6: make every first word of the sentence capital.
Step 7: print the new sentence.
Step 8: create an array for the count of vowels and consonants.
Step 9: find the number of vowels and consonants of each word.
Step 10: print each word along with the count of vowels and consonants.
Step 11: End

import java.util.*; //imported package


public class que11
{
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("enter a sentence in which all the words should have maximum 15
characters");
String s =sc.nextLine().trim();
if("!.".indexOf(s.charAt(s.length()-1))==-1)
{
System.out.println("invalid input");
System.exit(0);
}
else
s=s.substring(0,s.length()-1);
String sa[]=s.split(" "); //extracting words
UID: 7925885
26
for(int i=0;i<sa.length;i++)
if(Character.isLowerCase(sa[i].charAt(0))) //conversion of small letter to capital
sa[i]=(char)(sa[i].charAt(0)-32)+ sa[i].substring(1);
for(int i=0;i<sa.length;i++) //printing original sentence
System.out.print(sa[i]+" ");
System.out.println();
System.out.println("Word \t\t Vowel \t\t Consonants");
int v[] = new int[sa.length];
int c[] = new int[sa.length];
for(int i=0;i<sa.length;i++)
{
int t=0,t1=0;
for(int j=0;j<sa[i].length();j++)
if("AEIOUaeiou".indexOf(sa[i].charAt(j))!=-1) //finding the number of vowels and
consonants
t++;
else
t1++;
v[i]=t;c[i]=t1; //giving respective values
}
for(int i=0;i<sa.length;i++)
System.out.println(sa[i]+"\t\t"+v[i]+"\t\t"+c[i]); //printing statement }

} VARIABLE DESCRIPTION

String s Original string

String sa[] Array to store words

Int i,j Outer loop control variable

Int v[] To store vowels

Int c[] To store consonants

Int t1 To store the number of consonants in each word

Int t To store the number of vowels in each word

UID: 7925885
27
Q12) A circular prime is a prime number with the property that the number generated at
each intermediate Step when cyclically permuting its digits will be prime. For example, 1193
is a circular prime, since 1931, 9311 and 3119 all are also prime.

Step 1: Start
Step 2: enter a number.
Step 3: create a static and returning prime function.
Step 4: check if the entered number is prime or not.
Step 5: convert the number to the string.
Step 6: run a loop from 0 to the length of the number.
Step 7: check if the number formed by placing the first digit at last is prime or not.
Step 8: if the number is not prime print that number is not Circular prime and exit the program.
Step 9: as the loop ends print that the number is circular prime.
Step 10: End

import java.util.*; //importing package of scanner class


public class que12
{
static boolean prime(int n) //function to check prime
{
for(int i=2;i<=n/2;i++)
if(n%i==0)
return false;
return true;
}
public static void main()
{
Scanner sc= new Scanner(System.in); //object of scanner class
System.out.println("enter a number");
int n=sc.nextInt();
if(prime(n)==false) //validating condition
{
System.out.println("not circular prime");
System.exit(0);
}
String s=n+""; //number to string conversion
for(int i=1;i<(n+"").length();i++)
{
if(prime(Integer.parseInt(s.substring(1)+s.charAt(0)))) //condition for circular prime
numbers
s=s.substring(1)+s.charAt(0); //making circular numbers
else
{
System.out.println(n+" is not a circular prime number"); //printing statement
System.exit(0);
UID: 7925885
28
}
}
System.out.println(n+" is a circular prime number"); //printing statement
}
}

VARIABLE DESCRIPTION

Int n Store original number

Int i Loop control variable

String s String form of the number

UID: 7925885
29
Q13) Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of
rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and
less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to
input digits (0 - 7) only at each location, such that each row represents an octal number.

INPUT:
M=3
N=4
ENTER ELEMENTS FOR ROW 1: 1 1 3 7
ENTER ELEMENTS FOR ROW 2: 2 1 0 6
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
OUTPUT:
FILLED MATRIX DECIMAL EQUIVALENT

1 1 3 7 607

2 1 0 6 1094

0 2 4 5 165

Step 1: Start
Step 2: enter the number of rows and columns.
Step 3: check the validation of the number of rows and columns.
Step 4: create a double dimension array of the entered size by the user.
Step 5: create a new string array to keep the values of the entered number in string form to keep the leading 0’s.
Step 8: check the validation of the numbers entered.
Step 9: keep the count of leading 0’s so that matrix can be filled in proper manner.
Step 10: fill each matrix with the digits entered.
Step 11: print the matrix along with the decimal equivalent of each number.
Step 12: End

import java.util.*; //imported package


public class que13
{
static int convert(int n) //conversion of octal to decimal
{
int num=0;
String s = n+"";
for(int i = 0;i<=s.length()-1;i++)
num+= (int)Integer.parseInt((s.charAt(i)+""))*Math.pow(8,s.length()-i-1);
return num; //returning the decimal number
}
public static void main()
{

UID: 7925885
30
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter the values of M and N");
int M=sc.nextInt();
int N=sc.nextInt();
if(M>0&&M<10&&N>2&&N<6) //validating statements
{
int a[][]=new int[M][N];
for(int i=0;i<M;i++)
{
System.out.print("Enter elements for row "+(i+1)+" ");
for(int j=0;j<N;j++)
a[i][j]=sc.nextInt();
for(int j=0;j<N;j++)
if(a[i][j]>7)
{
System.out.println("Invalid input");
System.exit(0);
}
System.out.println();
}
System.out.println("FILLED MATRIX \t DECIMAL EQUIVALENT");
for(int i=0;i<M;i++) //printing the matrix in required form
{
String s="";
for(int j=0;j<N;j++)
{
System.out.print(a[i][j]);
s+=a[i][j];
}
System.out.print("\t\t"+convert(Integer.parseInt(s)));
System.out.println();
}
}
else
System.out.println("Out of range");
}
}

UID: 7925885
31
VARIABLE DESCRIPTION

Int n To store the number to find it’s decimal equivalent

Int M To store the breadth of array

Int N To store the length of array

Int a[][] To store the number

String a1[] To store each number to prevent the loss of leading 0’s

Int i,j loop control variable

UID: 7925885
32
Q14) Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
only. The words are to be separated by a single blank space and are in UPPER CASE.
Perform the following tasks:
Check for the validity of the accepted sentence only for the terminating character.
Arrange the words in ascending order of their length. If two or more words have the same
length, then sort them alphabetically.
Display the original sentence along with the converted sentence.
Test your program for the following data and some random data:

Example 1:
INPUT:
AS YOU SOW SO SHALL YOU REAP.
OUTPUT:
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL

Step 1: Start
Step 2: accept a sentence from the user.
Step 3: check if the sentence ends with .?! only or not.
Step 4: remove the punctuation at last.
Step 5: split the sentence of each word by the spaces present between them.
Step 6: use bubble sort technique to re arrange the words.
Step 7: swap the two words on the basis of the length of each word.
Step 8: arrange the words of equal length alphabetically.
Step 9: print the rearranged array.
Step 10 : End

import java.util.*; //imported package


public class que14
{
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("enter a sentence ending with .?! only in UPPERCASE");
String s =sc.nextLine().trim().toUpperCase(); //input sentence and converting into
upper case
if(".?!".indexOf(s.charAt(s.length()-1))!=-1) //validating condition
{
System.out.println(s);
s=s.substring(0,s.length()-1); //removing last punctuation
String sa[]=s.split(" "); //extraction of words
for(int i=0;i<sa.length;i++) //bubble sort technique to sort the array
for(int j=0;j<sa.length-i-1;j++)
{
if(sa[j].length()>sa[j+1].length())
{
String t=sa[j];

UID: 7925885
33
sa[j]=sa[j+1];
sa[j+1]=t;
}
if(sa[j].compareTo(sa[j+1])>0 && sa[j].length()==sa[j+1].length())
{
String t=sa[j];
sa[j]=sa[j+1];
sa[j+1]=t;
}
}
for(int i=0;i<sa.length;i++)
System.out.print(sa[i]+" "); //printing sorted array
}
else
System.out.println("Invalid input");
}
}
VARIABLE DESCRIPTION

String s To store original sentence

String sa[] To store the words

Int i Outer loop control variable

Int j Inner loop control variable

String t Temporary variable used to swap

UID: 7925885
34
Q15) Write a program to declare a square matrix A[][] of order MxM where ‘M’ is the number
of rows and the number of columns, such that M must be greater than 2 and less than 10.
Accept the value of M as the user input. Allow the user to input integers into this matrix.
Perform the following tasks:
Display the original matrix.
Rotate the matrix 90 degrees clockwise.
Find the sum of the elements of the four corners of the matrix. Test your program with
sample data and random data.

Step 1: Start
Step 2: input the number of rows and columns of the square matrix.
Step 3: check the validation of the input by the user.
Step 4: create an array of size entered by the user.
Step 5: input the values for the matrix.
Step 6: print the original array.
Step 7: print the matrix such that the array is rotated clockwise 90 degrees.
Step 8: print the sum of the corner element of the matrix.
Step 9: End

import java.util.*; //importing the package for scanner class


public class que15
{
public static void main()
{
Scanner sc=new Scanner(System.in); //creating object for scanner class
System.out.println("enter the value of no. of rows and columns of SQUARE matrix.");
int M=sc.nextInt();
if(M>2&&M<10)
{
int a[][]=new int[M][M];
for(int i=0;i<M;i++) //loops to enter array
for(int j=0;j<M;j++)
a[i][j]=sc.nextInt();
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++) //printing the elements of array
{
for(int j=0;j<M;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println("ROTATED MATRIX:");
for(int i=0;i<M;i++) //printing new array
{
for(int j=0;j<M;j++)
System.out.print(a[M-j-1][i]+"\t");
System.out.println();
UID: 7925885
35
}
System.out.println("Sum of corner elements is:
"+(a[0][0]+a[0][M-1]+a[M-1][M-1]+a[M-1][0])); //printing the sum of corner elements
}
else
System.out.println("Invalid Input");
}
}
VARIABLE DESCRIPTION

Int M To store the size of matrix

Int a[][] To store the matrix

Int i Outer loop control variable

Int j Inner loop control variable

UID: 7925885
36
Q16) A prime palindrome integer is a positive integer (without leading zeros) which is prime
as well as a palindrome. Given two positive integers m and n, where m < n, write a program
to determine how many prime-palindrome integers are there in the range between m and n
(both inclusive) and output them.
The input contains two positive integers m and n where m < 3000 and n < 3000. Display the
number of prime palindrome integers in the specified range along with their values in the
format specified below:
Test your program with the sample data and some random data:

Example 1
INPUT: M = 100
N = 1000
OUTPUT: The prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15

Example 2
INPUT: M = 100
N = 5000
OUTPUT: Out of range
Step 1: Start
Step 2: Import the util package for Scanner class
Step 3: Make a static boolean prime function to check if the number is prime or not
Step 4: Return false if the number is 1.
Step 5: If any number between 2 and num/2 divides then the number is not prime
Step 6: Return appropriate outputs
Step 7: Make a static boolean palindrome function.
Step 8: Return appropriate outputs according to the conditions
Step 9: Make an object of Scanner class and enter the range
Step 10: Check the validity of the range
Step 11: Run a loop to traverse the range and print the prime palindrome numbers.
Step 12: Print the frequency of the occurrence of prime palindrome numbers.
Step 13: End

import java.util.*;
public class que16
{
static boolean prime(int n) //A function to check prime
{
if(n==1)
return false;
for(int i = 2;i<=n/2;i++)
if(n%i==0)
return false; //returning statements
return true;
}
static boolean palindrome(int n) //function to check palindrome number
{
int rev=0,c=n; //storing the reverse and copy of original number
UID: 7925885
37
while(c!=0) //reverse the number
{
rev = rev*10+(c%10);
c/=10;
}
if(n==rev) //returning statements
return true;
else
return false;
}
public static void main() //main function to call other functions
{
Scanner sc= new Scanner(System.in); //Scanner’s object
System.out.println("Enter M and N:"); //input the range
int M =sc.nextInt();
int N =sc.nextInt();
if(M>3000 || N>3000) //invalidating conditions
System.out.println("OUT OF RANGE");
else
{
System.out.println("the prime palindrome integers are: ");
int c=0; //counting variable
for(int i = M;i<=N;i++) //finding prime palindrome numbers between the range
if(prime(i) && palindrome(i))
{
System.out.print(i+",");
c++;
}
System.out.println("\nFrequency of prime palindrome integers are: "+c); //printing
frequency
}
}
} VARIABLE DESCRIPTION

Int M Staring number (Range)

int N Ending number (Range)

Int i Outer loop control variable

Int c Counter variable

UID: 7925885
38
Q17) Write a program to accept a sentence as input. The words in the string are to be
separated by a blank. Each word must be in upper case. The sentence is terminated by
either “.”,”!” or “?”. Perform the following tasks:
(i) Obtain the length of the sentence. (measured in words)
(ii) Arrange the sentence in alphabetical order of the words.
Test your program with the sample data and some random data:

Example 1:
INPUT: NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
Length: 6
Rearranged Sentence:
INVENTION IS MOTHER NECESSITY OF THE

Example 2:
INPUT: BE GOOD TO OTHERS.
OUTPUT:
Length: 4
Rearranged Sentence: BE GOOD OTHERS TO
Step 1: Start
Step 2: Importing the util package
Step 3: Creating a class and main function
Step 4: Creating an object of scanner class
Step 5: Input the sentence with appropriate message.
Step 6: checking the valid condition.
Step 7: remove the punctuation from the sentence.
Step 8: create an array of words separated by space.
Step 9: sort the array using Bubble sort technique.
Step 10: print the new sentence.
Step 11:End

import java.util.*; //importing the util package


public class que17
{
public static void main() //main function
{
Scanner sc= new Scanner(System.in); //object of scanner class
System.out.println("Enter a sentence in uppercase only");
String s = sc.nextLine().toUpperCase(); //converting into upper case
if(".!?".indexOf(s.charAt(s.length()-1))==-1) //invalidating condition
System.out.println("INVALID SENTENCE");
else
{
s=s.substring(0,s.length()-1); //remove the punctuation
String sa[] = s.split(" "); //split the string using space
for(int i=0;i<sa.length;i++) //bubble sort technique to sort the array
for(int j = 0;j<sa.length-i-1;j++)
if(sa[j].compareTo(sa[j+1])>0)
UID: 7925885
39
{
String t= sa[j];
sa[j]=sa[j+1];
sa[j+1]=t;
}
System.out.println("Length: "+sa.length); //length of the array
System.out.println("Rearranged sentence: ");
for(int i =0;i<sa.length;i++) //printing new sentence
System.out.print(sa[i]+" ");
}
}
}

VARIABLE DESCRIPTION

String s Input sentence from the user

String sa[] Array of word separated by space

Int i Outer loop control variable

int j inner loop control variable

String t temporary variable

UID: 7925885
40
Q18) A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two
pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or
‘d-1’ digits. If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar
number. The first few Kaprekar numbers are: 9, 45, 297 ……..
Example 1:
9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number.
Example 2:
45
452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number.

Given the two positive integers p and q, where p < q, write a program to determine how
many Kaprekar numbers are there in the range between p and q (both inclusive) and output
them.
The input contains two positive integers p and q. Assume p < 5000 and q < 5000. You are to
output the number of Kaprekar numbers in the specified range along with their values in
the format specified below:

SAMPLE DATA:
INPUT:
p=1
q = 1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:-
1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8
Step 1: Start
Step 2: make a static boolean function to check if the number is kaprekar or not
Step 3: find out the square of the number
Step 4: extract the parts of the squared number
Step 5: check if the sum of the parts are equal to the number or not
Step 6: Obtain the range from the user
Step 7: check for the validation
Step 8: count the frequency of the numbers
Step 9: print the numbers in order
Step 10: End

import java.util.*; //import the util package


public class que18
{
static boolean kaprekar(int n) //function to check a kaprekar number
{
int s = n*n; //square of the number
int r = s%(int)Math.pow(10,(n+"").length()); //extracting the number for checking
int l = s/(int)Math.pow(10,(n+"").length()); //extracting the number for checking
if(l+r==n) //returning the value according to function
return true;
else
UID: 7925885
41
return false;
}
public static void main() //main function to evaluate
{
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("Enter 2 numbers p<q "); //enting the range
int p= sc.nextInt();
int q = sc.nextInt();
if(q>p) //invalid condition
System.out.println("Invalid input");
else
{
System.out.println("Kaprekar numbers are: ");
int c=0; //frequency of the numbers
for(int i =p;i<=q;i++) //loop running through the range
if(kaprekar(i)) //condition to evaluate
{
System.out.print(i+",");
c++;
}
System.out.println("\nFrequency of kaprekar number is: "+c); //printing the
frequency
}
} VARIABLE DESCRIPTION
}
int n parameter of the function

int s square of the number

Int r, l parts of the squared number

int q,p range input from the user

int c count of the kaprekar numbers

UID: 7925885
42
Q19) A composite Magic number is a positive integer which is composite as well as a
magic number.
Composite number: A composite number is a number which has more than two factors. For
example: Factors of 10 are: 1,2,5,10
Magic number: A Magic number is a number in which the eventual sum of the digit d is
equal to 1. For example: 28=2+8=10==>1+0=1
Accept two positive integers m and n, where m is less than n as user input. Display the
number of composite magic integers that are in the range between m and n (both inclusive)
and output them along with frequency of magic numbers in that range.

Step 1: Start
Step 2: make a boolean static composite function to check if the number is composite
Step 3: make a function to calculate the sum of the number
Step 4: run a while loop till number becomes 0
Step 5: get the value of sum of digits
Step 6: make a isMagic() function to check if the number is magic or not
Step 7: run a while while the number is one digit
Step 8: if the one digit number is one return true
Step 9: enter the range from the user in main function
Step 10: run a loop between the ranger and check teacher number
Step 11: along with that count the frequency also
Step 12: print the relevant details
Step 13: End

import java.util.*; //importing the util package


public class que19
{
static boolean composite(int n) //to check if the number is composite
{
for(int i=2;i<=n/2;i++) //return value according to the condition
if(n%i==0)
return true;
return false;
}
static int sod(int n) //evaluating the sum of digits
{
int s=0;
while(n!=0)
{
s+=n%10;
n/=10;
}
return s; //returning the sum of digits
}
static boolean ismagic(int n) //checking if the number in argument is magic
{
while(n>=9)
UID: 7925885
43
n=sod(n);
if(n==1)
return true;
else
return false;
}
public static void main() //main function to evaluate
{
Scanner sc=new Scanner(System.in); //object of Scanner class
System.out.println("enter the range such that m<n"); //input the range
int m=sc.nextInt();
int n=sc.nextInt();
if(m>=n) //validating condition
System.out.println("Invalid input");
else
{
int f=0; //frequency of the numbers
System.out.println("The composite magic number are:");
for(int i=m;i<=n;i++) //loop to run through the range
if(ismagic(i)&&composite(i))
{
System.out.print(i+" ");f++; //printing and updating the counter
}
System.out.println("\nFrequency: "+f); //printing the frequency
}
}} VARIABLE DESCRIPTION

int n parameter of the function

int i loop control variable

Int s sum of digits in the function

int m,n range input from the user

int f count of the magic composite numbers

UID: 7925885
44
Q20) An ISBN ( International Standard Book Number) is a ten digit code which uniquely
identifies a book. The first nine digits represent the Group, Publisher and Title of the book
and the last digit is used to check whether ISBN is correct or not.

Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is
necessary to make the last digit equal to ten; this is done by writing the last digit of the
code as X. To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit,
plus 8 times the third and so on until we add 1 time the last digit. If the final number leaves
no remainder when divided by 11, the code is a valid ISBN.Design a program to accept a
ten digit code from the user. For an invalid input, display an appropriate message. Verify
the code for its validity in the format specified below. Test your program with sample data
and some random data.

Example 1
INPUT CODE : 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER - VALID ISBN CODE

Example 2
INPUT CODE : 035680324
OUTPUT : INVALID INPUT

Example 3
INPUT CODE : 0231428031
OUTPUT : SUM = 122
LEAVES REMAINDER - INVALID ISBN CODE

Step 1: Start
Step 2: make a static int function to calculate the sum according to ISBN code
Step 3: calculate the sum according to index of ISBN number
Step 4: check for the presence of ‘X’ in the ISBN code
Step 5: add respective digits
Step 6: return the value obtained
Step 7: take input from the user
Step 8: check for valid length
Step 9: calculate the sum of ISBN number entered
Step 10: print relevant details
Step 11: End
import java.util.*; //import the package
public class que20
{
static int sum(String n) //evaluating the sum of ISBN
{
int s=0;
for(int i=0;i<n.length()-1;i++) //calculation of sum of all digits
s+= Integer.parseInt(n.charAt(i)+"")*(10-i); //storing the sum
if(n.charAt(n.length()-1)=='x'||n.charAt(n.length()-1)=='X') //checking for the
presence if 'X'
UID: 7925885
45
s+= 10;
else
s+= Integer.parseInt(n.charAt(n.length()-1)+"");
return s; //returning statements
}
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter an ISBN code: "); //input from user
String n =sc.next();
if(n.length()!=10) //not of valid length
System.out.println("Invalid Input");
else
{
System.out.println("Sum: "+sum(n)); //sum of all digits according to ISBN
if(sum(n)%11==0) //checking for valid ISBN code and printing relevant details
System.out.println("valid ISBN code");
else
System.out.println("invalid ISBN code");
}}}

VARIABLE DESCRIPTION

String n parameter of the function

int s sum of the number according to ISBN

Int n input number from the user

int i Loop control variable

UID: 7925885
46
Q21) Write a program to input a natural number less than 1000 and display it in words. Test
your program on the sample data and some random data.
Step 1: Start
Step 2: import the util package for Scanner class
Step 3: create the main function
Step 4: Create the object of Scanner class
Step 5: check the validity of the number
Step 6: print “zero” if the number is zero
Step 7: create two arrays which consist of the number names from 1- 19 and multiples of ten from 20
Step 8: check for the number of digits of the number then concatenate the number names to string
Step 9: print the final string
Step 10: End
import java.util.*; //importing the util class for Scanner
public class que21
{
public static void main() //main function for the execution of program
{
Scanner sc= new Scanner(System.in); //object of Scanner class
System.out.println("Enter a number less than"); //asking for input from user
int n=sc.nextInt(); //user input
if(n>=1000) //validating condition
System.out.println("Invalid Input");
else
{
if(n==0) //if the number is zero
System.out.println("Zero");
else //arrays for storing number names according to use
{
String
s1[]={"","one","two","three","four","five","six","seven","eight","nine","ten","eleven",
"twelve","thirteen", "fourteen","fifteen" ,"sixteen", "seventeen"
,"eighteen","nineteen"};
String
s2[]={"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
String num=n+"";String s="";
if(num.length()==3) //if the number is 3 digit
{
s+= s1[Integer.parseInt(num.charAt(0)+"")]+" hundred "; //storing for the value
of 100's
num =num.substring(1); //making the number shorter
if(Integer.parseInt(num)<=19) //for 10's number name
s+=s1[Integer.parseInt(num)];
else

UID: 7925885
47
s+= s2[Integer.parseInt(num.charAt(0)+"")]+"
"+s1[Integer.parseInt(num.charAt(1)+"")];
}
else if(num.length()==2) //if the number is 2 digit
{
if(Integer.parseInt(num)<=19) //extracting the word for less than 19 from
array
s+=s1[Integer.parseInt(num)];
else //otherwise from other array for names
s+= s2[Integer.parseInt(num.charAt(0)+"")]+"
"+s1[Integer.parseInt(num.charAt(1)+"")];
}
else
s+= s1[Integer.parseInt(num.charAt(0)+"")];
System.out.println(s); //printing the final number name
}
}
}
} VARIABLE DESCRIPTION

int n input number from the user

String s1[] Consist of the number names from 1 to 19

String s2[] Consist of the number names from 20 to 90


(multiples of 10 only)

String num store the string for of original number

String s Stores the number name

UID: 7925885
48
Q22) Encryption is a technique of coding messages to maintain their secrecy. A String
array of size 'n' where 'n' is greater than 1 and less than 10,stores single sentences (each
sentence ends with a full stop) in each row of the array. Write a program to accept the size
of the array. Display an appropriate message if the size is not satisfying the given
condition. Define a string array of the inputted size and fill it with sentences row-wise.
Change the sentence of the odd rows with an encryption of two characters ahead of the
original character. Also change the sentence of the even rows by storing the sentence in
reverse order. Display the encrypted sentences as per the sample data given below.
Example :

INPUT: n = 4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: make a static returning function to rotate the string passed as parameter
Step 4: run a for loop throughout the length of the string
Step 5: check for the special condition which arises when two is added to ‘y’ or ‘z’
Step 6: return the new string obtained
Step 7: make a static returning function to return the reversed string
Step 8: split the sentence and run the loop in reverse
Step 9: create the object of scanner class
Step 10: check for the validation of the user input
Step 11: take input from the user check its validation and remove the punctuation if present
Step 12: obtain the encoded string by passing it to the required function
Step 13: print the new array
Step 14: End

import java.util.*; //importing util package for Scanner class


public class que22
{
static String rotate(String s) //for rotating the string
{
String s1=""; //rotated string
for(int i=0;i<s.length();i++) //loop for traversing the string
{

UID: 7925885
49
if(Character.isLetter((char)(s.charAt(i)+2))) //if the character ahead is letter
s1+=(char)(s.charAt(i)+2);
else //if the character at +2 is not letter then rotating the string
s1+=(char)(s.charAt(i)-24);
}
return s1; //returning the final string
}
static String rev(String s) //reversing the string
{
String s1=""; //storing new string
String sa[]=s.split(" "); //array of words
for(int i=sa.length-1;i>=0;i--) //reversing the words
s1+=" "+sa[i];
return s1.trim(); //returning the string with no leading or trial spaces
}
public static void main()
{
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("Enter array size"); //asking for input form user
int x=sc.nextInt(); //user input
if(x<=1||x>=10) //for invalid condition
System.out.println("INVALID INPUT");
else
{
String s[]=new String[x]; //array to store sentences
System.out.println("Enter sentences");
String e=sc.nextLine().toUpperCase(); //user input due to use of scanner
for(int i=0;i<x;i++)
{
s[i]=sc.nextLine().toUpperCase(); //converting to uppercase
if("?!.".indexOf(s[i].charAt(s[i].length()-1))==-1) //validating condition
{
System.out.println("Invalid Input");
System.exit(0); //terminating the program
}
else
s[i]=s[i].substring(0,s[i].length()-1);
}
for(int i=0;i<s.length;i++) //for alternate encoding of strings
UID: 7925885
50
if(i%2==1)
s[i]=rev(s[i]);
else
s[i]=rotate(s[i]);
System.out.println("Encoded sentences:"); //printing the sentences
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
}
}

VARIABLE DESCRIPTION

String s Parameter passed to the function

String s1 Create a new string to return it in the function

int x user input

String s[] Array to store the sentences

int i loop control variable

UID: 7925885
51
Q23) A bank intends to design a program to display the denomination of an input amount
upto 5 digits. The available denominations with the bank are of rupees
1000,500,100,50,20,10,5,2 and 1. Design a program to accept the amount from the user and
display the break-up in descending order of denominations. (i,e preference should be given
to the highest denomination available) along with the total number of notes. [Note: only the
denomination used should be displayed].
Also print the amount in words according to the digits.

Example 1
INPUT: 14836
OUTPUT: ONE FOUR EIGHT THREE SIX
DENOMINATION:
1000 X 14 =14000
500 X 1 =500
100 X 3 =300
50 X 1 =50
5 X 1 =5
1 X 1 =1

Example 2
EXAMPLE 2:
INPUT: 235001
OUTPUT: INVALID AMOUNT

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: Enter the amount from the user
Step 5: Check its validity
Step 6: Create an array for number names and a variable to store number name of each digit
Step 7: run a loop across the length of the array consist of notes
Step 8: calculate the number of notes required
Step 9: print the information in the required format
Step 10: End

import java.util.*; //importing the util class


public class que23
{
public static void main()
{
Scanner sc=new Scanner(System.in); //creating the object of scanner
System.out.println("Enter Amount"); //user input
int n=sc.nextInt();
if((n+"").length()<=5) //validating condition
{
UID: 7925885
52
String
w[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"};
//number names
String num=n+""; //number to string
for(int i=0;i<num.length();i++)
System.out.print(w[Integer.parseInt(num.charAt(i)+"")]+" ");
int num1[]={1000,500,100,50,20,10,5,2,1};
System.out.println();
int a=n; //copy of original number
for(int i=0;i<9;i++) //loop to move through array
if(a/num1[i]>0) //if amount greater than note value
{
int x=a/num1[i]; //number of notes of a denomination
System.out.println(num1[i]+"*"+x+"="+(num1[i]*x)); //printing
a%=num1[i]; //amount changed
}
}
else
System.out.println("Invalid Input"); //print statement for invalid condition
}
} VARIABLE DESCRIPTION

int n input from the user

String w[] Number names of all digits from 0 to 9

String num String format of the number

int num1[] to store the notes availables

int i loop control variable

UID: 7925885
53
Q24) A smith number is a composite number, the sum of whose digits is the sum of the
digits of its prime factors obtained as a result of prime factorization (excluding 1). The first
few such numbers are 4, 22, 27, 58, 85, 94, 121.....
Example : 666 Prime factors are 2, 3, 3 and 37
Sum of the digits are (6+6+6) = 18
Sum of the digits of the factors 2+3+3+(3+7) = 18

Sample data:
Input 94
Output SMITH Number

Input 102
Output NOT SMITH Number

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: make a static boolean prime function to check if the number is prime or not
Step 4: create a function to calculate the sum of digits
Step 5: run a loop while the number does not becomes 0 and store each digit in separate variable
Step 6: return the sum of digits
Step 7: create the object of Scanner class and take input from user
Step 8: run a loop from 2 to the number
Step 9: store the sum of digit of every prime factor
Step 10: check if the sum of digit of the number is equal to the sum of factors or not
Step 11: print relevant details
Step 12: End

import java.util.*; //importing util class for scanner


public class que24
{
static boolean prime(int a) //for checking prime
{
for(int i=2;i<=a/2;i++) //loop for checking prime
if(a%i==0)
return false; //returning statements
return true;
}
static int sod(int a) //sum of digits
{
int s=0; //storing the sum of digits
while(a!=0) //extractingof each digit
{
s+=a%10;
a/=10; //updation
}
UID: 7925885
54
return s; //returning the sum
}
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter a number");
int n=sc.nextInt(); //input number from user
int sf=0; //sum of factors
for(int i = 2; i<= n; i++){
if(prime(i)){
int x = n;
while(x%i==0){ //sum of factors along with sum of digits
sf+=sod(i);
x /= i; //updation of loop variable
}
}
}
if(sf == sod(n)) //main condition for smith number
System.out.println("Smith number");
else
System.out.println("Not Smith number");
}
}

VARIABLE DESCRIPTION

int a parameter for the function

int n input number from the user

int sf stores the sum of factors

int x copy of original number

int i loop control variable

UID: 7925885
55
Q25) A unique-digit integer is a positive integer (without leading zeros) with no duplicate
digits. For example 7, 135, 214 are all unique-digit integers whereas 33, 3121, 300 are not.
Given two positive integers m and n, where m<30000 and n<30000. You are to output the
number of unique-digit integers in the specified range along with their values in the format
specified below:
SAMPLE DATA:

INPUT: m = 100 n = 120


OUTPUT: THE UNIQUE- DIGIT INTEGERS ARE : 102, 103, 104, 105, 106, 107, 108, 109, 120.
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: make a static returning function to check if the number in parameter is unique or not
Step 4: convert the number to string
Step 5: rum a loop to check every character
Step 6: check if the first index and the last index of the character is same or not
Step 7: Create an object of Scanner class
Step 8: enter the range from user and check for validation
Step 9: run a loop through the range and check if the number is unique or not
Step 10: increase the counter by one every time we come across unique number
Step 11: print the relevant details
Step 12: End

import java.util.*; //importing scanner class


public class que25
{
static boolean unique(int n) //function to check for unique
{
String s=n+""; //converting number to string
for(int i=0;i<s.length();i++)
if(s.indexOf((char)(i+48))!=s.lastIndexOf((char)(i+48))) //checking if all digits are
unique
return false;
return true;
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 numbers where the first one is smaller than the second");
int m=sc.nextInt();
int n=sc.nextInt(); //user input
int c=0; //counter
if((m>=n)||(m>=30000)||(n>=30000)) //for invalid conditions
UID: 7925885
56
System.out.println("INVALID INPUT");
else
{
System.out.print("The unique-digit numbers are :"); //printing according to format
for(int i=m;i<=n;i++) //loop to move from m to n
if(unique(i)) //checking condition
{
c++;
System.out.print(i+", ");
}
System.out.println(); //format of question
System.out.println("Frequency of unique-digit numbers are : "+c); //printing
frequency
}
}
}

VARIABLE DESCRIPTION

int n parameter for the function

String s parameter into String

int m,n Range input by the user (m<=n)

int c counter variable

int i loop control variable

UID: 7925885
57
Q26) Caesar Cipher is an encryption technique which is implemented as ROT13 (rotate by
13 places). It is a simple letter substitution cipher that replaces a letter with the letter 13
places after it in the alphabets, with the other characters remaining unchanged.
A/a B/b C/c D/d E/e F/f G/g H/h I/i J/j K/k L/l M/m
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/n O/o P/p Q/q R/r S/s T/t U/u V/v W/w X/x Y/y Z/z
Write a program to accept a plain text of length L, where L must be greater than 3 and less
than 100.
Encrypt the text if valid as per the Caesar Cipher.
Test your program with the sample data and some random data.

Example 1
INPUT:
Hello! How are you?
OUTPUT:
The cipher text is:
Uryyb! Ubj ner lbh?

Example 2
INPUT:
Encryption helps to secure data.
OUTPUT:
The cipher text is:
Rapelcgvba urycf gb frpher qngn.

Example 3
INPUT:
You
OUTPUT:
INVALID LENGTH

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: Enter the sentence from the user and check the validity
Step 5: run a loop for each character of the string
Step 6: stores the letter according to the cases and rotation
Step 7: at last add the special characters if present
Step 8: print the relevant details
Step 9: End

import java.util.*; //importing Scanner class


class que26{
public static void main()
{
Scanner sc = new Scanner(System.in); //object of Scanner created
System.out.println("Enter text");
String s=sc.nextLine(); //user input //length of entered string
if(s.length()>3&&s.length()<100) //checking for valid text
UID: 7925885
58
{
String ns=""; //stores cipher text
for(int i=0;i<s.length();i++) //traversing each character
{
if(Character.isLowerCase(s.charAt(i))) //if the character is lower Case
if(Character.isLetter(s.charAt(i)+13)) //if the char+13 is letter
ns+=(char)(s.charAt(i)+13);
else //if char-13 is letter
ns+=(char)(s.charAt(i)-13);
else if(Character.isUpperCase(s.charAt(i))) //if the character is Upper Case
if(Character.isLetter(s.charAt(i)+13)&& Character.isUpperCase(s.charAt(i)+13))
ns+=(char)(s.charAt(i)+13); //UpPer case again check because Z+13 cans
give small letters
else
ns+=(char)(s.charAt(i)-13);
else
ns+=s.charAt(i); //special characters
}
System.out.println("The ciphertext is :");
System.out.println(ns); //printing cipher text
}
else //for invalid text length
System.out.println("INVALID LENGTH");
}
} VARIABLE DESCRIPTION

String s Sentence from the user

String ns New string after the code is made

int i loop control variable

UID: 7925885
59
Q27. Write a program that inputs the names of people into two different arrays, A and B.
Array A has N number of names while Array B has M number of names, with no duplicates
in either of them. Merge array A and B into a single array C, such that the resulting array is
sorted alphabetically. Display all the three arrays, A, B and C, sorted alphabetically. Test
your program for the given data and some random data.
SAMPLE DATA:

INPUT
Enter number of names in Array A, N=2
Enter number of names in Array B, M=3
First array:(A) Suman Anil
Second array:(B) Usha Sachin John

OUTPUT
Sorted Merged array:(C)
Anil John Sachin Suman Usha
Sorted first array:(A)
Anil Suman
Sorted Second array:(B)
John Sachin Usha

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: Enter the number of names and then create the array
Step 5: sort the names alphabetically in the first array
Step 6: Sort the names alphabetically in the second array also
Step 7: print all the arrays according to the syntax given in the question
Step 8: End

import java.util.*; //for Scanner class


public class que27
{
public static void main()
{
Scanner sc = new Scanner(System.in); //Scanner object creation
System.out.println("Enter number of names in first array" );
int n=sc.nextInt(); //input from user
System.out.println("Enter number of names in second array");
int m=sc.nextInt(); //user input of array sizes
String a[]=new String[n];
String b[]=new String[m]; //arrays created
System.out.println("Enter names of first array");
int f=0; //variable to move through merged array
String c[]=new String[m+n]; //merged array
UID: 7925885
60
String w=sc.nextLine(); //empty string
for(int i=0;i<n;i++)
{
a[i]=sc.nextLine(); //names input
c[f++]=a[i]; //stored in merged array
}
System.out.println("Enter names of second array");
for(int i=0;i<m;i++)
{
b[i]=sc.nextLine(); //names input
c[f++]=b[i]; //stored in merged array
}
System.out.println("Sorted Merged Array: ");
String t=""; //temporary variable
for(int i=0;i<(m+n);i++) //bubble sort technique to sort alphabetically
for(int j=0;j<(m+n)-i-1;j++)
if(c[j].compareTo(c[j+1])>0)
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
for(int i=0;i<(m+n);i++) //merged array printing
System.out.print(c[i]+" ");
System.out.println();
System.out.println("Sorted First Array: ");
for(int i=0;i<n;i++) //bubble sort technique to sort alphabetically
for(int j=0;j<n-i-1;j++)
if(a[j].compareTo(a[j+1])>0)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(int i=0;i<n;i++) //first array printing
System.out.print(a[i]+" ");
System.out.println();
System.out.println("Sorted Second Array: ");
for(int i=0;i<m;i++) //bubble sort technique to sort alphabetically
for(int j=0;j<m-i-1;j++)
if(b[j].compareTo(b[j+1])>0)
{
UID: 7925885
61
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
for(int i=0;i<m;i++) //second array printing
System.out.print(b[i]+" ");
System.out.println();
}
}

VARIABLE DESCRIPTION

int n,m user input size of the array

String a[],b[] Array entered by the user

String c[] Merged array to store sorted names

int f counter variable to move through the merged array

int i,j loop control variable

String t temporary variable used in sorting

UID: 7925885
62
Q28. A new advanced Operating System, incorporating the latest hi-tech features has been
designed by Opera Computer Systems. The task of generating copy protection codes to
prevent software piracy has been entrusted to the Security Department. The Security
Department has decided to have codes containing a jumbled combination of alternate
uppercase letters of the alphabet starting from A upto K (namely among A,C,E,G,I,K). The
code may or may not be in the consecutive series of alphabets. Each code should not
exceed 6 characters and there should be no repetition of characters. If it exceeds 6
characters, display an appropriate error message. Write a program to input a code and its
length. At the first instance of an error display "Invalid" stating the appropriate reason. In
case of no error, display the message "Valid".
Test your program for the following data and some random data.

SAMPLE DATA
INPUT N=4 ABCE
OUTPUT Invalid! Only alternate letters permitted!

INPUT N=4 AcIK


OUTPUT Invalid! Only upper case letters permitted!

INPUT N=4 AAKE


OUTPUT Invalid! Repetition of characters not permitted!

INPUT N=7
OUTPUT ERROR! Length of string should not exceed 6 characters!

INPUT N=4 AEGIK


OUTPUT Invalid! String length not the same as specified!

INPUT N=3 ACE


OUTPUT Valid!

INPUT N=5 GEAIK


OUTPUT Valid!

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: ask for the length of the word from the user
Step 5: take a flag variable for conditions
Step 6: Enter the code from the user and match the length with entered length
Step 7: check all the valid conditions if it comes out to be false break the construct and loop
Step 8: if all the conditions are satisfied then initialise the variable to true
Step 9: print the relevant details
Step 10: End

import java.util.*; //import for Scanner class


UID: 7925885
63
public class que28
{
public static void main()
{
Scanner sc = new Scanner(System.in); //object of scanner class
System.out.println("Enter the length of the code");
int len =sc.nextInt(); //input of length of code
boolean f=false; //flag variable for all conditions
if(len>6) //validating condition
System.out.println("Invalid input: length of code exceeds 6");
else
{
System.out.println("Enter the code");
String s=sc.next(); //asking for the code from user
if(s.length()!=len) //length does not matches with entered length
System.out.println("Invalid input: entered code length is not equal to length");
else
for(int i=0;i<len;i++)
{
if(s.indexOf(s.charAt(i))!=s.lastIndexOf(s.charAt(i))) //checks for letter
repetition
{
f=false;
System.out.println("Invalid repetition of characters not permitted.");
break;
}
else if("ACEGIK".indexOf(s.charAt(i))==-1) //checks for valid letter
{
f=false;
System.out.println("Invalid only alternate letters permitted.");
break;
}
else if(Character.isLowerCase(s.charAt(i))) //checks for lowercase letters
{
f=false;
System.out.println("Invalid! Only upper case letters permitted!");
break;
}
else //if no condition goes false for all characters
f=true;
}
if(f) // all conditions satisfied
UID: 7925885
64
System.out.println("Valid");
}
}
}

VARIABLE DESCRIPTION

int len length of the code entered by the user

boolean f Flag variable to check if all the conditions are true

String s code entered by the user

int i,j loop control variable

UID: 7925885
65
Q29) Write a program to accept a sentence which may be terminated by either '.', '?', or '!'
only. Any other character may be ignored. The words may be separated by more than one
blank space and are in UPPER CASE.
Perform the following tasks.
Accept the sentence and reduce all the extra blank space between two words to a single
blank space.
Accept a word from the user which is part of the sentence along with its position number
and delete the word and display the sentence.
Test your program with the sample data and some random data.
Example 1
INPUT: A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.
WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6

OUTPUT: A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.

Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4

OUTPUT: AS YOU SOW, SO YOU REAP.

Example 3
INPUT: STUDY WELL ##.
OUTPUT: INVALID OUTPUT.

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: input a sentence from the user
Step 5: check the validation of the sentence
Step 6: remove extra spaces by comparing the next character
Step 7: input the word and the index of the word to be deleted
Step 8: run a loop and skip the index entered by the user
Step 9: Print the relevant details
Step 10: End

import java.util.*; //importing for scanner class


public class que29
{
public static void main()
{
Scanner sc=new Scanner(System.in); //object of scanner class
System.out.println("Enter a sentence only in uppercase.");
UID: 7925885
66
String s=sc.nextLine().trim().toUpperCase(); //removing spaces and convert into
uppercase
if(".!?".indexOf(s.charAt(s.length()-1))!=-1) //validating condition
{
String t="";
for(int i=0;i<s.length()-1;i++) //removing extra space
if(s.charAt(i)!=' '||s.charAt(i)!=s.charAt(i+1))
t+=s.charAt(i);
t+=s.charAt(s.length()-1); //storing the last character
System.out.print("WORD TO BE DELETED IN UPPERCASE ONLY: ");
String w =sc.next().toUpperCase(); //user input
System.out.print("WORD POSITION IN THE SENTENCE: ");
int a=sc.nextInt(); //user input of index number
s="";
String sa[]=t.split(" "); //splitting the words
for(int i=0;i<sa.length;i++)
if(i!=a-1) //for removing the word from new string
s+=sa[i]+" ";
System.out.println("OUTPUT: "+s); //final output
}
else
System.out.println("Invalid Input.");
}

} VARIABLE DESCRIPTION

String s Sentence entered by the user

String t New string with extra spaces removed

String w Word to be deleted

int a Index of the word

String sa[] Array of all the word

int i Loop control variable

UID: 7925885
67
Q30) A positive
natural number,
(for e.g.27) can be

represented as follows:
2+3+4+5+6+7
8+9+10
13+14

where every row represents a combination of consecutive natural numbers, which add up
to 27.
Write a program which inputs a positive natural number N and prints the possible
consecutive number combinations, which when added give N.

Test your program for the following data and some random data
SAMPLE DATA
INPUT N = 15
OUTPUT 7 8
12345
456

Step 1: Start
Step 2: import the util package for Scanner class
Step 3: Create the object of Scanner class
Step 4: input a number from the user
Step 5: check the validation of the number
Step 6: run a loop from one to the number to get numbers which can give the sum
Step 7: run a nested loop to obtain consecutive numbers
Step 8: if the sum of the number exceeds the original number break the loop
Step 9: if the sum is equal to the number print the string of numbers
Step 10: End

import java.util.*; //Scanner input


public class que30
{ Scanner sc = new Scanner(System.in); //object of scanner creation
System.out.println("Enter a natural number.");
int n=sc.nextInt(); //user input
if(n<=0) //invalid condition
System.out.println("INVALID INPUT");
else
public static void main()
UID: 7925885
68
{

{
int s=0; //stores sum of numbers
String sp=""; //stores string of numbers adding up to n
for(int i=1;i<n;i++)
{
sp=""; //for adding integers to string
s=0; //consecutive numbers
for(int j=i;j<n;j++)
{
s+=j; //adding consecutive integers
sp=sp+j+" "; //adding integers in string
if(s>=n) //if sum exceeds of becomes same as number
break;
}
if(s==n) //if sum is equal to number
System.out.println(sp);
}
}
}
}

VARIABLE DESCRIPTION

int n number entered by the user

int s stored the sum of consecutive numbers

String sp String of numbers which can give the sum to n

int i,j Loop control variable

UID: 7925885
69
ACKNOWLEDGEMENT
My success in this Java project wouldn't be possible without a
chorus of support. First, a heartfelt thanks to Pooja Ma'am for
her clear explanations, challenging assignments, and
insightful feedback. It's thanks to her that I navigated the
intricacies of Java with growing confidence. To my friends, I
owe their valuable input and fresh perspectives that helped
me tackle coding hurdles from different angles, honing my
problem-solving skills. And lastly, to my parents, whose
unwavering support and belief in me fuelled my motivation
throughout. This achievement is as much theirs as it is mine.

UID: 7925885
70

You might also like