-2

This is a simple program which adds digits of a given number

#include <stdio.h>
#include <conio.h>
void main()
{
    float sum = 0, num;
    printf("Enter any number\n");
    scanf("%f", num);
    while(num!=0)
    {
        sum = sum + (num%10);
        num = num/10;
    }
    printf("Addition of digits of %d is %d", num, sum);
    getch();
}

It throws me this error

In function 'main': error: invalid operands to binary % (have 'float' and 'int') idk why I tried to understand but there's no error and it doesn't print string which I have entered

1 Answers1

0

Quoting C11, chapter 6.5.5

The operands of the % operator shall have integer type.

num is a floating point variable. You may need to use fmod() instead.

That said,

 scanf("%f", num);

is wrong, you need to supply an address to a float, not a float variable itself. At least you'd need

 scanf("%f", &num);

keeping aside the success and sanity check.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261