1

How do you make squares from integer variables?

Here is my code:

#include <stdio.h>

int main(){

   int n, p1, p2, p3, p4;
   printf("Enter 4 numbers:")
   scanf("%d %d %d %d", &n, &p1, &p2, &p3);
   printf("%d %d %d %d\n", n, p1, p2, p3);
   return 0;

}

Edit 1: Thanks for all your answers guys! I would request for this thread to be closed.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

There are different ways of squaring a number. You will find four different ones below:

#include <stdio.h>

int main()
{
   int p1, p2, p3, p4;
   int p3s;

   printf("Enter 4 numbers:"); /* 
   scanf("%d %d %d %d", &p1, &p2, &p3, &p4);

   p1 = p1 * p1; /* square p1 value */

   p2 *= p2; /* square p2 value */

   p3s = p3 * p3; /* square p3 value */

   printf("%d %d %d %d\n", p1, p2, p3s, p4 * p4 /* square p4 value*/);

   return 0;
}

For (very) large numbers, the squares can overflow.

NZD
  • 1,780
  • 2
  • 20
  • 29
  • It works but is it possible to make a sum for all the squares? Sorry but I'm a noob to c/c++. –  Jul 30 '15 at 02:38
  • @Cakedy. Easy. Add `printf("%d\n", p1 + p2 + p3s + (p4 * p4));` after the last printf statement. – NZD Jul 30 '15 at 06:56