0% found this document useful (0 votes)
5 views

Computer Record Final 2025

The document contains multiple Java programs demonstrating various concepts such as checking special numbers, pattern printing, calculating series sums, identifying lucky numbers, ASCII values, linear search, matrix transposition, prime number generation, leap year checking, method overloading, and swapping numbers. Each program includes source code, sample inputs and outputs, and variable descriptions. The programs serve as educational examples for understanding basic programming constructs and algorithms.

Uploaded by

ramji.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Computer Record Final 2025

The document contains multiple Java programs demonstrating various concepts such as checking special numbers, pattern printing, calculating series sums, identifying lucky numbers, ASCII values, linear search, matrix transposition, prime number generation, leap year checking, method overloading, and swapping numbers. Each program includes source code, sample inputs and outputs, and variable descriptions. The programs serve as educational examples for understanding basic programming constructs and algorithms.

Uploaded by

ramji.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Special Number

Date:8.3.24
Program 1
A program to read and check the number is Special Number
or not

Source Code:
import java.util.*;
public class Special
,
public static void main()
,
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
int sum=0,prod=1,t=n;
while(t>0)
,
sum+=t%10;
prod*=t%10;
t/=10;
-
if(n == sum + prod)
System.out.println("It is a special number");
else
System.out.println("It is not a special number");

-
-

Sample: 1
Input:
123
Output:
It is not a special number

Sample: 2
Input:
19
Output:
It is a special number

Variable Description
Variable Datatype Description
n Int Receive number
from scanner
t Int
sum Int To store sum
prod int To store product
Pattern Printing
Date:21.3.24
Program 2
A program to display the following pattern using nested for
Loop
&&&&&
&###&
&###&
&###&
&&&&&
Source code:

public class Pattern


,
public static void main()
,
inti,j;
for(i=0;i<5;i++)
,
for(j=0;j<5;j++)
if(i==0 || j==0 || i == 4 || j == 4)
System.out.print("&");
else
System.out.print("#");
System.out.println();
-
-
-

Sample: 1
Input:

Input not needed.

Output:

&&&&&
&###&
&###&
&###&
&&&&&

Variable Description
Variable Datatype Description
i Int For loop
j int For loop
Sum of the Series
Date 25.3.24
Program 3
A program to read the values of x and n from the user.
Calculate and print the sum of the following series using
nested loop
Source Code:
import java.util.*;
public class Sum_Series
,
public static void main()
,
Scanner sc = new Scanner (System.in);
System.out.print("Enter value of n");
int n = sc.nextInt();
double sum =0,Tsum =0; int f;
for(int i =1;i<=n;i++)
, f=1; Tsum =0;
for(int j=1;j<=i;j++)
,
f*=j;
Tsum = sum +j;
-
sum+= Tsum/f;
-
System.out.print("The sum of the series:"+sum);
-
-

Sample: 1
Input:
Enter value of n
3
Output:
The sum of the series:5.166666666666667

Sample: 2
Input:
Enter value of n
2
Output:
The sum of the series:2.5
Variable Description
Variable Datatype Description
n Int Input

sum double Sum of series


Tsum Int Temporary sum

f Int Factorial value


used for each
term
i Int For loop
j Int For loop
Lucky Number
Date 28.3.24
Program 4
Write a program in java to input a number and check whether
it is a lucky number or not

Source code:
import java.util.*;
public class Lucky
,
public static void main()
,
Scanner sc =new Scanner(System.in);
System.out.println("Enter a number");
int n =sc.nextInt();
intt,sum=0;
t=n;
while(t>=10)
,
sum=0;
while(t>0)
,
sum+=t%10;
t/=10;
-
t=sum;
switch(t)
,
case 1: System.out.println("It is a lucky number");
break;
default: System.out.println("It is not a lucky number");
-
-
-

Sample: 1
Input:
Enter a number
19
Output:
It is a lucky number

Sample: 2
Input:
Enter a number
123
Output:
It is not a lucky number
Variable Description
Variable Datatype Description
n Int Input
t Int Temporary value
sum int To store sum
Character and ASCII code
Date: 4.4.24
Program 5
Declare a Single Dimensional character array of size 10.
Display the character and its ASCII Code.

