Pavanah Comp Pro
Pavanah Comp Pro
CLASS- XII-A
UID- 7925903
UID:7925903
ACKNOWLEDGEMENT
Thank you to everyone who played a part in making this project a reality.
UID:7925903
PROGRAMS
Ques 1-1. 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.
ALGORITHM
Step one: start
Step two: create a returning parameterised function reverse
Step three: extract the digits of the number and reverse the original number
Step four: create a static void main function
Step five: create the object of the class to call the functions
Step six: create the object of the scanner class and input the range
Step seven: find the minimum and maximum of the two number range inputted
by user
Step eight: declare a counter variable and frequency variable and flag variable
Step nine: run a loop from the minimum number to the maximum number
Step ten: run a loop to check if the number in the range is prime or not by
increasing the count of the factors from one till that number
UID:7925903
Step eleven: check if the square of reverse of original number is reverse of
square of the original number and the reversed original number and original
number is prime or not.
Step twelve: if the condition of step eleven evaluates to true then print the
particular number and increase the frequency.
Step thirteen: if no Adam prime numbers could be found within the range
specified print the suitable statement.
Step fourteen: end
PROGRAM
import java.util.*;
class primeadam
{
int reverse(int a)
{ int rev=0;
while(a!=0)
{
int d=a%10;
rev=rev*10+d;
a=a/10;
}
return rev;
}
UID:7925903
{
if(f==0)
{
f=1;
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
}
System.out.print(i+"\t");
f1++;
}
}
System.out.println("\n FREQUENCY IS:"+f1);
if(f==0)
System.out.println("No adam prime number could be found.");
VD TABLE
VARIABLE USE
a Formal parameter
d Digit extraction
f Used as a flag
f1 Frequency
c Counter
UID:7925903
UID:7925903
QUES 2-2. 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] a) Sort the
elements of the single dimensional array in ascending order using
any standard sorting technique and display the sorted elements. b)
Fill the square matrix b[][] in the following format. If the array a[]=
{5,2,1,8} the after sorting a[] ={1,2,5,8} Then the matrix b[][] would
fill as below:
1258
1251
1212
1125
ALGORITHM
Step one: start
Step two: create the void main class
Step three: create the object of the scanner class to input the variable array
Step four: enter the size of the array
Step five: check if the size is less than 2 or greater than 10, if so exit the main
function
Step six: declare a single dimensional array and a two dimensional array
Step seven: enter the values of the single dimensional array
Step eight: sort the single dimensional array using the bubble sort technique
Step nine: print the sorted array
Step ten: run a loop from i=n-1 till 0
Step eleven: declare the nested loop's j variable outside the loop
UID:7925903
Step twelve: run the loop from 0 to i
Step thirteen: transfer the elements from the single dimensional array to the
double dimensional array according the pattern asked
Step fourteen: print the new double dimensional array
Step fifteen: end
PROGRAM
import java.util.*;
class squaremat
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
int n=sc.nextInt();
if(n<2 && n>10)
{System.out.println("Invalid size");
System.exit(0);}
int a[]=new int[n];
int b[][]=new int[n][n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
{ for(int j=0;j<n-i-1;j++)
{ if(a[j]>a[j+1])
{int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;}
}
}
for(int i=0;i<n;i++)
System.out.print(a[i]+"\t");
System.out.println();
System.out.println("Matrix:");
int x=0;
for(int i=n-1;i>=0;i--)
{ 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++)
UID:7925903
b[i][k]=a[m++];
x++;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
System.out.print(b[i][j]+"\t");
System.out.println();
}
}
}
VD TABLE
VARIABLE USE
n Array size input from user
UID:7925903
QUES 3- 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 the highest capacity
available).
ALGORITHM
Step one: start
Step two: create the void main function
Step three: create the object of the scanner class to input the number of cartons by user
Step four: declare an array with the specific storage of cartons to evaluate the number
of cartons required to match the input by user
UID:7925903
Step five: declare a sum variable to add all the number of cartons required
Step six: run a loop till the input by user is not less than 6
Step seven: run a nested loop from the starting of the array till the end
Step eight: evaluate the number of cartons of sizes required and add them in the sum
variable
Step nine: print according to the format given in the question
Step ten: calculate the remaining carton of the input for it to be again calculated in the
loop
Step eleven: print the total number of cartons and the remaining cartons
Step twelve: end
PROGRAMMING
import java.util.*;
class cartons
{ public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of cartons");
int n=sc.nextInt();
System.out.println("Total no. of boxes:"+n);
int r=0; int s=0;
int a[]={48,24,12,6,2};
while(n>6)
{
for(int i=0;i<5;i++)
{
r=n/a[i];
s=s+r;
System.out.println(a[i]+"*"+r+"="+(a[i]*r));
n=n%a[i];
}
}
System.out.println("Total no. of cartons:"+s);
System.out.println("Remaining Boxes:"+n);
}
}
VD TABLE
UID:7925903
VARIABLE USE
n No. of boxes input from user
UID:7925903
Ques 4-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
ALGORITHM
Step one : start
Step two: declare a global variable n
Step three: create a returning boolean function isprime to check if the number inputted
by the user is prime or not
Step four: take a counter variable
Step five: run a loop from one till the number inputted
Step six: check the number of factors of the inputted number by the counter variable
Step seven: if the count is two then return true else false
Step eight: create a void input function
Step nine: create an object of the scanner class to input the number
Step ten: create a function void isgoldbatch
Step eleven: check if the number is odd or greater than 50 or less than 9 and print
invalid input if true
Step twelve: run a loop from i= 3 to quotient of the input number
Step thirteen: check if the value of i is prime and calculate it's remainder with the original
number and check if it's prime as well
Step fourteen: if the condition of step thirteen is true then print the numbers else
continue the loop
Step fifteen: create a main function to call all the function
UID:7925903
Step sixteen: create an object of the class
Step seventeen: call all the functions
Step eighteen: end
PROGRAM
import java.util.*;
class goldbatch
{ int n;
boolean isprime(int x)
{ int c=0;
for(int i=1;i<=x;i++)
{ if(x%i==0)
c++;}
if(c==2)
return true;
else
return false;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.");
n=sc.nextInt();
}
void isgoldbatch()
{
if(n%2==1 || n>50||n<9)
{ System.out.println("Invalid input");
return;
}
System.out.println("Number="+n);
for(int i=3;i<n/2;i++)
{
if(isprime(i) && isprime(n-i))
System.out.print(i+","+(n-i));
System.out.println();
}
}
public static void main()
{
goldbatch ob=new goldbatch();
ob.input();
UID:7925903
ob.isgoldbatch();
}
}
VD TABLE
VARIABLE USE
n Global variable to accept no. from user
c counter
UID:7925903
Ques 5-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.
ALGORITHM
Step one: start
Step two: create a int returning parameterised sum function.
Step three: declare a sum variable
Step four: find the sum of the number and return
Step five: create a int returning parameterised count function.
Step six: declare a count variable
Step seven: count the digits of the number and return it
Step eight: create the void main function
Step nine: create the object of the class
Step ten: create the object of the scanner class to input the range from user
UID:7925903
Step eleven: return invalid input if range is greater than 10000 or less than 100
and the number entered by user is greater than equal to 100
Step twelve: run a loop from m and check if the sum of any of the number from
the range is equal to the number inputted and break the loop
Step thirteen: print the required number along with the count
Step fourteen: end
PROGRAM
import java.util.*;
class smallestinteger
{ int sum(int x)
{
int s=0;
while(x!=0)
{ int d=x%10;
s=s+d;
x=x/10;
}return s;
}
int count(int x)
{
int c=0;
while(x!=0)
{
int d=x%10;
c++;
x=x/10;
}return c;
}
public static void main()
{ smallestinteger ob=new smallestinteger();
Scanner sc=new Scanner (System.in);
System.out.println("Enter m and n");
int m=sc.nextInt();
int n=sc.nextInt();
if(m>10000||m<100||n>=100)
{System.out.println("Invalid input");
return;
}
for(int i=m;;i++)
{
UID:7925903
if(ob.sum(i)==n)
{System.out.println("The required no. is:"+i);
System.out.println("Total no. of digits are:"+ob.count(i));
break;
}
}
}
}
VD TABLE
VARIABLE USE
x Formal parameter to accept values from
actual parameter
d Digit extraction
s Sum of digits
c Count of digits
UID:7925903
Ques 6-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.
ALGORITHM
Step one: start
Step two: create a parameterised returning function string date
Step three: create an array containg the days of all the months
Step four: if the year entered is a leap year then convert the days of February to 29
Step five: if the days are greater than 365 and if it is not a leap year then subtract 365
and a year to the year entered otherwise subtract 366
Step six: create a string month contacting the names of all the months
Step seven: run a loop to find the number of days till the days get less than month days
Step eight: when the days get less than the month days store the position of the month
and break the loop
Step nine: add "st", "rd", "nd" and "th" according to the date acquired
Step ten: return the date
Step eleven: create the main function
Step twelve: create the object of the scanner class to input the days, and year and the
number of days after
Step thirteen: if days entered are greater than 366 or less than 1 or year is greater than
9999 or the days entered after the current days is not between 1 and 100 print invalid
input
Step fourteen: print the date months and year by calling the different function
Step fifteen: end
PROGRAM
import java.util.Scanner;
class prac6
{
UID:7925903
String date(int d, int y)
{
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(y%4==0 && y%100!=0 || y%400==0)
m[1]+=1;
int mn=0;
if(d>365)
{
if(m[1]==28)
d-=365;
else
d-=366;
y++;
}
String date="";
String month[] =
{"January","February","March","April","May","June","July","August","September","October",
"November","December"};
for(int i=0;i<12;i++)
{
if(d<=m[i])
{
mn=i;
break;
}
d-=m[i];
}
if(d%10==1 && d!=11)
date+=d+"st ";
else if(d%10==2 && d!=12)
date+=d+"nd ";
else if(d%10==3 && d!=13)
date+=d+"rd ";
else
date+=d+"th ";
date+=month[mn]+" ";
date+=y;
return date;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
prac6 ob = new prac6();
System.out.println("Enter Day Number");
UID:7925903
int day=sc.nextInt();
System.out.println("Enter Year");
int year=sc.nextInt();
System.out.println("Enter a number between 1 and 100");
int N=sc.nextInt();
if(day>366||day<1||year>9999||N>100||N<1)
System.out.println("Invalid Input");
else
{
System.out.println("Day Number :"+day);
System.out.println("Date : "+ob.date(day,year));
System.out.println("Date after "+N+" days: "+ob.date(day+N,year));
}
}
}
VD TABLE
VARIABLE USE
d Formal parameter to accept value
UID:7925903
Ques 7- 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 intergers into this matrix. Perform following tasks on the matrix.
[2016] (a) Sort the non boundry elements in ascending order using any
standard sorting technique and rearrange them in matrix. (b) Calculate the
sum of both the diagonals. (c) Display the original matrix, rearranged matrix
and only the diagonal elements of the rearranged matrix with their sum.
ALGORITHM
Step one: start
Step two: create the void main function
Step three: create the object of the scanner class
Step four: enter the size(m) of the matrix
Step five: if the size is less than 3 or greater than 10 print invalid input
Step six: create a double dimensional array of size m*m
Step seven: create a single dimensional array of size (m-2)(m-2)
Step eight: create a variable to help in the input of the single dimensional array
Step nine: run a nested loop to input the numbers in the double dimensional array
Step ten: store all the non boundary elements in the single dimensional array
Step eleven: print the original Matrix
Step twelve: sort the single dimensional array using the bubble sort technique
Step thirteen: run a nested loop where the outer loop goes from 1 to m-1 and inner loop
goes from 1 to m-1 and store the sorted non boundary elements of the single
dimensional array in the double dimensional array
UID:7925903
Step fourteen: print the arranged matrix
Step fifteen: calculate the sum of the diagonal elements by creating a sum variable and
storing the sum of diagonal elements of the matrix
Step sixteen: print the sum
Step seventeen: end
PROGRAM
import java.util.*;
class ascending
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the matrix");
int m=sc.nextInt();
if(m<3||m>10)
{
System.out.println("Invalid size");
return;
}
int a[][]=new int[m][m];
int b[]=new int[(m-2)*(m-2)];
int c=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
if(i!=0&&i!=m-1&&j!=0&&j!=m-1)
b[c++]=a[i][j];
}
}System.out.println("Original matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(int i=0;i<b.length;i++)
{
UID:7925903
for(int j=0;j<b.length-1;j++)
{
if(b[j]>b[j+1])
{
int temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
c=0;
for(int i=1;i<m-1;i++)
{
for(int j=1;j<m-1;j++)
{
a[i][j]=b[c++];
}
}
System.out.println("Arranged matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("Sum of diagonal elements");
int s=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j||i+j==m-1)
{
s=s+a[i][j];
System.out.print(a[i][j]+"\t");
}
else
System.out.print("\t");
}
System.out.println();
}
System.out.println(s);
UID:7925903
}
}
VD TABLE
VARIABLE USE
m Size of array from user
c Index number
UID:7925903
Ques 8- Write a program to accept a sentence which may be
terminate by either ‘.’ ,’?’, or ‘!’ only. The words are to separated
by a single blank space and are in UPPERCASE. Perform the
following task: [2019]
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.
ALGORITHM
Step one: start
Step two: create a string returning parameterised function named palin
Step three: create a position variable
Step four: run a loop from the end of the length of the word till 0
Step five: extract the last character of the word and second last character
Step six: check if both the characters extracted are equal or not if yes then
continue the loop
UID:7925903
Step seven: in the else block of they are not equal store the position of the
character where it became unequal and break the loop
Step eight: run a loop from the position of the character till 0
Step nine: extract the characters and create a reversed word from it and return it
Step ten: create the void main function
Step eleven: create the object of the scanner class to input the string sentence
Step twelve: create the object of the class
Step thirteen: run a loop from the beginning till the end of the length of the string
and check if all of the characters are letters or not if not then print invalid input
exit the class
Step fourteen: again run loop from the beginning till the end of the string to check
if the words have double spaces between them
Step fifteen: check if the string ends with ".", "?" Or "!". If not then print invalid
input and exit the class
Step sixteen: create a string array from the string using split function
Step seventeen: run a loop and create all the non palindromic words to
palindromic and print them
Step eighteen: end
PROGRAM
import java.util.*;
class palprimestring
{
String palin(String x)
{
int p=0;
for(int i=x.length()-1;i>=0;i--)
{
if(x.charAt(i)==x.charAt(i-1))
continue;
else
{
p=i;
break;
}
}
for(int i=p-1;i>=0;i--)
x+=x.charAt(i);
return x;
UID:7925903
}
public static void main()
{
Scanner sc = new Scanner(System.in);
palprimestring ob = new palprimestring();
System.out.println("Enter a sentence in UPPERCASE which ends with '.' , '?' or '!' only");
String s = sc.nextLine();
for(int i=0;i<s.length();i++)
{
if("abcdefghijklmnopqrstuvwxyz".indexOf(s.charAt(i))!=-1)
{
System.out.println("ERROR!");
System.exit(0);
}
}
for(int i=0;i<s.length()-1;i++)
{
if((s.charAt(i)==s.charAt(i+1))&&(s.charAt(i)==' '))
{
System.out.println("ERROR!");
System.exit(0);
}
}
char l=s.charAt(s.length()-1);
if((l!='.')&&(l!='?')&&(l!='!'))
{
System.out.println("ERROR!");
System.exit(0);
}
String a[] = s.split(" ");
System.out.println(s);
String ns="";
for(int i=0;i<a.length;i++)
ns=ns+ob.palin(a[i])+" ";
System.out.println(ns);
}
}
VD TABLE
UID:7925903
VARIABLE USE
x Formal parameter to accept string from actual
parameter
UID:7925903
ALGORITHM
Step one: Start.
Step two: Define a class banner:
In the main method, prompt the user to enter the number of teams (N) and then validate the
input: Check if n is between 3 and 8 inclusive. If not, print "INVALID INPUT" and exit the
program.
Step three: Take input for team names and create a double dimensional character array and
with dimensions N x 100 to store team names. Use a loop to take input for each team name and
store it in the array.
Step four: Display the banner: Use nested loops to iterate through each character position
(column) in the array and for each column, print the characters of all teams in the same row. If a
team's name is shorter than the current column, print a space. Break the loop when all teams
have been printed, and no characters are left.
Step five: End.
PROGRAM
import java.util.*;
class banner
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no. of teams");
int n=sc.nextInt();
if(n<3||n>8)
{
System.out.println("Invalid no. of teams");
return;
}
char ch[][]=new char[n][100];
System.out.println("Enter the names of the team");
for(int i=0;i<n;i++)
{
String s=sc.nextLine();
for(int j=0;j<s.length();j++)
ch[i][j]=s.charAt(j);
}
int f=1;
for(int i=0;i<100;i++)
{
f=1;
for(int j=0;j<n;j++)
UID:7925903
{
System.out.print(ch[j][i]+"\t");
if(ch[j][i]!='\u0000')
f=f*0;
}
System.out.println();
if(f==1)
break;
}
}
}
VD TABLE
VARIABLE USE
n No. of teams from user
UID:7925903
Ques 10-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:
UID:7925903
ALGORITHM
Step one: start
Step two: create the object of the scanner class
Step three: input the size of the array
Step four: if the size is less than 2 and greater than 20 print invalid input
Step five: declare a double dimensional array
Step six : input the elements of the double dimensional array
Step seven: print the original Matrix
Step eight: print the mirrored matrix
Step nine: end
PROGRAM
import java.util.*;
class mirror
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
int m=sc.nextInt();
if(m<2||m>20)
{
System.out.println("Invalid input");
return;
}
int a[][]=new int[m][m];
System.out.println("Enter the elements of matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
a[i][j]=sc.nextInt();
}
System.out.println("Original Matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
UID:7925903
System.out.println();
}
System.out.println("Mirror Image:");
for(int i=0;i<m;i++)
{
for(int j=m-1;j>=0;j--)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
VD TABLE
VARIABLE USE
m Size of array from user
UID:7925903
Ques 11-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:
(ii) Find the number of vowels and consonants in each word and
display them with proper headings along with the words.
ALGORITHM
Step one: start
Step two: create the object of the scanner class
Step three: input the string sentence
Step four: if the string does not end with a "." Or a "?" Then print invalid input
Step five: create a string array from the string using split function
Step six: extract the first letter of each word and convert it to uppercase
Step seven: create a word, vowel and consonant counter variable
Step eight: now run another loop to check for the number of words, vowels and
consonants in the string.
Step nine: print the number of words, vowels and consonants in the required
manner
Step ten: end
PROGRAM
import java.util.*;
class converter
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence");
UID:7925903
String ss=sc.nextLine();
if((ss.charAt(ss.length()-1)!='.')&&(ss.charAt(ss.length()-1)!='?'))
{
System.out.println("Invalid ending");
return;
}
System.out.println("Original String:"+ss);
String s[]=ss.split(" ");
for(int i=0;i<s.length;i++)
{
if(Character.isDigit(s[i].charAt(0)))
continue;
else
{
s[i]=Character.toUpperCase(s[i].charAt(0))+s[i].substring(1,s[i].length());
}
}
for(int i=0;i<s.length;i++)
{
System.out.print(s[i]+" ");
}
System.out.println();
System.out.print("WORD \t VOWEL \t CONSONANT");
System.out.println();
for(int i=0;i<s.length;i++)
{ int v=0; int c=0;
for(int j=0;j<s[i].length();j++)
{
if("AEIOUaeiou".indexOf(s[i].charAt(j))!=-1)
v++;
else
c++;
}
System.out.print(s[i]+"\t"+v+"\t"+c);
System.out.println();
}
}
}
VD TABLE
UID:7925903
VARIABLE USE
ss String input from user
v Vowel counter
c Consonant counter
Ques 12- 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.
UID:7925903
ALGORITHM
Step one: start
Step two: create a returning parameterised function function prime to check if the
number is prime or not
Step three: create a count variable
Step four : run a loop from 1 till that number and check the number of factors of
tht number by increasing the count
Step five: if count= 2 then it is prime otherwise no
Step six: create the void main function
Step seven: create the object of the scanner class to input the number from user
Step eight: convert the number to string
Step nine: calculate the length of string
Step ten: create a flag variable
Step eleven: run a loop from 0 to length of number of number
Step twelve: convert the string back to int
Step thirteen: check if the number is prime or not, if not then change the value of
flag
Step fourteen: change the order of the number and shift it's first number to last .
Step fifteen: check if flag value has changed, if not then all numbers were
circulate prime otherwise no.
Step sixteen: end
PROGRAM
import java.util.*;
class circularprime
{
int prime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
return c;
}
public void main()
{
UID:7925903
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.");
int k=sc.nextInt();
String s=""+k;
int l=s.length();
int f=1;
for(int i=0;i<l;i++)
{
int m=Integer.parseInt(s);
if(prime(m)!=2)
f=0;
System.out.print(s+"\t");
s=s.substring(1)+s.charAt(0);
}
if(f==1)
System.out.println("Circular Prime");
else
System.out.println("Not circular prime");
VD TABLE
VARIABLE USE
n Formal parameter to accept number from
actual parameter
c Counter of factors
s String form of k
l Length of s
f Flag variable
UID:7925903
Ques 13- 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.
ALGORITHM
Step one: start
Step two: create the void main function
Step three:create the object of the scanner class to input the matrix
Step four: if length is less than equal to 0 or length is greater than 9 or breadth is
less than 3 or greater than 5, print invalid input
Step five: initialize a double dimensional array of length m and breadth n and fill it
by user input
Step six: if the element entered by user is greater than 7 or less than 0 then print
invalid input and exit
Step seven: run a loop for every element of the double dimensional array and
declare a sum variable inside the first loop
Step eight: calculate the sum of every row's decimal equivalent by converting
octal to binary numbers
Step nine: print the sum of every row
UID:7925903
Step ten: end
PROGRAM
import java.util.*;
class decimal
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
int m=sc.nextInt();
int n=sc.nextInt();
if(m<=0||m>9||n<3||n>5)
{
System.out.println("Invalid size");
return;
}
int a[][]=new int[m][n];
System.out.println("Enter the elements of matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
if(a[i][j]>7||a[i][j]<0)
{
System.out.println("invalid element");
return;
}
}
}
int k=n-1;
System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");
for(int i=0;i<m;i++)
{ int sum=0;
for(int j=0;j<n;j++)
{
sum=sum+(a[i][j]*((int)Math.pow(8,k)));
System.out.print(a[i][j]+" ");
k=k-1;
}
k=n-1;
UID:7925903
System.out.print("\t\t"+sum);
System.out.println();
}
}
}
VD TABLE
VARIABLE USE
m No. of rows from user
UID:7925903
Ques 14-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:
1. Check for the validity of the accepted sentence only for the
terminating character.
ALGORITHM
Step one: start
Step two: create the main function
Step three: create the object of the scanner class to input the string by user
Step four: input the string
Step five: if the string does not end with ".", "!" Or "?" Then print invalid input and exit
Step six: convert the string to uppercase
Step seven: create an array of string by using the split function
Step eight: arrange the words of the string according to their length using the bubble sort
technique of the length of the two words are equal then arrange them according to the
alphabetical order of their first letter only.
Step nine: print the converted strong
Step ten: end
PROGRAM
import java.util.*;
class lowtohigh
{
public static void main()
{
Scanner sc=new Scanner(System.in);
UID:7925903
System.out.println("Enter the sentence");
String ss=sc.nextLine();
if(ss.charAt(ss.length()-1)!='.'&&ss.charAt(ss.length()-1)!='?'&&ss.charAt(ss.length()-1)!='!')
{
System.out.println("Invalid input");
return;
}
ss=ss.toUpperCase();
System.out.println("Original String:"+ss);
ss=ss.substring(0,ss.length()-1);
String s[]=ss.split(" ");
for(int i=0;i<s.length;i++)
{
for(int j=0;j<s.length-i-1;j++)
{
if(s[j].length()>s[j+1].length())
{
String temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
if(s[j].length()==s[j+1].length())
{
if(s[j].compareTo(s[j+1])>0)
{
String temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
}
System.out.println("Converted String:");
for(int i=0;i<s.length;i++)
{
System.out.print(s[i]+" ");
}
}
}
UID:7925903
VD TABLE
VARIABLE USE
ss String input from user
UID:7925903
Ques 15-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..
Allow the user to input integers into this matrix. Perform the
following tasks:
ALGORITHM
Step one: create the main function
Step two: create the object of the scanner class to input the matrix
Step three: enter the size of the matrix
Step four: if the size is less than 3 or greater than 9 print invalid input
Step five: input the elements of the matrix
Step six: print the original Matrix
Step seven: rotate the matrix by 90°
Step eight: find the sum of the corner elements of the matrix by finding the row
and column number of the elements
Step nine: print the sum
Step ten: end
PROGRAM
import java.util.*;
class ninetydegree
{
UID:7925903
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the matrix");
int m=sc.nextInt();
if(m<3||m>9)
{
System.out.println("Invalid size");
return;
}
int a[][]=new int[m][m];
System.out.println("Enter the elements");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("90 degree");
for(int j=0;j<m;j++)
{
for(int i=m-1;i>=0;i--)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
int sum=a[0][0]+a[m-1][0]+a[0][m-1]+a[m-1][m-1];
System.out.println("Sum of corner elements:"+sum);
}
}
UID:7925903
VD TABLE
VARIABLE USE
m Size of array from user
UID:7925903
Ques 16-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 up to 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
INPUT
N=4
OUTPUT
INPUT
UID:7925903
N=4
AAKE
OUTPUT
INPUT
N=7
OUTPUT
INPUT
N=4
AEGIK
OUTPUT
INPUT
N=3
ACE
OUTPUT Valid!
Algorithm
Step 1: Start
Step 2: import the util package for Scanner class
UID:7925903
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
PROGRAM
import java.util.*;
public class prog16
{
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;
UID:7925903
}
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
System.out.println("Valid");
}
}
}
UID:7925903
Ques 17-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
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:
OUTPUT:
Example 2
INPUT:
OUTPUT:
UID:7925903
Rapelcgvba urycf gb frpher qngn.
Example 3
INPUT:
You
OUTPUT:
INVALID LENGTH
Algorithm
Step 1: Start
Step 2: import the scanner class to take input from user .
Step 3: Create the main function to cipher the string entered by user.
Step 4: In the main function create the object of Scanner class.
Step 5: Take a String input from the user.
Show 6: Calculate the length of the string entered.
Step 7: Check if the length of the String ranges between 3 and 100.
Step 8: If Step 7 evaluates to true then Create a blank String variable to store the new ciphered
string.
Step 9: Create a string having all the letters of the English alphabet
Step 10: Concat the string from step 9 with itself so we have a rotating string of all the alphabets
Step 11: Create a loop to extract the characters from the input string
Step 12: Check the index of each character of the input string from the String of all the rotating
alphabets
Step 13: Add 13 to the index number obtained
Step 14: Add the character at the (index+13)th position and add it to the new string.
Step 15: If the character obtained from the string is not a letter than add it directly to the new
string.
Step 16: Print the newly obtained ciphered string
Step 17: If the string is out of the specified range print "Invalid length".
Step 18: End.
Program
UID:7925903
{
Scanner sc = new Scanner(System.in); //object of scanner
System.out.println("Enter text");
String s=sc.nextLine(); //user input
int l=s.length(); //length of string
if(l>3&&l<100) //checking condition
{
String cs=""; //stores ciphered text
String upp="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
upp=upp.concat(upp); //upppercase letters string
String low="abcdefghijklmnopqrstuvwxyz";
low=low.concat(low); //lowwercase letters string
for(int i=0;i<l;i++)
{
char ch=s.charAt(i); //character extraction
if(Character.isLetter(ch)) //checks for letter
{
if(Character.isUpperCase(ch)) //encryption by shifting 13 letters ahead
cs+=upp.charAt(upp.indexOf(ch)+13);
else
cs+=low.charAt(low.indexOf(ch)+13);
}
else //no change for non-letter characters
cs+=ch;
}
System.out.println("The cipher text is :");
System.out.println(cs); //printing final text
}
else //for invalid text length
System.out.println("INVALID LENGTH");
}
}
UID:7925903
Q18. Write a program to accept a sentence which may be terminated by
either ‘.’, ‘?’ or ‘!’ only. The words are not separated by more than one blank
space and are in UPPERCASE. Perform the following tasks : i) Find the
number of words beginning and ending with a vowel. ii) Place the words
which begin and end with a vowel at the beginning followed by the
remaining words as they occur in the sentence. Test your program with
some sample data and some random data :
Example 1
INPUT:
OUTPUT:
Example 2
INPUT:
UID:7925903
TODAY.
OUTPUT:
TODAY
Example 3
INPUT:
OUTPUT:
Example 4
INPUT:
OUTPUT:
INVALID INPUT
Algorithm
Step 1: Start
Step 2: import the scanner class
UID:7925903
Step 3: create a parameterized string returning function named vowel to check for the presence
of vowel at the starting and ending of the string
Step 4: Check the character at the start and end of the string
Step 5: if both are vowel then return true
Step 6: create the object of Scanner class
Step 7: create the object of the class
Step 8: take a string input from the user which ends with '.' '!' or '?'.
Step 9: convert the string to uppercase.
Step 10: check if the last character of the string ends with '.' '!' or '?' if yes then continue with
step 11
Step 11: convert the string to array
Step 12: calculate the length of array
Step 13: create two strings to store the words starting and ending with vowels and those who do
not
Step 14: Add the strings to the string variable according to the criteria
Step 15: print the strings that begin and end with vowels and those who do not
Step 16: if step 10 evaluated to false then print "invalid string"
Step 17: end
Program
import java.util.*; //importing Scanner class
class prog18
{
boolean vowel(String x) //checks for vowel at first and last index
{
if("AEIUO".indexOf(x.charAt(0))!=-1) //if first character vowel
if("AEIUO".indexOf(x.charAt(x.length()-1))!=-1) //if last character vowel
return true;
else
return false;
else
return false;
}
UID:7925903
{
String a[] = s.split(" "); //string to array
System.out.println(s);
int l1=a.length;
String nv="";//stores sorted string
String nc="";
int v=0; //counter for words with vowels at start and end
for(int i=0;i<l1;i++)
{ if(ob.vowel(a[i]))
{
nv+=a[i]+" "; //adding vowel words first
v++; //counter
}
else
nc+=a[i]+" ";
}
System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL
="+v);
System.out.println(nv+""+nc); //printing
}
else //for invalid string
System.out.println("INVALID INPUT");
}
}
UID:7925903
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
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.
Algorithm
Step 1: import the scanner class
Step 2: create the main function
Step 3: create the object of Scanner class
Step 4: take a integer input from user
Step 5: create a copy of the input interger
Step 6: initialise a flag variable and and sum variable
Step 7: run a while loop to check if the number is magic number by adding the digit of the
inputted integer and giving the value stores in the sum to the original number and again running
the loop till the sum is reduced to a single digit number
Step 8: if the sum is equal to 1 change the value of flag to 1 else 0
Step 9: create a a loop to check all the factors of the number and count the number of factors
Step 10: if the number of factors exceed 2 then keep tha flag variable as 1 else 0
Step 11: if the flag variable is still 1 then print that the number is composite magic number
otherwise not
Step 12: end
Program
import java.util.*;
class prog19
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int j=n;
UID:7925903
int s=0;
int f=0;
while(n>=10)
{
s=0;
while(n>0)
{
int d=n%10;
s+=d;
n/=10;
}
n=s;
}
if(s==1)
f=1;
else
f=0;
int c=0;
for(int i=1; i<=j;i++)
{
if(j%i==0)
c++;
}
if(c>2)
f=1;
else
f=0;
if(f==1)
System.out.println("It is a composite magic number");
else
System.out.println("It is not a composite magic number");
}
}
UID:7925903
UID:7925903
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.
For example:
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.
Algorithm
Step 1: start
UID:7925903
Step 2: import the scanner class
Step 3: create a parameterized integer returning sum function to calculate the ISBN value of the
inputted value by user
Step 4: initialise a sum variable and multiplication counter
Step 5: run a loop to extract each character of the string inputted
Step 6: check if the character extracted is 'X' or not, if not then continue to step 7
Step 7: convert each of the string to integer
Step 8: multiply the converted integer by the multiplication counter
Step 9: if the character extracted is 'X' then directly multiple 10 by the multiplication counter
Step 10: reduce the multiplication counter by one
Step 11: return the sum
Step 12: create the main function
Step 13: create the object of Scanner
Step 14: create the object of class
Step 15: take input from the user
Step 16: check if the length of the input is 10 or not if yes then continue with the program
otherwise exit the class
Step 17: calculate the sum by calling the sum function
Step 18: check if the sum obtained is divisible by 11 or not
Step 19: if yes then the ISBN value is valid otherwise no.
Step 20: end
Program
import java.util.*;
class prog20
{
int sum(String sss) //calculates sum in ISBN code
{
int ss=0; //sum
int y=10; //multiplication counter
for(int i=0;i<sss.length();i++)
{ String d=sss.charAt(i)+"";
if(sss.charAt(i)!='X') //if character is a number
ss+=Integer.parseInt(d)*y;
else //if character is X
ss+=10*y;
y--;
}
return ss; //returns sum
}
UID:7925903
Scanner sc = new Scanner(System.in);
prog20 ob = new prog20(); //object creation
System.out.println("Enter a ISBN code");
String s=sc.nextLine(); //user input
if(s.length()!=10) //not of valid length
System.out.println("Invalid Input");
else
{
System.out.println("Sum = "+ob.sum(s));
if(ob.sum(s)%11==0) //if no remainder is left when divided by 11
System.out.println("Valid ISBN code");
else
System.out.println("Invalid ISBN code");
}
}
}
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.
Algorithm
1. Start the program.
2. Prompt the user to enter a number less than 1000.
3. Read the input number.
4. Check if the entered number is greater than 1000. If it is, display "Out of Range" and
terminate the program.
5. If the number is within the valid range:
6. Declare variables to store the words for hundreds, tens, and ones places.b. Define arrays for
words representing ones, teens, and tens digits.
UID:7925903
7. Calculate the hundreds, tens, and ones digits of the input number.
8. If the number has a hundreds digit:
9. Map the hundreds digit to its word representation.
10. If the number has tens and ones digits:
11. If the tens digit is 1, handle special cases (teens).
12. Otherwise, map the tens and ones digits to their word representations.
13. Construct the word representation of the number based on the calculated digits.
14. Handle special cases for formatting, such as when the tens or ones place is 0.
15. Convert the constructed word representation to uppercase.
16. Display the result.
17. End the program.
Program
import java.util.*;
class prog21
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number less than 1000");
int n=sc.nextInt();
if(n>1000)
System.out.println("Out of Range");
else
{
String wrd="", h="", t="",o="";//store the number in words.
int a,b,c;
String ones[]={"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String tens[]={"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
String eens[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen"};
a=n/100;//hundred digit
if(n>100)
a=n%100;
b=n/10;// tens digit
c=n%10;// ones digit
if(a!=0)
h=ones[a-1]+ "hundred";
if (b==0 && c!=0)
o=ones[c-1];
else if (c==0 && b!=0)
t=tens[b-1];
else if(b==1)
UID:7925903
t=eens[c-1];
else if(b!=0 && c!=0)
t=tens[b-1]+" "+ones [c-1];
else if (b==0 && c==0)
wrd=h;
else if(a==0)
wrd=t+" "+o;
else
wrd=h+" and "+t+" "+0;
System.out.println("\n"+wrd.toUpperCase());
}
}
}
Datatype Variable Description
String Ones Array of words of ones place dogit
String eens Array of words between 11 and 19
String Tens Array of words of tens place digit
Int N No as input
String Wrd No as words
UID:7925903
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.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
COOL IS IT.
Algorithm
UID:7925903
Step 2: create a string returning parameterized function to rotate the odd length strings by +2
characters each
Step 3: initalise a blank.string to store the new rotated string
Step 4: initalise and declare a string having all the alphabets in capital letters with additional 'AB'
at the end
Step 5: run a loop to extract each of the string from the input
Step 6: check the index of the character from the string from step 4:
Step 7: add 2 to the index and then input the character from the new index from the string at
step 4
Step 8: add the new charater to the blank String and return it
Step 9: create a string returning parameterized function to rotate the even length strings and
make them reversed
Step 10: convert the string to array
Step 11: run a loop to extract the characters of each string in array and store them in reverse
Step 12: return the new reversed string
Step 13: create the object of Scanner class
Step 14: create the object of the class
Step 15: enter the array size by the user
Step 16: if the length is less than 1 or greater than 10 then show invalid input
Step 17: declare a string areay
Step 18: input the sentences
Step 19: check the length of each string and if it is odd then rotate it by calling the oddrot()
function or if it is even then call the evenrev() function.
Step 20: print the final strings
Step 21: end
Program
import java.util.*;
class prog22
{
String oddrot(String a)
{
String rs=""; //stores new String
String A="ABCDEFGHIJKLMNOPQRSTUVWXYZAB"; //string to rotate character
for(int i=0;i<a.length();i++) //loop to extract character
{
char ch=a.charAt(i); //stores character at index i
if(Character.isLetter(ch)) //if character is letter
rs+=A.charAt(A.indexOf(ch)+2);
else
rs+=ch;
}
UID:7925903
return rs; //returning statement
}
String evenrev(String b)
{
String a[]=b.split(" "); //string to array
String rv=""; //stores new string
for(int i=a.length-1;i>=0;i--)
rv+=a[i]+" "; //adding word in reverse order
return rv; //returns reversed string to calling statement
}
public static void main()
{
Scanner sc = new Scanner(System.in);//scanner object
prog22 ob = new prog22(); //object of class
System.out.println("Enter array size");
int x=sc.nextInt(); //user input
if(x<=1||x>=10) //condition checking
System.out.println("INVALID INPUT");
else
{
String s[]=new String[x]; //array to store strings
System.out.println("Enter sentences");
String e=sc.nextLine(); //user input
for(int i=0;i<x;i++)
{
s[i]=sc.nextLine(); //user input
s[i]=s[i].toUpperCase(); //String to uppercase
}
for(int i=0;i<x;i++)
if(i%2==0) //at odd position
s[i]=ob.oddrot(s[i]);
else //at even position
s[i]=ob.evenrev(s[i]);
System.out.println("Encrypted Strings :");
for(int i=0;i<x;i++) //printing
System.out.println(s[i]);
}
}
}
UID:7925903
Datatype Variable Description
String A Array of words
String Rv Reversed String
Int X No as input
String S Array to store strings
Char Ch Character extracted
UID:7925903
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 enomination
used should be displayed]. Also print the amount in words according to the
digits. Example 1
INPUT: 14836
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
Algorithm
Step 1: start
Step 2: create an object of Scanner class
Step 3: print the user to enter a five digit amount
Step 4: if the entered number is greater than 99999 then show invalid input
Step 5: create an array having all possible denomination
Step 6: run a loop from 0 till the end of array
Step 7: divide the amount entered by the user and store it
UID:7925903
Step 8: print the denominaton acquired
Step 9: reduce the amount entered by finding the remainder
Step 10: create an array having digits in words
Step 11: initialise a reverse variable as 0
Step 12: create a copy of the amount
Step 13: reverse the amount entered
Step 14: convert each of the digits of the amount to words
Step 15: end
Program
import java.util.*;
class prog23
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int x;
System.out.print("Enter a five-digit amount: ");
x=sc.nextInt();//accept the amount
if(x>99999)
System.out.println("INVALID AMOUNT.");
else
{
int a[]={1000, 500, 100, 50, 20,10,5,2,1};
for(int i=0;i<a.length;i++)
{
int t=x/a[i]; //store the denomination
{
System.out.println(a[i]+"X"+t+"= \t"+(t*a[i]));
x=x%a[i];
}
}
String wrd[]={"one", "two", "three", "four", "five","six", "seven", "eight",
"nine"};//store the digits in words
int rev=0;// reverse of the amount
int p=x;//copy
while(p>0)
{
int d=p%10;
rev=rev*10+d;
p/=10;
}
while(rev>0)
UID:7925903
{
int d=rev%10;
System.out.print(wrd [d-1].toUpperCase()+" ");
rev/=10;
}
}
}
}
Algorithm
UID:7925903
Step 1: start
Step 2: import the scanner class
Step 3: create a parameterized integer returning function to check if a number is prime or
not
Step 4: initalise a counter variable
Step 5: run a loop from one till the number
Step 6: check and count all the factors of the number
Step 7: if the count is 2 then return 1 otherwise 0
Step 8: creat the main class
Step 9: create the object of Scanner class
Step 10: create the object of the class
Step 11: enter a number by the user
Step 12: check if the number is prime or not
Step 13: if it is not prime then calculate the sum of the digits and the sum of the prime
factors of the number
Step 14: if both the sum are equal then print it is a smith number otherwise print not a
smith number.
Step 15: end
Program
import java.util.*;
class prog24
{
int prime(int a)
{
int c=0;//counter
for(int i=1;i<=a;i++)
{
if(a%i==0)
c++;
}
if(c==2)
return 1;//returning value
else
return 0;
}
public static void main()
{
prog24 ob=new prog24();
Scanner sc=new Scanner(System.in);
System.out.println("Enter a No.");
int n=sc.nextInt();//input
UID:7925903
int z=ob.prime(n);//method call and value catch
if(z==0)
{
int s=0,s1=0,i=2,k=n;
while(k!=0)
{
s=s+k%10;//sum of digits
k/=10;
}
while(n>0)
{
if(n%i==0)
{
int p=i;//copy
while(p!=0)
{
s1+=p%10;
p/=10;
}
n/=i;//updation
}
else
++i;
}
if(s1==s)
System.out.println("Smith No.");
else
System.out.println("Not");
}
else
System.out.println("Not");
}
}
UID:7925903
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
Algorithm
Step 1: start
Step 2: import the scanner class
Step 3: create the object of Scanner class
Step 4: input a range from the user not above 30000
Step 5: create a flag variable
Step 6: run a loop within the range
Step 7: convert integer to string
Step 8: run the first loop from 0 till the length of the string number
Step 9: run another loop from i+1 till second last index of the length of the string number
Step 10: extract two characters one from the index of loop step 8 and another from index
of loop step 9 and check if they are equal
Step 11: if they are equal change flag to 1
Step 12: if flag is not equal to 1 then print the number
Step 13: repeat the loop till the range is complete
Step 14: end
UID:7925903
Program
import java.util.*;
class prog25
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the range where the range is less than 30000");
int m=sc.nextInt();//lower limit
int n=sc.nextInt();//upper limit
int f=0;//flag
for(int i=m;i<=n;i++)
{
f=0;
String s= Integer.toString(i);//integer to string
for(int a=0;a<s.length();a++)
{
for(int b=a+1;b<s.length()-1;b++)
{
if(s.charAt(a)==s.charAt(b))
f=1;
}
}
if(f!=1)
System.out.println(i);//printing of unique
}
}
}
Datatype Variable Description
Int M Lower limit
Int N Upper limit
Int F Flag
Int i Loop variable
String S Integer to String
UID:7925903
Q26. 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. About 'kaprekar' number: A
positive whole number 'n' that has 'd' number of digits is squared and split
into 2 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 pieces is equal to the
number then it's a kaprekar number.
Algorithm
Step 1: start
Step 2: import the scanner class
Step 3: create a main class
Step 4: create the object of Scanner class
Step 5: enter the range by user
Step 6: initialise a counter variable
Step 7: run a loop within the range
Step 8: square the current digit from the range
Step 9: initialise two blank String variable
Step 10: calculate the length of the current digit by converting it to string
Step 11: run a loop from 0 to half of the string
Step 12: store the characters of it in one of the blank String
Step 13: run a loop from half of the string till the end of the string
Step 14: store the characters of it in the other string
Step 15: reconvert both the string to integer
Step 16: if the sum of the two is equal to the original number then it is a karperkar
number
Step 17: repeat the loop till the end and print all the karperkar numbers
Step 18: end
UID:7925903
Program
import java.util.*;
class prog26
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter limits p and q such that p<q");
int p=sc.nextInt();//lower limit
int q=sc.nextInt();//upper limit
int c=0;
System.out.println("Karpekar No. between the range");
for(int j=p;j<=q;j++)
{
int sq=j*j;//square of no
String s1="",s2="";
String s=""+sq;//sq to string
int l=s.length();
for(int i=0;i<(l/2);i++)
{
char ch=s.charAt(i);//characeter extraction
s1=s1+""+ch;
}
for(int i=(l/2);i<l;i++)
{
char ch=s.charAt(i);
s2=s2+""+ch;
}
int z=Integer.valueOf(s2);//string to int
int z1=Integer.valueOf(s1);
if((z+z1)==j)
{
System.out.print(j+" ");
c++;//frequency
}
}
System.out.println("Frequency of Karperkar="+c);
}
}
UID:7925903
C Int Frequency counter
Sq Int Square of no
Z Int String to int
S String Sq to string
UID:7925903
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
First array:(A)
Suman Anil
Second array:(B)
OUTPUT
Anil Suman
UID:7925903
Algorithm
Step 1: start
Step 2: create the object of Scanner class
Step 3: enter the number of names in array A and in array B
Step 4: enter the names in both the arrays
Step 5: initialise a new array having size as sum of the two previous arrays
Step 6: enter all the names from array A and B to the new array C
Step 7: sort the array A by using nested loop and temporary variable by checking one name
with the entire list and replacing them alphabetically
Step 8: similarly sort array B
Step 9: finally sort array C
Step 10: print the sorted array C
Step 11: end
Program
for(int i=0;i<N;i++)
A[i]=sc.nextLine();
System.out.println("Names of array B");
for(int i=0;i<M;i++)
B[i]=sc.nextLine();
int x=0;
String C[]=new String[M+N];
for(int i=0;i<N;i++)
C[x++]=A[i];
for(int i=0;i<M;i++)
C[x++]=B[i];
String t="";//temporary variable for swapping
System.out.println("Sorted Array A : ");
UID:7925903
for(int i=0;i<N;i++)
{
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++)
System.out.print(A[i]+" ");
System.out.println("Sorted Array B : ");
for(int i=0;i<M;i++)
{
for(int j=0;j<M-i-1;j++)
if(B[j].compareTo(B[j+1])>0)
{
t=B[j];
B[j]=B[j+1];
B[j+1]=t;
}
}
for(int i=0;i<N;i++)
System.out.print(B[i]+" ");
System.out.println("Sorted Merged Array : ");
for(int i=0;i<(M+N);i++)
{
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++)
System.out.print(C[i]+" ");
}
}
UID:7925903
Variable Description
Int N Size of array 1
Int M Size of array 2
Int x Variable to go through array 4
String A[] Array 1 Names
Strung B[] Array 2 Names
String C[] Array of Merged Names in ascending order
String t Temporary variable
Algorithm
Step 1: start
Step 2: create object of Scanner class
Step 3: enter a sentence by user ending with '.' '!' or '?'
Step 4: print the original string
UID:7925903
Step 5: convert the string to array
Step 6: calculate the length of the array
Step 7: print the length which is equal to the number of words in the string
Step 8: sort the array in ascending order by comparing the words with each other in a nested
loop with the help of temporary variable
Step 9: print the sorted array
Step 10: end
Program
import java.util.*;
class prog28
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Sentence ending with either . ! ?");
String s=sc.nextLine().toUpperCase();//strnig as input
//and convert to upper case
char lc=s.charAt(s.length()-1);//last char
if(lc=='.' || lc=='?' || lc=='!')
{
System.out.println("Original String:"+s);
String a[]=s.split(" ");//array of words
int l=a.length;//no of words
System.out.println("No. of words:"+l);
System.out.println("alphabetical Order:");
String t="";//temporary variable for sorting
for(int i=0;i<l;i++)
{
for(int j=0;j<l-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<l;i++)
System.out.print(a[i]+" ");
}
}
}
UID:7925903
Variable Data type
String s String as input
Char lc Last char
String a[] Array of words
String t Temporary variable
Int l No of words
Algorithm
1. Begin the program.
2. Declare the realproject13 class.
3. Define the main() method.
4. Create a Scanner object to read input from the user.
5. Prompt the user to enter the range, specifying that both numbers must be less than 3000 and
the starting range
must be less than the ending range.
6. Read the input values for the starting and ending range.
7. Initialize an empty string s to store prime palindrome numbers.
8. Initialize a variable f to store the frequency of prime palindrome numbers.
9. Check if both input values are less than 3000 and if the starting range is less than the ending
range.
10. If the conditions are met:
11. a. Iterate through each number in the range.
UID:7925903
12. b. Initialize a variable c to count the factors of the number.
13. c. Determine if the number is prime by counting its factors.
14. d. If the number is prime:
15. i. Initialize a variable k as a copy of the number.
16. ii. Initialize a variable rev to store the reverse of the number.
17. iii. While k is greater than 0:
18. - Extract each digit of k.
19. - Build the reverse of the number rev.
20. iv. Check if the original number equals its reverse (i.e., if it's a palindrome).
21. v. If the number is a palindrome, append it to the string s and increment the frequency f.
22. Remove the leading comma from string s.
23. Print "The prime palindrome integers are:".
24. Print the string s containing prime palindrome numbers.
String n To store the word String m
To store the word String rev To store the reversed word int ll
To store the length of the word int To control the iterations of the loop
25. Print "Frequency of prime palindrome integers:" followed by the frequency f.
26. If the input values do not meet the conditions, print "INVALID INPUT".
27. End the program.
Program
import java.util.*;
class prog29
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter range such that m&n<3000 and m<n");
int m=sc.nextInt();
int n=sc.nextInt();
if(m<3000 && n<3000 && m<n)
{
System.out.println("Prime Palindrome No.:");
int p=0;
for(int i=m;i<=n;i++)
{
int c=0;//counter
for(int j=1;j<=i;j++)
{
if(i%j==0)
UID:7925903
c++;
}
if(c==2)
{
int co=i;//copy
int r=0;//reverse
while(co!=0)
{
int d=co%10;//digit extravtion
r=r*10+d;//reversing
co/=10;
}
if(r==i)
{
System.out.print(i+" ");
p++;//frequency
}
}
}
System.out.println("Frequency:"+p);
}
}
}
Variable Description
Int m Lower limit
Int n Upper limit
Int p Frequency of prime palindrome
Int c Counter of factors
Int r reverse
UID:7925903
Q-30 : 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
Algorithm
Step 1: import the scanner class
Step 2: create the object of Scanner class
Step 3: input a number by the user
Step 4: initialise a blank String variable
Step 5: run a loop i from 1 till the number
Step 6: initialise a sum variable as 0
Step 7: run a nested loop from i till the number
Step 8: add the number of j loop and store it in i
Step 9: store the numbers in the string variable
Step 10: if the sum is greater than or equal to the original number break from the loop
Step 11: if sum is equal to the number then print the numbers stored in the string and continue
the loop
Step 12: end
Program
import java.util.*;
class prog30
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n=sc.nextInt(); //no as input
UID:7925903
for(int i=1;i<n;i++)
{
String p="";
int s=0;//sum
for(int j=i;j<n;j++)
{
s+=j;
p=p+" + "+j; //adding consecutive int
if(s>=n)
break;
}
if(s==n)
{
System.out.println(p);
continue;
}
}
}
}
Variable Datatype
Int n No as input
String p Adding consecutive int
Int s Sum
Int I Loop variable
UID:7925903