0% found this document useful (0 votes)
4 views47 pages

Programming Types

The document provides Java programming exercises that involve creating classes for banking, transportation, EMI calculations, sorting algorithms, and matrix operations. It includes detailed specifications for each task, such as member variables and methods, along with sample code implementations. The exercises cover various programming concepts including input handling, calculations, and data display using arrays and classes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views47 pages

Programming Types

The document provides Java programming exercises that involve creating classes for banking, transportation, EMI calculations, sorting algorithms, and matrix operations. It includes detailed specifications for each task, such as member variables and methods, along with sample code implementations. The exercises cover various programming concepts including input handling, calculations, and data display using arrays and classes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Type : 01 - Description based program

Q1. Define a class with the following specifications: [SQP2025]


Class name: Bank
Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amount
Member methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:
Time in (Years) Rate %
Upto 1Ú2 9
> 1Ú2 to 1 year 10
> 1 to 3 years 11
> 3 years 12
a=p(1+r100)na=p(1+100r)n
void display () — display the details in the given format.
Principal Time Rate Amount

XXX XXX XXX XXX


Write the main method to create an object and call the above methods.

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]);

[Link]("Enter principal amount: ");


p = [Link]();

[Link]("Enter time period in years: ");


n = [Link]();
}

void calculate() {
if (n <= 0.5) {
r = 9;
} else if (n <= 1) {
r = 10;
} else if (n <= 3) {
r = 11;
} else {
r = 12;
}

a = p * [Link](1 + (r / 100), n);


}

void display() {
[Link]("Principal\tTime\tRate\tAmount");
[Link](p + "\t" + n + "\t" + r + "\t" + a);
}

public static void main(String args[]) {


Bank b = new Bank();
[Link]();
[Link]();
[Link]();
}
}

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);
}

public static void main(String[] args) {


Atransport obj=new Atransport();
[Link]();
[Link]();
[Link]();
}
}

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.

Print the details as follows:


Purchase amount:
Rate of interest:
Amount with interest:
EMI:
Define a class to accept the purchase amount and the number of years of the tenure, calculate and
print the details as per the above specifications.
Ans :
import [Link];

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)

// Method to input values


void input() {
Scanner sc = new Scanner([Link]);

[Link]("Enter purchase amount: ");


p = [Link]();

[Link]("Enter number of years: ");


n = [Link]();
}

// Method to calculate EMI


void calculate() {
// Decide rate of interest
if (p < 20000)
r = 12;
else
r = 15;

// Calculate amount using formula


amount = p * [Link]((1 + r / 100), n);

// Convert to EMI (months = years × 12)


int months = n * 12;
emi = [Link](amount / months);
}

// Method to display details


void display() {
[Link]("\nPurchase amount: " + p);
[Link]("Rate of interest: " + r + "%");
[Link]("Amount with interest: " + amount);
[Link]("EMI: " + emi);
}
// Main method
public static void main(String[] args) {
EMI obj = new EMI();
[Link]();
[Link]();
[Link]();
}
}
Type : 02 - Single Dimensional Array :

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];

[Link]("Enter those " + n + " elements=>");


for(i=0;i<n;i++)
{
arr[i]=[Link]();
}

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]("\n The array sorted in ascending order is :\n");


for(i=0;i<n;i++)
{
[Link](arr[i]+" ");
}
}
}

[Link] Sort :

import [Link].*;
public class SelectionSort
{
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];

[Link]("Enter those " + n + " elements=>");


for(i=0;i<n;i++)
{
arr[i]=[Link]();
}

for (i = 0 ;i<n-1; i++){


int min = i;

for (j = i+1; j<n; j++){


if (arr[j] < arr[min]){
min = j;
}
}
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}

[Link]("\n The array sorted in ascending order is :\n");


for(i=0;i<n;i++)
{
[Link](arr[i]+" ");
}
}
}

[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]();
}

[Link]("Enter locations of seven wonders");


for(i=0;i<7;i++)
{
locn[i]=[Link]();
}
[Link]("Enter a country name to be searched");
city=[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];

public class Sum {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input number of rows and columns


[Link]("Enter the number of rows (m): ");
int m = [Link]();
[Link]("Enter the number of columns (n): ");
int n = [Link]();

// Declare the matrix


int[][] matrix = new int[m][n];

// Input numbers of the matrix


[Link]("Enter the number of the matrix row by row:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = [Link]();
}
}

// Display numbers of the matrix


[Link]("The numbers of the matrix are :"); //Or,“The matrix elements are :”
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}

// Display the sum of elements row-wise


for (int i = 0; i < m; i++) {
int rowSum = 0;
for (int j = 0; j < n; j++) {
rowSum += matrix[i][j]; // Sum the elements in the row
}
[Link]("Sum of elements in row " + (i + 1) + ": " + rowSum);
}

// Display the sum of elements column-wise


for (int i = 0; i < m; i++) {
int colSum = 0;
for (int j = 0; j < n; j++) {
colSum += matrix[j][i]; // Sum the elements in the row
}
[Link]("Sum of elements in column " + (i + 1) + ": " + colSum);
}

}
}

Q2. Write a program to input elements in an array of size mm 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]();

}
}

[Link]("Original Array : ");


for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
[Link](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;

// Input elements in the 4x4 matrix


[Link]("Enter elements of the 4x4 matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = [Link]();
}
}

// Calculate sum excluding diagonal elements


for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
// Skip main diagonal (i == j) and secondary diagonal (i + j == 3)
if (i != j && (i + j) != 3) {
sum += matrix[i][j];
}
}
}

// Display the matrix


[Link]("Matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
[Link](matrix[i][j] + "\t");
}
[Link]();
}

// Display result
[Link]("Sum of the elements except diagonal elements = " + sum);
}
}

Q3. Write a program to create a two-dimensional array of size 23 and print all the odd
numbers in the array.
[Link] a program to create an array of size 23 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]();
}
}

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


for(int j = 0; j < 4; j++){
sum += a[i][j] * a[i][j];
}
}

double root = [Link](sum);


[Link]("NORM = " + root);
}
}
Type : 04 - Number logic program

[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");

}
}

Q3. Write a program to check a number is palindrome or not.


(A palindrome number is a number (such as 16461) that remains the same when its digits
are reversed)
Ans :
import [Link];
class PalindromeExample{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
int n,r,sum=0,temp;
[Link]("Enter an integer number=");
n=[Link]();
temp=n;
while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
[Link](temp+" is a palindrome number");
else
[Link](temp+" is not a palindrome 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.

int n, num, firstHalf, lastHalf, digits=0;


Scanner sc= new Scanner([Link]);
[Link](“Enter a numer=”);
n=[Link]();
num=n;
while(num>0)
{
digits++;
num=num/10;
}

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;

//Count the number of digits in num


while(num > 0) {
count++;
num /= 10;
}
// Extract the last d digits from square of num
int ld = (int)(sq % [Link](10, count));

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 {

public static void main(String[] args)


{
int number, r1, r2, num1, num2, count = 0;
Scanner sc = new Scanner([Link]);

[Link]("Enter the number you want to check: ");


number = [Link]();

//num1 and num2 are temporary variable


num1 = number;

//iterate over all digits of the number


while (num1 > 0)
{
//detrmins the last digit of the number
r1 = num1 % 10;
num2=num1/10;

while (num2 > 0)


{
//finds the last digit
r2 = num2 % 10;
//comparing the last digit
if (r1 == r2)
{
//increments the count variable by 1
count++;
}
//removes the last digit from the number
num2 = num2 / 10;
}
//removes the last digit from the number
num1 = num1 / 10;
}

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].*;

public class SumOfOddEvent

public static void main(String args[])

{
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;

[Link]("The program terminates.");

[Link]("The sum of even numbers =" +s1);


[Link]("The sum of odd numbers =" +s2);
}
}

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];

public class HappyNumber {


public static void main(String args[]) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a number: ");


int n = [Link]();
int num = n; // store original number

int sum, digit;

// Repeat until number becomes 1 (Happy) or 4 (Unhappy loop)


while (num != 1 && num != 4) {
sum = 0;

// find sum of squares of digits


while (num > 0) {
digit = num % 10;
sum = sum + (digit * digit);
num = num / 10;
}

num = sum; // update number


}

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]);

[Link]("Enter any positive numner: ");


n=[Link]();
t=n;
s=0;
f=1;
while((n%10)!=0)
{
i=n%10;
for(j=1;j<i;j++)
{
f=f*j;
}
s=s+f;
n=n/10;
f=1;

}
if(t==s)
{
[Link]("Number is Krishnamurti");
}
else
{
[Link]("Number is not Krishnamurti");
}
}
}
Type : 05 - String

Q1. Define a class to accept a string. Check if it is a Special String or not.


A String is Special if the number of vowels equals to the number of consonants. [IMP2025]
Example: MINE
Number of vowels = 2
Number of Consonants = 2
Ans :
import [Link];

public class SpecialString {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a string: ");


String str = [Link]().toUpperCase();

int vowels = 0, consonants = 0;


int len = [Link]();

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


char ch = [Link](i);
if (ch >= 'A' && ch <= 'Z') {
if (ch == 'A'
|| ch == 'E'
|| ch == 'I'
|| ch == 'O'
|| ch == 'U')
vowels++;
else
consonants++;
}
}

[Link]("Number of vowels = " + vowels);


