0% found this document useful (0 votes)
4 views19 pages

Ayush Java Program (Assignment 1) - 1.Pdf - 20250530 - 123821 - 0000

The document contains a series of Java programming tasks along with their algorithms, programs, and outputs. Each task covers basic programming concepts such as printing text, arithmetic operations, comparisons, and loops. The tasks also include user input handling and mathematical calculations, demonstrating fundamental programming skills in Java.

Uploaded by

sssagnik7
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)
4 views19 pages

Ayush Java Program (Assignment 1) - 1.Pdf - 20250530 - 123821 - 0000

The document contains a series of Java programming tasks along with their algorithms, programs, and outputs. Each task covers basic programming concepts such as printing text, arithmetic operations, comparisons, and loops. The tasks also include user input handling and mathematical calculations, demonstrating fundamental programming skills in Java.

Uploaded by

sssagnik7
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/ 19

Q 1. Write a java program to print ‘Hello’ and your name on screen.

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

2. Declare two numbers: numerator and denominator

3. Perform division (numerator ÷ denominator)

4. Display the result

5. End

PROGRAM-
public class DivideTwoNumbers {

public static void main(String[] args) {

int numerator = 50;

int denominator = 5;

if (denominator != 0){

int result = numerator / denominator;

System.out.println("The result is: " + result);

} else {

System.out.println("Division by zero is not allowed.");

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)

5. Display all the results


6. End
PROGRAM-
public class ArithmeticOperations {

public static void main(String[] args) {

int num1 = 15;

int num2 = 4;

int sum = num1 + num2;

int difference = num1 - num2;

int product = num1 * num2;

int quotient = num1 / num2;

int remainder = num1 % num2;

System.out.println("Addition: " + sum);

System.out.println("Subtraction: " + difference);

System.out.println("Multiplication: " + product);

System.out.println("Division: " + quotient);

System.out.println("Remainder: " + remainder);


}

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 {

public static void main(String[] args) {

double radius = 10;

double pi = 3.14;

double area = pi * radius * radius;

double perimeter = 2 * pi * radius;

System.out.println("Radius: " + radius);

System.out.println("Area of Circle: " + area);

System.out.println("Perimeter of Circle: " + perimeter);

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:

If a > b, print "a is greater"


Else if a < b, print "b is greater"
Else, print "Both are equal"

5. End

PROGRAM-
public class CompareNumbers {

public static void main(String[] args) {

int a = 19;

int b = 26;

if (a > b) {

System.out.println("a is greater than b");

} else if (a < b) {

System.out.println("b is greater than a");

} else {

System.out.println("a and b are equal");

OUTPUT-
Q 8) Write a Java program and compute the sum of an integer’s digits.
ALGORITHM-
1. Start

2. Take an integer number (e.g., num)

3. Initialize sum = 0

4. Repeat while num is not 0:

Get the last digit using digit = num % 10

Add the digit to sum

Remove the last digit using num = num / 10

5. Print the value of sum

6. End

PROGRAM-
public class SumOfDigits {
public static void main(String[] args) {
int num = 1234;

int sum = 0;

int temp = num;

while (num != 0) {

int digit = num % 10;

sum + = digit;

num = num / 10;

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

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:

If n % i == 0, then n is not prime

5. If no divisors found, then n is prime


6. Display the result
7. End

PROGRAM-
public class PrimeCheck {

public static void main(String[] args) {


int n = 29;
boolean isPrime = true;

if (n <= 1) {
isPrime = false;

} else {

for (int i = 2; i <= n / 2; i++) {


if (n % i == 0) {

isPrime = false;
break;
}

}
}

if (isPrime) {

System.out.println(n + " is a Prime Number.");


} else {
System.out.println(n + " is NOT a Prime Number.");
}

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

• Read the user’s choice

• Prompt the user to enter two numbers

• Use switch-case or if-else to perform the operation based on user’s choice:

• Case 1: Add the numbers


• Case 2: Subtract the numbers
• Case 3: Multiply the numbers
• Case 4: Divide the numbers (check for division by zero)

• Display the result

• End

PROGRAM-
import java.util.Scanner;

public class BasicCalculator {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int choice;

double num1, num2, result = 0;

// Display Menu

System.out.println("----- Basic Calculator -----");

System.out.println("1. Addition");

System.out.println("2. Subtraction");

System.out.println("3. Multiplication");

System.out.println("4. Division");

System.out.print("Enter your choice (1-4): ");

choice = sc.nextInt();

System.out.print("Enter first number: ");

num1 = sc.nextDouble();

System.out.print("Enter second number: ");


num2 = sc.nextDouble();

// Perform calculation

switch (choice) {

case 1:

result = num1 + num2;

System.out.println("Result = " + result);

break;

case 2:

result = num1 - num2;

System.out.println("Result = " + result);

break;

case 3:

result = num1 * num2;

System.out.println("Result = " + result);

break;

case 4:

if (num2 != 0) {

result = num1 / num2;

System.out.println("Result = " + result);

} else {

System.out.println("Error: Division by zero!");

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;

public class TemperatureConverter {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int choice;

double inputTemp, result;

System.out.println("----- Temperature Conversion Menu -----");

System.out.println("1. Celsius to Fahrenheit");

System.out.println("2. Fahrenheit to Celsius");

System.out.println("3. Celsius to Kelvin");

System.out.println("4. Kelvin to Celsius");

System.out.println("5. Exit");

System.out.print("Enter your choice (1-5): ");

choice = sc.nextInt();

switch (choice) {

case 1:

System.out.print("Enter temperature in Celsius: ");

inputTemp = sc.nextDouble();

result = (inputTemp * 9 / 5) + 32;

System.out.println("Fahrenheit = " + result);

break;
case 2:

System.out.print("Enter temperature in Fahrenheit: ");

inputTemp = sc.nextDouble();

result = (inputTemp - 32) * 5 / 9;

System.out.println("Celsius = " + result);

break;

case 3:

System.out.print("Enter temperature in Celsius: ");

inputTemp = sc.nextDouble();

result = inputTemp + 273.15;

System.out.println("Kelvin = " + result);

break;

case 4:

System.out.print("Enter temperature in Kelvin: ");

inputTemp = sc.nextDouble();

result = inputTemp - 273.15;

System.out.println("Celsius = " + result);

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 {

public static void main(String[] args) {

// 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 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


int number;

System.out.print("Enter a number: ");


number = sc.nextInt();

if (number % 3 == 0 && number % 5 != 0) {

System.out.println(number + " is divisible by 3 but not by 5.");

} else {

System.out.println(number + " does not satisfy the condition.");

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 {

public static void main(String[] args) {


int sum = 0;

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


if (i % 2 == 0) {

sum += i;
}
}

System.out.println("Sum of even numbers from 1 to 50 is: " + sum);

}
}

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:");

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


if (i % 6 == 0) {
System.out.print(i + " ");
}
}
System.out.println();
}
}
OUTPUT-

You might also like