0% found this document useful (0 votes)
3 views13 pages

Basic Java Programs (3)

Uploaded by

Priyanshu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views13 pages

Basic Java Programs (3)

Uploaded by

Priyanshu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

1. Program to print "Hello, World!

"

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

2. Program to check if a number is even or odd

import java.util.Scanner;

public class EvenOdd {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();

if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}

3. Program to find the factorial of a number

import java.util.Scanner;

public class Factorial {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int factorial = 1;

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


factorial *= i;
}

System.out.println("Factorial of " + num + " is " + factorial);


}
}

4. Program to check if a number is prime

import java.util.Scanner;

public class PrimeNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isPrime = true;

if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}

5. Program to print Fibonacci series up to a given number

import java.util.Scanner;

public class FibonacciSeries {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int count = scanner.nextInt();
int num1 = 0, num2 = 1;

System.out.print("Fibonacci Series of " + count + " numbers: ");

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


System.out.print(num1 + " ");
int sum = num1 + num2;
num1 = num2;
num2 = sum;
}
}
}

6. Program to reverse a string

import java.util.Scanner;

public class ReverseString {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
String reversedStr = "";

for (int i = str.length() - 1; i >= 0; i--) {


reversedStr += str.charAt(i);
}

System.out.println("Reversed string: " + reversedStr);


}
}

7. Program to find the largest of three numbers

import java.util.Scanner;

public class LargestNumber {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter third number: ");
int num3 = scanner.nextInt();

int largest;

if (num1 >= num2 && num1 >= num3) {


largest = num1;
} else if (num2 >= num1 && num2 >= num3) {
largest = num2;
} else {
largest = num3;
}

System.out.println("The largest number is " + largest);


}
}

8. Program to check if a string is a palindrome

import java.util.Scanner;

public class Palindrome {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = scanner.nextLine();
String reversedStr = "";

for (int i = str.length() - 1; i >= 0; i--) {


reversedStr += str.charAt(i);
}

if (str.equals(reversedStr)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
}
}

9. Program to swap two numbers without using a third variable


import java.util.Scanner;

public class SwapNumbers {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();

num1 = num1 + num2;


num2 = num1 - num2;
num1 = num1 - num2;

System.out.println("After swapping: ");


System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
}
}

10. Program to find the sum of digits of a number

import java.util.Scanner;

public class SumOfDigits {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int sum = 0;

while (num != 0) {
sum += num % 10;
num /= 10;
}

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


}
}

11.Program to find the average of two numbers.


import java.util.Scanner;

public class AverageCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the first number

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


double num1 = scanner.nextDouble();

// Prompt the user to enter the second number

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


double num2 = scanner.nextDouble();

// Calculate the average

double average = (num1 + num2) / 2;

// Print the result

System.out.println("The average of " + num1 + " and " + num2 + " is: " + average);

// Close the scanner

scanner.close();
}
}

12. Program to write Concatenate Two Strings .

import java.util.Scanner;

public class StringConcatenation {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the first string


System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();

// Prompt the user to enter the second string

System.out.print("Enter the second string: ");


String str2 = scanner.nextLine();

// Concatenate the two strings

String concatenatedString = str1 + str2;

// Print the result

System.out.println("The concatenated string is: " + concatenatedString);

// Close the scanner


scanner.close();

}
}

13. Program to find leap year .

import java.util.Scanner;

public class LeapYearChecker {


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

// Prompt the user to enter a year


System.out.print("Enter a year: ");
int year = scanner.nextInt();

// Check if the year is a leap year


boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

// Print the result


if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
// Close the scanner
scanner.close();
}
}

14. Program to Reverse of a number .

import java.util.Scanner;

public class NumberReversal {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number

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


int number = scanner.nextInt();

// Reverse the number

int reversedNumber = 0;
while (number != 0) {
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit; number /= 10;
}
// Print the reversed number

System.out.println("Reversed number: " + reversedNumber);

// Close the scanner


scanner.close();
}
}

15. Program to find Sum of the natural numbers.

import java.util.Scanner;
public class SumOfNaturalNumbers {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


// Prompt the user to enter a number
System.out.print("Enter a number: ");
int n = scanner.nextInt();

// Calculate the sum of natural numbers up to n

int sum = 0; for (int i = 1; i <= n; i++)


{
sum += i;
}

// Print the sum


System.out.println("Sum of natural numbers up to " + n + " is: " + sum);

// Close the scanner


scanner.close();
}
}

16 . Program to find Armstrong's number.

import java.util.Scanner;
public class ArmstrongNumber
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number

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


int number = scanner.nextInt();
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 += Math.pow(remainder, n);
originalNumber /= 10;
}
if (result == number)
System.out.println(number + " is an Armstrong number.");
else System.out.println(number + " is not an Armstrong number.");

// Close the scanner


scanner.close();
}
}

17. Program counts and prints the number of digits in a given number.

import java.util.Scanner;

public class CountDigits


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

// Prompt the user to enter a number

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


int number = scanner.nextInt();

// Calculate the number of digits

int count = 0; int temp = number;


while (temp != 0)
{
temp /= 10; count++;
}

// Print the number of digits

System.out.println("Number of digits in " + number + " is: " + count);

// Close the scanner


scanner.close();
}
}

18. Program prints the multiplication table for a given number up to a specified range.

import java.util.Scanner;

public class MultiplicationTable {

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter a number

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


int number = scanner.nextInt();

// Prompt the user to enter the range

System.out.print("Enter the range: ");


int range = scanner.nextInt();

// Print the multiplication table

System.out.println("Multiplication Table for " + number + ":");


for (int i = 1; i <= range; i++)
{
System.out.println(number + " * " + i + " = " + (number * i));
}

// Close the scanner

scanner.close();
}
}

19 . Program to Find Maximum and Minimum in Array.

import java.util.Scanner;
public class MaxMinArraySimple
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();

// Declare an array of integers

int[] array = new int[n];

// Prompt the user to enter the elements of the array

System.out.println("Enter the elements of the array:");


for (int i = 0; i < n; i++)
{
array[i] = scanner.nextInt();
}
// 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

System.out.println("Maximum element in the array: " + max);


System.out.println("Minimum element in the array: " + min);

// Close the scanner


scanner.close();
}
}

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

System.out.println("Sum of elements in the array: " + sum);


}
}

You might also like