Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views97 pages

Computer Application Practical File

The document contains multiple Java programs demonstrating various programming concepts such as features of Java, arithmetic operations, area and perimeter calculations, conditional statements, and loops. Each program includes a variable description table outlining the variable names, data types, and purposes. The programs cover a range of topics from basic operations to user input handling and conditional logic.

Uploaded by

herahelenz
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)
7 views97 pages

Computer Application Practical File

The document contains multiple Java programs demonstrating various programming concepts such as features of Java, arithmetic operations, area and perimeter calculations, conditional statements, and loops. Each program includes a variable description table outlining the variable names, data types, and purposes. The programs cover a range of topics from basic operations to user input handling and conditional logic.

Uploaded by

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

PROGRAM 1

public class Features


{
public static void main(String args[])
{
System.out.println(" Features of Java");
System.out.println("Java is a Robust Language");
System.out.println("Java is Secure");
System.out.println("Java is Platform Independent");
System.out.println("Java is Simple");
System.out.println("Java is Case Sensitive");
System.out.println("Java is an Object Oriented Language");
}
}

NO VARIABLES USED

1
PROGRAM 2
public class Operators
{
public static void main(String args[])
{
int a = 30;
int b = 24;
int c = a - b;

System.out.println("Sum: " + (a + b));


System.out.println("Difference: " + c);
System.out.println("Product: " + (a * b));
System.out.println("Quotient: " + (a / b));
}
}

VARIABLE DESCRIPTION TABLE:

name a b c

Data type int int int

purpose To declare and store To declare and store To store the


the value of first the value of second numerical value after
number number subtracting two
numbers

2
PROGRAM 3
public class p3
{
public static void main(String args[])
{
double c = 135.0;
double r = 135.0 / (2.0 * Math.PI);
System.out.println("The radius is: " + r);

double a = Math.PI * Math.pow(r, 2);


System.out.println("The Area is: " + a);
}
}

VARIABLE DESCRIPTION TABLE:

name c r a

Data type double double double

purpose To declare and store To calculate radius To calculate area and


the value of and store the value store the value
circumference as 135

3
PROGRAM 4
public class Rectangle
{
public static void main(String args[])
{
int l = 4;
int b = 8;
int Area = l * b;
int Perimeter = 2 * (l + b);

System.out.println("Area: " + Area);


System.out.println("Perimeter: " + Perimeter);
}
}

VARIABLE DESCRIPTION TABLE:

name l b area perimeter

Data type int int int int

purpose To declare and To declare and To calculate the To calculate the


store value of store the value area and store perimeter and
length as 4 of breadth as 8 the value store the value

4
PROGRAM 5
public class p5
{
public static void main(String args[])
{
double s = 15.0;
double a = s * s;
double p = 4.0 * s;
double d = Math.sqrt(s * s + s * s);

System.out.println("The value of one side is: " + s);


System.out.println("The area is: " + a);
System.out.println("The perimeter is: " + p);
System.out.println("The value of one diagonal is: " + d);
}
}

VARIABLE DESCRIPTION TABLE:

name s a p d

Data type double double double double

purpose To declare and To calculate the To calculate the to calculate the


store the value are and store perimeter and diagonal and
of one side as 15 the value store the value store the value

5
PROGRAM 6
public class Weights
{
public static void main(String args[])
{
double kg = 4.8;
double g = kg * 1000;

System.out.println("Grams: " + g);


}
}

VARIABLE DESCRIPTION TABLE:

name Data type purpose

g double To declare and store the


value of grams as 4.8

kg double To calculate the kilogram


value and storing it

6
PROGRAM 7
public class ASCII
{
public static void main(String args[])
{
char ch = 'Z';
int a = ch;
char cb = 'z';
int b = cb;
int c = b + a;
int d = b - a;

System.out.println("Sum: " + c);


System.out.println("Difference: " + d);
}
}

VARIABLE DESCRIPTION TABLE:

name Data type purpose

ch char To store a character ('Z')

a int To store ASCII value of ch

cb char To store another character ('z')

b int To store ASCII value of cb

c int To store the sum of ASCII values a + b

d int To store the difference b - a

7
PROGRAM 8
public class eight {
public static void main(int seconds) {
int hours = seconds / 3600;
int rs = seconds % 3600;
int minutes = rs / 60;
int s = rs % 60;

System.out.println(hours + " Hours");


System.out.println(minutes + " Minutes");
System.out.println(s + " Seconds");
}
}

VARIABLE DESCRIPTION TABLE:


Name of the Variable Data type Purpose
seconds int To take input from user and
store it
hours int To declare and store the
value after division
rs int To declare and store the
value after division
(remaining seconds)
minutes int To declare and store the
value after division
s int To declare and store the
value after division(seconds)

8
PROGRAM 9
public class temperature
{
public static void main(String args[])
{
double f = 98.6; // Example value
double c = (f - 32.0) * 5.0 / 9.0;

System.out.println("Fahrenheit value is: " + f);


System.out.println("Celsius value is: " + c);
}
}

VARIABLE DESCRIPTION TABLE:


name Data type purpose

f double To declare and store


fahrenheit value form user

