Armstrong Number java - java

I've got a problem with some simple code. I haven't seen where is the problem in my code. It returns false when it should return true, since 153 is an Armstrong Number.
Following is my code:
public class Armstrong {
static double nArms, unidad, decena, centena, aux;
Armstrong(){
}
Armstrong(double nArms){
this.nArms = nArms;
}
public boolean esArmstrong(double nArms){
aux = nArms % 100;
centena = nArms / 100;
decena = aux / 10;
unidad = aux % 10;
this.nArms = Math.pow(unidad, 3) + Math.pow(decena, 3) +Math.pow(centena, 3);
if(this.nArms == nArms){
return true;
}else{
return false;
}
}
public static void main(String[] args) {
Armstrong arms = new Armstrong();
System.out.println(arms.esArmstrong(153));
}
}

You are using double when you intend to do integer arithmetic. For example, when you write
centena = nArms / 100;
you are doing floating point division, (and centena is assigned the value 1.53) but you want to perform integer division. Use int, long (or BigInteger) instead.

As others already mentioned never use Double for Integer calculation
Now If I were you I would have optimized my code to this
int check=0;
int n=num; // num is user-input number
while(n>0)//suppose n =153
{
int rem=n%10;
check=check+(int)Math.pow(rem,3);
n=n/10;
}
if(check==num)
System.out.println(num+" is Armstrong");
/*First time while loop runs rem = 3 and n= 15
So check = 0+3*3*3=27
Second time while loop runs rem = 5 and n= 1
So check = 27+5*5*5 = 152
Again n =1 so rem = 1 and check = 152+1*1*1 = 153
This time the thw while fails the exits
ultimately check == num so it is armstrong */

