Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Monday, June 8, 2020

Program to calculate square of a number


C Program to calculate square of a number:
By Ahmad Irshad | Programming Exercises and Solutions > Calculate square of a number


Write a C program to calculate square of a given number. How to calculate and waht is the logic to find the square of a number is given below.

Required knowledge for this exercise is: 


Before writing any program you just imagine about output screen first. For example, what input to be given, and output to be displayed. Let's have a look at the below example: 

INPUT:

Enter any number = 5

OUTPUT:

Square of a given number is = 25


Logic to find square of a given number:

Step by step logic to find square of a given number.

1) Print a massage to the user like "Enter any number:" using printf();
2) Input a number a number using scanf(); and store it in a variable num.
3) Now calculate square using farmulla square = num * num;
4) At last print the value of square. that's it!

Similarly you can calculate the cube of any given number by using farmula 
cube = num * num* num;

Program to calculate square of a number: 

  1. /* C program to calculate square of a given number */ #include <stdio.h> int main() { int num, square; /* * Input a number from user */
  2. printf("Enter any number: "); scanf("%d", &num); /* * Calculate square of a given number */ square = num * num;
  3. /* * Print result of sqaur */ printf("Square of a given number %d is = %d\n", num, square); return 0; }

Output:

  1. Enter any  number = 5
  2. Square of a given number  5 is = 25

you can also write the above program by using pow() function. suppose you want to calculate power of a given number for example, 5 raised to the power 2. like (5²) = 5 * 5 = 25. Similarly 2 raised to the power 4. like 2^4 = 2 * 2 * 2 * 2 = 16. Now let's have a look at the below exmaple how to write program to find power of a number using pow() function.

Visit to know more about function

Program to calculate power of a given number using pow() function:

  1. /* C program to calculate power of a given number using pow(); */ #include <stdio.h> #include <math.h> int main() { int base, power, result;

  2. /* * Input base and power from user for example: 5 raised to the power 2
  3. * (i.e 5²) here, 5 is called base, and 2 is called power.
  4. */
  5. printf("Enter base: "); scanf("%d", &base)
  6.        printf("Enter power: ");
  7. scanf("%d", &power);
  8. /* Calculate the power of a given base number using pow();*/ result = pow(base, power);
  9. /* Print the result */  printf("%d raised to the power %d is = %d", base, power, result); return 0; }

Output:

  1. Enter base : 5
  2. Enter power : 2
  3. 5 raised to the power 2  = 25

  4. Enter base = 2
  5. Enter power = 4
  1. 2 raised to the power 4 = 16




Next Topic to be updated soon..>>




Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us