Ad Code

Responsive Advertisement

Finding Armstrong Numbers within a Range by deafult assign by values from and to, in all language's code available PYTHON,JAVA,C,CPP

 

Finding Armstrong Numbers within a Range by deafult assign by values from and to

PYTHON,JAVA,C,CPP

Python code

lower = 1
upper = 200
for num in range(lower, upper + 1):
order = len(str(num))
sum = 0
temp = num

while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10

if num == sum:
print(num, end=",") 

Output:

1,2,3,4,5,6,7,8,9,153,

    Explanation about the above code clearly explained below:

    • The Python code directly assigns values to lower and upper.
    • The code uses a for loop with the range function to iterate through each number in the given range.
    • The len(str(num)) expression is used to calculate the number of digits in num.
    • The code uses ** for exponentiation.
    • The output is done using the print function. The end parameter ensures numbers are printed with a comma as a separator.

    Java code

    public class NarcissisticNumbers {

        public static void main(String[] args) {

            int lower = 1;

            int upper = 200;

            int num, order, sum, temp, digit;

            

            for (num = lower; num <= upper; num++) {

                order = (int) Math.floor(Math.log10(num)) + 1;

                sum = 0;

                temp = num;

                

                while (temp > 0) {

                    digit = temp % 10;

                    sum += Math.pow(digit, order);

                    temp /= 10;

                }

                

                if (num == sum) {

                    System.out.print(num + ",");

                }

            }

        }

    }

    Output:

    1,2,3,4,5,6,7,8,9,153,

      Explanation about the above code clearly explained below:

      1. The Java code defines a class named ArmstrongNumbers with a main method.
      2. The variables and loops have the same purpose and behavior as in the C and C++ codes.
      3. The Math.floor function is used to calculate the logarithm of num and is cast to an int.
      4. The Math.pow function is used for exponentiation.
      5. The output is done using System.out.print instead of printf or std::cout.





      C code

      #include <stdio.h>

      #include <math.h>


      int main() {

          int lower = 1;

          int upper = 200;

          int num, order, sum, temp, digit;

          

          for (num = lower; num <= upper; num++) {

              order = floor(log10(num)) + 1;

              sum = 0;

              temp = num;

              

              while (temp > 0) {

                  digit = temp % 10;

                  sum += pow(digit, order);

                  temp /= 10;

              }

              

              if (num == sum) {

                  printf("%d,", num);

              }

          }

          

          return 0;

      }

      Output:

      1,2,3,4,5,6,7,8,9,153,


      Explanation about the above code clearly explained below:

      • The C code uses the <stdio.h> and <math.h> libraries for input/output and mathematical functions, respectively.

      • The lower and upper variables define the range for finding Armstrong numbers.
      • The order variable calculates the number of digits in num using the floor(log10(num)) + 1 expression.
      • The sum, temp, and digit variables are used for the calculations.
      • The for loop iterates through each number in the range from lower to upper.
      • The while loop extracts each digit from temp using modulo arithmetic and calculates the sum of digits raised to the power of order.
      • The printf statement outputs the Armstrong numbers found.


      C++ code

      #include <iostream>
      #include <cmath>

      int main() {
          int lower = 1;
          int upper = 200;
          int num, order, sum, temp, digit;
          
          for (num = lower; num <= upper; num++) {
              order = floor(log10(num)) + 1;
              sum = 0;
              temp = num;
              
              while (temp > 0) {
                  digit = temp % 10;
                  sum += pow(digit, order);
                  temp /= 10;
              }
              
              if (num == sum) {
                  std::cout << num << ",";
              }
          }
          
          return 0;
      }

      Output:

      1,2,3,4,5,6,7,8,9,153,

        Explanation about the above code clearly explained below:

        • The C++ code includes the <iostream> and <cmath> libraries for input/output and mathematical functions, respectively.
        • The variables and loops have the same purpose and behavior as in the C code.
        • The std::cout statement is used for output instead of printf.
        • The program returns 0 to indicate successful execution.

        Post a Comment

        0 Comments

        Close Menu