[Go to site: main page, start]

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

Java Basic Programs

The document contains a series of basic Java programs demonstrating fundamental programming concepts such as calculating factorials, checking for palindromes, reversing strings, and determining prime numbers. Each program includes user input, processing logic, and output examples. The programs also cover topics like Fibonacci series, swapping variables, finding the largest element in an array, counting vowels, Armstrong numbers, and more.

Uploaded by

varshii092004
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)
2 views13 pages

Java Basic Programs

The document contains a series of basic Java programs demonstrating fundamental programming concepts such as calculating factorials, checking for palindromes, reversing strings, and determining prime numbers. Each program includes user input, processing logic, and output examples. The programs also cover topics like Fibonacci series, swapping variables, finding the largest element in an array, counting vowels, Armstrong numbers, and more.

Uploaded by

varshii092004
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

JAVA BASIC PROGRAMS:

1 Factorial:
import [Link];

public class Factorial {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

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

int num = [Link]();

int fact = 1;

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

fact *= i;

[Link]("Factorial of " + num + " is: " + fact);

Output:

Enter a number: 5

Factorial of 5 is: 120

2 palindrome:
import [Link];

public class PalindromeCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

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

String str = [Link]();

String reversed = "";

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

reversed += [Link](i);
}

if ([Link](reversed)) {

[Link](str + " is a palindrome.");

} else {

[Link](str + " is not a palindrome.");

Output:

Enter a string: madam

madam is a palindrome.

3 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 reversed = "";

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

reversed += [Link](i);

[Link]("Reversed string: " + reversed);

Output:

Enter a string: hello

Reversed string: olleh


4 Prime or not:
import [Link];

public class PrimeCheck {

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

Output:

Enter a number: 29

29 is a prime number.
5 Fibonacci:
import [Link];

public class FibonacciSeries {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter number of terms: ");

int n = [Link]();

int a = 0, b = 1;

[Link]("Fibonacci Series: ");

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

[Link](a + " ");

int next = a + b;

a = b;

b = next;

Output:

Enter number of terms: 10

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

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

6 Swapping of 2 variables without 3rd variable:


public class SwapWithoutTemp {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

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

int a = [Link]();

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


int b = [Link]();

a = a + b;

b = a - b;

a = a - b;

[Link]("After swapping:");

[Link]("a = " + a);

[Link]("b = " + b);

Output:

Enter first number (a): 6

Enter second number (b): 3

After swapping:

a=3

b=6

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

7 Largest element in an array:


import [Link];

import [Link];

public class LargestInArray {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter size of the array: ");

int n = [Link]();

int[] arr = new int[n];

[Link]("Enter " + n + " elements:");

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

arr[i] = [Link]();

}
int max = arr[0];

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

if (arr[i] > max)

max = arr[i];

[Link]("Largest element is: " + max);

Output:

Enter size of the array: 5

Enter 5 elements:

Largest element is: 5

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

8 Count of vowels in a string:


import [Link];

public class CountVowels {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

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

String input = [Link]().toLowerCase();

int count = 0;

for (int i = 0; i < [Link](); i++) {

char ch = [Link](i);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {


count++;

[Link]("Number of vowels: " + count);

Output:

Enter a string: varsha

Number of vowels: 2

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

9 Amstrong number:
import [Link];

public class ArmstrongNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

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

int num = [Link](), original = num, sum = 0;

while (num != 0) {

int digit = num % 10;

sum += digit * digit * digit;

num /= 10;

if (sum == original)

[Link](original + " is an Armstrong number.");

else

[Link](original + " is not an Armstrong number.");

Output:
Enter a number: 555

555 is not an Armstrong number.

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

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

Output:

Enter a number: 65

65 is Odd

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

11 Reverse Array:
import [Link];

import [Link];

public class ReverseArray {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter array size: ");


int n = [Link]();

int[] arr = new int[n];

[Link]("Enter array elements:");

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

arr[i] = [Link]();

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

int temp = arr[i];

arr[i] = arr[n - i - 1];

arr[n - i - 1] = temp;

[Link]("Reversed array: " + [Link](arr));

Output:

Enter array size: 5

Enter array elements:

3 6 7 2 10

Reversed array: [10, 2, 7, 6, 3]

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

12 Sum of elements in a array:


import [Link];

public class ArraySum {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter array size: ");

int n = [Link]();

int[] arr = new int[n];

int sum = 0;

[Link]("Enter elements:");
for (int i = 0; i < n; i++) {

arr[i] = [Link]();

sum += arr[i];

[Link]("Sum of array elements: " + sum);

Output:

Enter array size: 4

Enter elements:

2658

Sum of array elements: 21

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

13 Find Duplicate Elements in an Array:


import [Link];

public class FindDuplicates {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter array size: ");

int n = [Link]();

int[] arr = new int[n];

[Link]("Enter array elements:");

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

arr[i] = [Link]();

[Link]("Duplicate elements:");

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

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

if (arr[i] == arr[j]) {

[Link](arr[i]);
break;

Output:

Enter array size: 4

Enter array elements:

1233

Duplicate elements:

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

14 Count Digits in a Number:


import [Link];

public class CountDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

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

int num = [Link](), count = 0;

if (num == 0) count = 1;

else {

while (num != 0) {

num /= 10;

count++;

[Link]("Number of digits: " + count);

}
}

Output:

Enter a number: 6865

Number of digits: 4

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

15 Find Minimum and Maximum in an Array:


import [Link];

public class MinMaxInArray {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter array size: ");

int n = [Link]();

int[] arr = new int[n];

[Link]("Enter array elements:");

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

arr[i] = [Link]();

int min = arr[0], max = arr[0];

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

if (arr[i] < min) min = arr[i];

if (arr[i] > max) max = arr[i];

[Link]("Minimum element: " + min);

[Link]("Maximum element: " + max);

Output:

Enter array size: 4

Enter array elements:

78 54 6 96
Minimum element: 6

Maximum element: 96

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

You might also like