c double To calculate and store the


celsius value

9
PROGRAM 10
public class P10
{
public static void main(String args[])
{
double sp = 120.0; // Example selling price

double c1 = sp / 1.2; // Cost price on 20% profit


double c2 = sp / 0.8; // Cost price on 20% loss
double tl = c1 + c2;

System.out.println("Cost price on profit = " + c1);


System.out.println("Cost price on loss = " + c2);
System.out.println("Total cost price = " + tl);
}
}

VARIABLE DESCRIPTION TABLE:


NAME c1 c2 tl sp

Data type double double double double

purpose To calculate the To calculate the To calculate the To declare and


profit and loss and storing total cost price store the value
storing it it and store the of selling price
value as an input from
user

10
PROGRAM 11(With using a third variable)
public class Practical_11 {
public static void main(int a, int b) {
int c = a;
a = b;
b = a;

System.out.println("a is:" + a);


System.out.println("b is:" + c);
}
}

VARIABLE DESCRIPTION TABLE:


Name of the variable Data type Purpose

a integer To input numerical value


from user

b integer To input numerical value


from user

11
PROGRAM 11(Without using a third variable)

public class Practical11 {


public static void main(int a, int b) {
System.out.println("a=" + b);
System.out.println("b=" + a);
}
}

INPUT
A = 12
B =17
OUTPUT

VARIABLE DESCRIPTION TABLE:


Name of Variable Data type Purpose

a int To accept integer value from


user and store it

b int To accept integer value from


user and store it

12
PROGRAM 12
import java.util.*;
public class Practical12
{
public static void main()
{
Scanner sc = new Scanner(System.in);
double a;
System.out.println("Enter your Marks");
a = sc.nextDouble();

if (a >= 90)
{
System.out.println("Your Grade is A");
}
else if (a >= 70)
{
System.out.println("B");
}
else if (a >= 50)
{
System.out.println("C");
}
else
{
System.out.println("D");
}
}
}

OUTPUT

VARIABLE DESCRIPTION
Name of Variable Data type Purpose

a double To take marks as input from user

13
PROGRAM 13
public class Practical13 {
public static void main(double a) {
double b, c, d, e, f, g;
b = Math.abs(a);
c = Math.sqrt(a);
d = Math.cbrt(a);
e = Math.round(a);
f = Math.ceil(a);
g = Math.floor(a);

System.out.println("Absolute of the entered number:" + b);


System.out.println("Square root of the entered number:" + c);
System.out.println("Cube root of the entered number:" + d);
System.out.println("Round off of the entered number:" + e);
System.out.println("Ceil value of the entered number:" + f);
System.out.println("Floor of the entered number:" + g);
}
}
Entered no:25.6

VARIABLE DESCRIPTION TABLE:


Name of a c d b e f g
the
variable

Data type double double double double double double double

Purpose To Store To store a To store To To store To store To store


numerica square cube root absolute rounded ceil value floor
l value root value store value value
from value value
user

14
PROGRAM 14
public class Practical14 {
public static void main(double a, double b, double c) {
System.out.println("The entered numbers are :" + a + b + c);

if (a < b & b < c) {


System.out.println("The second smallest no:" + b);
} else if (b < a & a < c) {
System.out.println("The second smallest no:" + a);
} else if (a < c & c < b) {
System.out.println("The second smallest no:" + c);
} else {
System.out.println("No value");
}
}
}

Entered numbers are 14 15 16

VARIABLE DESCRIPTION TABLE:


Name of variable Data type Purpose

a integer TO store the first value from


user

b integer To store the second value


from user

c integer To store third value from user

PROGRAM 15

15
import java.util.*;
public class p15
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a, b, c;
System.out.println("Enter the First no:");
a = sc.nextInt();
System.out.println("Enter the Second no:");
b = sc.nextInt();
System.out.println("Enter the Third no:");
c = sc.nextInt();

if (a*a + b*b == c*c)


System.out.println("Pythagoras Triplet Formed");
else
System.out.println("Pythagoras Triplet not formed");
}
}

VARIABLE DESCRIPTION TABLE:


Name Data type purpose

a integer To declare and store the value 1 from user

b integer To declare and store the value 2 from user

c integer To declare and store the value 3 from user

PROGRAM 16

16
import java.util.*;
public class p16
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double a;
double b = 200.0;
System.out.println("Enter amount in units");
a = sc.nextDouble();
System.out.println("Total cost=");

if (a <= 99)
System.out.println((2 * a) + b);
else if (a >= 100 && a <= 199)
System.out.println((1.80 * a) + b);
else if (a >= 200)
System.out.println((1.50 * a) + b);
}
}

VARIABLE DESCRIPTION TABLE:


name Data type purpose

a double

b double

PROGRAM 17

17
public class p17
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
double a;
System.out.println("Enter the Number:");
a = sc.nextDouble();

if (a % 5 == 0 && a % 3 == 0)
System.out.println("The number is divisible both by 5 & 3");
else if (a % 5 != 0 && a % 3 == 0)
System.out.println("The number is divisible only by 3");
else if (a % 3 != 0 && a % 5 == 0)
System.out.println("The number is divisible only by 5");
else if (a % 3 != 0 && a % 5 != 0)
System.out.println("The number is not divisible both by 5 & 3");
}
}

