Calculate Square of a Number in C and C++

Following code calculate the square of a number in C and C++ language. Just write or copy the code in Turbo C IDE, compile and execute it.

C Source Code

   1:  #include<stdio.h> 
   2:  #include<conio.h> 
   3:  void main(void) 
   4:  { 
   5:  clrscr (); 
   6:  int num,square=0; 
   7:  printf("Enter Number to calculate the square="); 
   8:  scanf ("%d",&num); 
   9:  square = num*num; 
  10:  printf ("Square of Number:%d = %d",num,square); 
  11:  getch(); 
  12:  }

C++ Source Code

   1:  #include<stdio.h> 
   2:  #include<conio.h> 
   3:  #include<iostream.h> 
   4:  void main(void) 
   5:  { 
   6:  clrscr (); 
   7:  int num,square=0; 
   8:  cout<<"Enter Number to calculate the square="; 
   9:  cin>>num; 
  10:  square = num*num; 
  11:  cout<<"Square of Number of:"<<num<<" is "<<square; 
  12:  getch(); 
  13:  }
Be Sociable, Share!

One response to Calculate Square of a Number in C and C++

Leave a Reply