Author Topic: Error in an Armstrong number. with other IDE it results fine.  (Read 3766 times)

ramanuj.in

  • Guest
Error in an Armstrong number. with other IDE it results fine.
« on: January 31, 2013, 04:21:45 pm »
Please find error in the program code copied below also attached in .c file.

Input: 153
Result: Not an Armstrong number
Expected:  An Armstrong number
Error: Computing pow(5) resulting 124, though it shall give the correct value.
 
Procedure: Storing 5 in array of lane[1] then storing pow(lane[1],5) in array of cube[1]. Cube[1] resulting a erogenous value of 124 but it shall give the correct value 125.

Please find the solution. And let me know.

Thank you,
Regards,
Ramanuj Dad  

Code:

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

void main()
{
int gnt,ent,lane[5],j,cube[5];

j=0;lane[0]=0;lane[1]=0;lane[2]=0;lane[3]=0;lane[4]=0;lane[5]=0;ent=0;gnt=0;
cube[0]=0;cube[1]=0;cube[2]=0;cube[3]=0;cube[4]=0;cube[5]=0;

printf("Enter any number between from 0 to 100000:");
scanf("%d",&gnt);

ent=gnt;

    printf("\nThis is number printed from each array location:");

    if(ent>=10000 && ent<=99999)
    {
        lane[j]=ent/10000;
        ent=ent-(lane[j]*10000);
        printf("%d",lane[j]);
        j++;
    }

    if(ent>=1000 && ent<=9999)
    {
        lane[j]=ent/1000;
        ent=ent-(lane[j]*1000);
        printf("%d",lane[j]);
        j++;
    }

    if(ent>=100 && ent<=999)
    {
        lane[j]=ent/100;
        ent=ent-(lane[j]*100);
        printf("%d",lane[j]);
        j++;
    }

    if(ent>=10 && ent<=99)
    {
        lane[j]=ent/10;
        ent=ent-(lane[j]*10);
        printf("%d",lane[j]);
        j++;
    }

    if(ent>=1 && ent<=9)
    {
        lane[j]=ent/1;
        ent=ent-(lane[j]*1);
        printf("%d",lane[j]);
        j++;
    }

    cube[0]=pow(lane[0],3);
    cube[1]=pow(lane[1],3);
    cube[2]=pow(lane[2],3);
    cube[3]=pow(lane[3],3);
    cube[4]=pow(lane[4],3);

    if(cube[0]+cube[1]+cube[2]+cube[3]+cube[4]==gnt)
    {
        printf("\n\n%d is an armstrong number.",gnt);
    }
    else
    {
        printf("\n\n%d is not an armstrong number.",gnt);
    }

    /*printf("\nlane [1] is %d, cube [1] is %d",lane[1],cube[1]);*/

}

[attachment deleted by admin]
« Last Edit: January 31, 2013, 04:35:24 pm by ramanuj.in »

Offline jarod42

  • Multiple posting newcomer
  • *
  • Posts: 87
Re: Error in an Armstrong number. with other IDE it results fine.
« Reply #1 on: January 31, 2013, 05:02:17 pm »
Unrelated to CodeBlocks.

Note that you have a buffer overflow by assigning lane[5] and cube[5].

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Error in an Armstrong number. with other IDE it results fine.
« Reply #2 on: January 31, 2013, 06:00:19 pm »
Please respect the forum rules and do not ask questions not related to C::B in this forum/website.
Thank you !

Topic locked !