1
1.Electricity Bill using Conditional Statement
PROGRAM:
import java.util.*; // Importing Scanner class for taking input
class ElectricBill {
String n;
int units;
double bill, rate;
void acceptData() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name and Units consumed");
n = sc.nextLine(); // Taking name as input
units = sc.nextInt(); // Taking units consumed as input
void computeData() {
if (units <= 100)
rate = (units * 2); // Rate for first 100 units
else if (units > 100 && units <= 300)
rate = ((2 * 100) + (units - 100) * 3); // Rate for next 200 units
else
rate = ((2 * 100) + (200 * 3) + (units - 300) * 5); // Rate for units above 300
if (units > 300)
bill = rate + ((2.5 / 100) * rate);
else
bill = rate;
void displayData() {
2
System.out.println("Name\tUnits\tRate\tAmount");
System.out.println(n + "\t" + units + "\t" + rate + "\t" + bill);
public static void main() {
ElectricBill a = new ElectricBill();
a.acceptData();
a.computeData(); // Compute the bill based on units consumed
a.displayData();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 n String Stores the name of the consumer
2 units int Stores the number of units consumed
3 bill double Stores the total bill amount
4 rate double Stores the rate per unit based on consumption
3
2.Abundant or Deficient or Perfect Number using Iteration
PROGRAM:
import java.util.*;
class Numbers
int n, s = 0;
void display()
Scanner sc = new Scanner(System.in); // Create a Scanner object for input
System.out.println("Enter Number");
n = sc.nextInt();
for(int i = 1; i < n; i++)
if(n % i == 0) // Check if i is a divisor of n
s += i; // Add the divisor to the sum
if(s < n)
System.out.println("It is a Deficient Number");
else if(s > n)
System.out.println("It is an Abundant Number");
else if(s == n)
System.out.println("It is a Perfect Number");
}
4
public static void main()
Numbers s = new Numbers(); // Create an instance of Numbers
s.display();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Description
Type
1 n int Stores the number entered by the user
2 s int Stores the sum of the divisors of the number
5
3.Menu driven using Iterative Constructs
PROGRAM:
import java.util.*;
class Menu {
int n;
void display() {
Scanner sc = new Scanner(System.in); // Create a Scanner object for input
System.out.println("Enter Option");
n = sc.nextInt();
switch (n) {
case 1:
int p = 0;
System.out.println("Enter number");
p = sc.nextInt();
int s = 0;
int sum1 = 0;
for (int i = 1; i <= p; i++) {
s += i; // Calculate the series sum
sum1 += s;
s *= 10;
System.out.println("The sum is " + sum1);
break;
case 2:
System.out.print("Enter the number of terms: ");
int g = sc.nextInt();
6
int a = 0, b = 1, c = 2;
System.out.print(a + " " + b + " " + c + " ");
for (int i = 4; i <= g; i++) {
int d = a + b + c; // Generate the next term in the series
System.out.print(d + " ");
a = b;
b = c;
c = d;
System.out.println();
break;
case 3:
int x = 0;
int l = 0;
System.out.println("Enter n");
l = sc.nextInt();
System.out.println("Enter value of x");
x = sc.nextInt();
double sum2 = 0;
for (int i = 1; i <= l; i++) {
if (i % 2 == 0) {
sum2 -= (Math.pow(x, i) / (i + 1)); // Subtracting term for even i
} else {
sum2 += (Math.pow(x, i) / (i + 1)); // Adding term for odd i
}
7
System.out.println("The sum is " + sum2);
break;
default:
System.out.println("Invalid option");
public static void main() {
Menu a = new Menu(); // Create an instance of Menu
a.display();
OUTPUT:
8
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 n int Stores the option selected by the user
2 p int Stores the number entered by the user for the first case
3 s int Stores the intermediate sum value for the series in the first case
4 sum1 int Stores the total sum of the series in the first case
5 g int Stores the number of terms entered by the user for the second
case
6 a, b, c int Stores the terms in the Fibonacci-like series
7 x int Stores the value of x entered by the user for the third case
8 l int Stores the value of n entered by the user for the third case
9 sum2 double Stores the sum calculated in the third case
9
4.Method Overloading
PROGRAM:
class Overload
void show()
char ch = 'A';
for(int i = 1; i <= 4; i++)
for(int j = 1; j <= i; j++)
if(i % 2 == 0)
System.out.print("O"); // Print 'O' if i is even
else
System.out.print(ch); // Print character if i is odd
if(ch == 'A')
ch++;
System.out.println();
}
10
void show(int n, char ch)
int sum;
if(ch == 's')
sum = (n * n); // Calculate square if ch is 's'
else
sum = (n * n * n); // Calculate cube if ch is not 's'
System.out.println("The required output is " + sum);
void show(int a, int b)
double z = 0;
z = ((Math.abs(a * b)) - (Math.sqrt((b * b) + (4 * a)))) / (Math.cbrt((2 * a) + b)); // Complex
calculation
System.out.println("Output = " + z);
public static void main()
Overload a = new Overload(); // Create an instance of Overload
a.show();
a.show(4, 's');
11
a.show(4, 'c');
a.show(3, 4);
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 ch char Stores the character to be printed
2 n int Stores the number for which square or cube is to be calculated
3 sum int Stores the result of the square or cube calculation
4 a, b int Stores the input values for the complex calculation
5 z double Stores the result of the complex calculation involving a and b
12
5.Purchase using Constructor
PROGRAM:
import java.util.*;
class ShowRoom {
String name;
long mobno;
double cost, dis, amt;
void input() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name:");
name = sc.nextLine();
System.out.println("Enter Mobile number:");
mobno = sc.nextLong();
System.out.println("Enter Cost:");
cost = sc.nextDouble();
void calculate() {
if (cost <= 10000) {
dis = (5.0 / 100) * cost; // Discount for cost <= 10000
} else if (cost > 10000 && cost <= 20000) {
dis = (10.0 / 100) * cost; // Discount for cost between 10001 and 20000
} else if (cost > 20000 && cost <= 35000) {
dis = (15.0 / 100) * cost; // Discount for cost between 20001 and 35000
} else {
dis = (20.0 / 100) * cost; // Discount for cost > 35000
}
13
amt = cost - dis;
void display() {
System.out.println("Customer name: " + name);
System.out.println("Mobile Number: " + mobno);
System.out.println("Amount: " + amt);
public static void main() {
ShowRoom a = new ShowRoom(); // Create an instance of ShowRoom
a.input();
a.calculate();
a.display();
OUTPUT:
14
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 name String Stores the name of the customer
2 mobno long Stores the mobile number of the customer
3 cost double Stores the cost of the product
4 dis double Stores the calculated discount based on the cost
5 amt double Stores the final amount after applying the discount
15
6.Volume of Sphere, Cylinder and Cone using Overloaded Methods
class Over
double volume(double R)
double vol = 0;
vol = ((4/3) * (22/7) * Math.pow(R, 3)); // Calculate the volume of a sphere
return vol;
double volume(double H, double R)
double vol = 0;
vol = ((22/7) * (Math.pow(R, 2)) * H); // Calculate the volume of a cylinder
return vol;
double volume(double L, double B, double H)
double vol = 0;
vol = L * B * H; // Calculate the volume of a cuboid
return vol;
public static void main()
Over a = new Over(); // Create an instance of Over
System.out.println(a.volume(21.0));
System.out.println(a.volume(17.0, 14.0));
16
System.out.println(a.volume(12.0, 7.0, 8.0));
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 R double Radius of the sphere or cylinder
2 H double Height of the cylinder or cuboid
3 L double Length of the cuboid
4 B double Breadth of the cuboid
5 vol double Stores the calculated volume
17
7.Sum of Digits – Static Methods
PROGRAM:
import java.util.*;
class Number
static int sumOfDigits(int n)
int sum = 0;
while(n > 0)
int r = 0;
r = n % 10;
sum += r;
n /= 10;
return sum;
static void call()
Scanner sc = new Scanner(System.in);
int n1 = 0, n2 = 0;
int sum2 = 0;
int sum1 = 0;
for(int i = 1; i <= 2; i++)
System.out.println("Enter Number " + i);
18
if(i % 2 == 0)
n2 = sc.nextInt();
else
n1 = sc.nextInt();
while(n1 > 0)
int r = 0;
r = n1 % 10;
sum1 += r;
n1 /= 10;
while(n2 > 0)
int r = 0;
r = n2 % 10;
sum2 += r;
n2 /= 10;
int sum = 0;
sum = sum1 + sum2; // Calculate the sum of digits of both numbers
System.out.println("Sum of Each digit = " + sum);
public static void main()
Number a = new Number(); // Create an instance of Number
19
System.out.println(a.sumOfDigits(237)); // Call sumOfDigits with 237
a.call(); // Call the call method
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 n int Stores the number for which sum of digits is calculated
2 r int Stores the remainder (last digit) during sum calculation
3 sum int Stores the sum of digits of a number
4 n1, n2 int Stores the two numbers entered by the user
5 sum1, sum2 int Stores the sum of digits for n1 and n2 respectively
20
8.Count ODD and EVEN Numbers in an SDA
PROGRAM:
import java.util.*;
import java.util.Map;
class Num
int n;
void display()
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of terms");
n = sc.nextInt();
int[] A;
A = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Enter Number " + (i + 1));
A[i] = sc.nextInt();
int count1 = 0, count2 = 0;
for (int i = 0; i < n; i++)
if (A[i] % 2 == 0)
count1 += 1; // Increment even count
else
count2 += 1; // Increment odd count
21
System.out.println("The number of ODD numbers is " + count2);
System.out.println("The number of EVEN numbers is " + count1);
public static void main()
Num a = new Num(); // Create an instance of Num
a.display();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 n int Stores the number of terms
2 A int[] Array to store the entered numbers
3 count1 int Counter for even numbers
4 count2 int Counter for odd numbers
22
9.Binary Search Algorithm
PROGRAM:
import java.util.*;
class BinSearch
int n;
void display()
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Search element");
n = sc.nextInt();
int[] A = {31, 36, 45, 50, 60, 75, 86, 90};
int L, U, M = 0, flag = 0;
L = 0;
U = A.length - 1;
while(L <= U)
M = (L + U) / 2;
if(n > A[M])
L = M + 1; // Adjust the lower bound
else if(n < A[M])
U = M - 1; // Adjust the upper bound
else
flag = 1; // Element found
break;
}
23
if(flag == 1)
System.out.println("Search successful");
else
System.out.println("Search Unsuccessful");
public static void main()
BinSearch a = new BinSearch(); // Create an instance of BinSearch
a.display();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 n int Stores the search element entered by the user
2 A int[] Array containing the elements to search within
3 L int Lower bound index for binary search
4 U int Upper bound index for binary search
5 M int Middle index for binary search
6 flag int Indicator to show if the element is found (1) or not (0)
24
10.Selection Sort Algorithm
PROGRAM:
import java.util.*;
class chr
char ch;
int n;
void display()
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Alphabet to be entered");
n = sc.nextInt();
char[] A = new char[n];
for (int i = 0; i < n; i++)
System.out.println("Enter Character " + (i + 1));
A[i] = sc.next().charAt(0);
char small, temp;
int pos;
for (int i = 0; i < A.length; i++)
small = A[i];
pos = i;
for (int j = i + 1; j < A.length; j++)
{
25
if (A[j] > small)
small = A[j]; // Update smallest character
pos = j; // Update position of smallest character
temp = A[i];
A[i] = A[pos];
A[pos] = temp; // Swap characters to sort in descending order
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " "); // Print sorted characters
public static void main()
chr a = new chr(); // Create an instance of chr
a.display();
OUTPUT:
26
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 ch char Stores a single character
2 n int Stores the number of characters to be entered
3 A char[] Array to store the entered characters
4 small char Stores the smallest character during sorting
5 temp char Temporary variable for swapping characters
6 pos int Stores the position of the smallest character during sorting
27
11.Double Dimensional Array
PROGRAM:
import java.util.*;
class Matrix
void display()
Scanner sc = new Scanner(System.in);
int[][] A = new int[3][3];
int sumElements = 0, sumEven = 0, sumLeftDiagonal = 0, sumRightDiagonal = 0;
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
A[i][j] = sc.nextInt();
sumElements += A[i][j];
if (A[i][j] % 2 == 0) {
sumEven += A[i][j]; // Sum of even elements
int[] rowSum = new int[3];
int[] colSum = new int[3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
rowSum[i] += A[i][j];
colSum[j] += A[i][j];
28
if (i == j) {
sumLeftDiagonal += A[i][j]; // Sum of left diagonal elements
if (i + j == 2) {
sumRightDiagonal += A[i][j]; // Sum of right diagonal elements
System.out.println("Sum of all elements: " + sumElements);
System.out.println("Sum of even elements: " + sumEven);
for (int i = 0; i < 3; i++) {
System.out.println("Sum of row " + (i + 1) + ": " + rowSum[i]);
for (int j = 0; j < 3; j++) {
System.out.println("Sum of column " + (j + 1) + ": " + colSum[j]);
System.out.println("Sum of left diagonal: " + sumLeftDiagonal);
System.out.println("Sum of right diagonal: " + sumRightDiagonal);
public static void main()
Matrix a = new Matrix(); // Create an instance of Matrix
a.display();
}
29
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 n int Stores the number of terms to be entered
2 A int[][] 2D array to store the elements of the matrix
3 sumElements int Stores the sum of all elements in the matrix
4 sumEven int Stores the sum of all even elements in the
matrix
5 sumLeftDiagonal int Stores the sum of elements in the left diagonal
6 sumRightDiagonal int Stores the sum of elements in the right diagonal
7 rowSum int[] Array to store the sum of each row
8 colSum int[] Array to store the sum of each column
30
12.Bubble Sort Algorithm
PROGRAM:
import java.util.*;
class BSort {
void sort() {
Scanner sc = new Scanner(System.in);
char A[] = new char[5];
char temp;
for (int i = 0; i < 5; i++) {
System.out.println("Enter " + (i + 1) + " String element");
A[i] = sc.next().charAt(0);
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length - 1; j++) {
if (A[j] > A[j + 1]) {
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp; // Swap elements to sort in ascending order
System.out.println("Sorted Array (Ascending)");
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " "); // Print sorted array
}
31
public static void main() {
BSort a = new BSort(); // Create an instance of BSort
a.sort();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 A char[] Array to store the input characters
2 temp char Temporary variable for swapping elements
3 sc Scanner Scanner object for input
4 i, j int Loop control variables
32
13.Piglatin using String methods
PROGRAM:
import java.util.*;
class Piglatin
String s, w = "", a = "";
void display()
Scanner sc = new Scanner(System.in);
System.out.println("Enter String:");
s = sc.nextLine();
char A[] = s.toUpperCase().toCharArray();
if ("AEIOU".indexOf(A[0]) != -1)
w = s + "AY"; // Append "AY" if the first character is a vowel
} else {
for (int i = 1; i < s.length(); i++)
w += A[i];
w += A[0] + "AY"; // Move first consonant to the end and append "AY"
System.out.println("Pig Latin: " + w);
public static void main()
{
33
Piglatin a = new Piglatin(); // Create an instance of Piglatin
a.display();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 s String Stores the input string
2 w String Stores the Pig Latin converted string
3 a String Unused variable in the current implementation
4 A char[] Array of characters from the input string
34
14.Linear Search Algorithm
PROGRAM:
import java.util.*;
class Wonders7
String s;
void display()
Scanner sc = new Scanner(System.in);
System.out.println("Country name");
s = sc.next();
if(s.equalsIgnoreCase("Mexico"))
System.out.println("MEXICO-CHICHEN ITZA"); // Wonder in Mexico
else if(s.equalsIgnoreCase("Brazil"))
System.out.println("BRAZIL-CHRIST THE REDEEMER"); // Wonder in Brazil
else if(s.equalsIgnoreCase("India"))
System.out.println("INDIA-TAJMAHAL"); // Wonder in India
else if(s.equalsIgnoreCase("China"))
System.out.println("CHINA-GREAT WALL OF CHINA"); // Wonder in China
else if(s.equalsIgnoreCase("Peru"))
System.out.println("PERU-MACCHU PICCHU"); // Wonder in Peru
else if (s.equalsIgnoreCase("Jordan"))
System.out.println("JORDAN-PETRA");
else if(s.equalsIgnoreCase("Italy"))
System.out.println("ITALY-COLLOSEUM");
else
35
System.out.println("Sorry not found");
public static void main()
Wonders7 a = new Wonders7(); // Create an instance of Wonders7
a.display();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 s String Stores the name of the country entered by the user
2 sc Scanner Scanner object for taking user input
36
15.Frequency of a Character in a String
PROGRAM:
import java.util.Scanner;
class LetterFrequency {
String sentence;
int[] freq = new int[26]; // Array to hold the frequency of each letter
void getInput() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
sentence = sc.nextLine().toLowerCase(); // Convert the sentence to lowercase
void calculateFrequency() {
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
if (ch >= 'a' && ch <= 'z') { // Check if the character is a letter
freq[ch - 'a']++; // Increment the frequency count
void displayFrequency() {
System.out.println("Letter frequencies:");
for (char ch = 'a'; ch <= 'z'; ch++) {
if (freq[ch - 'a'] > 0) {
System.out.println(ch + ": " + freq[ch - 'a']);
}
37
public static void main()
LetterFrequency lf = new LetterFrequency(); // Create an instance of LetterFrequency
lf.getInput();
lf.calculateFrequency();
lf.displayFrequency();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 sentence String Stores the input sentence
2 freq int[] Array to hold the frequency of each letter
3 ch char Stores the current character during processing
4 sc Scanner Scanner object for taking user input
38
16.Palindromic String
PROGRAM:
import java.util.*;
class Palindrome
String s;
String w="";
void display()
Scanner sc=new Scanner(System.in);
System.out.println("Enter word");
s=sc.next();
for(int i=0;i<s.length();i++)
char ch=s.charAt(i);
if(ch!=' ')
w=ch+w; // Build the reversed string
if(w.equals(s))
System.out.println("It is a palindrome"); // Check if the word is a palindrome
else
System.out.println("It is not a palindrome");
public static void main()
Palindrome a=new Palindrome(); // Create an instance of Palindrome
a.display();
39
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 s String Stores the input word
2 w String Stores the reversed version of the word
3 ch char Stores each character during reversal
4 sc Scanner Scanner object for taking user input
40
17.Counting characters using Character class methods
PROGRAM:
import java.util.*;
class Frequency
String s;
int c1, c2, c3, c4, c5;
void display()
Scanner sc = new Scanner(System.in);
System.out.println("Enter String");
s = sc.nextLine();
for(int i = 0; i < s.length(); i++) // Loop through the string
char ch = s.charAt(i);
if(Character.isLetter(ch))
c1 += 1; // Count letters
if(Character.isDigit(ch))
c2 += 1; // Count digits
if(Character.isUpperCase(ch))
c3 += 1; // Count uppercase letters
if(Character.isLowerCase(ch))
c4 += 1; // Count lowercase letters
if(Character.isWhitespace(ch))
c5 += 1; // Count whitespace characters
System.out.println("No of letters are " + c1);
41
System.out.println("No of digits are " + c2);
System.out.println("No of Lower case letters are " + c4);
System.out.println("No of upper case letters are " + c3);
System.out.println("No of white spaces are " + c5);
public static void main()
Frequency a = new Frequency(); // Create an instance of Frequency
a.display();
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 s String Stores the input string
2 c1 int Counts the number of letters in the string
3 c2 int Counts the number of digits in the string
4 c3 int Counts the number of uppercase letters in the string
5 c4 int Counts the number of lowercase letters in the string
6 c5 int Counts the number of whitespace characters in the string
42
18.Scope of Data members and Member methods
PROGRAM:
import java.util.*;
class Applicant
private long ANO;
private String name;
private float Agg;
private char Grade;
public void ENTER()
Scanner sc=new Scanner(System.in);
System.out.println("Enter Adm.No");
ANO=sc.nextLong(); // Input Admission Number
System.out.println("Enter Name");
name=sc.next();
System.out.println("Enter Aggregate Marks");
Agg=sc.nextFloat(); // Input Aggregate Marks
private void GradeMe()
if(Agg>=80)
Grade='A'; // Grade A for Aggregate >= 80
else if(Agg<80&&Agg>=65)
Grade='B'; // Grade B for 65 <= Aggregate < 80
else if(Agg<65&&Agg>=50)
43
Grade='C'; // Grade C for 50 <= Aggregate < 65
else
Grade='D'; // Grade D for Aggregate < 50
void RESULT()
System.out.println("AdmissionNumber:"+ANO+"\nName:"+name+"\nAggregate
Marks:"+Agg+"\nGrade:"+Grade);
public static void main()
Applicant a=new Applicant(); // Create an instance of Applicant
a.ENTER();
a.GradeMe();
a.RESULT();
OUTPUT:
44
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 ANO long Stores the admission number
2 name String Stores the name of the applicant
3 Agg float Stores the aggregate marks of the applicant
4 Grade char Stores the grade based on aggregate marks
45
19.Manipulating String
PROGRAM:
import java.util.*;
class Convert {
String s;
String w = "";
void display() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter String");
s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isUpperCase(ch)) {
w += Character.toLowerCase(ch); // Convert uppercase to lowercase
} else if (Character.isLowerCase(ch)) {
w += Character.toUpperCase(ch); // Convert lowercase to uppercase
} else {
w += ch; // Keep non-alphabet characters unchanged
System.out.println("Converted String: " + w);
public static void main() {
Convert converter = new Convert(); // Create an instance of Convert
converter.display();
}
46
OUTPUT:
VARIABLE DESCRIPTION TABLE:
S.No Variable Data Type Description
1 s String Stores the input string
2 w String Stores the converted string
3 ch char Stores each character during conversion
4 sc Scanner Scanner object for taking user input
47
20.Student Marks using Single Dimensional Arrays
PROGRAM:
import java.util.*;
class Student
int n;
String[] name;
int[] mark;
void getInput()
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of students:");
n = sc.nextInt();
sc.nextLine();
name = new String[n];
mark = new int[n];
for (int i = 0; i < n; i++)
System.out.println("Enter Name:");
name[i] = sc.nextLine();
System.out.println("Enter Mark:");
mark[i] = sc.nextInt();
sc.nextLine();
void calculateAndDisplay()
48
int sum = 0;
for (int i = 0; i < n; i++)
sum += mark[i]; // Calculate sum of marks
int avg = sum / n; // Calculate average marks
int sumDeviation = 0;
for (int i = 0; i < n; i++)
sumDeviation += Math.abs(mark[i] - avg);
int avgDeviation = sumDeviation / n; // Calculate average deviation
System.out.println("Average Marks: " + avg);
System.out.println("Average Deviation: " + avgDeviation);
public static void main()
Student a = new Student(); // Create an instance of Student
a.getInput();
a.calculateAndDisplay();
}
49
OUTPUT:
Variable Description Table:
S.No Variable Data Type Description
1 n int Stores the number of students
2 name String[] Array to store the names of the students
3 mark int[] Array to store the marks of the students
4 sum int Stores the sum of the marks
5 avg int Stores the average marks
6 sumDeviation int Stores the sum of the deviations from
average
7 avgDeviation int Stores the average deviation