jeudi 12 août 2021

"if" condition problem in java Armstrong code

I wrote the armstrong number question in java by myself(sorry if its silly, I'm new to programming). The "result is supposed to give 1 value when i enter a coorect armstrong number but it gives 0,why?

Code-

import java.util.Scanner;
public class Exercise1_4 {
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int result = 0;
            int i = 0;
            int sum = 0;
            while (n > 0) {
                i = n % 10;
                sum += i * i * i;
                n = n / 10;
            }
            if (sum == n) {
                System.out.print("1");
            } else {
                System.out.print("0");
            }

Aucun commentaire:

Enregistrer un commentaire