Source Code:
import java.util.*;
public class ASCII
,
public static void main()
,
Scanner sc = new Scanner (System.in);
char a*+ = new char *10+;
System.out.println("Enter 10 charectars");
for(int i =0; i<10;i++)
a*i+ = sc.next().charAt(0);

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


System.out.println("Charectar:"+a*i++" ASCII value:"+(int)a*i+);
-
-
Sample: 1
Input:
Enter 10 characters
ABCDEFGHIJ
Output:
Character: A ASCII value: 65
Character: B ASCII value: 66
Character: C ASCII value: 67
Character: D ASCII value: 68
Character: E ASCII value: 69
Character: F ASCII value: 70
Character: G ASCII value: 71
Character: H ASCII value: 72
Character: I ASCII value: 73
Character: J ASCII value: 74

Sample: 2
Input:
Enter 10 characters
1234567890
Output:

Character: 1 ASCII value: 49


Character: 2 ASCII value: 50
Character: 3 ASCII value: 51
Character: 4 ASCII value: 52
Character: 5 ASCII value: 53
Character: 6 ASCII value: 54
Character: 7 ASCII value: 55
Character: 8 ASCII value: 56
Character: 9 ASCII value: 57
Character: 0 ASCII value: 48
Variable Description
Variable Datatype Description
a*+ Char Character array
i int For loop
Linear Search
Date: 15.4.24
Program 6
Declare a SDA of size “n” to store long data type. Read “n”
numbers and a number to be searched for from the user.
Count the number of occurrences
Source Code:
import java.util.*;
public class Linear
,
public static void main()
,
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
int n = sc.nextInt();
long a *+ = new long*n+;
System.out.println("Enter "+n+" numbers");
for(int i =0;i<n;i++)
a*i+ = sc.nextLong();
System.out.println("Enter search number");
long s = sc.nextLong();
int c=0;
for(int i =0;i<n;i++)
if(s == a*i+)
,
c++;
-
if(c==0)
System.out.println("There are no occurrences");
if(c>0)
System.out.println("There are "+c+" occurrences");

-
-

Sample: 1
Input:

Enter size of array


5
Enter 5 numbers
10 20 30 40 50
Enter search number
30
Output:

There are 1 occurrences


Sample: 2
Input:

Enter size of array


6
Enter 6 numbers
15 25 15 35 15 45
Enter search number
15

Output:

There are 3 occurrences

Variable Description
Variable Datatype Description
n Int Size of array
a*+ Long Array
i Int For loop
s Int Search number
c int Counter
Transpose of a square Matrix
Date: 26.4.24
Program 7
Declare a DDA integer array of size n x n. Display the original
matrix, and its transpose in a matrix form. Find the sum of
the border elements
Source Code:
import java.util.*;

public class Transpose


,
public static void main()
,
Scanner sc = new Scanner (System.in);

System.out.println(" Enter the size of array");


int n=sc.nextInt();
inti,j,sum=0;
int a*+*+ =new int *n+*n+;

System.out.println("Enter the numbers");


for(i=0;i<n;i++)
for(j=0;j<n;j++)
a*i+*j+=sc.nextInt();
System.out.println("Original Matrix");
for(i=0;i<n;i++)
,
for(j=0;j<n;j++)
System.out.print(a*i+*j+);

System.out.println("");
-
System.out.println("Transpose form");
for(j=0;j<n;j++)
,
for(i=0;i<n;i++)
System.out.print(a*i+*j+);

System.out.println("");
-
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==0||j==0||i==n-1||j==n-1)
sum+=a*i+*j+;
System.out.println("Sum of the border elements ="+sum);
-
-

Sample: 1
Input:

Enter the size of array


2
Enter the numbers
12
34
Output:

Original Matrix
12
34

Transpose form
13
24

Sum of the border elements = 10


Sample: 2
Input:

Enter the size of array


3
Enter the numbers
123
456
789
Output:

Original Matrix
123
456
789

Transpose form
147
258
369

Sum of the border elements = 40

Variable Description
Variable Datatype Description
n Int Dimension of the
square matrix.
a*+ Int Matrix entered by
the user.
i Int For loop
j Int For loop
sum int Sum of border
elements
Prime Fill
Date: 14.6.24
Program 8
Declare and define a method prime() which takes an integer,
returns Boolean depends on the given number is prime or
not

Source code:

public class Prime


,
boolean prime(int n)
,
int I;
if(n<=1)
return false;
for(i=2;i<n;i++)
if(n%i==0)
return false;

