BugCrush 2 Question 2 - BUGC202 - EDITORIAL

BugCrush 2 Question 2 - BUGC202 - EDITORIAL

PROBLEM LINK

Practice
Contest

Author: codechefsrm
Editorialist : codechefsrm

DIFFICULTY

EASY

PREREQUISITES

Mathematics, Loops

PROBLEM

Problem Description:
Program to find the Armstrong numbers.

Input:
407

Output:
0 1 2 3 4 5 6 7 8 9 153 370 371 407

Rules:
Bugs will be mainly logical errors, syntax errors etc. They are specific to C language.
Since it is a debugging contest, you will be provided with a bugged solution below the Constraints section.
Please, see that you must adhere to the problem code provided, make changes only where necessary.
Participants can copy the code and compile it using an online compiler (Eg. CodeChef IDE).
Once the bugs are eliminated from the code, the clean code should be submitted by using the “Submit” button on the top-right corner.
Participants will be penalised for changing the entire problem solution or writing their own solution, completely different from the buggy code as provided in the
problem statement as our main intention is to test the debugging abilities of the participants.

Buggy Code in C

Please copy the following code and paste it into your compiler for debugging.

#include <stdio/h>
#include &lt;math/h>
void is_armstrong(number)
{
  int i=0,sum=0,rem=0,n=0,p=0,q=0;
  for(i=0,i<=number,i++)
  {
    p=i;
    q=i;
    while(p!==0)
    {
      p=p/10;
      n++;
    }
   while(q>0)
   {
     rem=q;
     sum+=pow(rem,n);
     q=q/10;
   }
   if(sum=i)
    printf("%d ",sum)
    p=0;
    q=0;
    n=0;
    sum=0;
  }
  
}
int main() {
	int x;
  scanf("%d",&x);
  is_armstrong[x];
	return 0;
}

Solution:

#include <stdio.h>
#include <math.h>
void is_armstrong(int number)
{
  int i=0,sum=0,rem=0,n=0,p=0,q=0;
  for(i=0;i<=number;i++)
  {
    p=i;
    q=i;
    
    while(p!=0)
    {
      p=p/10;
      n++;
    }
    
   while(q>0)
   {
     rem=q%10;
     sum+=pow(rem,n);
     q=q/10;
   }
   if(sum==i)
    printf("%d ",sum);
    p=0;
    q=0;
    n=0;
    sum=0;
  }
  
}

int main() {
	int x;
  scanf("%d",&x);
  is_armstrong(x);
	return 0;
}

Explanation

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. The above code
prints the Armstrong numbers between 0 to x