Home > Mobile >  Program prints 0 as result of multiplication of two positive integers obtained via scanf("%f&qu
Program prints 0 as result of multiplication of two positive integers obtained via scanf("%f&qu

Time:09-28

My Code:

#include <stdio.h>

int main()
{
    int l, b, a;
    printf("Enter the length of the rectangle: ");
    scanf("%f", &l);

    printf("Enter the breadth of the rectangle: ");
    scanf("%f", &b);

    printf("Area of rectangle is %f", l * b);
    return 0;
}

As I give any input it doesn't show me its product, but 0.000000 instead:

As I given input 2 and 3 it should print Area of rectangle is 6

CodePudding user response:

%f expects its corresponding argument to have type float and you are passing int to it, so changing it to %d would fix the issue, as %d expects its corresponding argument to have type int.

#include <stdio.h>
 
int main() {
   int length, breadth, area;
 
   printf("\nEnter the Length of Rectangle: ");
   scanf("%d", &length);
 
   printf("\nEnter the Breadth of Rectangle: ");
   scanf("%d", &breadth);
 
   area = length * breadth;
   printf("\nArea of Rectangle: %d\n", area);
 
   return 0;
}
  • Related