A
A
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:
Variable Description
Variable Datatype Description
n Int
t Int
sum Int
prod int
Pattern Printing
Date:21.3.24
Program 2
A program to display the following pattern using nested for
Loop
&&&&&
&###&
&###&
&###&
&&&&&
Source code:
sum double
Tsum Int
f Int
i Int
j Int
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:
Variable Description
Variable Datatype Description
n Int
t Int
sum int
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);
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:
Variable Description
Variable Datatype Description
n Int
a[] Long
i Int
s Int
c int
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.*;
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];
Variable Description
Variable Datatype Description
n Int
a[] Int
i Int
j Int
sum int
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:
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:
Variable Description
Variable Datatype Description
n Int
i Int
a[] Int
b Int
c Int
j int
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 {
if (y % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
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:
Variable Description
Variable Datatype
y Int
n int
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();
}
}
Sample 1:
Variable Description
Variable Datatype Description
n Int
d Int
t Int
c Int
n Int
t Int
Sum Int
c Int
n int
Sample 1:
Variable Description
Variable Datatype Description
a int
x int
y int
B int
temp int
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;
}
int min = i;
Sample 1:
Variable Description
Variable Datatype Description
n int
i int
arr[] char
min int
j int
temp char
n int
s int
a[] char
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:
Variable Description
Variable Datatype Description
l Double
b Double
a Double
p double
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.*;
Variable Description
Variable Datatype Description
Car_type String
Km Long
bill long
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:
Variable Description
Variable Datatype Description
Start Int
End Int
P Int
Q Int
A Int
B Int
C Int
i int
}
}
}
System.out.println("Number of vowels "+v);
System.out.println("Number of consonants "+c);
}
}
Sample 1:
Variable Description
Variable Datatype Description
S String
V Int
C Int
N Int
Ch Char
i int
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:
Variable Description
Variable Datatype Description
S String
Ch Char
temp String
C int
N Int
I int
word int
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:
return temp;
}
Sample 1:
Variable Description
Variable Datatype Description
C int
N Int
Rev Int
S String
r String
c Int
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:
Variable Description
Variable Datatype Description
S String
Temp String
N Int
C Char
i int
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 1:
Variable Description