MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
Write a java program to perform various operations
[Link]: 2
based on some given data:
Date:
a) Temperature conversion
Aim: To write a Java program to convert temperature from Celsius to Fahrenheit and from
Fahrenheit to Celsius.
Theory: Temperature conversion is done using standard formulas.
• Fahrenheit = (Celsius × 9/5) + 32
• Celsius = (Fahrenheit − 32) × 5/9
Algorithm:
1. Import the Scanner class.
2. Create a Scanner object.
3. Read temperature in Celsius.
4. Convert Celsius to Fahrenheit using the formula.
5. Display the Fahrenheit value.
6. Read temperature in Fahrenheit.
7. Convert Fahrenheit to Celsius using the formula.
8. Display the Celsius value
Program Code:
import [Link];
public class Practical_02{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("enter the celsius :");
int cels=[Link]();
int far=(cels*9/5)+32;
Gopichand Kalasani (92400118999) 1
MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
[Link](" farahniet :"+far);
}
}
Output:
Result: The program successfully converted temperature between Celsius and Fahrenheit.
b) Digit extraction
Aim: To write a Java program to extract and display each digit of a given number
Theory: Digit extraction is performed using arithmetic operations. The modulus operator %
is used to obtain the last digit of the number, and integer division / is used to remove the last
digit
Algorithm:
1. Import the Scanner class.
2. Create a Scanner object
3. Read a number from the user.
4. Use a loop while the number is greater than 0.
5. Extract the last digit using %.
6. Display the digit.
7. Remove the last digit using /.
Gopichand Kalasani (92400118999) 2
MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
8. Repeat until the number becomes 0
Program Code:
public class Practical_02{
public static void main (String[] a){
Scanner sc=new Scanner([Link]);
[Link]("Enter the number:");
int num = [Link]();
while(num>00){
int digit=num%10;
[Link]("digit :"+digit);
num=num/10;
}
[Link]();
}
}
Output:
Result: The program successfully extracted and displayed each digit of the entered number
Gopichand Kalasani (92400118999) 3
MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
c) Number sign check
Aim: To write a Java program to check whether a number is positive, negative, or zero
Theory: Conditional statements such as if–else are used to control the flow of a program.
By comparing the entered number with zero, the program can determine whether the
number is positive, negative, or equal to zero
Algorithm:
1. Import the Scanner class.
2. Create a Scanner object.
3. Read a number from the user
4. Check if the number is greater than zero.
5. If true, display “Positive Number”
6. Else if the number is less than zero, display “Negative Number”Program Code:
7. Otherwise, display “Number is Zero”.
Program code :
public class Practical_02{
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter a number :");
int number=[Link]();
if (number>0) {
[Link]("Number is Positive");
else if(number<0){
[Link]("Number is negative");
Gopichand Kalasani (92400118999) 4
MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
else {
[Link]("Number is zero");
Output:
Result : The program correctly identified whether the number was positive, negative, or
zero.
D) Profit/loss calculation
Aim: To write a Java program to calculate profit or loss based on cost price and selling
price..
Theory: Profit or loss is determined by comparing the cost price (CP) and selling price
(SP). • If SP is greater than CP, there is a profit.
• If CP is greater than SP, there is a loss.
• If both are equal, there is no profit or loss.
Algorithm:
1. Import the Scanner class.
2. Create a Scanner object.
3. Read the cost price.
Gopichand Kalasani (92400118999) 5
MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
4. Read the selling price.
5. Compare selling price with cost price
6. If SP > CP, calculate and display profit
7. Else if CP > SP, calculate and display loss.
8. Otherwise, display no profit or loss.
Program Code:
public class Practical_02{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the Cost Price : ");
double cp=[Link]();
[Link]("Enter the Selling Price : ");
double sp=[Link]();
if(sp>cp){
[Link]("Profit : "+(sp-cp));
}
else if(cp>sp){
[Link]("Loss : "+(cp-sp));
}
else{
[Link]("No profit or Loss");
}
}
}
Output:
Gopichand Kalasani (92400118999) 6
MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
Result: The program successfully calculated and displayed profit or loss based on cost and
selling price..
e) Student admission eligibility based on grade determination
Aim: To write a Java program to determine student admission eligibility based on marks.
Theory: Admission eligibility can be decided using conditional statements. The program
checks the marks entered by the user and assigns a grade.
Algorithm:
1. Import the Scanner class.
2. Create a Scanner object.
3. Read the student’s marks.
4. Check if marks are greater than or equal to 85.
5. If true, display Grade A eligibility.
6. Else if marks are greater than or equal to 60, display Grade B eligibility.
7. Else if marks are greater than or equal to 40, display Grade C eligibility
Program Code:
public class Practical_02{
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
Gopichand Kalasani (92400118999) 7
MARWADI UNIVERSITY
DEPARTMENT OF AI, ML & DS
Java programming(01AI0401)
[Link]("Enter the Marks : ");
int marks=[Link]();
if(marks>=85){
[Link]("Grade A :- Eligible for Admission");
else if (marks>=60){
[Link]("Grade B :- Eligible for Admission");
else if (marks>=40){
[Link]("Grade C :- Eligible for Admission");
Output:
Result: The program correctly determined admission eligibility based on the entered
marks
Gopichand Kalasani (92400118999) 8