0% found this document useful (0 votes)
5 views5 pages

0java Programs

The document contains multiple Java programs demonstrating basic programming concepts such as generating Fibonacci series, finding prime numbers, working with one-dimensional and two-dimensional arrays, checking for Armstrong and palindrome numbers, and calculating factorials. Each program includes user input and outputs the results based on the provided input. The examples illustrate fundamental programming techniques and algorithms.

Uploaded by

pbpiyushbest
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)
5 views5 pages

0java Programs

The document contains multiple Java programs demonstrating basic programming concepts such as generating Fibonacci series, finding prime numbers, working with one-dimensional and two-dimensional arrays, checking for Armstrong and palindrome numbers, and calculating factorials. Each program includes user input and outputs the results based on the provided input. The examples illustrate fundamental programming techniques and algorithms.

Uploaded by

pbpiyushbest
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

FIBONACCI SERIES

import [Link]; public


class FibonacciSeries
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of terms: ");
int n = [Link](); int firstTerm = 0,
secondTerm = 1;
[Link]("Fibonacci Series up to " + n + " terms:");
for (int i = 1; i <= n; ++i)
{
[Link](firstTerm + " "); int
nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
[Link]();
}
}
Enter the number of terms: 10 Fibonacci
Series up to 10 terms:
0 1 1 2 3 5 8 13 21 34

PRIME NUMBERS
import [Link]; public
class PrimeNumbers
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the upper limit to find prime numbers: ");
int limit = [Link]();
[Link]("Prime numbers up to " + limit + " are:");
for (int num = 2; num <= limit; num++)
{
if (isPrime(num)) {
[Link](num + " ");
}
}
[Link]();
}
Enter the upper limit to find prime numbers: 30 Prime
numbers up to 30 are:
2 3 5 7 11 13 17 19 23 29

ONE DIMENSIONAL
ARRAY
public class OneDimensionalArray
{
public static void main(String[] args)
{ int[] numbers = {10, 20, 30, 40, 50};
[Link]("Array elements:");
for (int i = 0; i < [Link]; i++)
{
[Link]("Element at index " + i + ": " + numbers[i]);
}
[Link]("\nArray elements using enhanced for loop:");
for (int number : numbers)
{
[Link](number);
}
}
}
Array elements:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Array elements using enhanced for loop:
10
20
30
40
50

TWO-DIMENSIONAL
ARRAY
public class TwoDimensionalArray
{
public static void main(String[] args)
{
// Declare and initialize a 2D array (3 rows and 4 columns)
int[][] matrix = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
[Link]("2D Array (Matrix):");
for (int i = 0; i < [Link]; i++)
{
for (int j = 0; j < matrix[i].length; j++)
{
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
2D Array (Matrix):
1234
5678 9
10 11 12
import [Link];

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

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


int number = [Link]();

if (isArmstrong(number)) {
[Link](number + " is an Armstrong number.");
} else {
[Link](number + " is not an Armstrong number.");
}

[Link]();
}

public static boolean isArmstrong(int number) {


int originalNumber = number;
int sum = 0;
int numberOfDigits = [Link](number).length();

while (number > 0) {


int digit = number % 10;
sum += [Link](digit, numberOfDigits);
number /= 10;
}

return sum == originalNumber;


}
}
Enter a number: 153
153 is an Armstrong number.
Enter a number: 123
123 is not an Armstrong number.

PALINDROME NUMBER
import [Link]; public class
PalindromeNumber { public static void
main(String[] args) { Scanner scanner =
new Scanner([Link]);

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


int number = [Link]();

if (isPalindrome(number)) {
[Link](number + " is a palindrome number.");
} else {
[Link](number + " is not a palindrome number.");
}

[Link]();
}

public static boolean isPalindrome(int number) {


int originalNumber = number;
int reversedNumber = 0;

while (number > 0) {


int digit = number % 10; // Get the last digit
reversedNumber = reversedNumber * 10 + digit; // Build the reversed number
number /= 10; // Remove the last digit
}

return originalNumber == reversedNumber; // Check if original and reversed are the same
}
}
Enter a number: 121 121 is
a palindrome number.
Enter a number: 123
123 is not a palindrome number.

FACTORIAL NUMBER
import [Link]; public class Factorial {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);

[Link]("Enter a non-negative integer: ");


int number = [Link]();

if (number < 0) {
[Link]("Factorial is not defined for negative numbers.");
} else {
long factorial = calculateFactorial(number);
[Link]("Factorial of " + number + " is: " + factorial);
}

[Link]();
}

public static long calculateFactorial(int n) {


long result = 1; for
(int i = 1; i <= n; i++) {
result *= i; // Multiply result by i
}
return result;
}
}
Enter a non-negative integer: 5
Factorial of 5 is: 120

You might also like