The problem of determining the number of Armstrong Number

I am trying to check if the number provided by the user is armstrong number . Something is wrong, although I can not understand.

Any help is appreciated.

The code below.

#include<stdio.h>

int fun(int);

int main()
{
    int x,a,b,y=0;

    printf("enter the number you want to identify is aN ARMSTRONG OR NOT:");
    scanf("%d",&a);

    for(int i=1 ; i<=3 ; i++)
    {
        b = a % 10;
        x = fun(b);
        y = x+y;
        a = a/10;
    }

    if(y==a)
        printf("\narmstrong number");
    else
        printf("\nnot an armstrong number");

    return 0;
}

int fun(int x)
{
    int a;
    a=x*x*x;
    return (a);
}
+3
source share
5 answers

The main problem is that you are not keeping a record of the number you start with. You divide aby 10 times (ends with 0), and then compare 0 with 153. They are not equal.

, 4- , 1-, 1. fun() cube(); , power(), N- .


, power() - .. 6-10 , . -DDEBUG, , , , . , ; . , , , . . scanf(), , .

146511208, . 370 371 .

#include <stdio.h>
#include <stdbool.h>

#ifndef DEBUG
#define DEBUG 0
#endif

static int power(int x, int n)
{
    int r = 1;
    int c = n;
    while (c-- > 0)
        r *= x;
    if (DEBUG) printf("    %d**%d = %d\n", x, n, r);
    return r;
}

static bool isArmstrongNumber(int n)
{
    int y = 0;
    int a = n;
    int p;
    for (p = 0; a != 0; a /= 10, p++)
        ;
    if (DEBUG) printf("    n = %d, p = %d\n", n, p);
    a = n;
    for (int i = 0; i < p; i++)
    {
        y += power(a % 10, p);
        a /= 10;
    }
    return(y == n);
}

int main(void)
{
    while (1)
    {
        int a;
        printf("Enter the number you want to identify as an Armstrong number or not: ");
        if (scanf("%d", &a) != 1 || a <= 0)
            break;
        else if (isArmstrongNumber(a))
            printf("%d is an Armstrong number\n", a);
        else
            printf("%d is not an Armstrong number\n", a);
    }

    return 0;
}
+3

, a ( ). 1, 153, 370, 371, 407. , for test, a , .

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

int power(int, int);
int numberofdigits(int);

//Routine to test if input is an armstrong number.
//See: http://en.wikipedia.org/wiki/Narcissistic_number if you don't know
//what that is. 

int main()
{
    int input;
    int digit;
    int sumofdigits = 0;

    printf("enter the number you want to identify as an Armstrong or not:");
    scanf("%d",&input);

    int candidate = input;
    int digitcount = numberofdigits(input);

    for(int i=1 ; i <= digitcount ; i++) 
    {
        digit = candidate % 10;
        sumofdigits = sumofdigits + power(digit, digitcount);
        candidate = candidate / 10;
    }

    if(sumofdigits == input)
        printf("\n %d is an Armstrong number", input);
    else
        printf("\n %d is NOT an Armstrong number", input);
    return 0;
}

int numberofdigits(int n);
{
  return log10(n) + 1;
}

int power(int n, int pow)
{
  int result = n;
  int i=1;
  while (i < pow) 
  {
    result = result * n; 
    i++;
  }
}

:

  • , ; , , .
  • . : int x,a,b,y=0; , vars 0 y. vars, . . , , .
  • : , armstrong, . , , . , , , , , , .
  • WTF fun(x) do?. fun() , ?
  • , armstrong3, , return (input == 153) || (input == 370) || ....
+2
/* 
Name: Rakesh Kusuma

Email Id:  rockykusuma@gmail.com

Title: Program to Display List of Armstrong Numbers in 'C' Language

*/



#include<stdio.h>

#include<math.h>

int main()

{

int temp,rem, val,max,temp1,count;

int num;

val=0;

num=1;

printf("What is the maximum limit of Armstrong Number Required: ");

scanf("%d",&max);

printf("\nSo the list of Armstrong Numbers Before the number %d are: \n",max);

while(num <=max)

    {         
        count = 0;

        temp1 = num;

        while(temp1!=0)

        {
            temp1=temp1/10;

            count++;
        }   

        if(count<3)

        count = 3;

            temp = num;

            val = 0;

            while(temp>0)

            {

                rem = temp%10;

                val = val+pow(rem,count);

                temp = temp/10;

            }

            if(val==num)

            {

                printf("\n%d", num);

            }

     num++; 

    }

 return 0;

 }
0

Mark No: Armstrong or Do not use C language

#include<stdio.h>
#include<conio.h>
void main()
{
    A:
    int n,n1,rem,ans;
    clrscr();
    printf("\nEnter No. :: ");
    scanf("%d",&n);

    n1=n;
    ans=0;
    while(n>0)
    {
        rem=n%10;
        ans=ans+(rem*rem*rem);
        n=n/10;
    }

    if(n1==ans)
    {
        printf("\n Your Entered No. is Armstrong...");
    }
    else
    {
        printf("\n Your Entered No. is not Armstrong...");
    }

    printf("\n\nPress 0 to Continue...");
    if(getch()=='0')
    {
        goto A;
    }
    printf("\n\n\tThank You...");
    getch();
}
0
source

All Articles