Ayush Java Program (Assignment 1) - 1.Pdf - 20250530 - 123821 - 0000
Ayush Java Program (Assignment 1) - 1.Pdf - 20250530 - 123821 - 0000
ALGORITHM-
1) Start
2) Display the text “hello”
3) Display your name
4) End
PROGRAM-
public class HelloName
{
public static void main(String[] args)
{
System.out.println("Hello");
System.out.println("My name is Ajoy");
}
}
OUTPUT-
Q 2) Write a Java program to print the sum of two numbers.
ALGORITHM-
1. Start
2. Declare two integer variables
3. Assign values to the variables (or take input)
4. Calculate the sum of the two numbers
5. Display the result
6. End
PROGRAM-
public class SumOfTwoNumbers
{
public static void main(String[] args)
{
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
}
}
OUTPUT-
Q 3) WRITE A JAVA PROGRAM TO DIVIDE TWO NUMBERS AND PRINT
THEM ON THE SCREEN.
ALGORITHM-
1. Start
5. End
PROGRAM-
public class DivideTwoNumbers {
int denominator = 5;
if (denominator != 0){
} else {
OUTPUT-
Q 4) Write a Java program to print the sum(addition),multiply,subtract,divide
and remainder of two numbers.
ALGORITHM-
1. Start
2. Declare two integer variables
3. Assign values to the variables (or take input)
4. Perform:
Addition
Subtraction
Multiplication
Division
Modulus (remainder)
int num2 = 4;
OUTPUT-
Q 5) Write a Java program to print the area and perimeter of a circle.
ALGORITHM-
1. Start
2. Declare a variable for the radius
3. Use the formula:
Area = π × r²
Perimeter (Circumference) = 2 × π × r
4. Compute area and perimeter
5. Display the results
6. End
PROGRAM-
public class Circle {
double pi = 3.14;
OUTPUT-
Q 7) Write a Java program to compare two numbers.
ALGORITHM-
1. Start
2. Declare two integer variables: a and b
3. Assign values to a and b
4. Use conditional statements to compare a and b:
5. End
PROGRAM-
public class CompareNumbers {
int a = 19;
int b = 26;
if (a > b) {
} else if (a < b) {
} else {
OUTPUT-
Q 8) Write a Java program and compute the sum of an integer’s digits.
ALGORITHM-
1. Start
3. Initialize sum = 0
6. End
PROGRAM-
public class SumOfDigits {
public static void main(String[] args) {
int num = 1234;
int sum = 0;
while (num != 0) {
sum + = digit;
OUTPUT-
Q 9) Write a Java program to find a number is prime or not.
ALGORITHM-
1. Start
2. Take an integer input n
3. If n is less than or equal to 1, it is not prime
4. Loop from 2 to n / 2:
PROGRAM-
public class PrimeCheck {
if (n <= 1) {
isPrime = false;
} else {
isPrime = false;
break;
}
}
}
if (isPrime) {
OUTPUT-
Q 11) WAP in JAVA that displays a Menu and performs a basic calculation where
1 for Addition, 2 for Subtraction, 3 for Multiplication and 4 for Division
ALGORITHM-
• Start
• Display the menu with options for Addition, Subtraction, Multiplication, and Division
• End
PROGRAM-
import java.util.Scanner;
int choice;
// Display Menu
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
choice = sc.nextInt();
num1 = sc.nextDouble();
// Perform calculation
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
if (num2 != 0) {
} else {
break;
default:
System.out.println("Invalid choice!");
} sc.close();
OUTPUT-
Q 12) WAP in JAVA that displays a Menu and performs Temperature Conversion where: 1)
Celsius to Fahrenheit 2) Fahrenheit to Celsius 3) Celsius to Kelvin 4) Kelvin to Celsius 5) Exit
ALGORITHM-
1. Start
2. Display the temperature conversion menu
3. Read the user’s choice
4. If choice is 5, exit the program
5. Else, prompt the user to enter the required temperature
6. Use switch-case or if-else to perform the conversion:
o 1: Celsius to Fahrenheit →
F = (C × 9/5) + 32
o 2: Fahrenheit to Celsius →
C = (F − 32) × 5/9
o 3: Celsius to Kelvin →
K = C + 273.15
o 4: Kelvin to Celsius →
C = K − 273.15
7. Display the result
8. End
PROGRAM-
import java.util.Scanner;
int choice;
System.out.println("5. Exit");
choice = sc.nextInt();
switch (choice) {
case 1:
inputTemp = sc.nextDouble();
break;
case 2:
inputTemp = sc.nextDouble();
break;
case 3:
inputTemp = sc.nextDouble();
break;
case 4:
inputTemp = sc.nextDouble();
break;
case 5:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice!");
sc.close();
OUTPUT-
Q 18) WAP in JAVA to solve the equation 2x + 5 = 13 for x
ALGORITHM-
1. Start
2. Given equation:2x + 5 = 13
3. Rearrange the equation to solve for x:
4. →
Subtract 5 from both sides 2x = 8
5.
6.
→
Divide both sides by 2 x = 4
Display the result
7. End
PROGRAM-
public class SolveEquation {
// Given equation: 2x + 5 = 13
// Solving for x
int result = (13 - 5) / 2;
System.out.println("The solution of the equation 2x + 5 = 13 is:");
System.out.println("x = " + result);
}
}
OUTPUT-
Q 19) WAP in JAVA to check if a number is divisible by 3 but not 5.
ALGORITHM-
1. Start
2. Prompt the user to enter a number
3. Check two conditions:
o The number should be divisible by 3 →number % 3 == 0
o The number should not be divisible by 5 → number % 5 != 0
4. If both conditions are true, display: "Number is divisible by 3 but not by 5"
5. Else, display: "Condition not satisfied"
6. End
PROGRAM-
import java.util.Scanner;
public class DivisibilityCheck {
} else {
sc.close();
OUTPUT-
Q 20) WAP in JAVA to print the sum of even numbers from 1 to 50.
ALGORITHM-
1. Start
2. Initialize a variable sum = 0
3. Loop from 1 to 50 (inclusive)
4. In each iteration, check if the number is even →
i % 2 == 0
5. If even, add it to sum
6. After the loop ends, display the sum
7. End
PROGRAM-
public class SumOfEvenNumbers {
sum += i;
}
}
}
}
OUTPUT-
Q 21) WAP in JAVA to print all numbers between 1 and 50 that are divisible by 6.
ALGORITHM-
1. Start
2. Loop through numbers from 1 to 50
3. For each number, check if it is divisible by 6 → number % 6 == 0
4. If the condition is true, print the number
5. End
PROGRAM-
public class DivisibleBySix {
public static void main(String[] args)
{
System.out.println("Numbers between 1 and 50 divisible by 6 are:");