Programming Types
Programming Types
Ans :
import [Link];
public class Bank
{
private double p;
private double n;
private double r;
private double a;
void accept() {
Scanner in = new Scanner([Link]);
void calculate() {
if (n <= 0.5) {
r = 9;
} else if (n <= 1) {
r = 10;
} else if (n <= 3) {
r = 11;
} else {
r = 12;
}
void display() {
[Link]("Principal\tTime\tRate\tAmount");
[Link](p + "\t" + n + "\t" + r + "\t" + a);
}
Q2. Anshul transport company charges for the parcels of its customers as per the following
specifications given below :
Class name : Atransport
Memer variables :
String name - to store the name of the customer
int w - to store the weight of the parcel in Kg.
int charge - to store the charge of the parcel
Member functions
void accept() - to accept the name of the customer, weight of the parcel
from the user (using Scanner class)
void calculate() - to calculate the charge as per the weight of the parcel as per
the following criteria :
Weight in Kg Charge per Kg
Upto 10 Kgs Rs. 25 per Kg
Next 20 Kgs Rs. 20 per Kg
Above 30 Kgs Rs. 10 per Kg
A surcharge of 5% is charged on the bill.
void print() - to print the name of the customer, weight of the parcel, total
bill inclusive of surcharge in a tabular form in the following format :
Name Weight Bill amount
………. …….. …………
Define a class with the above-mentioned specifications, create the main method, create an object
and invoke the member methods. [SQP-2020]
Ans :
import [Link].*;
class Atransport
{
String name;
int w;
int charge;
void accept()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter Customer Name : ");
name=[Link]();
[Link]("Enter Parcel Weight : ");
w=[Link]();
}
void calculate()
{
if(w<=10)
charge=w*25;
else if (w<=30)
charge=250+((w-10)*20);
else
charge=250+400+((w-30)*10);
charge+=charge+5/100;
}
void print()
{
[Link]("Name\tWeight\tBill amount");
[Link]("-----\t-------\t---------");
[Link](name+"\t"+w+"\t"+charge);
}
Q3. The BHDB company offer EMI (Equated Monthly Instalments) based loans for the purchase
of electronic devices based on the purchase amount the rate of interest is offered as follows:
Purchase amount less than Rs.20000, rate of interest is 12% otherwise the rate of interest is 15%.
Amount with interest for the specified number of years is calculated using the formula Amount =
p(1+r/100)^n.
Wherep is the purchase amount, r is the rate of interest, n is the number of years.
After the amount is calculated it is converted into EMI by dividing the amount by the number of
months of the tenure, which has to be a whole number rounded off to the nearest integer.
class EMI {
double p; // purchase amount
int n; // number of years
double r; // rate of interest
double amount; // amount after interest
long emi; // EMI (rounded)
Sorting
[Link] Sort :
import [Link].*;
public class BubbleSort
{
public static void main(String args[])
{
int i,j,n,temp;
Scanner in = new Scanner([Link]);
[Link]("Enter number of elements");
n=[Link]();
int arr[]=new int[n];
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1]) // for string : if(arr[j].compareTo(arr[j+1])>0)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
[Link] Sort :
import [Link].*;
public class SelectionSort
{
public static void main(String args[])
{
int i,j,n,temp;
[Link] a program to input name and percentage of 35 students of class X in two separate one
dimensional arrays. Arrange students details according to their percentage in the descending
order using selection sort method. Display name and percentage of first ten toppers of the class.
[SQP2020]
Or, To input name and marks of 15 students in 2 single dimensional arrays. And print the
name and marks of the students rank-wise. (Use Bubble sort technique) [TCA-350]
Or, Write a program to accept the name and marks in computer science of “n” students in an
array and print the name and marks of students merit wise. [TCA-423]
Ans :
import [Link];
class BubbleSort
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int n, i,j,tempm;
String tempn;
[Link]("Enter number of students in the class : ");
n=[Link]();
String name[]=new String[n];
int marks[]=new int[n];
for(i=0;i<n;i++)
{
[Link]("Enter name of student "+(i+1)+ ": ");
name[i]=[Link]();
[Link]("Enter Computer Science Marks :");
marks[i]=[Link]();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(marks[i]<marks[j])
{
tempm=marks[i];
marks[i]=marks[j];
marks[j]=tempm;
tempn=name[i];
name[i]=name[j];
name[j]=tempn;
}
}
}
[Link]("...............................");
[Link]("Merit wise Name and Marks of Students");
[Link]("Name\t\t Marks");
for(i=0;i<n;i++)
[Link](name[i]+"\t\t"+marks[i]);
}
}
[Link] a program to initialize the ‘Seven Wonders’ of the World along with their locations in two
different arrays. Search for a name of the country input by the user. If found, display the country
along with its Wonder, otherwise display ‘Sorry Not Found!’
Seven Wonders : Chichen Itza, Christ the Redeemer, Taj Mahal, Great Wall of China, Machu
Picchu, Petra, Colosseum.
Locations : Mexico, Brazil, India, China, Peru, Jordan, Italy
Example : Input : Country Name India
Output : India Taj Mahal
Ans :
import [Link].*;
public class SevenWonders
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int i,j,f=-1;
String wond[]=new String[7];
String locn[]=new String[7];
String city;
[Link]("Enter seven wonders");
for(i=0;i<7;i++)
{
wond[i]=[Link]();
}
for(i=0;i<7;i++)
{
if([Link](locn[i])) // or, locn[i].equals(city)
{
f=1;
break;
}
}
if(f==1)
{
[Link]("Search successful");
[Link](locn[i]+": \t Wonders : \t"+wond[i]);
}
else
[Link]("Search unsuccessful, no such location in the list");
}}
Type : 03 - Double Dimensional Array :
[Link] a program in Java to store the numbers in a 4*4 matrix in a Double Dimensional
Array. Find the sum of the numbers of each row and the sum of the numbers of each column
of the matrix by using an input statement.
Or, Define a class to accept values into 4x4 array and find and display the sum of each row.
Example:
A[][]={{1,2,3,4},{5,6,7,8},{1,3,5,7},{2,5,3,1}} [SQP2025]
Output:
sum of row 1 = 10 (1+2+3+4)
sum of row 2 = 26 (5+6+7+8)
sum of row 3 = 16 (1+3+5+7)
sum of row 4 = 11 (2+5+3+1)
Ans : Ans :
import [Link];
}
}
Q2. Write a program to input elements in an array of size mm and print the sum of left
diagonal and product of the right diagonal.
Ans :
import [Link].*;
class Diagonal {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int i, j, m, sl=0, pr=1;
[Link]("Enter the size : ");
m=[Link]();
int arr[][]=new int[m][m];
for (i=0; i<m;i++)
{
for(j=0;j<m;j++)
{
[Link]("Enter the elements in row no "+i+" and comumn no "+j+" :");
arr[i][j]=[Link]();
}
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
{
sl=sl+arr[i][j];
}
if((i+j)==(m-1))
{
pr=pr*arr[i][j];
}
}
}
[Link]("Sum of left diagonal : "+sl);
[Link]("Product of right diagonal : "+pr);
}
}
Or, Write a program to input elements in a matrix 4x4 order and display sum of all the elements except
diagonal elements.
Example : 2 5 6 8
1 3 9 6
4 7 3 1
2 8 0 3
Sum of the elements except diagonal elements =5+6+1+6+4+1+8+0=31
import [Link];
public class MatrixSumExceptDiagonals {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int[][] matrix = new int[4][4];
int sum = 0;
// Display result
[Link]("Sum of the elements except diagonal elements = " + sum);
}
}
Q3. Write a program to create a two-dimensional array of size 23 and print all the odd
numbers in the array.
[Link] a program to create an array of size 23 and print the factorial of all the numbers
separately.
Q5. Write a program in Java to store the numbers in a 3*4 matrix in a Double Dimensional
Array. Find the sum of all the numbers of the matrix and display the sum using an input
statement.
Or,
Define a class to accept values into a 4 × 4 integer array. Calculate and print the NORM of the array.
[ICSE2025]
NORM is the square root of sum of squares of all elements.
1 2 1 3
5 2 1 6
3 6 1 2
3 4 6 3
Sum of squares of elements = 1 + 4 + 1 + 9 + 25 + 4 + 1 + 36 + 9 + 36 + 1 + 4 + 9 + 16 + 36 + 9 = 201
NORM = Square root of 201 = 14.177446878757825
Ans :
Or,
import [Link];
class Norm{
public static void main(String[] args){
Scanner in = new Scanner([Link]);
int a[][] = new int[4][4];
int sum = 0;
[Link]("Enter array elements:");
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
a[i][j] = [Link]();
}
}
[Link] a program to check whether a given number is Perfect number or not. [A perfect
number is a positive integer that is equal to the sum of its positive divisors, excluding the
number itself. Eg : 6=1+2+3]
Ans :
import [Link].*;
class PerfectNumber{
public static void main(String args []){
int s, n, i;
Scanner in=new Scanner([Link]);
[Link]("Enter any positive numner: ");
n=[Link]();
s=0;
for(i=1;i<n;i++)
{
if((n%i)==0)
{
s=s+i;
}
if(n==s)
{
[Link]("Number is perfect");
}
else
{
[Link]("Number is not perfect");
}
}
}
Q2. Niven number A number which is divisible by the sum of it’s digits. Example : 126 Sum of
the digits = 1+2+6=9 , so, 126 is divisible by 9 [2016]
Ans :
import [Link].*;
class Niven_Number {
public static void main(String args[]) {
Scanner sc=new Scanner([Link]);
[Link]("\nEnter number of terms:");
int n=[Link]();
int rem,sum=0;
int ncopy=n;
while(n!=0) {
rem=n%10 ;
sum=sum+rem;
n=n/10;
}
if(ncopy % sum == 0)
[Link](ncopy+ " is a Niven number");
else
[Link](ncopy +" is not a Niven number");
}
}
Q4. A tech number has even number of digits. If the number is split in two equal halves,
then the square of sum of these halves is equal to the number itself. Write a program to
generate and print all four digits tech numbers. [2019]
Example:
Consider the number 3025
Square of sum of the halves of 3025 = (30 + 25)2
= (55)2
= 3025 is a tech number.
Ans :
public class TechNumbers
{
public static void main(String args[]) {
for (int i = 1000; i <= 9999; i++) {
int secondHalf = i % 100;
int firstHalf = i / 100;
int sum = firstHalf + secondHalf;
if (i == sum * sum)
[Link](i);
}
}
}
Or, // Check the number is tech number or not.
if(digit%2==0)
{
num=n;
firstHalf=num % (int) [Link](10,digits/2);
lastHalf =num / (int) [Link](10,digits/2);
if([Link](firstHalf+lastHalf,2)==num)
{
[Link](n+ “ is a Tech Number”);
}
else
{
[Link](n+ “ is not a Tech Number”);
}
}
else
{
[Link](n+ “ is not a Tech Number”);
}
Q4. Automorphic number : (Automorphic number is the number which is contained in the
last digit(s) of its square.) [2010]
Example : 25 is an Automorphic number as its square is 625 and 25 is present as the
last two digits. 52=25, 62=36, 762=5776, 3762=141376
Ans :
import [Link];
public class Automorphic
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter number: ");
int num = [Link]();
int numCopy = num;
int sq = num * num;
int count = 0;
if (ld == numCopy)
[Link](numCopy + " is automorphic");
else
[Link](numCopy + " is not automorphic");
break;
default:
[Link]("Incorrect Choice");
break;
}
}
}
Q5. Write a program to accept a number and check and display whether it is
a Unique number or not using function.
(The number will be unique if it is positive integer and there are no repeated digits in the
number. In other words, a number is said to be unique if and only if the digits are not
duplicate.)
Example:
For example, 20, 56, 9863, 145, etc. are the unique numbers while 33, 121, 900, 1010, etc. are
not unique numbers
Ans :
import [Link];
public class UniqueNumber {
if (count == 0)
{
[Link]("The number is unique number.");
}
else
{
[Link]("The number is not unique number.");
}
}
}
Q6. Write a program to display the sum of even numbers and odd numbers separately from a
set of numbers entered by the user. The program terminates when the user enters any non-
numeric character.
import [Link].*;
{
Scanner in=new Scanner([Link]);
int n,s1=0,s2=0;
while([Link]())
{
[Link]("Enter integers to continue & an alphabet to terminate");
n=[Link]();
if(n%2==0)
s1=s1+n;
else
s2=s2+n;
Q7. Write a program to accept a number and check whether the given number is Happy number
or not.
(A happy number is a number which eventually reaches 1 when replaced by the sum of the
square of each digit.
For example, consider the number 320.
32 + 22 + 02 ⇒ 9 + 4 + 0 = 13
12 + 32 ⇒ 1 + 9 = 10
12 + 0 2 ⇒ 1 + 0 = 1
Hence, 320 is a Happy Number.)
import [Link];
if (num == 1)
[Link](n + " is a Happy Number.");
else
[Link](n + " is NOT a Happy Number.");
[Link]();
}
}
[Link] a program to input a number and print whether the number is a special number or
not.
(A number is said to be a special number, if the sum of the factorial of the digits of the number
is same as the original number). [2011]
Example:
145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145.
(Where ! stands for factorial of the number and the factorial value of a number is the product of all
integers from 1 to that number, example 5! = 1 * 2 * 3 * 4 * 5 = 120)
Or, Krishnamurti number is a number that is equal to the sum of factorial of its digits.
145=1!+4!+5!=1+(1*2*3*4)+(1*2*3*4*5)=1+24+120
Ans :
import [Link].*;
class Krishnamurti{
public static void main(String args []){
int s, n, i,t,f,j;
Scanner in=new Scanner([Link]);
}
if(t==s)
{
[Link]("Number is Krishnamurti");
}
else
{
[Link]("Number is not Krishnamurti");
}
}
}
Type : 05 - String
if (vowels == consonants)
[Link]("It is a Special String");
else
[Link]("It is not a Special String");
}
}
Q2. Write a Java program to count no. of letter in uppercase, letter in lower case, digit, space and
special character in the string given by user.
Or,
Define a class to accept a String and print if it is a Super String or not. A String is Super if the
number of uppercase letters are equal to the number of lowercase letters. [Use Character and
String methods only] [ICSE2025]
Example: “COmmITmeNt”
Number of uppercase letters = 5
Number of lowercase letters = 5
String is a Super String
import [Link].*;
class CountCharInString{
Q3. Write a program to accept a string in lower case and replace ‘e’ with ‘*’ in the given string.
Display the new string. [15]
Sample input : beautiful flower
Sample output : b*autiful flow*r
Ans :
import [Link];
public class CharReplace
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a string : ");
String str = [Link]();
str = [Link]();
String newStr = " ";
int len = [Link]();
for (int i = 0; i < len; i++)
{
char ch = [Link](i);
if ([Link](i) == 'e')
{
newStr = newStr + '*';
}
else
{
newStr = newStr + ch;
}
}
[Link]("Output : "+newStr);
}
}
Q4. Write a program in Java to enter a String. Display after converting case (Upper case to lower
and lower case to upper)
Sample Input : ComPuter AppiliCation
Sample Output : cOMpUTER aPPILIcATION
Ans :
Ans :
import [Link].*;
class LowerCaseToUpperCaseAndViceVersa{
int i,l,lc,uc=0;
char ch;
String st;
Scanner in=new Scanner([Link]);
else
[Link](ch);
}}
Or,
import [Link].*;
class LowerCaseToUpperCaseAndViceVersa {
int i,l,lc,uc=0;
char ch;
String st, newstr="";
Scanner in=new Scanner([Link]);
else
newstr=newstr+ch;
}
[Link](newstr);
}
}
Q5. Write a program in Java to enter a String and frame a word by joining all the first characters
of each word. Display the new word.
Sample Input : Rabindra Nath Tagore
Sample Output : RNT
Ans :
import [Link].*;
class RNT{
int i,len,;
int ch;
String st;
Scanner in=new Scanner([Link]);
Or,
import [Link].*;
class RNT{
int i,len;
int ch;
String st;
Scanner in=new Scanner([Link]);
int i,l;
int ch;
String st;
Scanner in=new Scanner([Link]);
Or,
import [Link].*;
class RNT{
int i,len;
int ch;
String st;
Scanner in=new Scanner([Link]);
[Link]("Enter a Name : ");
st=[Link]();
len=[Link]();
for(i=0;i<len;i++) // or, p=indexOf(‘ ’); s=lastIndexOf(‘ ’)
{
ch=[Link](i);
if (i==len-1||[Link](i+1)==' ')
[Link]([Link](i));
}
}
}
Q6. Write a program to accept a word and convert it into lower case, if it is in upper case.
Display the new word by replacing only the vowels with the letter following it. [2011]
Sample Input: Computer
Sample Output: cpmpvtfr
Answer
import [Link];
public class VowelReplace
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a word: ");
String str = [Link]();
str = [Link]();
String newStr = " ";
int len = [Link]();
if ([Link](i) == 'a' ||
[Link](i) == 'e' ||
[Link](i) == 'i' ||
[Link](i) == 'o' ||
[Link](i) == 'u') {
}
else {
newStr = newStr + ch;
}
}
[Link](newStr);
}
}
Q7. Write a program to input a sentence and convert it into uppercase and count and display the
total number of words starting with a letter 'A'. [2019]
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE
EVER CHANGING.
Sample Output: Total number of words starting with letter 'A' = 4
Answer
import [Link];
public class WordsWithLetterA
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
str = " " + str; //Add space in the begining of str
int c = 0;
int len = [Link]();
str = [Link]();
for (int i = 0; i < len - 1; i++) //or, for (int i = 0; i < len; i++)
{
if ([Link](i) == ' ' && [Link](i + 1) == 'A')
c++;
}
[Link]("Total number of words starting with letter 'A' = " + c);
}
}
Or, //Not adding space in the begining of str
import [Link];
public class WordsWithLetterA
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int c = 0;
int len = [Link]();
str = [Link]();
for (int i = 0; i < len - 1; i++) //or, for (int i = 0; i < len; i++)
{
if (i==0 && [Link](0) == 'A' ||[Link](i) == ' ' && [Link](i+1) == 'A')
// or, if (i==0 && [Link](0) == 'A' ||[Link](i) == 'A' && [Link](i-1) == ' ')
c++;
}
[Link]("Total number of words starting with letter 'A' = " + c);
}
}
8. Write a program to input a sentence and print the palindromic words in the sentence.
[TCA430]
Ans :
import [Link].*;
class PalindromicWord {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
String str, word="", wordrev="";
int i, l;
char ch;
[Link]("Enter a sentence : ");
str=[Link]();
str=str+" ";
l=[Link]();
for(i=0;i<l;i++)
{
ch=[Link](i);
if(ch!=' ')
{
word=word+ch;
wordrev=ch+wordrev;
}
else
{
if([Link](wordrev))
{
[Link](word);
}
word="";
wordrev="";
}
}
[Link] a program to input a sentence and display the word of the sentence that contains
maximum number of vowels.
Sample Input : HAPPY NEW YEAR
Sample Output : The word with maximum number of vowels : YEAR
Ans :
import [Link].*;
class MAxVowelWord {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
int i, l, count=0, maxCount=0;
char ch;
String word="", mWord="";
[Link]("Enter a sentence :");
String str = [Link]();
str=str+" ";
l=[Link]();
for( i=0;i<l;i++)
{
ch=[Link]([Link](i));
if(ch=='A' || ch=='E'|| ch=='I' || ch=='O' || ch=='U' )
{
count++;
}
if(ch==' ')
{
if(count>maxCount)
{
maxCount=count;
mWord=word;
}
word="";
count=0;
}
else{
word+=ch;
}
}
10. Write a program to accept a sentence and display longest token present in that sentence along
with its length.
Input : She is a beautiful girl.
Output :
Longest token is : beautiful
Length of longest token is : 9
import [Link].*;
class MaxWord {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
String s, w="", bw="";
int i, l;
char c;
[Link]("Enter a sentence : ");
s=[Link]();
s=s+" ";
l=[Link]();
for(i=0;i<l;i++)
{
c=[Link](i);
if(c!=' ')
w=w+c;
else
{
if([Link]()>[Link]())
{
bw=w;
}
w="";
}
}
[Link]("Longest token is : "+bw);
[Link]("Length of longest token is : "+[Link]());
}
}
Question: 11
A string is said to be symmetrical if it contains as many digits as it contains alphabets (upper or
lower case). Design a class that takes as input 6 strings and prints out for each whether the strings are
symmetric or not. For example :
Sayan (not symmetric)
Da22321vid (symmetric)
Rav3321i (symmetric)
Ajay112 (not symmetric)
Ans :
import [Link].*;
public class ArrayWordsShortig
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int i,j,n,l;
String s;
String arr[]=new String[20];
[Link]("Enter 6 strings : ");
for(i=0;i<6;i++)
arr[i]=[Link]();
for(i=0;i<6;i++){
s=arr[i];
int digcount=0,alcount=0;
l=[Link]();
for(j=0;j<l;j++)
{
if([Link](j)>='0' && [Link](j)<='9') // or, if([Link]([Link](j)))
{
digcount++;
}
if(([Link](j)>='a' && [Link](j)<='z')|| ([Link](j)>='A' && [Link](j)<='Z'))
// or, if([Link]([Link](j)))
{
alcount++;
}
}
if(digcount>=alcount)
[Link](s+ " is symmetric string " );
else
[Link](s+ " is not symmetric string ");
}}
Question : 12
Write a program to check unique word. (A word is called a Unique Word if no letter in the word is
repeated.)
import [Link];
public class UniqueWord
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a word : ");
String str = [Link]();
boolean isUnique = true;
int len = [Link]();
char ch = [Link](i);
if (!isUnique)
break;
}
if (isUnique)
[Link](str+ " is a unique word.");
else
[Link](str+ " is not a unique word.");
}
}
Question : 13
Write a program to accept a string and replace all vowels with next letter and consonants with
previous letter present in the alphabets in the given string.
Sample Input: hellow world
Sample Output: gfkkpv vpqkc
import [Link];
class Replace {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
String str, newStr =" ";
int len,i;
char ch;
// Accept input from the user
[Link]("Enter a String: ");
str = [Link]();
str = [Link]();
len = [Link]();
for (i = 0; i < len; i++) {
ch = [Link](i);
if ([Link](i) == 'a' ||
[Link](i) == 'e' ||
[Link](i) == 'i' ||
[Link](i) == 'o' ||
[Link](i) == 'u') {
// Replace vowel with the next letter in the alphabet
newStr=newStr+(char)(ch + 1);
}
else if ([Link](i) >='a ' && [Link](i) <='z '){
// Replace consonant with the previous letter in the alphabet
newStr = newStr+(char)(ch-1);
}
else {
// If it's not a letter, keep the character as it is
newStr = newStr+ch;
}
}
[Link]("Transformed String : "+newStr);
}
}
Or,
Define a class to accept a string and convert the same to uppercase, create and display the new
string by replacing each vowel by immediate next character and every consonant by the
previous character. The other characters remain the same. [SQP2025]
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024
import [Link];
Question :14
Sam designs a program to check the strength of a password. A strong password should satisfy the
following conditions: [CFPQ2024]
→length of the password should be atleast12 characters
→should at least have 4 uppercase letters, 4 lowercase letters, 2 digits, 2 special characters
Define a class accept the password and check whether the password is strong or not.
Ans :
import [Link];
class PasswordCheck {
String pass; // to store password
if ([Link](ch))
upper++;
else if ([Link](ch))
lower++;
else if ([Link](ch))
digit++;
else
special++;
}
// Check conditions
if ([Link]() >= 12 && upper >= 4 && lower >= 4 && digit >= 2 && special >= 2) {
[Link]("Strong Password");
} else {
[Link]("Weak Password");
}
}
// Main method
public static void main(String[] args) {
PasswordCheck obj = new PasswordCheck();
[Link]();
[Link]();
}
}
Q15. Write a program that encodes a word into Piglatin. To translate word into Piglatin word,
convert the word into uppercase and then place the first vowel of the original word as the start of
the new word along with the remaining alphabets. The alphabets present before the vowel being
shifted towards the end followed by "AY". [2013]
Sample Input 1: London
Output: ONDONLAY
Sample Input 2: Olympics
Output: OLYMPICSAY
Ans :
import [Link];
public class Piglatin
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter word: ");
String word = [Link]();
int len = [Link]();
word=[Link]();
String piglatin="";
int flag=0;
if(flag == 0)
{
piglatin = word + "AY";
}
[Link](word + " in Piglatin format is " + piglatin);
}
}
Question : 16
Create a class to take as input a sequence of words. Your task is to invert the sequence of entered
words. For example :
My Name is Harry
Output : Harry is Name My
Ans :
import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
int i, l;
char ch;
String word="", reverse="";
[Link]("Enter a line :");
String str = [Link]();
str=str+" ";
l=[Link]();
for( i=0;i<l;i++)
{
ch=[Link](i);
if(ch!=' ')
{
word=word+ch;
}
else{
reverse=word+" "+reverse;
word="";
}
}
Or,
import [Link];
[Link]();
}
}
Or,
import [Link].*;
class HelloWorld {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
int i;
[Link]("Enter a name of 3 words:");
String line = [Link]();
[Link] a program to input a sentence from the user and display the modified sentence by
reversing each word of the sentence.
Sample Input : Computer is fun
Output : retupmoC si nuf
Ans : import [Link].*;
class wordReverse{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
String s, s1="", w="";
char ch;
int i;
[Link]("Enter any sentence");
s= [Link]();
s=s+" ";
for(i=0;i<[Link]();i++)
{
ch=[Link](i);
if(ch!=' ')
w = ch + w;
else
{
s1=s1+" "+w;
w="";
}
}
[Link]([Link]());
}
}
Question : 18
Write a program n Java to accept two words and check whether they are Anagram or not.
Anagram : A word that is made with the combination of the letters present in the original word.
E.g. A word is FLOW and the other word is WOLF, which is formed with the combinations of the
letters present in the original word. Thus, FLOWand WOLE are Angrams.
Ans :
import [Link];
public class Angram
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int x, i, j, k=0, v=0,y;
String name, name1;
char b=0, c=0;
[Link]("Enter your first word");
name=[Link]();
x=[Link]();
for (i = 0; i <x; i++)
{
b = [Link](i); // int b;, sum=sum+b;
k=k+(int)b;
}
}
}
Or,
import [Link].*;
{
public static void main (String args[])
{
int p1, p2, i, j, t = 0;
String str1, str2;
char chr1, chr2;
Scanner in = new Scanner ([Link]);
[Link] ("Enter first word");
str1 = [Link]();
str1 = [Link]();
[Link] ("Enter second word");
str2 = [Link]();
str2 = [Link]();
p1 = [Link]();
p2 = [Link]();
if ( p1 == p2 )
{
for ( i = 0; i < p1; i++ )
{
chr1 = [Link](i);
t = 0;
for ( j = 0; j < p2; j++ )
{
chr2 = [Link](j);
if ( chr1 == chr2 )
t = 1;
}
if ( t == 0 )
break;
}
if ( t == 0 )
[Link] ( str1 + " and " + str2 + " are not Anagram words" );
else
[Link] ( str1 + " and " + str2 + " are Anagram words" );
}
else
[Link] ( "Wrong Input !! Re-enter words for Anagram" );
}
}
Q19. Write a program to accept any string from the user in lowercase and remove all the repeated
letters.
Sample Input : hello learner
Output : helo arn
Ans :
import [Link];
// Input
[Link]("Enter a string in lowercase: ");
String str = [Link]();
// Output
[Link]("Output: " + result);
[Link]();
}
}
Or,
import [Link].*;
class repeatRemove {
public static void main(String args[]) {
Scanner sc=new Scanner([Link]);
String s, sl="";
int i,l,j,k;
char ch;
[Link]("Enter any sentence");
s=[Link]();
l=[Link]();
char chl[]=new char[l];
for(i=0;i<l;i++)
{
chl[i]=[Link](i);
}
for(i=0;i<l-1;i++)
{
for(j=i+1;j<l;j++)
{
if (chl[i]==chl[j])
{
l=l-1;
for(k=j;k<l;k++)
{
chl[k]=chl[k+1];
}
}
}
}
for(i=0;i<l;i++)
{
sl=sl+chl[i];
}
[Link]("The modified sentence is\n"+sl);
}
}
Type : 06 - Fuanction overloading, pattern, sum of series, etc
// Generating pattern
[Link](4, 5);
Ans :
public class Overloading
{
public void num_calc(int num, char ch)
{
if (ch == 's')
{
double square = [Link](num, 2); //or, num*num
[Link]("The square of the number= " + square);
}
else
{
double cube = [Link](num, 3); // or, num*num*num
[Link]("The cube of the number= " + cube);
}
}
public void num_calc(int a, int b, char ch)
{
if (ch == 'p')
{
int product = a * b;
[Link]("The product of the numbers= " + product);
}
else
{
int sum = a + b;
[Link]("The sum of the number " + sum);
}
}
public void num_calc(String str1, String str2)
{
if ([Link](str2))
{
[Link]("Two strings are equal");
}
else
{ [Link]("Two strings are not equal");
}
}
public static void main(String args[])
{
Overloading ob=new Overloading();
ob.num_calc (7, ‘s’);
ob.num_calc (5, 2, ‘p’);
ob.num_calc (“Sanjay”, “Sumana”);
}
Ans :
import [Link];
int transform(int n) {
int sum = 0;
while (n > 0) {
int d = n % 10;
sum += d;
n /= 10;
}
return sum;
}
void transform(String s) {
String str = [Link]();
[Link](str);
}
[Link]();
[Link]("Enter a string: ");
String str = [Link]();
[Link](str);