Home > Enterprise >  I want to print a series of Armstrong numbers which lie between m and n. Here m and n are the two in
I want to print a series of Armstrong numbers which lie between m and n. Here m and n are the two in

Time:01-06

I am trying to print the series but whenever I set the range (input given by me) above 407. I only get the output till 407. However, when I set the range below 407 it gives me the result according to the input I have given. Can anybody tell me what I'm doing wrong?

I used an online compiler (www.onlinegdb.com) to write my code.

Here is the code.

#include<stdio.h>  
#include<stdlib.h>
int 
main () 
{
  
int m, n;
 
printf 
    ("Enter two numbers to find the Armstrong numbers that lie between them.\n");
scanf ("%d%d", &m, &n);

system("clear");

if(m>n)
{
    m = m   n;
    n = m - n;
    m = m - n;
}

for (; m < n; m  )
{

int i = m   1, r, s = 0, t;

t = i;

while (i > 0)
{
 r = i % 10;
 s = s   (r * r * r);
 i = i / 10;
}

if (t == s)
printf ("%d ", t);
}

return 0;
}



enter image description here

enter image description here

CodePudding user response:

Try this code!!!


#include <math.h>
#include <stdio.h>



int main() {



  int low, high, number, originalNumber, rem, count = 0;
  double result = 0.0;



  printf("Enter two numbers(intervals): ");
  scanf("%d %d", &low, &high);



  printf("Armstrong numbers between %d and %d are: ", low, high);



  // swap numbers if high < low
  if (high < low) {
    high  = low;
    low = high - low;
    high -= low;
  }


   
  // iterate number from (low   1) to (high - 1)
  // In each iteration, check if number is Armstrong
  for (number = low   1; number < high;   number) {
    originalNumber = number;



    // number of digits calculation
    while (originalNumber != 0) {
      originalNumber /= 10;
        count;
    }



    originalNumber = number;



    // result contains sum of nth power of individual digits
    while (originalNumber != 0) {
      rem = originalNumber % 10;
      result  = pow(rem, count);
      originalNumber /= 10;
    }



    // check if number is equal to the sum of nth power of individual digits
    if ((int)result == number) {
      printf("%d ", number);
    }



    // resetting the values
    count = 0;
    result = 0;
  }





  return 0;


}

CodePudding user response:

Try this code :

#include <stdio.h>
#include <math.h>

int main()
{
int start, end, i, temp1, temp2, remainder, n = 0, result = 0;

printf(“Enter start value and end value : “);
scanf(“%d %d”, &start, &end);
printf(“\nArmstrong numbers between %d an %d are: “, start, end);

for(i = start   1; i < end;   i)
{
temp2 = i;
temp1 = i;

while (temp1 != 0)
{
temp1 /= 10;
  n;
}

while (temp2 != 0)
{
remainder = temp2 % 10;
result  = pow(remainder, n);
temp2 /= 10;
}

if (result == i) {
printf(“%d “, i);
}

n = 0;
result = 0;

}
printf(“\n”);
return 0;
}
  •  Tags:  
  • Related