About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Friday, August 30, 2013

C Program to print table of n and square of n using pow()

Print Table of n and Square of n

#include<stdio.h>
#include<conio.h>
void main()
{
      int n;
      printf("Not Square");
      printf("-----------------n");
      for(n=1;n <=10;n++)
          printf("%d\t%d\n",n,n*n);
      getch();
}
Output :
 No Square
 -------------
  1 1
  2 4
  3 9
  4 16
  5 25
  6 36
  7 49
  8 64
  9 81
 10 100

Explanation of Program :

We can alternately write above program like this -
printf("%d\t%d\n",n,pow(n,2));
In order to use above line in the code we need to include math.h header file inside C program.
#include<math.h>

Alternate Way Using Pow :

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
      int n;
      printf("Not Square\n");
      printf("-----------------n");
      for(n=1;n <=10;n++)
          printf("%d\t%d\n",n,pow(n,2));
      getch();
}

No comments: