Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java เพื่อแสดงหมายเลข Armstrong ระหว่างสองช่วงเวลา


ในบทความนี้ เราจะเข้าใจวิธีการแสดงตัวเลข Armstrong ระหว่างตัวเลขสองตัวที่ระบุใน Java ตัวเลขอาร์มสตรองคือตัวเลขที่เท่ากับผลรวมของลูกบาศก์ของตัวเลขของมันเอง

จำนวนเต็มเรียกว่าหมายเลขลำดับอาร์มสตรอง n หากทุกหลักแยกออกและลูกบาศก์และสรุปผลรวมจะเหมือนกับตัวเลขเช่น abcd... =a3 + b3 + c3 + d3 + ...

ในกรณีของตัวเลขอาร์มสตรอง 3 หลัก ผลรวมของลูกบาศก์ของแต่ละหลักจะเท่ากับตัวเลขนั้นเอง ตัวอย่างเช่น 153 คือหมายเลข Armstrong

153 = 13 + 53 + 33

ตัวอย่างเช่น 370 คือหมายเลข Armstrong

370 = 27 + 343 + 0

ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -

ป้อนข้อมูล

สมมติว่าข้อมูลที่เราป้อนคือ −

1 & 500

ผลผลิต

ผลลัพธ์ที่ต้องการจะเป็น −

The Armstrong numbers between 1 and 500 are 1, 153, 370, 371, 407

อัลกอริทึม

Step1 - Start
Step 2 - Declare four integers: my_input_1, my_input_2, i and sum
Step 3 - Prompt the user to enter two integer value/ define the integers
Step 4 - Read the values
Step 5 - Run a for loop to generate Armstrong numbers using %, / and * operator
Step 6 - Divide by 10 and get remainder for ‘check’ .
Step 7 - Multiply ‘rem’ thrice, and add to ‘sum’, and make that the current ‘sum’.
Step 8 - Divide ‘check’ by 10, and make that the current ‘check’.
Step 9 - Display the result
Step 10 - Stop

ตัวอย่างที่ 1

ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์ของเรา โปรแกรม Java เพื่อแสดงหมายเลข Armstrong ระหว่างสองช่วงเวลา .

import java.util.Scanner;
public class ArmstrongNumbers {
   public static void main(String args[]){
      int my_low, my_high, check, my_rem, my_sum, i;
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("Required packages have been imported");
      System.out.println("A scanner object has been defined ");
      System.out.println("Enter the first number :");
      my_low = my_scanner.nextInt();
      System.out.println("Enter the limit :");
      my_high = my_scanner.nextInt();
      System.out.println("The Armstrong numbers are :");
      for (i = my_low; i<my_high; i++){
         my_sum = 0;
         check = i;
         while(check != 0) {
            my_rem = check % 10;
            my_sum = my_sum + (my_rem * my_rem * my_rem);
            check = check / 10;
         }
         if(my_sum == i){
            System.out.println(i);
         }
      }
   }
}

ผลลัพธ์

Required packages have been imported
A scanner object has been defined
Enter the first number :
1
Enter the limit :
500
The Armstrong numbers are :
1
153
370
371
407

ตัวอย่างที่ 2

ในที่นี้ มีการกำหนดจำนวนเต็มก่อนหน้านี้ และเข้าถึงและแสดงค่าบนคอนโซล

public class ArmstrongNumbers {
   public static void main(String args[]){
      int my_low, my_high, check, my_rem, my_sum, i;
      my_low = 1;
      my_high = 500;
      System.out.printf("The first number is %d and the limit is %d ", my_low, my_high);
      System.out.println("\nThe Armstrong numbers are :");
      for (i = my_low; i<my_high; i++){
         my_sum = 0;
         check = i;
         while(check != 0) {
            my_rem = check % 10;
            my_sum = my_sum + (my_rem * my_rem * my_rem);
            check = check / 10;
         }
         if(my_sum == i){
            System.out.println(i);
         }
      }
   }
}

ผลลัพธ์

The first number is 1 and the limit is 500
The Armstrong numbers are :
1
153
370
371
407