return true;
-
public void main()
,
int a*+= new int *10+;
int c=0,b=100;
while(c<10)
,
if(prime(b))
,
a*c+=b;
c++;
-
b++;
-
for(int j=0;j<10;j++)
System.out.println(a*j+);
-
-
Sample: 1
Input:
No input required for this program
Output:

101
103
107
109
113
127
131
137
139
149

Variable Description
Variable Datatype Description
n Int Boolean value
i Int For loop
a*+ Int Array to store
prime numbers.
b Int Current number to
check if prime or
not.
c Int Counter for prime
numbers.
j int For loop
Leap Year
Date: 28.6.24
Program 9
Declare and define a static method which takes an integer
and returns Boolean result

Source Code;
import java.util.*;
public class LeapYear ,

public static booleanisLeapYear(int y) ,


if (y % 4 == 0) ,
if (y % 100 == 0) ,

if (y % 400 == 0) ,
return true;
- else ,
return false;
-
- else ,
return true;
-
- else ,
return false;
-
-
public static void main(String*+ args) ,

LeapYear check = new LeapYear();


Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();
if(check.isLeapYear(n))

System.out.println("It is a leap year");


else
System.out.println("It is not a leap year");
-
-

Sample: 1
Input:
Enter a year
2000
Output:
It is a leap year

Sample: 2
Input:
Enter a year
1900
Output:
It is not a leap year

Variable Description
Variable Datatype Description
y Int Method for
checking
whether it’s a
leap year or not
n int Year entered
Method Overloading
Date: 5.7.24
Program 10
Define a overloaded method Number()
void Number(int num, int d) – To count and display the
frequency of a digit in a number
void Number(int n) – To find and display the sum of even
digits present in the number
void Number(char c, int n) – to print the character, in a
triangular format with n number of lines
Complete the main function as a menu driven program to
invoke the above methods

Source Code:
public class Method_overload
,
void Number(int n, int d)
,
int t=n,c=0;
while(t>0)
,
if(t%10==d)
c++;
t/=10;
-
System.out.println("Number "+n+" Frequency "+c);
-

void Number(int n)
,
int t=n,sum=0;
while(t>0)
,
if(t%2==0)
sum+=t%10;
t/=10;
-
System.out.println("The sum of even numbers is "+sum);
-
void Number(char c,int n)
,
inti,j;
for(i=0;i<=n;i++)
,
for(j=0;j<=i;j++)
System.out.print(c);

System.out.println();
-
-

public void main()


,
Number(234567);
Number(1233445555,5);
Number('$',5);
-
-

Sample: 1
Input:

Number(234567);
Output:

The sum of even numbers is 12


Sample: 2
Input:

Number(1233445555, 5);
Output:

Number 1233445555 Frequency 5

