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

A

The document contains a series of Java programs that demonstrate various programming concepts such as special numbers, pattern printing, series summation, lucky numbers, ASCII codes, linear search, matrix transposition, prime number generation, leap year checking, method overloading, swapping numbers, pure and impure methods, and rectangle area and perimeter calculations. Each program includes source code, variable descriptions, and sample outputs. The programs are designed to help users understand fundamental programming techniques and logic.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

A

The document contains a series of Java programs that demonstrate various programming concepts such as special numbers, pattern printing, series summation, lucky numbers, ASCII codes, linear search, matrix transposition, prime number generation, leap year checking, method overloading, swapping numbers, pure and impure methods, and rectangle area and perimeter calculations. Each program includes source code, variable descriptions, and sample outputs. The programs are designed to help users understand fundamental programming techniques and logic.

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

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

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:
Variable Description
Variable Datatype Description
i Int
j int

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:
Variable Description
Variable Datatype Description
n Int

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

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:
Variable Description
Variable Datatype Description
a[] Char
i int

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

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:

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:

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:

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 {

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

public void main()


{
Number(234567);
Number(1233445555,5);
Number('$',5);
}
}

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

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

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:
Variable Description
Variable Datatype Description
n int
i int
arr[] char
min int
j int
temp char
n int
s int
a[] char

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:

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

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:

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

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

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:

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

Variable Datatype Description


n Int
name String
code Int
i Int
b Int
j Int
a string

You might also like