[Link]("Number of consonants = " + consonants);

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{

public static void main(String args []){

int i,l,lc=0,uc=0, space=0, digit=0,spchar=0;


int ch;
String st;
Scanner in=new Scanner([Link]);

[Link]("Enter any sentence ");


st=[Link]();
l=[Link]();
for(i=0;i<l;i++)
{
ch=[Link](i);
if(ch>=97 && ch<=122) // or if(ch>='a' && ch<='z') // if([Link](ch))
lc=lc+1;
else if(ch>=65 && ch<=90) or, else if(ch>='A' && ch<='Z') // else if([Link](ch))
uc=uc+1;
else if(ch==32) // or else if(ch==' ')
space++;
else if(ch>=48 && ch<=57) // or, else if(ch>='0' && ch<='9') // else if ([Link](ch))
digit++;
else
spchar++;
}

[Link]("Lowercase characters ="+lc);


[Link]("Uppercase characters ="+uc);
[Link]("Space ="+space);
[Link]("Digit ="+digit);
[Link]("Special characters ="+spchar);
}}

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{

public static void main(String args []){

int i,l,lc,uc=0;
char ch;
String st;
Scanner in=new Scanner([Link]);

[Link]("Enter a line ");


st=[Link]();
l=[Link]();
for(i=0;i<l;i++)
{
ch=[Link](i);
if(ch>='a' && ch<='z'){
lc=ch-32;
[Link]((char)lc);}
else if(ch>='A' && ch<='Z'){
uc=ch; //or, uc=ch+32
uc=uc+32;
[Link]((char)uc);
}

else
[Link](ch);

}}
Or,
import [Link].*;
class LowerCaseToUpperCaseAndViceVersa {

public static void main(String args []){

int i,l,lc,uc=0;
char ch;
String st, newstr="";
Scanner in=new Scanner([Link]);

[Link]("Enter a line ");


st=[Link]();
l=[Link]();
for(i=0;i<l;i++)
{
ch=[Link](i);
if(ch>='a' && ch<='z'){
newstr=newstr+[Link](ch);
}

else if(ch>='A' && ch<='Z'){


newstr+=[Link](ch);
}

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{

public static void main(String args []){

int i,len,;
int ch;
String st;
Scanner in=new Scanner([Link]);

[Link]("Enter a Name : ");


st=[Link]();
len=[Link]();
[Link]([Link](0));
for(i=0;i<len;i++) // or, p=indexOf(‘ ’); s=lastIndexOf(‘ ’)
{
ch=[Link](i);
if (ch==' ')
[Link]([Link](i+1));
}
}
}

Or,
import [Link].*;
class RNT{

public static void main(String args []){

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==0||[Link](i-1)==' ') //or, if (i==0||[Link](i-1)==32)
[Link]([Link](i));
}
}
}
Or,
Write a program in Java to enter a String and frame a word by joining all the last characters of
each word. Display the new word.
Sample Input : Rabindra Nath Tagore
Sample Output : AHE
Ans :
import [Link].*;
class RNT{

public static void main(String args []){

int i,l;
int ch;
String st;
Scanner in=new Scanner([Link]);

[Link]("Enter a Name : ");


st=[Link]();
st=[Link]()
l=[Link]();
for(i=0;i<l;i++) // or, p=indexOf(‘ ’); s=lastIndexOf(‘ ’)
{
ch=[Link](i);
if (ch==' ')
[Link]([Link](i-1));
}
[Link]([Link](l-1));
}
}

Or,
import [Link].*;
class RNT{

public static void main(String args []){

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]();

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


char ch = [Link](i);

if ([Link](i) == 'a' ||
[Link](i) == 'e' ||
[Link](i) == 'i' ||
[Link](i) == 'o' ||
[Link](i) == 'u') {

char nextChar = (char)(ch + 1);


newStr = newStr + nextChar;

}
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;
}
}

[Link]("The word with maximum number of vowels :"+mWord);


}
}

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]();

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

char ch = [Link](i);

for (int j = i + 1; j < len; j++) {


if (ch == [Link](j)) {
isUnique = false;
break;
}
}

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];

public class StringConvert


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
str = [Link]();
int l = [Link]();

String res = "";

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


char ch = [Link](i);
if ("AEIOU".indexOf(ch) != -1) {
res += (char)(ch + 1);
}
else if ([Link](ch)) {
res += (char)(ch - 1);
}
else {
res += ch;
}
}
[Link]("Output String:");
[Link](res);
}
}

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

// Method to input password


void input() {
Scanner sc = new Scanner([Link]);
[Link]("Enter password: ");
pass = [Link]();
}

// Method to check strength


void check() {
int upper = 0, lower = 0, digit = 0, special = 0;

// Count different types of characters


for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);

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;

