How to divide int * type?

61 views Asked by At

I'm trying to find from input to if is "Armstrong Number" or not. Here's my code. Probably I have another arrow but I can't divide (/) or multiplication (*) with int * variables. Why is that?
Also what is the best way to finding "Armstrong Number" and put information for user?

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

int main()
{
   int n = 0, c = 0, result = 0;
   printf( "Enter a number: " );
   scanf("%d", &n);
   int digit = log10(n)+1;

   int ntemp1 = n;
   while (ntemp1 != 0){
      ntemp1 /= 10;
      c++;
   }

   ntemp1 = n;
   for (int i = 0; i < c; i++){
      result += pow(ntemp1 % 10, c);
      ntemp1 /= 10;
   }
   if (result == n)
      printf("\nIt is an Armstrong Number!\n");
   else
      printf("\nIt is not an Armstrong Number!\n");
}

Also can I make this with arrays or maybe have different ways?

1

There are 1 answers

0
ikegami On
scanf("%d", s);

Undefined behaviour since s is NULL. You need to pass a pointer to somewhere scanf can store an int.


scopy /= 10;

Dividing a pointer makes no sense. Division requires a number.


scopy[j]

If we ignore the division to get to this spot, you assigned NULL to scopy, and is derefencing a NULL pointer is undefined behaviour.


As for fixes, I don't know what you are trying to do, but your code should start with

unsigned n = 0;
printf( "Enter a number: " );
if ( scanf( "%u", &n ) != 1 ) {
   fprintf( stderr, "Invalid input.\n" );
   exit( 1 );
}