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

Java Practical Programs

The document contains five Java practical programs demonstrating basic functionalities such as calculating the product of two numbers, printing the square of a number using command line arguments, greeting users with their name and surname, finding the largest number from a list of natural numbers, and checking if a string is a palindrome. Each program includes the code and sample output for clarity. These examples serve as practical exercises for learning Java programming concepts.

Uploaded by

nairasheikh29
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)
2 views3 pages

Java Practical Programs

The document contains five Java practical programs demonstrating basic functionalities such as calculating the product of two numbers, printing the square of a number using command line arguments, greeting users with their name and surname, finding the largest number from a list of natural numbers, and checking if a string is a palindrome. Each program includes the code and sample output for clarity. These examples serve as practical exercises for learning Java programming concepts.

Uploaded by

nairasheikh29
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

Java Practical Programs with Outputs

1. Program to read two numbers and print their product


Code:
import [Link];

public class Product {


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

int a, b, product;

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


a = [Link]();

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


b = [Link]();

product = a * b;

[Link]("Product = " + product);


}
}

Output:
Enter first number: 5
Enter second number: 4
Product = 20

2. Program to print square using command line arguments


Code:
public class Square {
public static void main(String[] args) {

int num = [Link](args[0]);

int square = num * num;

[Link]("Square = " + square);


}
}

Output:
java Square 6
Square = 36
3. Program to send name and surname through command line arguments
Code:
public class Welcome {
public static void main(String[] args) {

String name = args[0];


String surname = args[1];

[Link]("Welcome " + name + " " + surname);


}
}

Output:
java Welcome Humaira Tasneem
Welcome Humaira Tasneem

4. Program to find the largest number out of n natural numbers


Code:
import [Link];

public class Largest {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int n, num, largest = 0;

[Link]("Enter how many numbers: ");


n = [Link]();

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


[Link]("Enter number: ");
num = [Link]();

if(num > largest) {


largest = num;
}
}

[Link]("Largest number = " + largest);


}
}

Output:
Enter how many numbers: 5
Enter number: 12
Enter number: 45
Enter number: 8
Enter number: 67
Enter number: 23
Largest number = 67

5. Program to check palindrome string


Code:
import [Link];

public class Palindrome {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

String str, rev = "";

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


str = [Link]();

for(int i = [Link]() - 1; i >= 0; i--) {


rev = rev + [Link](i);
}

if([Link](rev)) {
[Link](str + " is a Palindrome");
} else {
[Link](str + " is not a Palindrome");
}
}
}

Output:
Enter a string: MADAM
MADAM is a Palindrome

You might also like