import java.util.Scanner;
public class Armstrong {
public static void main(String args[]) {
System.out.println("Input number of digit to find out Armstrong");
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
String s[] = new String[r];
System.out.println("Please enter digits");
StringBuilder sb = new StringBuilder(r);
for (int i = 0; i < r; i++) {
String userInput = sc.next();
s[i] = userInput;
sb.append(s[i]);
}
int e = Integer.parseInt(sb.toString()); //this is the Integer value entered to check Armstrong number
int d;
int k[] = new int[r];
for (int j = 0; j < r; j++) {
d = Integer.parseInt(s[j]);
k[j] = d * d * d * d* d* d;
}
int m[] = new int[r + 1];
int n[] = new int[r];
for (int l = 1; l <= r; l++) {
n[l - 1] = m[l - 1] + k[l - 1];
m[l] = n[l - 1];
}
if (e == m[r]) {
System.out.println("Entered number is Armstrong number");
} else {
System.out.println("Entered number is not an Armstrong number");
}
}

public void isArmstrong(String n)
{
char[] s=n.toCharArray();
int sum=0;
for(char num:s)
{
int i=Integer.parseInt(Character.toString(num));
int cube=i*i*i;
sum +=cube;
}
if(sum==Integer.parseInt(n))
{
System.out.println("Its an Armstrong Number");
}
else
{
System.out.println("Its not an Armstrong Number");
}
}

import java.util.*;
public class Main
{
public static void check_armstrong(int n)
{
/*Function to check whether a number is an armstrong number or not
Print true if yes else false */
int sum=0;
int temp=n;
while(n>0){
int remainder=n%10;
sum+=remainder*remainder*remainder;
n=n/10;
}
if(temp==sum){
System.out.println(true);
}else
System.out.println(false);
/* Do not change the code beyond this point*/
}
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int n =sc.nextInt();
check_armstrong(n);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int a, c, d, e = 0, temp = 0;
a = sc.nextInt();
c = a;
int z = a;
while (c > 0) {
c = c / 10;
temp++;
}
System.out.println("//");
int temp2 = temp;
while (temp2 > 0) {
int temp1 = 1;
d = a % 10;
for (int i = 0; i < temp; i++) {
temp1 = temp1 * d;
}
e = e + temp1;
a = a / 10;
temp2--;
}
if (z == e) {
System.out.println("number is armstrong");
} else {
System.out.println("number is not armstrong");
}
}

Related

if condition is not working.when i try to execute if conditon,else is working

what's wrong with this?
if condition is not executing.
import java.util.Scanner;
public class ArmstrongNum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
int n = sc.nextInt();
int temp = n;
int rem = 0;
int sum = 0;
while (n != 0) {
rem = n % 10;
sum = sum + (rem * rem * rem);
n = n / 10;
}
if (temp == n) {
System.out.println("number is a AMSTRONG");
} else {
System.out.println("NUMBER IS NOT AMSTRONG");
}
}
}
Your logic is wrong. if(temp==n) { is after while(n!=0) { so the only way it could be true is if temp == 0.
Your Calcualtion is fine but your compare is wrong. For example the number 153, which is a armstrong number, your variables will have the following values at the end of the loop:
temp = 153
rem = 1
sum = 153
n = 0
So you should not compare temp to n temp == n , how you currently do but temp to sum temp == sum
Also take in mind that your check will only work for three digits numbers, because your power is allways three. So for other armstrong numbers it simply won't work, for example:
54748 = 55 + 45 + 75 + 45 + 85 = 3125 + 1024 + 16807 + 1024 + 32768
In this solution i used the Math libary and the power but there are more ways if you don't like this to calc the length of an number
public class ArmstrongNum {
private static final Scanner SC = new Scanner(System.in);
private static boolean isArmstrongNumber(int n){
int temp = n;
int rem;
int length = (int) (Math.log10(n) + 1);
int sum = 0;
while (n != 0) {
rem = n % 10;
sum = sum + (int) Math.pow(rem, length);
n = n / 10;
}
return temp == sum;
}
public static void main(String[] args) {
System.out.print("ENTER THE NUMBER: ");
int n = SC.nextInt();
if (isArmstrongNumber(n)) {
System.out.printf("%d is a Armstrong number", n);
} else {
System.out.printf("%d is no Armstrong number", n);
}
}
}
Try another way
public static void main(String[] args) {
try {
Scanner sc= new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
int n = sc.nextInt();
if (n == 0) {
throw new Exception("Number is 0");
}
int sum = 0;
String number = String.valueOf(n);
char[] chars = number.toCharArray();
double length = chars.length;
for (char item : chars) {
double result = Math.pow(Double.parseDouble(String.valueOf(item)), length);
sum = sum + (int) result;
}
if (sum == n ) {
System.out.println("number is a AMSTRONG");}
else {
System.out.println("NUMBER IS NOT AMSTRONG");
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}

Amstrong number return in blank result (java)

public class Amstrong {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
String a = String.valueOf(i);
int b = a.charAt(0);
int c = a.charAt(1);
int d = a.charAt(2);
int e = b * b * b + c * c * c + d * d * d;
if (e == i) {
System.out.println(i);
}
}
}
}
//Help me out please, no error occurred but the result returned in the blank
You are not converting the characters to digits correctly.
One of the possible ways to fix it:
int b = a.charAt(0)-'0';
int c = a.charAt(1)-'0';
int d = a.charAt(2)-'0';
Another way:
int b = Character.digit (a.charAt(0),10);
int c = Character.digit (a.charAt(1),10);
int d = Character.digit (a.charAt(2),10);
Either way will give your the output:
153
370
371
407
Another way you can do it is:
import java.util.*;
public class ArmstrongNumber3
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number");
int n = sc.nextInt();
int b = n;
int sum = 0;
int d = 0;
while (n != 0) {
d = n % 10;
sum += (d * d * d);
n = n / 10;
}
if (sum == b) {
System.out.println("Armstrong");
} else
{
System.out.println("Not Armstrong");
}
}
}
import java.util.Scanner;
public class Amstrong {
public static void main(String args[]) {
Integer num, temp, len = 0, initVal;
double finalVal = 0;
Scanner scn = new Scanner(System.in);
System.out.println("Enter the number");
num = scn.nextInt();
initVal = num;
temp = num;
while (temp != 0) {
temp = temp / 10;
len++;
}
for (int i = 0; i <= len; i++) {
temp = num % 10;
finalVal = finalVal + Math.pow(temp, len);
num = num / 10;
}
if (Double.valueOf(initVal) == finalVal) {
System.out.println("Amstrong");
} else {
System.out.println("Not Amstrong");
}
}
}

Add the factors of a number

