1. Program to print "Hello, World!
"
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
2. Program to check if a number is even or odd
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if (num % 2 == 0) {
[Link](num + " is even.");
} else {
[Link](num + " is odd.");
}
}
}
3. Program to find the factorial of a number
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
[Link]("Factorial of " + num + " is " + factorial);
}
}
4. Program to check if a number is prime
import [Link];
public class PrimeNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](num + " is a prime number.");
} else {
[Link](num + " is not a prime number.");
}
}
}
5. Program to print Fibonacci series up to a given number
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int count = [Link]();
int num1 = 0, num2 = 1;
[Link]("Fibonacci Series of " + count + " numbers: ");
for (int i = 1; i <= count; ++i) {
[Link](num1 + " ");
int sum = num1 + num2;
num1 = num2;
num2 = sum;
}
}
}
6. Program to reverse a string
import [Link];
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversedStr = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversedStr += [Link](i);
}
[Link]("Reversed string: " + reversedStr);
}
}
7. Program to find the largest of three numbers
import [Link];
public class LargestNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
[Link]("Enter third number: ");
int num3 = [Link]();
int largest;
if (num1 >= num2 && num1 >= num3) {
largest = num1;
} else if (num2 >= num1 && num2 >= num3) {
largest = num2;
} else {
largest = num3;
}
[Link]("The largest number is " + largest);
}
}
8. Program to check if a string is a palindrome
import [Link];
public class Palindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversedStr = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversedStr += [Link](i);
}
if ([Link](reversedStr)) {
[Link](str + " is a palindrome.");
} else {
[Link](str + " is not a palindrome.");
}
}
}
9. Program to swap two numbers without using a third variable
import [Link];
public class SwapNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
[Link]("After swapping: ");
[Link]("First number: " + num1);
[Link]("Second number: " + num2);
}
}
10. Program to find the sum of digits of a number
import [Link];
public class SumOfDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
[Link]("Sum of digits: " + sum);
}
}
[Link] to find the average of two numbers.
import [Link];
public class AverageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter the first number
[Link]("Enter the first number: ");
double num1 = [Link]();
// Prompt the user to enter the second number
[Link]("Enter the second number: ");
double num2 = [Link]();
// Calculate the average
double average = (num1 + num2) / 2;
// Print the result
[Link]("The average of " + num1 + " and " + num2 + " is: " + average);
// Close the scanner
[Link]();
}
}
12. Program to write Concatenate Two Strings .
import [Link];
public class StringConcatenation {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter the first string
[Link]("Enter the first string: ");
String str1 = [Link]();
// Prompt the user to enter the second string
[Link]("Enter the second string: ");
String str2 = [Link]();
// Concatenate the two strings
String concatenatedString = str1 + str2;
// Print the result
[Link]("The concatenated string is: " + concatenatedString);
// Close the scanner
[Link]();
}
}
13. Program to find leap year .
import [Link];
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a year
[Link]("Enter a year: ");
int year = [Link]();
// Check if the year is a leap year
boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
// Print the result
if (isLeapYear) {
[Link](year + " is a leap year.");
} else {
[Link](year + " is not a leap year.");
}
// Close the scanner
[Link]();
}
}
14. Program to Reverse of a number .
import [Link];
public class NumberReversal {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int number = [Link]();
// Reverse the number
int reversedNumber = 0;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit; number /= 10;
}
// Print the reversed number
[Link]("Reversed number: " + reversedNumber);
// Close the scanner
[Link]();
}
}
15. Program to find Sum of the natural numbers.
import [Link];
public class SumOfNaturalNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int n = [Link]();
// Calculate the sum of natural numbers up to n
int sum = 0; for (int i = 1; i <= n; i++)
{
sum += i;
}
// Print the sum
[Link]("Sum of natural numbers up to " + n + " is: " + sum);
// Close the scanner
[Link]();
}
}
16 . Program to find Armstrong's number.
import [Link];
public class ArmstrongNumber
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int number = [Link]();
int originalNumber, remainder, result = 0, n = 0; originalNumber = number;
// store the number of digits of number
while (originalNumber != 0)
{
originalNumber /= 10; ++n;
}
originalNumber = number;
// check if the number is an Armstrong number
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += [Link](remainder, n);
originalNumber /= 10;
}
if (result == number)
[Link](number + " is an Armstrong number.");
else [Link](number + " is not an Armstrong number.");
// Close the scanner
[Link]();
}
}
17. Program counts and prints the number of digits in a given number.
import [Link];
public class CountDigits
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int number = [Link]();
// Calculate the number of digits
int count = 0; int temp = number;
while (temp != 0)
{
temp /= 10; count++;
}
// Print the number of digits
[Link]("Number of digits in " + number + " is: " + count);
// Close the scanner
[Link]();
}
}
18. Program prints the multiplication table for a given number up to a specified range.
import [Link];
public class MultiplicationTable {
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
// Prompt the user to enter a number
[Link]("Enter a number: ");
int number = [Link]();
// Prompt the user to enter the range
[Link]("Enter the range: ");
int range = [Link]();
// Print the multiplication table
[Link]("Multiplication Table for " + number + ":");
for (int i = 1; i <= range; i++)
{
[Link](number + " * " + i + " = " + (number * i));
}
// Close the scanner
[Link]();
}
}
19 . Program to Find Maximum and Minimum in Array.
import [Link];
public class MaxMinArraySimple
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of elements in the array: ");
int n = [Link]();
// Declare an array of integers
int[] array = new int[n];
// Prompt the user to enter the elements of the array
[Link]("Enter the elements of the array:");
for (int i = 0; i < n; i++)
{
array[i] = [Link]();
}
// Initialize variables to store max and min values
int max = array[0]; int min = array[0];
// Iterate through the array to find max and min
for (int i = 1; i < n; i++)
{
if (array[i] > max)
{
max = array[i];
}
if (array[i] < min)
{
min = array[i];
}}
// Print the maximum and minimum elements
[Link]("Maximum element in the array: " + max);
[Link]("Minimum element in the array: " + min);
// Close the scanner
[Link]();
}
}
20. Program to find sum of element in array .
public class SimpleSumArray
{
public static void main(String[] args)
{
// Initialize an array of integers
int[] array = {2, 4, 6, 8, 10};
// Calculate the sum of elements in the array
int sum = 0; for (int num : array) { sum += num;
}
// Print the sum of elements
[Link]("Sum of elements in the array: " + sum);
}
}