[Go to site: main page, start]

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

Java Programs for Basic Operations

The document contains multiple Java programs demonstrating basic functionalities such as printing a welcome message, adding two numbers, and checking for prime numbers. Each program includes user input and displays the results accordingly. The examples illustrate fundamental programming concepts in Java, including methods and conditional statements.

Uploaded by

parthaownuse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Programs for Basic Operations

The document contains multiple Java programs demonstrating basic functionalities such as printing a welcome message, adding two numbers, and checking for prime numbers. Each program includes user input and displays the results accordingly. The examples illustrate fundamental programming concepts in Java, including methods and conditional statements.

Uploaded by

parthaownuse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;
class program1
{
Public static void main(String args[])
{
[Link](“Welcome to Java”);
}
}

----------------------------------------------------------------------------------------------------------------

Output
Welcome to Java

----------------------------------------------------------------------------------------------------------------

import [Link];

public class AddTwoNumbers


{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);

// Getting two numbers from the user


[Link]("Enter first number: ");
int num1 = [Link]();

[Link]("Enter second number: ");


int num2 = [Link]();

// Adding two numbers


int sum = num1 + num2;

// Displaying the result


[Link]("The sum is: " + sum);
}
}

----------------------------------------------------------------------------------------------------------------
Output

Enter first number: 10


Enter second number: 20
The sum is: 30

----------------------------------------------------------------------------------------------------------------
import [Link];

public class AddTwoNumbersUsingMethod {

// Method to add two numbers


public static int add(int a, int b)
{
return a + b;
}

public static void main(String[] args)


{
Scanner scanner = new Scanner([Link]);

// Take two numbers from the user


[Link]("Enter first number: ");
int num1 = [Link]();

[Link]("Enter second number: ");


int num2 = [Link]();

// Call the method


int result = add(num1, num2);

// Display result
[Link]("The sum is: " + result);
}
}

----------------------------------------------------------------------------------------------------------------

Output

Enter first number: 12


Enter second number: 8
The sum is: 20

----------------------------------------------------------------------------------------------------------------

import [Link];

public class PrimeCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");


int n = [Link]();
boolean isPrime = true;

if (n < 2) {
isPrime = false;
} else {
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
[Link](n + " is a prime number.");
else
[Link](n + " is NOT a prime number.");

[Link]();
}
}

Output

If n = 8

Enter a number: 8

8 is NOT a prime number.

If n = 7

Enter a number: 7

7 is a prime number.

You might also like