Variable Description
Variable Datatype Description
n Int Input number to
analyze.
d Int Digit to find the
frequency of in the
number.
t Int Temporary number
used to process the
original number n
c Int Frequency of the
digit.
Sum Int Sum of even digits
of the number n
i Int For loop
j int For loop
Swapping of two numbers
Date: 12.7.24
Program 11
Swap_int is a overloaded function has the following
definition. Define the methods as given
void swap_int(int x,int y): swaps the two parametric variables
void swap_obj(swap x, swap y): swaps the two integers data
in an objects
Complete the main method which establish the concept of
call by value and call by reference
Source Code:
import java.util.Scanner;
class swap
,
int a;
public void main()
,
Scanner sc = new Scanner(System.in);
swap o1 = new swap();
swap o2 = new swap();
System.out.print("Enter the first number: ");
int x = sc.nextInt();
o1.a = x;
System.out.print("Enter the second number: ");
int y = sc.nextInt();
o2.a = y;
System.out.println("\n\n CALL by Value");
System.out.println("Before swapping in main():");
System.out.println("x = " + x + ", y = " + y);
swap_int(x, y);
System.out.println("After swapping in main() :");
System.out.println("x = " + x + ", y = " + y);
System.out.println("\n\n CALL by Reference");
System.out.println("Before swapping in main() : ");
System.out.println("x = " + o1.a + ", y = " + o2.a);
swap_obj(o1,o2);
System.out.println("After swapping in main() :");
System.out.println("x = " + o1.a + ", y = " + o2.a);
-
public void swap_int(int a, int b)
,
System.out.println("Values of x and y in Method before
swapping ");
System.out.println("x = " + a + ", y = " + b);
int temp = a;
a = b;
b = temp;
System.out.println("Values of x and y in Method after
swapping ");
System.out.println("x = " + a + ", y = " + b);
-
public void swap_obj(swap m, swap n)
,
System.out.println("Values of x and y in Method before
swapping ");
System.out.println(" x = " + m.a + ", y = " + n.a);
int temp = m.a;
m.a = n.a;
n.a = temp;
System.out.println("Values of x and y in Method after
swapping ");
System.out.println(" x = " + m.a + ", y = " + n.a);
-
-
Sample: 1
Input:

Enter the first number: 10


Enter the second number: 20
Output:

CALL by Value
Before swapping in main():
x = 10, y = 20
Values of x and y in Method before swapping
x = 10, y = 20
Values of x and y in Method after swapping
x = 20, y = 10
After swapping in main():
x = 10, y = 20

CALL by Reference
Before swapping in main():
x = 10, y = 20
Values of x and y in Method before swapping
x = 10, y = 20
Values of x and y in Method after swapping
x = 20, y = 10
After swapping in main():
x = 20, y = 10
Sample: 2
Input:

Enter the first number: 5


Enter the second number: 15
Output:

CALL by Value
Before swapping in main():
x = 5, y = 15
Values of x and y in Method before swapping
x = 5, y = 15
Values of x and y in Method after swapping
x = 15, y = 5
After swapping in main():
x = 5, y = 15

CALL by Reference
Before swapping in main():
x = 5, y = 15
Values of x and y in Method before swapping
x = 5, y = 15
Values of x and y in Method after swapping
x = 15, y = 5
After swapping in main():
x = 15, y = 5
Variable Description
Variable Datatype Description
a int Value before
swapping
x int First integer to be
swapped.
y int Second integer to
be swapped.
b int Value before
swapping
temp int Temporary variable
used for swapping.
Pure and Impure Methods
Date: 26.7.24
Program 12
Define the below Methods to apply the concept of pure and
impure methods
Pronic(),Selection()
Define the below methods to apply the concept of pure and
impure methods
Pronic() which takes a integer and returns boolean result true
if the number is Pronic, otherwise false. Pronic number is the
number which is the product of two consecutive integers)
ii. Selection() which takes a Character array and arranges the
elements in alphabetical order. Complete the main method as
a menu driven program to invoke the above methods. Read
the data and printing the output should be done in main
method.

Source Code:

importjava.util.*;

class Pure_Impure
,
booleanPronic(int n)
,
int i;
for(i=1;i<n;i++)
if(i*(i+1)==n)
,
return true;
-
return false;
-

public void Selection(char*+ arr)


,
int n = arr.length;
System.out.println("Before Arranging");
for (int i = 0;i < n; i++)
System.out.println(arr*i+);

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


int min = i;

for (int j = i + 1; j < n; j++) ,


if (arr*j+ <arr*min+) ,
min = j;
-
-

char temp = arr*min+;


arr*min+ = arr*i+;
arr*i+ = temp;
-
System.out.println("After Arranging");
for (int i = 0;i < n; i++)
System.out.println(arr*i+);
-

public void main()


,
Scanner sc = new Scanner (System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
if(Pronic(n))
System.out.println("It is a Pronic number");
else
System.out.println("It is not a Pronic number");
System.out.println("Enter size of array");
int s = sc.nextInt();
char a*+ = new char *s+;
System.out.println("Enter the charectars");
for(int i=0;i<s;i++)
a*i+ = sc.next().charAt(0);
Selection(a);
-
-
Sample: 1
Input:

Enter a number
6
It is a Pronic number
Enter size of array
5
Enter the characters
elant
Output:

It is a Pronic number
Enter size of array
5
Enter the characters
Before Arranging
e
l
a
n
t
After Arranging
a
e
l
n
t
Sample: 2
Input:

Enter a number
7
It is not a Pronic number
Enter size of array
4
Enter the characters
dcab
Output:

It is not a Pronic number


Enter size of array
4
Enter the characters
Before Arranging
d
c
a
b
After Arranging
a
b
c
d
Variable Description
Variable Datatype Description
n int Input number to
check if it is pronic
i int For loop
arr*+ char Char array
min int Minimum value
j int For loop
temp char Temporary variable
for swapping
s int Size of the array
a*+ char Char Array
Area and perimeter
Design a class as Rectangle
Date: 8.8.24
Program 13
Design a class as Rectangle. The class has length, breadth,
area and perimeter as data members. It has methods
i. to obtain the value of length and breadth, ii. to calculate
area and perimeter. to display the given and calculated data.
Write a main method to invoke these methods without
creating an object.

Source Code:

import java.util.*;
public class Rectangle
,
doublel,b,a,p;

void Input()
,
Scanner sc = new Scanner(System.in);
System.out.println("Enter length and breadth");
l= sc.nextDouble();
b= sc.nextDouble();
-

void Calc()
,
a=l*b;
p=2*(l+b);
-
void Output()
,
System.out.println("Lenght:"+l+" Breadth:"+b+" Area:"+a+"
Perimeter:"+p);
-
public void main()
,
Input();
Calc();
Output();
-
-
Sample: 1
Input:

Enter length and breadth


5
10
Output:

Length: 5.0 Breadth: 10.0 Area: 50.0 Perimeter: 30.0

Sample: 2
Input:

Enter length and breadth


7
3
Output:

Length: 7.0 Breadth: 3.0 Area: 21.0 Perimeter: 20.0


Variable Description
Variable Datatype Description
l Double length
b Double breadth
a Double Area
p double Perimeter
Cab Service
Date: 13.8.24
Program 14
Design a class CabService with the following description:
Member variables/data members: String car type - To store
the type of car (AC or NON AC) double km - To store the
kilometers travelled. double bill-To calculate and store the bill
amount CabService() - Default constructor to initialize data
members. String data members to and double data members
to 0.0. Member methods: void accept()-To accent car type
and km. void calculate ()-To calculate the bill as per the rules
given above. CAR TYPE: KILOMETER TRAVELLED: TOTAL BILL:
void display() - To display the bill.Create an object of the class
in the main method and invoke the member methods

Source Code:
import java.util.*;

public class Cab_Service


,
String car_type; double km,bill;
Cab_Service()
,
car_type = "";
bill = 0.0;
km = 0.0;
-
void accept()
,
Scanner sc = new Scanner(System.in);
System.out.println("Enter for AC or NON AC car");
car_type = sc.nextLine();
System.out.println("Enter number of Kilometer of ride");
km = sc.nextDouble();
-
void calculate()
, switch(car_type)
,
case "AC":
,
if(km<5)
bill =150.0;
else
bill = km*10; break;
-
case "NON AC":
,
if(km<5)
bill =120.0;
else
bill =km*8; break;
-
default:
System.out.println("Wrong Information Given");
--
void display()
,
System.out.println("Car Type "+car_type);
System.out.println("Kilometers Travelled: "+km);
System.out.println("Total Bill "+bill);
-
public void main()
,
Cab_Service CS = new Cab_Service();
CS.accept();
CS.calculate();
CS.display();
-
-
:Sample: 1

Input:

Enter for AC or NON AC car


AC
Enter number of Kilometer of ride
10
Output:

Car Type: AC
Kilometers Travelled: 10.0
Total Bill: 100.0

Sample: 2
Input:

Enter for AC or NON AC car


NON AC
Enter number of Kilometer of ride
4
Output:

Car Type: NON AC


Kilometers Travelled: 4.0
Total Bill: 120.0
Variable Description
Variable Datatype Description
Car_type String Car type
Km Long Kilometers
traveled
bill long Total bill
Fibonacci Series
Date: 22.8.24
Program 15
A class Fibo has been defined to generate the Fibonacci series
Source Code:
import java.util.*;
public class Fibo
,
int start,end;
public static void main()
,
Scanner sc = new Scanner (System.in);
Fibo F = new Fibo ();
System.out.println("Enter the start and end terms");
int p = sc.nextInt();
int q = sc.nextInt();
Fibo Fi = new Fibo(p,q);
Fi.display();

-
Fibo()
,
start = 0;
end = 0;
-
Fibo(int p, int q)
,
start =p;
end =q;
-
int fibo(int n)
,
int a = 0, b= 1, c=1;
for(int i = 3;i<=n;i++)
,
c= a+b;
a= b;
b= c;
-
return c;
-

void display(),
for(int i = start; i<=end ; i++)
System.out.println(fibo(i) );
-
-
Sample: 1
Input:

Enter the start and end terms


15
Output:

1
1
2
3
5

Sample: 2
Input:

Enter the start and end terms


37
Output:

2
3
5
8
13
Variable Description
Variable Datatype Description
Start Int Stores the start
term for the
Fibonacci series
End Int Stores the end
term for the
Fibonacci series
p Int Temporary variable
for start term input
q Int Temporary variable
for end term input
a Int First number in
Fibonacci series
b Int Second number in
Fibonacci series
c Int Stores the current
Fibonacci number
i int For loop
Vowels and Consonant
Date: 29.8.24
Program 16
Write a program to input a word and count the number of
vowels and consonants present in it.
Source Code:
import java.util.*;
public class V_C
,
public static void main()
,
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word");
String S = sc.next(); int v =0,c=0;
int n = S.length(); char ch;

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


,
ch= S.charAt(i);
if(Character.isLetter(ch))
,
switch (ch)
,
case 'a': v++; break;
case 'A': v++; break;
case 'e': v++; break;
case 'E': v++; break;
case 'i': v++; break;
case 'I': v++; break;
case 'o': v++; break;
case 'O': v++; break;
case 'u': v++; break;
case 'U': v++; break;
default : c++;

-
-
-
System.out.println("Number of vowels "+v);
System.out.println("Number of consonants "+c);
-
-
Sample: 1
Input:

Enter a word
Hello
Output:

Number of vowels 2
Number of consonants 3

Sample: 2
Input:

Enter a word
Programming

Output:

Number of vowels 3
Number of consonants 8

Variable Description
Variable Datatype Description
s String Input word
v Int Counts the number of
vowels
c Int Counts the number of
consonants
n Int Length
Ch Char Stores each character
i int For loop
Word Extraction
Date: 3.10.24
Program 17
Write a program to input a sentence and convert it into
uppercase. Count and display the total number of words
starts and ends with letter 'A'.
Source Code:
import java.util.*;
public class Word_Extraction
,
public static void main()
,
Scanner sc = new Scanner (System.in);
System.out.println(“Enter a Word”);
String S = sc.nextLine();
char ch= ‘1’; int c =0;
int n = S.length(); String temp =””;
int I; int word = 0;
for(I =0;i<n;i++)
,
ch= S.charAt(i);
if(ch == ‘a’ || ch== ‘A’)
word = 1;
if(Character.isWhitespace(ch))
if(S.charAt(i-1) == ‘a’ || S.charAt(i-1) == ‘A’ && word == 1)
c++;
else
word = 0;
if(i== n-1)
if(S.charAt(i) == ‘a’ || S.charAt(i) == ‘A’ && word == 1)
c++;
else
word = 0;

-
System.out.println(“Number of words starting and ending with a
is : “+c);
for(I =0;i<n;i++)
,
ch= S.charAt(i);
temp += Character.toUpperCase(ch);
-
System.out.println(“New Senstence is \n”+temp);
-
-

Sample: 1
Input:

Enter a Word
My name is Aditya
Output:

Number of words starting and ending with a is : 1


New Sentence is
MY NAME IS ADITYA

Sample: 2
Input:

Enter a Word
John Lennon is a musician
Output:

Number of words starting and ending with a is : 1


New Sentence is
JOHN LENNON IS A MUSICIAN
Variable Description
Variable Datatype Description
s String Input string
Ch Char Description
temp String Stores uppercase
string
c int Counts words
starting and ending
with 'a'
n Int Length of the string

i int For loop


word int To store the
number of words
Palindrome
Date: 10.10.24
Program 18
int reverse(int n) - returns the reversed value of 'n'.
String reverse(String word) - returns its reversed 'word'.
Complete the main method to invoke these functions depend
on the user choice (Using switch case)
Source Code:
import java.util.*;
public class Palindrome
,
public void main()
,
Scanner sc = new Scanner (System.in);
System.out.println(“Enter 1 for Integer, 2 for String”);
int c = sc.nextInt();
switch I
,
case 1:

System.out.println(“Enter value of n”);


int n = sc.nextInt();
int rev =reverse(n);
System.out.println(“Reverse of n is: “+rev);
if(n== rev)
System.out.println(“It is a palindromic Number”);
else
System.out.println(“It is not a Palindromic Number”);
break;
case 2:
System.out.println(“Enter a string”);
String S = sc.next();
String r = reverse(S);
System.out.println(“Reverse of given String is: “+r);
if(S.compareToIgnoreCaseI==0)
System.out.println(“It is a Palindromic Word”);
else
System.out.println(“It is not a Palindromic Word”);
break;
default:
System.out.println(“Wrong number given”);
-
-
int reverse (int t)
,
int rev=0;
while(t>0)
,
rev = rev*10 + t%10;
t/=10;
-
return rev;
-
String reverse(String S)
,
String temp= “”;
char c; int s= S.length();
for(int I =0; i<s; i++)
temp+=S.charAt(s-1-i);

return temp;
-

-
Sample 1:
Input:

Enter 1 for Integer, 2 for String


1
Enter value of n
121
Output:

Reverse of n is: 121


It is a palindromic Number
Sample 2:
Input:

Enter 1 for Integer, 2 for String


2
Enter a string
Madam
Output:

Reverse of given String is: madaM


It is a Palindromic Word

Sample 3:
Input:

Enter 1 for Integer, 2 for String


3
Output:

Wrong number given


Variable Description
Variable Datatype Description
c int Stores user's
choice (1 or 2).
n Int Stores input int for
checking
palindrome.
rev Int Stores reversed
integer value.
s String Stores length
r String Stores reversed
string value
String Rearrange
Date: 18.10.24
Program 19
Write a program in Java to enter a string in a mixed case.
Arrange all the alphabets of string in such a way that all the
lower case letters to the left of the string and the upper case
letters to the right. Special characters in the middle.
Source Code:

import java.util.*;
public class String_Rearrange
,
public static void main()
,
Scanner sc = new Scanner(System.in);
System.out.println("Enter a large String");
String S = sc.next();
String temp= "";
int n = S.length(); char c;
for(int i =0;i<n;i++)
,
c= S.charAt(i);
if(Character.isLowerCase(c))
temp+=c;

-
for(int i =0;i<n;i++)
,
c= S.charAt(i);
if(!Character.isLetter (c))
temp+=c;
-
for(int i =0;i<n;i++)
,
c= S.charAt(i);
if(Character.isUpperCase(c))
temp+=c;
-
System.out.println("Arranged form is\n"+temp);
-
-
Sample 1
Input:

Enter a large String


aB1cD2eF!
Output:

Arranged form is:


ace12!BDF
Sample 2
Input:

Enter a large String


Hello123World!
Output:

Arranged form is:


ello123!HW

Variable Description
Variable Datatype Description
s String Stores the input
string entered by the
user.
temp String Stores the rearranged
string
n Int To store length
c Char To store character
i int For loop
Bubble Sort – String
Date: 22.10.24
Program 20
To input 'n' number of country names in a string array and its
corresponding ISD code in a integer data type array. Convert
the names into uppercase and arrange the names in a
descending order of alphabet and its ISD code using bubble
sort.

Source Code:
import java.util.Scanner;
class Countries_Bubble
,
public static void main()
,
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the value of n”);;
int n=sc.nextInt();;
String name*+=new String*n+;
int code*+=new int*n+;
System.out.println(“Enter the name of the country along with
the ISD code”);
for(int i=0;i<n;i++)
,
name*i+=sc.next();
code*i+=sc.nextInt();
-
for(int i=0;i<n;i++)
name*i+=name*i+.toUpperCase();
String a;
int b;
for (int I = 0; I < n; i++)
for (int j = I + 1; j < n; j++)
if (name*i+.compareTo(name*j+) < 0)
,
a=name*i+;
name*i+=name*j+;
name*j+=a;
b=code*i+;
code*i+=code*j+;
code*j+=b;
-
for(int i=0;i<n;i++)
System.out.println(“Name of country:”+name*i++”\t”+” ISD
code:”+code*i+);
-
-

Sample Input:

Enter the value of n


3
Enter the name of the country along with the ISD code
India 91
UnitedStates 1
Australia 61
Sample Output:

Name of country: UNITEDSTATES ISD code: 1


Name of country: INDIA ISD code: 91
Name of country: AUSTRALIA ISD code: 61
Variable Description

Variable Datatype Description


n Int Number of
countries.
name String To store the names
of the countries
code Int To store the ISD
codes of the
countries
i Int For loop
b Int For swapping ISD
codes
j Int For loop
a string For swapping
names of countries

You might also like