Write C program to print all strong numbers between 2 numbers


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write C program to print all strong numbers between 2 numbers


  1. #include <stdio.h>
  2.  
  3. // Function declarations
  4. long long fact(int num);
  5. void printStrongNumbers(int start, int end);
  6.  
  7.  
  8. int main()
  9. {
  10. int start, end;
  11.  
  12. /* Input start and end range */
  13. printf("Enter the lower limit: ");
  14. scanf("%d", &start);
  15. printf("Enter the upper limit: ");
  16. scanf("%d", &end);
  17.  
  18. printf("List of strong numbers between %d to %d are: \n", start, end);
  19. printStrongNumbers(start, end);
  20.  
  21. return 0;
  22. }
  23.  
  24.  
  25.  
  26. //Printing all strong numbers in a given range
  27. void printStrongNumbers(int start, int end)
  28. {
  29. long long sum;
  30. int num;
  31.  
  32. // Iterates from start to end
  33. while(start != end)
  34. {
  35. sum = 0;
  36. num = start;
  37.  
  38. // Calculating sum of factorial of digits
  39. while(num != 0)
  40. {
  41. sum += fact(num % 10);
  42. num /= 10;
  43. }
  44.  
  45. // If sum of factorial of digits equal to current number
  46. if(start == sum)
  47. {
  48. printf("%d ", start);
  49. }
  50.  
  51. start++;
  52. }
  53. }
  54.  
  55. //Recursively find factorial of any number
  56. long long fact(int num)
  57. {
  58. if(num == 0)
  59. return 1;
  60. else
  61. return (num * fact(num-1));
  62. }
  63.  

Result

C language

     





Related Topics




Popular Posts