VARIABLE DESCRIPTION TABLE:


name Data type purpose

a double To declare and store the


value from user

18
PROGRAM 18
import java.util.*;

public class eighteen {


public static void main(String args[]) {
Scanner discounts = new Scanner(System.in);
double price, discounted5, discounted10, discounted15, discounted20;
double dis5, dis10, dis15, dis20;

System.out.println("***Enter total purchase price***");


price = discounts.nextDouble();

discounted5 = 0.05 * price;


discounted10 = 0.1 * price;
discounted15 = 0.15 * price;
discounted20 = 0.2 * price;

dis5 = price - discounted5;


dis10 = price - discounted10;
dis15 = price - discounted15;
dis20 = price - discounted20;

if (price <= 2000) {


System.out.println("Discount=5%");
System.out.println("Gift=Calculator");
System.out.println("Discounted price=" + dis5);
} else if (price >= 2001 && price <= 5000) {
System.out.println("Discount=10%");
System.out.println("Gift=School bag");
System.out.println("Discounted price=" + dis10);
} else if (price >= 5001 && price <= 10000) {
System.out.println("Discount=15%");

19
System.out.println("Gift=Wall clock");
System.out.println("Discounted price=" + dis15);
} else {
System.out.println("Discount=20%");
System.out.println("Gift=Wrist watch");
System.out.println("Discounted price=" + dis20);
}
}
}

VARIABLE DESCRIPTION TABLE:

Name of the variable Data type Purpose


price Double To declare and store user
input as double
discounted5 Double To store discounted price at
5 percent
discounted10 Double To store discounted price at
10 percent
discounted15 Double To store discounted price at
15 percent
discounted20 Double To store discounted price at
20 percent
disc5 Double To store final price at 5
percent discount
disc10 Double To store final price at 10
percent discount
disc15 Double To store final price at 15
percent discount
disc20 Double To store final price at 20
percent discount

20
PROGRAM 19
Code:
public class P19
{
public static void main(String args[])
{
int i;
int a=21;
for(i=1;i<=10;i++)
System.out.println("21x"+1+"=" + a * i);
}
}

VARIABLE DESCRIPTION TABLE:


name Data type purposer

a int To declare and store the


value as 21

i int To store a value

21
PROGRAM 20
import java.util.*;
public class p20 {
public static void main(String args[]) {
Scanner last = new Scanner(System.in);
System.out.print("Enter number");
int n = last.nextInt();
int f = 1;

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


f *= i;
}

System.out.println("The factorial of " + n + " is " + f);


}
}

VARIABLE DESCRIPTION TABLE:


name Data type purpose

n int To declare and store value


from user

f int To declare a value

PROGRAM 21
import java.util.*;

22
public class p21 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int number = sc.nextInt();
int same = Math.abs(number); // store absolute number
int sum = 0;
int count = 0;

while (same > 0) {


int last = same % 10; // get last digit
sum += last; // add it to sum
same /= 10; // remove last digit
count++; // count digit
}

if (number == 0) {
sum = 0;
count = 1;
}

System.out.println("Sum of the digits: " + sum);


System.out.println("Number of digits: " + count);
}
}

VARIABLE DESCRIPTION TABLE:


name same number last count sum

Data type int int int int int

23
purpose To store the To accept and To store int To store the To perform
absolute store users value after int value calculations
value of users value calculation during while the
input looping(contr loop runs
ol variable)

PROGRAM 22
import java.util.*;
public class p22 {
public static void main(String args[]) {
int i, f1 = 1, f2 = 1, f3 = 1, f4 = 1, f5 = 1;

for (i = 5; i >= 1; i--) {


f1 *= i;
}

24
for (i = 4; i >= 1; i--) {
f2 *= i;
}

for (i = 3; i >= 1; i--) {


f3 *= i;
}

for (i = 2; i >= 1; i--) {


f4 *= i;
}

for (i = 1; i >= 1; i--) {


f5 *= i;
}

int a = f1 + f2 + f3 + f4 + f5;
System.out.println("The sum: " + a);
}
}

VARIABLE DESCRIPTION TABLE:


Name i f1 f2 f3 f4 f5 a

Data type int int int int int int int

purpose To loop To loop To loop To loop To loop To loop To store


and store and store and store and store and store and store value
int value int value int value int value int value int value after

25
while while while while while while calculatio
being being being being being being n
incremen incremen incremen incremen incremen incremen
ted(contr ted and ted and ted and ted and ted and
ol multiplie multiplie multiplie multiplie multiplie
variable) d d d d d

PROGRAM 23

import java.util.*;
public class p23 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int p, q, i, a;

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


System.out.println("From");
p = sc.nextInt();
System.out.println("to");
q = sc.nextInt();

System.out.println("The Buzz numbers between " + p + " and " + q + " = ");
for (i = p; i <= q; i++) {
a = i - 7;
if (a % 10 == 0 || i % 7 == 0) {
System.out.println(i);
}
}
}
}

