Programs
Programs
Program:
class swap //declaring of the class
{
public void disp(int a , int b) //declaring of method
{// let us take a=3, b=4
a=a+b; //a=3+4 =7
b=a-b; //b=7-4 =3
a=a-b; //a=7-3 =4
[Link]("a="+a); //to print a[a=4]
[Link]("b="+b); //to print b[b=3]
} //terminating the method
} //terminating the class
Variable description:
Output:
2
Steps:
1. Declare a class named array.
2. Inside the class, declare an integer array a of size 10, and integer
variables small, pos, and temp.
3. Declare a method disp inside the class.
4. Inside the disp method, perform the following steps:
1. Create a Scanner object sc.
2. Print a message asking the user to enter the values of the array.
3. Use a for loop to iterate from 0 to 9. In each iteration, read an
integer from the user and store it in the corresponding index of
the array a.
4. Use a nested for loop to sort the array in ascending order using
the selection sort algorithm. In each outer loop iteration (with
index i):
1. Initialize small with the value of a[i] and pos with i.
2. Use an inner for loop to iterate from i+1 to 9. In each inner
loop iteration (with index j):
1. If a[j] is less than small,
update small with a[j] and pos with j.
3. After the inner loop, swap a[i] and a[pos] using
the temp variable.
5. Use another for loop to iterate from 0 to 9. In each iteration, print
the value of a[i].
5. End the disp method.
6. End the array class.
Program:
import [Link].*;
class array //Declaring of the class
{int a[]=new int[10]; //Declaring the array
int small,pos,temp; //Data member for swapping
public void disp() //Declaring of method
{
Scanner sc=new Scanner([Link]); //Scanner statement
[Link]("enter the values of the array"); //For printing the
statement
for(int i=0;i<10;i++) //Declaring a for loop
{
a[i]=[Link](); //For input of the values of array a[]
} //Terminating the loop
for(int i=0;i<10;i++) //Declaring a for loop
4
{
small=a[i]; //To store the smallest value
pos=i; //To store the position
for(int j=i+1;j<10;j++) //Declaring a for loop
{
if(a[j]<small) //For sorting of array in ascending order
{
small=a[j];
pos=j;
} //Terminating the
} //Terminating the loop
temp=a[i]; //Swapping of the array
a[i]=a[pos]; //Swapping of the array
a[pos]=temp; //Swapping of the array
} //Terminating the loop
for(int i=0;i<10;i++) //Declaring a for loop
{
[Link](a[i]); //Printing the array
} //Terminating the loop
} //Terminating the method
} //Terminating the class
Variable description:
5
Output:
6
5. Declare a method calculate inside the class. This method does the
following:
1. Declare a double variable discountpercentage.
2. Check the value of price and set discountpercentage accordingly.
3. Calculate the discount amount as discountpercentage/100 * price.
4. Calculate the net amount as price - discountamount.
5. Print the discount percentage.
6. Print the net amount to be paid.
6. Declare a method disp inside the class. This method does the following:
1. Print the item name.
2. Call the calculate method.
7. Declare a main method. This method does the following:
1. Create an Eshop object item.
2. Call the accept method on item.
3. Call the disp method on item.
8. End the Eshop class.
Program:
import [Link].*; // Importing the [Link] package
class Eshop // Declaring the class
{
String name; // For storing the name
double price; // For storing the price
public void accept() // Declaring the method
{
Scanner sc=new Scanner([Link]); // Scanner statement
[Link]("Enter the name of the item:"); // Printing the
statement
name= [Link](); // To input the name
[Link]("Enter the price of the item:"); // Printing the
statement
price=[Link](); // To input the price
} // Terminating the method
public void calculate() // Declaring the method
{
double discountpercentage;
// Checking the price range and setting the discount percentage
accordingly
if(price>=1000&&price<=25000)
{
discountpercentage=5.0;
}
8
else if(price>=25000&&price<=57000)
{
discountpercentage=7.5;
}
else if(price>=57000&&price<=100000)
{
discountpercentage=10.0;
}
else if(price>100000)
{
discountpercentage=15.0;
}
else
{
discountpercentage=0.0;
}
double discountamount=(discountpercentage/100)*price; // For
calculation of discount
double netamount=price-discountamount; // for calculation of net
amount
[Link]("discount percentage: "+discountpercentage+"%"); //
Printing the discount percentage
[Link]("net amount to be paid: "+netamount); // Printing the
net amount to be paid
}
public void disp() // Declaring the method
{
[Link]("item name: "+name); // Printing the item name
calculate(); // Calling the calculate method
} // Terminating the method
public static void main(String[] args) // Declaring the method
{
Eshop item=new Eshop(); // Creating an object of the class
[Link](); // Calling the accept method
[Link](); // Calling the disp method
} // Terminating the method
} // Terminating the class
9
Variable description:
Output:
10
11
Steps:
1. Declare a class named armstrong.
2. Inside the class, declare two integer variables r and s. Initialize s to 0.
3. Declare a method disp inside the class that takes an integer
parameter n.
4. Inside the disp method, perform the following steps:
1. Declare an integer variable p and assign n to it.
2. Use a while loop to iterate as long as n is not equal to 0. In each
iteration:
1. Calculate n modulo 10 and assign the result to r.
2. Add r cubed to s and assign the result to s.
3. Divide n by 10 and assign the result to n.
3. After the loop, check if p is equal to s. If so, print “armstrong no”.
Otherwise, print “not a armstrong no”.
5. End the disp method.
6. End the armstrong class
Program:
class armstrong //declaring of the class
{int r,s=0; //data member
public void disp(int n) //declaring of method
{ //let the value of n inputted is153
int p= n; //p=n=153
while(n!=0) //the loop will execute until n=0
{
r=n%10; //r=153%10=3 || r=15%10=5 || r=1
s=s+r*r*r; //s=0+3*3*3=27 || s=27+5*5*5=152 || s=152+1=153
n=n/10; //n=15 || n=1 || n=0
} //terminating the loop
if(p==s) //if p=s
{
[Link]("armstrong no"); // armstrong no
}
else //if p!=s
{
[Link]("not a armstrong no"); // no is not armstrong
}
} //terminating the method
12
Output:
13
Steps:
1. Import the [Link] package.
2. Declare a class named Employee.
3. Inside the class, declare instance
variables name, hra, da, pf, gp, np, basic, and code.
4. Declare a default constructor for the class that initializes basic, hra, da,
and pf to 0.
5. Declare a parameterized constructor for the class that takes id, name,
and basic as parameters and initializes code, name, and basic with
these values.
6. Declare a method compute inside the class. This method does the
following:
1. Calculate 10% of basic and assign it to hra.
2. Calculate 55% of basic and assign it to da.
3. Assign 1000 to pf.
4. Calculate the sum of basic, hra, and da and assign it to pf.
5. Subtract pf from gp and assign the result to np.
7. Declare a method disp inside the class. This method does the following:
1. Print the value of code.
2. Print the value of name.
3. Print the value of basic.
4. Print the value of hra.
5. Print the value of da.
6. Print the value of pf.
7. Print the value of gp.
8. Print the value of np.
8. Declare a main method. This method does the following:
1. Create an Employee object obj with id 001, name “ad”,
and basic 2000.
2. Call the compute method on obj.
3. Call the disp method on obj.
14
Program:
import [Link].*; // Importing the [Link] package
class Employee // Declaring the class
{
String name; double hra,da,pf,gp,np; int basic,code; // Declaring the
data members
Employee() // Declaring the constructor
{
basic=0;
hra=0.0;
da=0.0;
pf=0.0;
} // Terminating the constructor
Employee(int id,String name,int basic) // Declaring the constructor
{
code=id;
[Link]=basic;
[Link]=name;
} // Terminating the constructor
public void compute() // Declaring the method
{
hra=basic*0.1;
da=basic*0.55;
pf=1000;
pf=basic+hra+da;
np=gp-pf;
} // Terminating the method
public void disp() // Declaring the method
{
[Link]("code="+code); // Printing the code
[Link]("name="+name); // Printing the name
[Link]("basic="+basic); // Printing the basic
[Link]("hra="+hra); // Printing the hra
[Link]("da="+da); // Printing the da
[Link]("pf="+pf); // Printing the pf
[Link]("gp="+gp); // Printing the gp
[Link]("np="+np); // Printing the np
} // Terminating the method
public static void main(String[] args) // Declaring the method
{
15
Variable description:
Output:
16
Steps:
1. Import the [Link] package.
2. Declare a class named Perfect.
3. Inside the class, declare a method disp.
4. Inside the disp method, perform the following steps:
1. Create a Scanner object sc.
2. Print a message asking the user to enter the range.
3. Read an integer from the user input and store it in range.
4. Use a for loop to iterate from 1 to range. In each iteration (with
index i):
1. Initialize an integer variable sum to 0.
2. Use another for loop to iterate from 1 to i-1. In each iteration
(with index j):
1. If i is divisible by j, add j to sum.
3. After the inner loop, if sum is equal to i, print i.
5. End the disp method.
6. End the Perfect class.
Program:
import [Link]; // Importing the [Link] package
class Perfect // Declaring the class
{
public void disp() // Declaring the method
{
Scanner sc = new Scanner([Link]); // Creating a scanner object
[Link]("Enter the range: "); // Printing the statement
int range = [Link](); // Reading the input
for(int i=1;i<=range;i++) // Looping through the range
{
int sum=0; // Initializing the sum variable
for(int j=1;j<i;j++) // Looping through the range
{
if(i%j==0) // Checking if i/j=0
17
{
sum+=j; // Adding j to the sum
} // Terminating the if block
} // Terminating the inner loop
if(sum==i) // Checking if the sum is equal to i
{
[Link](i); // Printing the number
} // Terminating the if block
} // Terminating the outer loop
} // Terminating the method
} // Terminating the class
Variable description:
Output:
18
19
[Link] to input the length of the array a and b,then input their
respective values and add them in another array and print it.
Steps:
1. Import the [Link] package.
2. Declare a class named arrayaddition.
3. Inside the class, declare three integer arrays a, b, and c.
4. Declare a method disp inside the class.
5. Inside the disp method, perform the following steps:
1. Create a Scanner object sc.
2. Print a message asking the user to enter the length of the
array.
3. Read an integer from the user input and store it in n.
4. Initialize a, b, and c as integer arrays of size n, n,
and 2n respectively.
5. Print a message asking the user to enter the values of array a.
6. Use a for loop to iterate from 0 to n-1. In each iteration, read an
integer from the user input and store it in the corresponding
index of a.
7. Print a message asking the user to enter the values of array b.
8. Use a for loop to iterate from 0 to n-1. In each iteration, read an
integer from the user input and store it in the corresponding
index of b.
9. Use a for loop to iterate from 0 to n-1. In each iteration, copy
the value from the corresponding index of a to c, and the value
from the corresponding index of b to c at index i+n.
10. Print a blank line.
11. Use a for loop to iterate from 0 to 2n-1. In each iteration,
print the value from the corresponding index of c.
6. End the disp method.
7. End the arrayaddition class.
Program:
import [Link].*; // Importing the [Link] package
class arrayaddition // Declaring the class
{
int a[],b[],c[]; // Declaring the data members
public void disp() // Declaring the method
{
20
Variable description:
Output:
22
23
Steps:
1. Declare a class named patern.
2. Inside the class, declare a method disp.
3. Inside the disp method, perform the following steps:
1. Use a for loop to iterate from 1 to 5. In each iteration (with
index i):
1. Use another for loop to iterate from 1 to i. In each iteration,
print “*”.
2. Print a newline character.
4. End the disp method.
5. End the patern class.
Program:
class patern //Declaring the class
{
public void disp() // Declaring the method
{
for (int i =1;i<=5;i++) // Loop to iterate through rows
{
for (int j =1;j<=i;j++) // Loop to iterate through columns
{
[Link]("*"); // To print the pattern
}// Terminating the loop
[Link]( ); // Move to next line
}// Terminating the loop
}// Terminating the method
}// Terminating the class
Variable description:
24
Output:
25
[Link] a menu driven program to input a choise from user for the
following conversion: -
1) kilometers to centimeters
2)hours to minutes
3) Celsius to Fahrenheit
Steps:
1. Import the [Link] package.
2. Declare a class named menudriven.
3. Inside the class, declare three integer arrays a, b, and c.
4. Declare a method disp inside the class.
5. Inside the disp method, perform the following steps:
1. Create a Scanner object sc.
2. Print a message asking the user to enter a choice for conversion.
3. Read an integer from the user input and store it in ch.
4. Use a switch case to perform different conversions based on ch.
1. Case 1: Ask the user to input distance in kilometers, read it,
convert it to centimeters, and print the result.
2. Case 2: Ask the user to input time in hours, read it, convert it
to minutes, and print the result.
3. Case 3: Ask the user to input temperature in Celsius, read it,
convert it to Fahrenheit, and print the result.
4. Default: Print “wrong input”.
6. End the disp method.
7. End the menudriven class.
Program:
import [Link].*; // Importing java utility package for Scanner class
class menudriven // Defining a class named 'menudriven'
{
public void disp() // Defining a public method named 'disp'
{
Scanner sc= new Scanner([Link]); // Creating a new Scanner object
'sc' for taking user inputs
[Link]("Enter 1-km to cm, 2-h to min, 3-c to f"); // Displaying
conversion options to the user
int ch=[Link](); // Taking user's choice as input
switch(ch) // Switch case for performing different conversions based on
user's choice
26
{
case 1: // Case 1 for kilometre to centimetre conversion
[Link]("Input for km"); // Asking user to input kilometre
double km=[Link](); // Taking kilometre as input
[Link](km+" km = "+(km*100000)+" cm"); // Displaying
the converted value in centimeters
break;
case 2: // Case 2 for hour to minute conversion
[Link]("Input for hour"); // Asking user to input hours
double h=[Link](); // Taking hours as input
[Link](h+" h = "+(h*60)+" min"); // Displaying the
converted value in minutes
break;
case 3: // Case 3 for Celsius to Fahrenheit conversion
[Link]("Input for centigrade"); // Asking user to input
temperature in Celsius
double c=[Link](); // Taking Celsius temperature as input
[Link](c+" C° = "+((c*9/5)+32)+" F°"); // Displaying the
converted value in Fahrenheit
break;
default: // Default case for invalid input
[Link]("wrong input"); // Displaying error message for
invalid input
break;
}//Terminating the switch case
} //Terminating the method
} //Terminating the class
Variable description:
27
Output:
28
[Link] to input the no of rows for a pascal triangle and print it.
Steps:
1. Declare a class named Pascal.
2. Inside the class, declare a method disp that takes an integer
parameter n.
3. Inside the disp method, perform the following steps:
1. Declare an integer array pas of size n+1 and initialize the first
element to 1.
2. Use a for loop to iterate from 0 to n-1. In each iteration (with
index i):
1. Use another for loop to iterate from 0 to i. In each iteration,
print the value from the corresponding index of pas followed
by a space.
2. Print a newline character.
3. Use another for loop to iterate from i+1 to 1. In each
iteration (with index k), add the value from the (k-1)th index
of pas to the kth index of pas.
4. End the disp method.
5. End the Pascal class.
Program:
class Pascal // Declaring the class
{
public void disp(int n) // Declaring the method
{
int pas[] = new int[n+1]; // Declaring an integer array 'pas' of size 'n+1'
pas[0] = 1; // Initializing the first element of the array to 1
for (int i=0;i<n;i++) // Outer loop runs 'n' times
{
for (int j=0;j<=i;j++) // Inner loop runs 'i+1' times
{
[Link](pas[j]+" "); // Printing the 'j'th element of the array
followed by a space
}// Terminating the loop
[Link](); // Printing a newline character
for (int k=i+1;k>=1;k--) // Loop runs from 'i+1' to 1
{
pas[k] = pas[k]+pas[k-1]; // Updating the 'k'th element of the array by
adding the '(k-1)'th element to it
29
Variable description:
Output:
30
31
Steps:
1. Declare a class named Time.
2. Inside the class, declare two integer variables hrs and min.
3. Declare a method input that takes two integer parameters h and m and
assigns them to hrs and min respectively.
4. Declare a method addtime that takes
two Time objects obj1 and obj2 as parameters. Inside this method:
1. Create a new Time object output.
2. Add the hrs of obj1 and obj2 and assign it to hrs of output.
3. Add the min of obj1 and obj2 and assign it to min of output.
4. If min of output is greater than or equal to 60,
increment hrs of output by 1 and set min of output to 60.
5. Print the hrs and min of output.
5. Declare a main method. Inside this method:
1. Create two Time objects t1 and t2.
2. Call the input method on t1 with arguments 1 and 30.
3. Call the input method on t2 with arguments 2 and 40.
4. Call the addtime method with t1 and t2 as arguments.
6. End the Time class.
Program:
class Time // Declaring the class
{
int hrs,min; // Declaring two integer variables 'hrs' and 'min'
void input(int h, int m) // Defining a method named 'input' that takes two
integers as input
{
hrs = h; // Assigning the value of 'h' to 'hrs'
32
Output:
34
[Link] a menu driven program to input the name of the student and
the marks out of 300 and choose the stream accordingly as:-
Science mks>200, Commerce mks>100, Arts mks>75
And fail if mks<75
Steps:
1. Declare a class named marks.
2. Inside the class, declare three variables: a String a for the name,
an int mks for the marks, and a String stm for the stream.
3. Declare a method disp inside the class. This method does the following:
1. Create a Scanner object sc.
2. Print a message asking the user to enter their name and read the
input into a.
3. Print a message asking the user to enter their marks out of 300
and read the input into mks.
4. Call the print method.
4. Declare a method print inside the class. This method does the
following:
1. Check the value of mks and set stm accordingly:
1. If mks is between 201 and 300, set stm to “science”.
2. If mks is between 101 and 200, set stm to “commerce”.
3. If mks is between 76 and 100, set stm to “arts”.
4. Otherwise, set stm to “try again”.
2. Print stm.
5. End the marks class.
Program:
import [Link].*; // Importing java utility package for Scanner class
class marks // Defining a class named 'marks'
{
String a; // Declaring a String variable 'a' for storing name
int mks; // Declaring an integer variable 'mks' for storing marks
String stm; // Declaring a String variable 'stm' for storing stream
public void disp() // Defining a method named 'disp' for taking inputs
{
Scanner sc=new Scanner([Link]); // Creating a new Scanner object
'sc' for taking user inputs
[Link]("Enter name"); // Asking user to enter name
a=[Link](); // Taking name as input
35
Variable description:
36
Output:
37
Steps:
1. Declare a class named Find.
2. Inside the class, declare a method disp.
3. Inside the disp method, perform the following steps:
1. Create a Scanner object sc.
2. Declare and initialize two arrays state and city with state names
and corresponding city names.
3. Declare an integer variable x and assign it the length of
the state array.
4. Declare an integer variable pos and initialize it to 0.
5. Print a message asking the user to enter the name of a state and
read the input into a String variable val.
6. Use a for loop to iterate from 0 to x-1. In each iteration (with
index i):
1. If the ith element of state is equal to val (ignoring case),
print the capital of the state, update pos to i+1, and break
the loop.
7. If pos is still 0, print “Record not found”.
4. End the disp method.
5. End the Find class.
Program:
import [Link].*; // Importing java utility package for Scanner class
class Find // Defining a class named 'Find'
{
public void disp() // Defining a method named 'disp'
{
Scanner sc=new Scanner([Link]); // Creating a new Scanner object
'sc' for taking user inputs
String state[]={"Maharashtra","Karnataka","Gujarat"};// Declaring an
array 'state' and initializing it with names of states
38
Variable description:
39
Output:
40
41
Steps:
1. Declare a class named transport.
2. Inside the class, declare a double variable charge.
3. Declare a method disp that takes an integer parameter n.
4. Inside the disp method, perform the following steps:
1. If n is less than or equal to 50, calculate charge as n times 10.25.
2. Else if n is greater than 50 and less than or equal to 150,
calculate charge as 50 times 10.25 plus n times 15.75.
3. Else if n is greater than 150 and less than 255,
calculate charge as 50 times 10.25 plus 100 times 15.75
plus n times 20.20.
4. Else if n is greater than 255, calculate charge as 50 times 10.25
plus 100 times 15.75 plus 75 times 20.20 plus (n minus 255)
times 25.00.
5. Print charge.
5. End the disp method.
6. End the transport class.
Program:
{
charge=n*10.25; // If true, calculating 'charge' as 'n' times 10.25
}
else if(n>50&&n<=150) // Checking if 'n' is greater than 50 and less
than or equal to 150
{
charge=(50*10.25)+(n*15.75); // If true, calculating 'charge' as 50
times 10.25 plus 'n' times 15.75
}
else if(n>150&&n<255) // Checking if 'n' is greater than 150 and less
than 255
{
charge=(50*10.25)+(100*15.75)+(n*20.20); // If true, calculating
'charge' as 50 times 10.25 plus 100 times 15.75 plus 'n' times 20.20
}
else if(n>255) // Checking if 'n' is greater than 255
{
charge=(50*10.25)+(100*15.75)+(75*20.20)+(n-255)*25.00; // If true,
calculating 'charge' as 50 times 10.25 plus 100 times 15.75 plus 75 times
20.20 plus ('n'-255) times 25.00
}
[Link]("Charge="+charge); // Printing the value of 'charge'
} // Terminating the method
} // Terminating the class
Variable description:
Output:
43
44
Program:
Output:
46
16. Define a class to except a word from the user and encode the word
into Piglatin form. To translate a word into PigLatin, convert the word
into uppercase and then place the first vowel of the original word as
the start of the new word along with the remaining letters. The letters
present before the vowel are shifted towards the end followed by "AY".
Steps:
Program:
import [Link]; // Importing Scanner class from [Link] package
class PigLatin
{ // Class declaration
public static void main(String[] args) // Main method declaration
{
Scanner sc = new Scanner([Link]); // Creating a new Scanner object
for user input
[Link]("Enter a word:"); // Prompting user to enter a word
String word = [Link]().toUpperCase(); // Reading user input and
converting it to uppercase
int index = -1; // Initializing 'index' to -1
for (int i = 0; i < [Link](); i++)
{ // Loop running from 0 to length of 'word'-1
if ("AEIOU".indexOf([Link](i)) != -1) // Checking if 'i'th character of
'word' is a vowel
{
index = i; // If true, updating 'index' to 'i'
47
Variable description:
Output:
48
49
17. Define a class to accept a string from the user and input a character
to replace with if found in the string otherwise display the toggled
string.
Example:
Input : Computer Science
Character Input : e
Output :Computer Sci*nc*
Input : Computer Science
Character Input : a
Output : cOMPUTER sCIENCE
Steps:
1. Declare a class named toggle.
2. Inside the class, declare three variables: a char c,
two String variables a and b, and an int n initialized to 0.
3. Declare a method disp that takes a String parameter s and
a char parameter z.
4. Inside the disp method, perform the following steps:
1. Get the length of s and assign it to an int variable l.
2. Use a for loop to iterate from 0 to l-1. In each iteration (with
index i):
1. Get the ith character of s and assign it to c.
2. If c is equal to z, append “*” to a and set n to 1. Otherwise,
append c to a.
3. If n is 1, print a. Otherwise, use a for loop to iterate from 0 to l-1.
In each iteration:
1. Get the ith character of s and assign it to c.
2. If c is an uppercase letter, append the lowercase of c to b.
If c is a lowercase letter, append the uppercase of c to b.
Otherwise, append c to b.
5. End the disp method.
6. End the toggle class.
Program:
class toggle // Class declaration
{
char c; // Variable declaration
String a,b; // Variable declaration
int n=0; // Variable declaration
50
Variable description:
Output:
52
53
18.A Tech Number has an even number of digits. If the number is split
in two equal halves, then the square of the sum of these halves is equal
to the number itself. Write a program to generate and print all four
digits tech numbers.
Steps:
1. Declare a class named tech_no.
2. Inside the class, declare a method disp.
3. Inside the disp method, use a for loop to iterate from 1000 to 9998. In
each iteration, call the tech method with the current number as an
argument.
4. Declare a method tech that takes an integer parameter n. Inside this
method:
1. Divide n by 100 to get the first half and assign it to an integer
variable firsthalf.
2. Get the remainder of n divided by 100 to get the second half and
assign it to an integer variable lowerhalf.
3. Add firsthalf and lowerhalf and assign it to an integer variable tot.
4. Calculate the square of tot and assign it to an integer variable s.
5. If s is equal to n, print n.
5. End the tech method.
6. End the disp method.
7. End the tech_no class.
Program:
class tech_no // Class declaration
{
public void disp() // Method declaration
{
for(int i=1000;i<=9999;i++) // Loop running from 1000 to 9999
{
tech(i); // Calling the 'tech' method with 'i' as argument
}
}
public void tech(int n) // Method declaration
{
int firsthalf=n/100; // Getting the first half of 'n'
int lowerhalf=n%100; // Getting the second half of 'n'
int tot=firsthalf+lowerhalf; // Adding the first half and the second half
int s=(int)[Link](tot,2); // Squaring the sum
if(s==n) // Checking if the square of the sum is equal to 'n'
54
{
[Link](n); // If true, printing 'n'
}
} // Terminating the method
} // Terminating the class
Variable description:
Output:
55
19. Write a program to input a number and check and print whether it
is a 'Pronic' number or not. Use a method int Pronic(int n) to accept a
number. The method returns 1, if the number is 'Pronic', otherwise
returns zero (0).
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
Steps:
1. Declare a class named PronicNumber.
2. Inside the class, declare a method pronic that takes an integer
parameter n.
3. Inside the pronic method, perform the following steps:
1. Declare an integer variable Pronic and initialize it to 0.
2. Use a for loop to iterate from 1 to n-1. In each iteration (with
index i):
1. If the product of i and i+1 is equal to n, set Pronic to 1 and
break the loop.
3. Return Pronic.
4. Declare a main method. Inside this method:
1. Create a Scanner object in.
2. Print a message asking the user to enter a number to check and
read the input into an integer variable num.
3. Create a PronicNumber object obj.
4. Call the pronic method on obj with num as an argument and
assign the result to an integer variable r.
5. If r is 1, print that num is a pronic number. Otherwise, print
that num is not a pronic number.
5. End the PronicNumber class
Program:
import [Link]; // Importing Scanner class from [Link] package
class PronicNumber // Defining a class named 'PronicNumber'
{
public int pronic(int n) // Method to check if a number is a pronic number
{
int Pronic = 0; // Variable to store the result
56
Variable description:
Output:
58
59
Program:
import [Link]; // Importing Scanner class from [Link] package
class Factorial // Defining a class named 'Factorial'
{
public long fact(int n) // Method to calculate factorial of a number
{
long f = 1; // Variable to store the factorial
for (int i = 1; i <= n; i++) // Loop from 1 to n
{
f *= i; // Multiply 'f' with 'i'
60
}
return f; // Return the factorial
}
public static void main(String args[]) // Main method
{
Factorial obj = new Factorial(); // Create an object of the Factorial class
Scanner in = new Scanner([Link]); // Create a Scanner object
[Link]("Enter m: "); // Prompt the user to enter 'm'
int m = [Link](); // Read the user input for 'm'
[Link]("Enter n: "); // Prompt the user to enter 'n'
int n = [Link](); // Read the user input for 'n'
double s = (double)([Link](n)) / ([Link](m) * [Link](n - m)); //
Calculate 's' using the formula
[Link]("S=" + s); // Print 's'
} //Terminating the method
} //Terminating the class
Variable description:
61
Output:
62
Program:
class patern2 // Defining a class named 'patern2'
{
public void disp(String s) // Method to display a pattern based on a string
{
int i,j; // Variables for loop control
for(i=[Link]()-1;i>=0;i--) // Loop from the end of the string to the
beginning
{
for(j=i;j<[Link]();j++) // Loop from the current character to the end of
the string
{
char x=[Link](j); // Get the character at the current position
[Link](x); // Print the character
}
[Link](); // Print a newline after each line of the pattern
63
}
} //Terminating the method
} //Terminating the class
Variable description:
Output:
64
65
Steps:
1. Declare a class named GradeCalculator.
2. Inside the class, declare a method disp.
3. Inside the disp method, perform the following steps:
1. Create a Scanner object scanner.
2. Print a message asking the user to enter total marks and read the
input into a double variable totalMarks.
3. Print a message asking the user to enter obtained marks and read
the input into a double variable obtainedMarks.
4. Calculate the percentage as (obtainedMarks / totalMarks) * 100.
5. Use if-else statements to print the grade based on the
percentage:
1. If the percentage is greater than 90, print “Grade: A++”.
2. Else if the percentage is greater than 75, print “Grade: A+”.
3. Else if the percentage is greater than 60, print “Grade: A”.
4. Else if the percentage is greater than 50, print “Grade: B+”.
5. Else if the percentage is greater than 45, print “Grade: B”.
6. Else if the percentage is greater than 35, print “Grade: C”.
7. Else, print “Grade: D”.
4. End the disp method.
5. End the GradeCalculator class.
Program:
import [Link]; // Importing Scanner class from [Link] package
for user input
class GradeCalculator // Defining a public class named 'GradeCalculator'
{
public void disp() // Method to calculate and display the grade
66
{
Scanner scanner = new Scanner([Link]); // Creating a Scanner object
for user input
[Link]("Enter total marks:"); // Prompting the user to enter
total marks
double totalMarks = [Link](); // Reading the total marks
entered by the user
[Link]("Enter obtained marks:"); // Prompting the user to
enter obtained marks
double obtainedMarks = [Link](); // Reading the obtained
marks entered by the user
double percentage = (obtainedMarks / totalMarks) * 100; // Calculating
the percentage of marks obtained
// Checking the percentage and displaying the corresponding grade
if (percentage > 90)
{
[Link]("Grade: A++"); // If percentage is greater than 90,
grade is A++
}
else if (percentage > 75)
{
[Link]("Grade: A+"); // If percentage is greater than 75,
grade is A+
}
else if (percentage > 60)
{
[Link]("Grade: A"); // If percentage is greater than 60,
grade is A
}
else if (percentage > 50)
{
[Link]("Grade: B+"); // If percentage is greater than 50,
grade is B+
}
else if (percentage > 45)
{
[Link]("Grade: B"); // If percentage is greater than 45,
grade is B
}
else if (percentage > 35)
{
[Link]("Grade: C"); // If percentage is greater than 35,
grade is C
}
else
67
{
[Link]("Grade: D"); // If percentage is less than or equal to
35, grade is D
}
} //Terminating the method
} //Terminating the class
Variable description:
Output:
68
69
[Link] a program in Java that takes input and store integer elements
in a 2-D array of size 3x3 and find the sum of it's both diagonal
elements.
Steps:
1. Declare a public class named TwoDimention.
2. Inside the class, declare a method disp.
3. Inside the disp method, perform the following steps:
1. Create a Scanner object sc.
2. Declare and initialize a 3x3 integer array mat.
3. Use a nested for loop to read the user input and fill mat.
4. Declare two integer variables first_sum and second_sum and
initialize them to 0.
5. Use a nested for loop to print mat and
calculate first_sum and second_sum.
6. Print first_sum and second_sum.
4. End the disp method.
5. End the TwoDimention class.
Program:
import [Link]; // Importing Scanner class from [Link] package
for user input
public class TwoDimention // Defining a public class named
'TwoDimention'
{
public void disp( ) // Method to display the matrix and calculate the sums
of the diagonals
{
Scanner sc=new Scanner([Link]); // Creating a Scanner object for
user input
int row=3; // Defining the number of rows in the matrix
int column=3; // Defining the number of columns in the matrix
int mat[][] = new int[row][column]; // Declaring a 3x3 matrix
// Nested loop to read the user input and fill the matrix
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
[Link]("Enter the value left to right row by row"); //
Prompting the user to enter the values
70
Output:
72
Steps:
1. Import the Scanner class from the [Link] package.
2. Define a class named sum.
3. Inside the sum class, define the main method.
4. Inside the main method, do the following:
1. Declare a 4x4 integer array arr.
2. Declare an integer variable sum and initialize it to 0.
3. Create a Scanner object sc for user input.
73
Program:
import [Link]; // Importing Scanner class from [Link] package
for user input
class sum // Defining a public class named 'Main'
{
public static void main(String[] args) // Main method
{
int[][] arr = new int[4][4]; // Declaring a 4x4 array
int sum = 0; // Variable to store the sum of the elements of the array
Scanner sc = new Scanner([Link]); // Creating a Scanner object for
user input
[Link]("Enter the elements:"); // Prompting the user to enter
the elements of the array
// Nested loop to read the user input and fill the array
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
arr[i][j] = [Link](); // Reading the user input and storing it in the
array
}
}
// Nested loop to calculate the sum of the elements of the array
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
sum += arr[i][j]; // Adding the current element to the sum
}
}
74
Variable description:
Output:
75
Steps:
1. Import the Scanner class from the [Link] package.
2. Define a class named buzz.
3. Inside the buzz class, define a method named disp.
4. Inside the disp method, do the following:
1. Create a Scanner object sc for user input.
2. Print a message to prompt the user to enter a number.
3. Read the user input and store it in an integer variable num.
4. Check if num is divisible by 7 or ends with 7:
1. If num is divisible by 7 or ends with 7, print that num is a
Buzz number.
2. Otherwise, print that num is not a Buzz number.
5. End the buzz class
Program:
import [Link]; // Import the Scanner class
class buzz
{
public void disp()
{
Scanner sc = new Scanner([Link]); // Create a Scanner object
[Link]("Enter a number:"); //printing “Enter a number”
int num = [Link](); // Read user input
// Check if the number is divisible by 7 or ends with 7
if(num % 7 == 0 || num % 10 == 7)
{
[Link](num + " is a Buzz number."); // Output if the number
is a Buzz number
}
else
{
[Link](num + " is not a Buzz number."); // Output if the
number is not a Buzz number
}
} //Terminating the method
} //Terminating the class
77
Variable description:
Output:
78
Steps:
1. Define a class named palindrom.
2. Inside the palindrom class, declare two integer variables r and s.
3. Inside the palindrom class, define a method named disp that takes an
integer n as input.
4. Inside the disp method, do the following:
1. Store the original number n in an integer variable p.
2. Use a while loop to reverse the number n:
1. Get the last digit of n and store it in r.
2. Multiply s by 10 and add r to it, then store the result back
in s.
3. Remove the last digit from n.
3. Check if p is equal to s:
1. If p is equal to s, print that the number is a palindrome.
2. Otherwise, print that the number is not a palindrome.
5. End the palindrom class
Program:
class palindrom // Defining a class named 'palindrom'
{
int r,s; // Declaring two integer variables 'r' and 's'
public void disp(int n) // Method to check if a number is a palindrome
{
int p=n; // Storing the original number in 'p'
while(n!=0) // Loop to reverse the number
{
r=n%10; // Getting the last digit of the number
s=s*10+r; // Adding the last digit to the reversed number
n=n/10; // Removing the last digit from the number
}
if(p==s) // Checking if the original number is equal to the reversed
number
{
[Link]("Palindrom number"); // If yes, print that the number
is a palindrome
}
else
{
[Link]("Not a Palindrom number"); // If no, print not a
palindrome number
79
}
} // Terminating the method
} //Terminating the class
Variable description:
Output:
80
Conclusion
81
Bibliography
For making this project I have taken
help from:-
Internet:- i) [Link]
ii)
[Link]
People:- I have taken help from
my friends, teachers and parents
to make this project complete.