for(int i = 0; i < len; i++)


{
char x = [Link](i);
if(x=='A' || x=='E' || x=='I' || x=='O' || x=='U')
{
piglatin=[Link](i) + [Link](0,i) + "AY";
flag=1;
break;
}
}

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="";
}
}

[Link]("The reverse word :"+reverse);


}
}

Or,
import [Link];

public class InvertWords {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter a line : ");


String str=[Link]();
sc=new Scanner(str);

String sentence = "";

// Read all words one by one


while ([Link]()) {
String word = [Link]();
sentence = word + " " + sentence; // build reverse order
}

[Link]("Output: " + [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]();

int lastSpaceIdx = [Link](' ');


[Link]([Link](lastSpaceIdx + 1));
for( i=lastSpaceIdx;i>0;i--)
{
if([Link](i)==' ')
{
[Link](" ");
[Link]([Link](i, lastSpaceIdx));
lastSpaceIdx=i;
}
}
//[Link](" ");
[Link](" "+[Link](i, lastSpaceIdx));
}
}

[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;
}

[Link]("Enter your second word");


name1=[Link]();
y=[Link]();
for (j = 0; j <y; j++)
{
c = [Link](j); // int b;, sum=sum+b;
v=v+(int)c;
}
if(k==v)
[Link]("The words are Anagram");
else
[Link]("The words are not Anagram");

}
}

Or,

import [Link].*;

public class Anagram

{
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];

public class RemoveRepeatedLetters {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input
[Link]("Enter a string in lowercase: ");
String str = [Link]();

String result = "";

// Go through each character


for (int i = 0; i<[Link](); i++) {
char ch = [Link](i);
boolean repeat = false;

// Compare with all previous characters


for (int j = 0; j <i; j++) {
if ([Link](j) == ch) {
repeat = true;
break;
}
}

// If not repeated earlier, add to result


if (!repeat) {
result = result + ch;
}
}

// 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

Q1. Define a class to overload the method perform as follows: [ICSE2024]


double perform (double r, double h) — to calculate and return the value of curved surface area of cone
CSA=πrl l=r2+h2
void perform (int r, int c) — Use NESTED FOR LOOP to generate the following format
r = 4, c = 5
output
12345
12345
12345
12345
void perform (int m, int n, char ch) — to print the quotient of the division of m and n if ch is Q else print
the remainder of the division of m and n if ch is R
Answer
import [Link];

public class OverloadPerform


{
double perform(double r, double h) {
double l = [Link]((r * r) + (h * h));
double csa = [Link] * r * l;
return csa;
}

void perform(int r, int c) {


for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
[Link](j + " ");
}
[Link]();
}
}

void perform(int m, int n, char ch) {


if (ch == 'Q') {
int q = m / n;
[Link]("Quotient: " + q);
} else if (ch == 'R') {
int r = m % n;
[Link]("Remainder: " + r);
} else {
[Link]("Invalid Character!");
}
}

public static void main(String[] args) {


OverloadPerform mo = new OverloadPerform();

// Calculating CSA of a cone


double csa = [Link](3.0, 4.0);
[Link]("Curved Surface Area of Cone: " + csa);

// Generating pattern
[Link](4, 5);

// Printing quotient or remainder


[Link](20, 6, 'Q');
[Link](20, 6, 'R');
}
}

Q2. Design a class to overload a function num_cal() as follows : [ICSE 2009]


i) void num_calc (int num, char ch) with one integer argument and one character argument,
computes the square of integer argument if choice ch is ‘s’ otherwise finds its cube.
ii) void num_calc(int a, int b, char ch) with two integer arguments and one character
argument. It computes the product of integer arguments if ch is ‘p’ else adds the integers.
iii) void num_calc(String s1, String s2) with two string arguments, which prints whether the
strings are equal or not.

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”);
}

Q3. Define a class to overload the method transform as follows: [IMP2025]


int transform(int n) – to return the sum of the digits of the given number
Example: n = 458
output : 17
void transform(String s) – to convert the given String to upper case and print
Example: if S = “Blue”
Output : BLUE
void transform (char ch) – to print the character ch in 3 rows and 3 columns using nested loops.
Example: if ch = ‘@’
Output :
@@@
@@@
@@@

Ans :
import [Link];

public class OverloadTransform {

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);
}

void transform(char ch) {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
[Link](ch);
}
[Link]();
}
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
OverloadTransform obj = new OverloadTransform();

[Link]("Enter an integer: ");


int num = [Link]();
[Link]("Sum of digits: " + [Link](num));

[Link]();
[Link]("Enter a string: ");
String str = [Link]();
[Link](str);

[Link]("Enter a character: ");


char ch = [Link]().charAt(0);
[Link](ch);
}
}

You might also like