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

Java Basics Code

The document presents basic Java programs that demonstrate fundamental programming concepts. It includes examples for reversing a number, checking for prime numbers, generating Fibonacci series, calculating factorials, identifying Armstrong numbers, checking for palindromes, summing digits, and finding factors of a number. Each program is accompanied by code snippets illustrating the implementation of these concepts.

Uploaded by

diyakrishna399
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 views2 pages

Java Basics Code

The document presents basic Java programs that demonstrate fundamental programming concepts. It includes examples for reversing a number, checking for prime numbers, generating Fibonacci series, calculating factorials, identifying Armstrong numbers, checking for palindromes, summing digits, and finding factors of a number. Each program is accompanied by code snippets illustrating the implementation of these concepts.

Uploaded by

diyakrishna399
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
You are on page 1/ 2

Basic Java Programs

1. Reverse a Number

int num = 1234, rev = 0;


while(num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
System.out.println("Reversed: " + rev);

2. Check Prime Number

int num = 29;


boolean isPrime = true;
for(int i = 2; i <= num/2; i++) {
if(num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(num + " is prime? " + isPrime);

3. Fibonacci Series

int n = 10, a = 0, b = 1;
System.out.print("Fibonacci: ");
for(int i = 0; i < n; i++) {
System.out.print(a + " ");
int next = a + b;
a = b;
b = next;
}

4. Factorial

int n = 5, fact = 1;
for(int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);

5. Armstrong Number

int num = 153, temp = num, sum = 0;


while(temp != 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
System.out.println(num + " is Armstrong? " + (sum == num));

6. Palindrome Number

int num = 121, temp = num, rev = 0;


while(temp != 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp /= 10;
}
System.out.println(num + " is Palindrome? " + (rev == num));

7. Sum of Digits

int num = 1234, sum = 0;


while(num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of digits: " + sum);

8. Factors of a Number

int num = 12;


System.out.print("Factors: ");
for(int i = 1; i <= num; i++) {
if(num % i == 0) {
System.out.print(i + " ");
}
}

You might also like