26
VARIABLE DESCRIPTION TABLE:
name p q i a

Data type int int int int

purpose To accept and To accept and Control variable To store value


store value from store value from after calculation
user user

PROGRAM 24 a
import java.util.*;
public class p24_1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Range");
int a = sc.nextInt();
int sum = 5;
for (int i = 5; i <= a; i++)
{
if (i % 2 != 0)

27
sum += i;
}
System.out.println(sum);
}
}

VARIABLE DESCRIPTION TABLE:


name a sum i

Data type int int int

purpose To accept and store To store value after Control variable


input from user calculation

28
PROGRAM 24 b
import java.util.*;

public class p24_2 {


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

System.out.println("Enter the number of terms (n):");


int n = sc.nextInt();

System.out.println("Enter the value of a:");


double a = sc.nextDouble();

double S = 1; // Initialize S with the first term (1)

// Calculate the rest of the terms from i = 2 to n


for (int i = 2; i <= n; i++) {
S += (11 * i) / Math.pow(a, i - 1);
}

System.out.println("The result of the series is: " + S);


sc.close();
}
}

VARIABLE DESCRIPTION TABLE:


name n a s i

Data type int double double int

purpose To accept and To accept and To initialize Control variable


store input from store input from
user user

29
PROGRAM 24 c
import java.util.*;
public class p24_3 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the range");
double r = sc.nextDouble();
System.out.println("Enter a number");
double a = sc.nextDouble();
double s = 0;

for (int i = 1; i <= r; i++) {


double b = Math.pow(a, i);
s += i / b;
}

System.out.println(s);
}
}

VARIABLE DESCRIPTION TABLE:

name r a s i b

Data type double double double int double

purpose To accept To accept To initialize Control variable To calculate and


and store and store store value
input from input from
user user

30
PROGRAM 25 a
import java.util.*;

public class p25 {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a, s, i;
s = 0;

System.out.println("Enter a number");
a = sc.nextInt();

for (i = 2; i <= 20; i++) {


s += a * i;
}

System.out.println(s);
}
}

VARIABLE DESCRIPTION TABLE:


name a s i

Data type int int int

purpose To accept and To initialize Control variable


store input from
user

31
PROGRAM 25 b
public class p25_2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of digits to display");
int a = sc.nextInt();
int b = 0;

for (int c = 1; c <= a; c++) {


b = b * 10 + c;
System.out.println(b + " ");
}
}
}

VARIABLE DESCRIPTION TABLE:


name a b c

Data type int int int

purpose To accept and To initialize and Control variable


store input from store value after
user calculation

32
PROGRAM 26
import java.util.*;

public class p26 {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int a = sc.nextInt();

int b = Math.abs(a);
int sum = 0;

while (b > 0) {
int last = b % 10; // Get the last digit
sum += last; // Add it to sum
b /= 10; // Remove the last digit
}

// Check if the number is divisible by the sum of its digits


if (a % sum == 0) {
System.out.println("Niven number");
} else {
System.out.println("Not a Niven number");
}
}
}

VARIABLE DESCRIPTION TABLE:


name last a b sum

Data type int int int int

purpose To store value To accept and To store To store value


after calculation store input from absolute value after calculation
user

33
PROGRAM 27 a
public class p27_a {
public static void main(String args[]) {
int i;
for (i = 5; i >= 1; i--) {
for (int a = 1; a <= i; a++) {
System.out.print("#");
}
System.out.println();
}
}
}

name i a

Data type int int

purpose Control variable Control variable

