Uses the ? operator to square an integer value entered by the user. - C Operator

C examples for Operator:Conditional Operator

Introduction

However, this program preserves the sign (10 squared is 100 and -10 squared is -100).

Demo Code

#include <stdio.h>

int main(void)
{
   int isqrd, i;//from   ww w . j  av a2s .  c  om
   printf("Enter a number: ");
   scanf("%d", &i);

   isqrd = i>0 ? i*i : -(i*i);

   printf("%d squared is %d", i, isqrd);

   return 0;
}

Result


Related Tutorials