[Go to site: main page, start]

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

Java Programs for Palindrome and Armstrong

Uploaded by

Sachin Angre
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 views3 pages

Java Programs for Palindrome and Armstrong

Uploaded by

Sachin Angre
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

1 Problem Statement

Write a Program whether number entered is palindrome or not.

Instructions

Input: MaM

Output: palindrome or Not palindrome

class PalindromeExample{

public static void main(String args[]){

int r,sum=0,temp;

int n=454;//It is the number variable to be checked for palindrome

temp=n;

while(n>0){

r=n%10; //getting remainder

sum=(sum*10)+r;

n=n/10;

if(temp==sum)

[Link]("palindrome number ");

else

[Link]("not palindrome");

}
Console input : MaM

2 Problem Statement

Write a Program whether number entered is armstrong or not.

Instructions

To find entered number is Armstrong or not.

Input: 371

output: Not Armstrong and Armstrong

import [Link].*;

public class Main

public static void main(String[] args)

Scanner myObj = new Scanner([Link]);

int number = [Link]();

int originalNumber, remainder, result = 0;

originalNumber = number;

while (originalNumber != 0)

remainder = originalNumber % 10;

result += [Link](remainder, 3);


originalNumber /= 10;

if(result == number)

[Link]("Armstrong");

else

[Link]("Not Armstrong");

3 Problem Statement

Write a Program to print all even from 1 to N using for loop.

Instructions

To print the list of even numbers.

Input: 2

output: 24

Common questions

Powered by AI

An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. The provided Java program checks for Armstrong numbers by initially storing the input number as the original number. It then repeatedly extracts each digit, raises it to the power of three (since the example uses three-digit numbers), and sums these values to calculate the result. After processing all digits, it compares this sum to the initial number. If they are equal, it is an Armstrong number; otherwise, it is not .

The provided Java program uses a for loop to print all even numbers from 1 to N. The loop begins with an initializer that starts at the first even number (i=2) and iterates up to the input number N. Within the loop, the program prints the current number on each iteration, then increments the counter by 2 to ensure only even numbers are selected. This process continues until the loop reaches the specified N, effectively printing all even numbers between the range of 1 to N inclusive .

Potential input validation issues include handling non-numeric or invalid inputs for both palindrome and Armstrong number checks, which require numeric inputs by design. To address this, the input could be wrapped in a try-catch block to handle exceptions or be accompanied by a pre-check for numeric input before proceeding with calculations. Additionally, edge cases, such as zero or negative inputs, should be considered to ensure meaningful inputs or gracefully handle invalid or non-standard cases .

One potential improvement could involve error-checking mechanisms such as validating input to ensure it is numerical where necessary, especially with user input. Additionally, the palindrome program could be improved by accounting for leading zeros in the reversed number, which may be problematic in some contexts. Other improvements might include modularizing the code into functions for readability and reusability or implementing input error checking to manage unexpected inputs .

Algorithm choice affects performance primarily through time complexity and memory usage. The numerical algorithms used for both palindrome and Armstrong detection are efficient for small numbers due to their linear time complexity. However, as numbers become very large, these algorithms may become slower, especially for the Armstrong check which requires multiple power calculations per digit. Optimization can improve performance in larger cases, such as using memoization for calculating powers or parallelizing digit examination in hardware supportive environments. Algorithm choices inherently limit scalability when moving from fixed-length to arbitrary-length numeral systems, and redesigns might be needed for significantly larger scales .

String-based methods for palindrome detection involve converting the number into a string and checking if the string is the same forwards and backwards, effectively creating the reverse by simple string operations. This differs from numerical methods, which involve the algorithmic reverse and comparison of numbers directly. While numerical methods inherently work with the numeric data type and might be slightly more memory efficient, string methods are often simpler to implement and understand, especially when built-in language functions are used. However, both methods result in a similar time complexity of O(n).

The computational complexity for both the palindrome and Armstrong number checking algorithms is O(n), where n is the number of digits in the number. For palindromes, the number must be reversed by processing each digit once, thus requiring linear time relative to the number of digits. Similarly, the Armstrong number checker processes each digit to compute the power and accumulate the sum, which also involves examining each digit in a single pass through the number .

Potential edge cases for the palindrome detection algorithm include numbers with leading zeros, which might affect the interpretation of the reversed number if treated numerically. Another case is negative numbers, which the program might not handle correctly since negative signs are not flipped, making them inherently non-palindromic in typical scenarios. Lastly, single digit numbers trivially always result in true, and continuous zeros could disrupt reversing assumptions; thus, handling these deviations explicitly ensures the robustness of the algorithm .

The algorithm for determining if a number is a palindrome in the provided Java program involves reversing the number and comparing it to the original. The process begins by storing the original number in a temporary variable. It then involves repeatedly extracting the last digit using modulo operation, constructing the reversed number by multiplying the previous result by 10 and adding the last digit, and finally dividing the original number by 10 to remove the last digit. This continues until the original number is reduced to zero. If the reversed number matches the original stored in the temporary variable, the number is a palindrome .

To accommodate numbers with more than three digits when checking for Armstrong numbers, the program should calculate the power dynamically based on the number of digits in the input number. This can be achieved by first determining the number of digits, which can be done by converting the number to a string and obtaining its length, or by iterating through divisions by 10 until the number is exhausted. Then, each digit would be raised to this number of digits rather than a fixed value of three, allowing the program to correctly determine if numbers with any number of digits are Armstrong numbers .

You might also like