34
PROGRAM 27 b
import java.util.*;
public class p27_2
{
public static void main(String[] args) {
int n = 4;

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


for (int j = 1; j <= i; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION TABLE:


name n i j

Data type int int int

purpose To store an int value Control variable Control variable

35
PROGRAM 28(a)
public class twentyeighta
{
public static void main(String args[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION TABLE:


Name of Variable Data type Purpose
i int To declare i as an integer
and assign a value
j int To declare j as an integer
and assign a value

36
PROGRAM 28(b)
public class twentyeight{
public static void main(String args[] {
int a = 15;
for (int i = 5; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(a + " ");
a--;
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION TABLE:


Name of variable Data type Purpose
a Int To declare ‘a’ as an integer
and assign a value
i Int To declare ‘i’ as an integer
and assign a value
j int To declare ‘j’ as an integer
and assign a value.

37
PROGRAM 28(c)
public class twentyeightc
{
public static void main(String args[])
{
int a = 1;
for (int i = 5; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(a + " ");
}
System.out.println();
a += 2;
}
}
}

VARIABLE DESCRIPTION TABLE:


Name of Variable Data type Purpose
a Int To assign value and declare
as an integer
i Int To assign value and declare
as an integer
j Int To assign value and declare
as an integer

38
PROGRAM 28(d)
public class twentyeightd
{
public static void main(String args[])
{
for (int i = 9; i >= 1; i -= 2)
{
for (int j = i; j <= 9; j += 2)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION TABLE:


Name of Variable Data type Purpose
i Int To assign value and declare
as an integer
j int To assign value and declare
as an integer

39
PROGRAM 29
import java.util.*;
public class twentynine {
public static void main(String args[]) {
Scanner a = new Scanner(System.in);
System.out.println("ENTER A NUMBER");
int i = a.nextInt();
int j = i;
for (int c = 0; c <= 99; c++) {
int sum = 0;
for (int q = i; q > 0; q /= 10) {
int e = q % 10;
sum += e * e;
}
i = sum;
if (i == 1) {
System.out.println(j + " IS A HAPPY NUMBER");
break; }
}
if (i != 1) {
System.out.println(j + " ISN’T A HAPPY NUMBER");
}
}
}

40
VARIABLE DESCRIPTION TABLE:
Name of Variable Data type Purpose
i Int To take input as an integer
j Int To assign value and declare
as an integer
c Int To assign value and declare
as an integer
sum Int To assign value and declare
as an integer
q Int To assign value and declare
as an integer
e Int To assign value and declare
as an integer
`

41
PROGRAM 30
import java.util.*;

public class p30 {


public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number:");
int a = sc.nextInt();
int j;
int b = a - 1;

for (j = 1; j < b; j++) {


System.out.println("");
}

if (a % j == 0) {
System.out.println("Composite Number");
} else if (a % j != 0) {
System.out.println("Not a Composite Number");
}
}
}

42
VARIABLE DESCRIPTION TABLE:
Name of Variable Data type Purpose

j Int To assign value and declare as


an integer

a Int To accept and store value


from user

b Int To assign value after


calculations and declare as
an integer

43
PROGRAM 31
CODE:
import java.util.*;
class p31
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
int n,l,s=0,a=10;
System.out.println("Enter a number");
int b=sc.nextInt();
n=b;
do
{
l=n%10;
s*=10;
s+=l;
n/=10;
}
while(n>0);
System.out.println("the reversed digit: " + s);
System.out.println("Absolute difference: " + (b-s));
}
}

44
VARIABLE DESCRIPTION TABLE:
name Data type purpose

sc Scanner To accept input from user

n int To store value for calculation

l int To store remainder during reversal

s int To store reversed number

a int Unused (initially assigned 10)

b int To accept and store number from user

45
PROGRAM 32
CODE:
import java.util.*;
class p32
{
Scanner sc= new Scanner(System.in);
int vno,hours;
double bill;
void input()
{
System.out.println("Enter the vehicle number: ");
vno=sc.nextInt();
System.out.println("Enter the number of hours: ");
hours=sc.nextInt();
}
void calculate()
{
if (hours==1)
{
bill=3.0;
}
else if(hours>1)
{
double d1=(hours-1)*1.50;
bill=3.0 + d1;
}

46
}
void display()
{
System.out.println(" the vehicle number : " + vno);
System.out.println(" the number of hours : " +hours);
System.out.println(" the amount : " + "rupees "+bill);
}
public static void main(String args[])
{
p32 obj=new p32();
obj.input();
obj.calculate();
obj.display();
}

47
VARIABLE DESCRIPTION TABLE:
name Data type purpose

sc Scanner To accept input from user

vno int To accept and store vehicle number

hours int To accept and store hours from user

bill double To store calculated bill

d1 double To store intermediate calculation value

48
PROGRAM 33
CODE:
import java.util.*;
class p33
{
Scanner sc = new Scanner (System.in);
int taxi_no,km,bill;
String name;
void input()
{
System.out.println("Enter The Name");
name=sc.nextLine();
System.out.println("Enter number of kilometres travelled");
km=sc.nextInt();

}
void calculate()
{
if(km==2)
{ bill = 25;

}
else if(km>2&&km<=10)
{ bill = 10*km;
}
else if(km>10&&km<=20)
{ bill = 15 *km;

49
}
else if(km>20)
{ bill = 20 *km;
}
}
void main()
{
System.out.println("The Name is : " + name);
System.out.println("The Distance is : " + km);
System.out.println("The Bill is : " + bill);
}
public static void main(String args[])
{
p33 ob = new p33();
ob.input();
ob.calculate();
ob.main();
}
}

50
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

taxi_no int Unused (declared but not used)

km int To accept and store kilometers from user

bill int To store calculated bill

name String To accept and store name from user

51
PROGRAM 34
CODE:
import java.util.*;
class p34
{
Scanner sc = new Scanner (System.in);
int pan;
String name;
double taxincome,tax,exd;
void input()
{
System.out.println("Enter The Personal Account Number");
pan=sc.nextInt();
System.out.println("Enter The Annual Taxable Income");
taxincome=sc.nextDouble();
System.out.println("Enter The Name");
name=sc.nextLine();
}
void cal()
{
if(taxincome<=250000)
{
tax = 0;
}
else if(taxincome>25000&&taxincome<=500000)
{
exd=taxincome-250000;

52
tax=exd*0.1;
}
else if(taxincome>500000&&taxincome<=1000000)
{
exd=taxincome-500000;
tax=30000+(exd*0.2);
}
else if(taxincome>1000000)
{
exd=taxincome-1000000;
tax=30000+(exd*0.2);
}
}
void display()
{
System.out.println(" The Name : " + name);

System.out.println(" The Personal Account Number : " + pan);

System.out.println(" The Annual Taxable Income : Rs." + taxincome);

System.out.println(" The Tax : Rs." + tax);

}
public static void main(String args[])
{
p34 ob = new p34();

53
ob.input();
ob.cal();
ob.display();
}
}

VARIABLE DESCRIPTION TABLE:


name Data type purpose

sc Scanner To accept input from user

pan int To accept and store PAN from user

name String To accept and store name from user

taxincome double To accept and store income from user

tax double To store calculated tax

exd double To store excess income over slabs

54
PROGRAM 35
CODE:
import java.util.*;
class p35
{
Scanner sc = new Scanner (System.in);
int bno,days,charge,d1,d2;
long phno;
String name;
void input()
{
System.out.println("Enter The Name");
name=sc.nextLine();
System.out.println("Enter The Book Number");
bno=sc.nextInt();
System.out.println("Enter The Phone Number");
phno=sc.nextLong();
System.out.println("Enter The No.Of Days");
days=sc.nextInt();
}
void compute()
{
if(days<=5)
{
charge=days*500;
}
else if(days>5&&days<=9)

55
{
d1=(days-5)*400;
charge=(5*500)+d1;
}
else if(days>9)
{
d1=(days-5)*400;
d2=(days-9)*200;
charge=(5*500)+d1+d2;
}
}
void display()
{
System.out.println(" The Name : " + name);

System.out.println(" The Book Number : " + bno);

System.out.println(" The Phone Number : " + phno);

System.out.println(" The No.Of Days : " + days);

System.out.println(" The Charge : Rs. " + charge);

}
public static void main(String args[])
{
p35 ob = new p35();

56
ob.input();
ob.compute();
ob.display();
}

VARIABLE DESCRIPTION TABLE:


name Data type purpose

sc Scanner To accept input from user

bno int To accept and store book number from user

days int To accept and store number of days

charge int To store calculated charge

d1 int To store partial charge calculation

d2 int To store additional charge calculation

phno long To accept and store phone number from user

name String To accept and store name from user

57
PROGRAM 36
CODE:
import java.util.*;
class p36
public class Shape
{
void polygon(int n, char ch)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(ch + " ");
}
System.out.println();
}
}
void polygon(int x, int y)
{
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
System.out.print("@ ");
}
System.out.println();
}

58
}
public static void main(String[] args)
{
Shape s = new Shape();
System.out.println("Square of side 4 with '*':");
s.polygon(4, '*');
System.out.println("Rectangle of 5x3 with '@':");
s.polygon(5, 3);
}
}

VARIABLE DESCRIPTION TABLE:


name Data type purpose

n int To accept and store the number of rows/columns for square polygon

ch char To accept and print a specific character

x int To accept and store number of rows (rectangle)

y int To accept and store number of columns (rectangle)

i int Control variable for outer loop

j int Control variable for inner loop

59
PROGRAM 37

import java.util.*;
class p37 {
void p(int side) {
int perimeter = 4 * side;
System.out.println("Perimeter of a Square: " + perimeter);
}

void p(int length, int breadth) {


int perimeter = 2 * (length + breadth);
System.out.println("Perimeter of a Rectangle: " + perimeter);
}

void p(double radius) {


double perimeter = 2 * (22.0 / 7.0) * radius;
System.out.println("Perimeter of a Circle: " + perimeter);
}

public static void main(String args[]) {


p37 ob = new p37();
ob.p(3);
ob.p(5, 4);
ob.p(6);

60
}
}

Variable Description Table:

name Data type purpose

side int To accept square side for calc

perimeter int To store calculated perimeter

length int To accept rectangle length

breadth int To accept rectangle breadth

radius double To accept radius of circle

perimeter double To store perimeter of circle

61
PROGRAM 38

import java.util.*;
public class p38 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
int count = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' ||
ch == 'O' || ch == 'U') {
count++;
}
}
System.out.println("Number of vowels: " + count);
}
}

62
Variable Description Table:

name Data type purpose

sc Scanner To accept input from user

str String To accept and store input text

count int To store number of vowels

len int To store length of the string

i int Control variable for loop

ch char To store each character checked

63
PROGRAM 39
import java.util.*;
public class p39 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a non-palindrome word: ");
String word = sc.nextLine();
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--) {
reverse = reverse + word.charAt(i);
}
String palindrome = word + reverse;
System.out.println("Palindromic word: " + palindrome);
}
}

Variable Description Table:

name Data type purpose

sc Scanner To accept input from user

word String To accept and store input word

reverse String To store reversed word

palindrome String To store final palindromic string

i int Control variable for loop

64
PROGRAM 40
import java.util.*;
public class p40 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
str = str.toUpperCase();
int a = 0, e = 0, i = 0, o = 0, u = 0;
for (int j = 0; j < str.length(); j++) {
char ch = str.charAt(j);
if (ch == 'A') a++;
else if (ch == 'E') e++;
else if (ch == 'I') i++;
else if (ch == 'O') o++;
else if (ch == 'U') u++;
}
if (a > 0) System.out.println("Frequency of vowel 'A' is " + a);
if (e > 0) System.out.println("Frequency of vowel 'E' is " + e);
if (i > 0) System.out.println("Frequency of vowel 'I' is " + i);
if (o > 0) System.out.println("Frequency of vowel 'O' is " + o);
if (u > 0) System.out.println("Frequency of vowel 'U' is " + u);
}
}

65
Variable Description Table:

name Data type purpose

sc Scanner To accept input from user

str String To accept and store input string

j int Control variable for loop

ch char To store character in loop

a int To store count of 'A'

e int To store count of 'E'

i int To store count of 'I'

o int To store count of 'O'

u int To store count of 'U'

66
PROGRAM 41
import java.util.*;
public class p41 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input
System.out.print("Enter a string: ");
String str = sc.nextLine();
// Convert to uppercase to handle both cases
str = str.toUpperCase();
String newStr = "";
// Loop to remove vowels
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Check if the character is NOT a vowel
if (ch != 'A' && ch != 'E' && ch != 'I' && ch != 'O' && ch != 'U') {
newStr = newStr + ch; // Append consonants and spaces
}
}
System.out.println("String after removing vowels: " + newStr);
}
}

67
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

str String To accept and store input string

newStr String To store string without vowels

i int Control variable for loop

ch char To store character in loop

68
PROGRAM 42
/*Write a PROGRAM in Java to accept two characters and display
the sum and difference of their ASCII value.*/
import java.util.*;
class p42
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
char ch,cb;
int s,d;
System.out.println("Enter two characters");
ch=sc.next().charAt(0);
cb=sc.next().charAt(0);

int h=(int)ch;
int b=(int)cb;
s=h+b;
d=h-b;
System.out.println("Sum of "+ch+" and "+cb+" : " +s);
System.out.println("Difference of "+ch+" and "+cb+" : " +d );
}
}

69
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

ch char To accept first character

cb char To accept second character

s int To store sum of ASCII values

d int To store difference of ASCII values

h int To store ASCII of first character

b int To store ASCII of second character

70
PROGRAM 43
import java.util.*;
class p43
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
char ch,cb;
int s,h;
System.out.println("Enter a charactera");
ch=sc.next().charAt(0);
h=(int)ch;
s=h+1;
cb=(char)s;
System.out.println(" The Next Alphabet in accords to "+ch+" is : " + cb);
}
}

71
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

ch char To accept and store character input

cb char To store next alphabet

h int To store ASCII of input character

s int To store ASCII of next character

72
PROGRAM 44
import java.util.*;
class p44
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[]= new int[20];
int e=0,o=0;
{
System.out.println("Enter 20 numbers");
for(int i=0;i<20;i++)
{
a[i]=sc.nextInt();
if(a[i]%2==0)
e+=a[i];
else
o+=a[i];
}
System.out.println(" The Sum of all Even Numbers : " + e);
System.out.println(" The Sum of all Odd Numbers : " + o);
}
}
}

73
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

a[] int[] To store 20 integers

e int To store sum of even numbers

o int To store sum of odd numbers

i int Control variable for loop

74
PROGRAM 45
import java.util.*;
public class PROGRAM45
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Year: ");
int a[] = new int[5];
int x = 0;
int f = 0;
for (int i = 0; i < a.length; i++)
{
a[i] = sc.nextInt();
}
System.out.println("Enter Year to be searched: ");
x = sc.nextInt();
for (int i = 0; i < a.length; i++)
{
if (a[i] == x)
{
System.out.println("Record Exists");
f = 1;
break;
}
}
if (f == 0) {

75
System.out.println("Record does not Exist");
}
}
}

VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from the user

a[] int[] To store 5 years entered by the user

x int To store the year to be searched

f int To act as a flag for whether match is


found

i int Control variable for loop

76
PROGRAM 46
import java.util.*;
public class PROGRAM46
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] a = new int[10];
System.out.println("Enter 10 integers:");
int max = a[0];
int min = a[0];
for (int i = 0; i < 10; i++)
{
a[i] = sc.nextInt();
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
}

77
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

a[] int[] To store 10 numbers

max int To store the maximum number

min int To store the minimum number

i int Control variable for loop

78
PROGRAM 47
import java.util.*;
class p47
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int a[]=new int[10];
int s=0,i;
System.out.println("Enter 10 numbers : ");
for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
s+=a[i];
}

System.out.println("The Sum of All 10 integers: " + s);


}
}

79
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

a[] int[] To store 10 numbers

s int To store sum of the numbers

i int Control variable for loop

80
PROGRAM 48
public class PROGRAM48
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] roll_no = new int[10];
String[] name = new String[10];
int[] m1 = new int[10];
int[] m2 = new int[10];
double[] percentage = new double[10];
char[] grade = new char[10];
for (int i = 0; i < 10; i++)
{
System.out.println("Roll number: ");
roll_no[i] = sc.nextInt();
sc.nextLine();
System.out.println("Name: ");
name[i] = sc.nextLine();
System.out.println("Marks in Maths: ");
m1[i] = sc.nextInt();
System.out.println("Marks in Science: ");
m2[i] = sc.nextInt();
percentage[i] = ((m1[i] + m2[i]) / 200.0) * 100.0;
if (percentage[i] >= 80)
{
grade[i] = 'A';

81
}
else if (percentage[i] >= 60)
{
grade[i] = 'B';
}
else if (percentage[i] >= 40)

85

{
grade[i] = 'C';
}
else
{
grade[i] = 'D';
}
System.out.println();
System.out.println(roll_no[i] + " " + name[i] + " " + m1[i] + " " + m2[i] + " " +
percentage[i] + " " + grade[i]);
}
}
}

82
83
84
VARIABLE DESCRIPTION TABLE:

Name Data type Purpose/Description

name String To store a word

percentage double To store a decimal value

grade char To store a character

i int To store an integer

85
PROGRAM 49
import java.util.*;

public class p49


{
public static void main(String[] args)

Scanner sc = new Scanner(System.in);

String[] name= new String[10];

int[] totalmarks =new int[10];

double[] d = new double[10];

int sum = 0;

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

System.out.print("Name:");

name[i] =sc.nextLine();

86
System.out.print("Total marks: ");

totalmarks[i] = sc.nextInt();

sc.nextLine();

sum += totalmarks[i];

double average = sum/10;

System.out.println("Average total marks: + "+ average + "\n");

System.out.println("Name Total Marks Deviation");

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


{
d[i] =totalmarks[i]- average;

System.out.println(name[i]+ “/t/t/t”+ totalmarks[i] + "/t/t/t "+d[i]);

}
}
}

87
88
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

name[] String[] To store names of 10 students

totalmarks[] int[] To store total marks of 10 students

d[] double[] To store deviation from average

sum int To store sum of total marks

average double To store average of total marks

i int Control variable for loop

89
PROGRAM 50
import java.util.*;
public class p50 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input size of the square matrix
System.out.print("Enter the size of the square matrix (n): ");
int n = sc.nextInt();
int[][] arr = new int[n][n];
// Input elements
System.out.println("Enter the elements of the matrix:");

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


for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
int leftSum = 0, rightSum = 0;
// Calculate sums of diagonals
for (int i = 0; i < n; i++) {
leftSum += arr[i][i]; // Left diagonal: [0][0], [1][1], ...
rightSum += arr[i][n - 1 - i]; // Right diagonal: [0][n-1], [1][n-2], ...
}
// Output
System.out.println("Sum of Left Diagonal = " + leftSum);
System.out.println("Sum of Right Diagonal = " + rightSum);
}

90
}

VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

n int To accept and store matrix size

arr[][] int[][] To store matrix elements

i int Outer loop control variable

j int Inner loop control variable

leftSum int To store sum of left diagonal

rightSum int To store sum of right diagonal

91
PROGRAM 51
import java.util.*;
public class PROGRAM51{
public static void main(String[] args)
{
int[] a = {1982, 1987, 1995, 1996, 2003, 2006, 2007, 2009, 2010};
Scanner sc = new Scanner(System.in);
System.out.print("Enter your graduation year: ");
int x = sc.nextInt();
int l = 0;
int h = a.length - 1;
int count = 0;
if (x >= a[l] && x <= a[h])
{
do
{
int mid = (l + h) / 2;
if (a[mid] == x)
{
count = 1;
break;
}
else if (x < a[mid])
{
h = mid - 1;
}
else

92
{
l = mid + 1;
}
}
while (l <= h);
}
if (count == 1)

96

{
System.out.println("Record exists.");
}
else
{
System.out.println("Record does not exist.");
}
}
}

93
VARIABLE DESCRIPTION TABLE:

Name Data type Purpose/Description

x int To store an integer value

l int To store an integer value

h int To store an integer value

count int To store an integer value

mid int To store an integer value

94
PROGRAM 52
import java.util.*;
public class p52 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String names[] = new String[6];
// Input
System.out.println("Enter 6 names:");
for (int i = 0; i < 6; i++) {
names[i] = sc.next();
}
// ----- Bubble Sort -----
System.out.println("\nBubble Sort:");
for (int i = 0; i < 6 - 1; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
if (names[j].compareTo(names[j + 1]) > 0) {
// Swap
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
for (int i = 0; i < 6; i++) {

System.out.print(names[i] + " ");


}

95
// ----- Selection Sort -----
// Reset the array
names[0] = "Anto";
names[1] = "Sri";
names[2] = "Arsh";
names[3] = "Vishav";
names[4] = "Reyansh";
names[5] = "Avi";
System.out.println("\n\nSelection Sort:");
for (int i = 0; i < 6 - 1; i++) {
int min = i;
for (int j = i + 1; j < 6; j++) {
if (names[j].compareTo(names[min]) < 0) {
min = j;
}
}
// Swap
String temp = names[i];
names[i] = names[min];
names[min] = temp;
}
for (int i = 0; i < 6; i++) {
System.out.print(names[i] + " ");
}
}
}

96
VARIABLE DESCRIPTION TABLE:

name Data type purpose

sc Scanner To accept input from user

names[] String[] To store names for sorting

i int Control variable for outer loops

j int Control variable for inner loops

temp String Temporary variable for swapping names

min int To store index of minimum element (selection sort)

97

You might also like