public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int n = 0;
int g = 0;
int term = 0;
int temp = 0;
int sum = 0;
int factor = 1;
System.out.print("Input N:");
n = x.nextInt();
g = n;
if (n <= 0) {
System.out.println("Please enter a positive integer");
System.exit(0);
}
if (n > 0) {
System.out.print("The factors are:");
while (factor < n) {
if (n % factor == 0) {
System.out.print(factor + ",");
}
factor++;
}
}
}
If I input number 8, the factors are 1,2, and 4. What I am trying to achieve is to add the factors of 8 which are 1,2 and 4, which would result in 7.
import java.util.Scanner;
public class Demo {
public static void main(String[] args)
{
Scanner x=new Scanner(System.in);
int n=0;int g=0; int term=0;int temp=0;
int sum=0; int factor=1;
System.out.print("Input N:");
n=x.nextInt();
g=n;
int number = 0;
if (n<=0)
{
System.out.println("Please enter a positive integer");
System.exit(0);
}
if (n>0)
{
System.out.print("The factors are:");
while (factor<n)
{
if (n%factor==0)
{
System.out.println(factor+",");
number+=factor;
}
factor++;
}
}
System.out.println("Sum = "+number);
}
}
import static java.lang.System.*;
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner kb = new Scanner(in);
out.print("Enter a number: ");
int num = kb.nextInt();
int sum = 0;
int x = 1;
for(x = 1; x <= num; x++){
if (num % x == 0){
sum = sum + x;
}
}
out.print(sum);
}
}

JAVA How to return list of elements with amstrong numbers from interval [100; 999]

I am new in java and I have got assigment with armstrong numbers.
I am already created new class ArmstrongNumber.java where I initialized method from this website: http://www.programmingsimplified.com/java/source-code/java-program-armstrong-number
Now in a class where is main method I created another method where I am calling ArmstrongNumber class and now I have to return armstrong number from interval from [100 till 999].
There is where I am stuck now .
public static void armtrongNumbs()
{
ArmstrongNumber returnObj = new ArmstrongNumber(); // here i m calling class.
int start = 100;
int end = 999;
for(int i = start; i<= end; i++)
{
number = i + number;
returnObj.Armstrong(number);
}
//returnObj.Armstrong();
}
How could my loop return only armstrong numbers?
Edit: ArmstrongNumber class
class ArmstrongNumber
{
public void Armstrong(int number)
{
int n, sum = 0, temp, remainder, digits = 0;
Scanner in = new Scanner(System.in);
System.out.println("Input a number to check if it is an Armstrong number");
n = in.nextInt();
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
static int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
}
Based on your requirement, you need logic of ArmstrongNumber.java and mold it to suit as per your requirements.
You just need to use the following code and can stop worrying about using ArmstrongNumber.java
package hello;
public class Abc {
public static void main(String[] args) {
int n, sum, temp, remainder, digits;
int start = 100;
int end = 999;
for (int i = start; i <= end; i++) {
sum = 0;
digits = 0;
temp = i;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp / 10;
}
temp = i;
while (temp != 0) {
remainder = temp % 10;
sum = sum + power(remainder, digits);
temp = temp / 10;
}
if (i == sum)
System.out.println(i + " is an Armstrong number.");
}
}
static int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p * n;
return p;
}
}
Here you can see, how the sum and digits are initialised to zero for every number and then the rest of logic is same. You can verify that 153, 370, 371, 407 are printed as Armstrong numbers.
Hope this helps
try like
public int[] Armstrong(int start ,int end){
int a[],i=0;
for(int i = start; i<= end; i++)
{
number = i + number;
int n, sum = 0, temp, remainder, digits = 0;
Scanner in = new Scanner(System.in);
System.out.println("Input a number to check if it is an Armstrong number");
n = in.nextInt();
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
a[i++]=n;
else
System.out.println(n + " is not an Armstrong number.");
}
return a;
}
static int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p * n;
return p;
}
}

I'm trying to write a program for Palindromes but my output is not as expected

this is my script
public class palindrome {
public static void main(String[] args) {
int rev = 0;
Scanner r = new Scanner(System.in);
System.out.println("enter any value");
int n = r.nextInt();
while (n != 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
System.out.println(rev);
if (n == rev) {
System.out.println("number is palindrome");
} else {
System.out.println("not palindrome");
}
}
In your solution n is always 0.
public static boolean isPalindrome(int number) {
int palindrome = number; // copied number into variable
int reverse = 0;
while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
// if original and reverse of number is equal means
// number is palindrome in Java
if (number == reverse) {
return true;
}
return false;
}
http://java67.blogspot.co.il/2012/09/palindrome-java-program-to-check-number.html
You should make a copy of n into another variabile
public static void main(String[] args) {
int rev = 0;
Scanner r = new Scanner(System.in);
System.out.println("enter any value");
int n = r.nextInt();
int original = n;
while (n != 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
System.out.println(rev);
if (original == rev) {
System.out.println("number is palindrome");
} else {
System.out.println("not palindrome");
}

Categories