Computer Programming - Numbers

Every programming language provides support for manipulating different types of numbers such as simple whole integers and floating point numbers. C, Java, and Python categorize these numbers in several categories based on their nature.
Let's go back and check the data types chapter, where we listed down the core data types related to numbers −
These data types are called primitive data types and you can use these data types to build more data types, which are called user-defined data types.
We have seen various mathematical and logical operations on numbers during a discussion on operators. So we know how to add numbers, subtract numbers, divide numbers, etc.
First let's see how to print various types of numbers available in C programming language −
#include <stdio.h>

int main() {
   short  s;
   int    i;
   long   l;
   float  f;
   double d;

   s = 10;
   i = 1000;
   l = 1000000;
   f = 230.47;
   d = 30949.374;

   printf( "s: %d\n", s);
   printf( "i: %d\n", i);
   printf( "l: %ld\n", l);
   printf( "f: %.3f\n", f);
   printf( "d: %.3f\n", d);
}
Rest of the coding is very obvious, but we used %.3f to print float and double, which indicates the number of digits after the decimal to be printed. When the above program is executed, it produces the following result −
s: 10
i: 1000
l: 1000000
f: 230.470
d: 30949.374

The following table lists down various useful built-in mathematical functions available in C programming language which can be used for various important mathematical calculations.
For example, if you want to calculate the square root of a number, for example, 2304, then you have a built-in function available to calculate the square root.
double cos(double);
This function takes an angle (as a double) and returns the cosine.
double sin(double);
This function takes an angle (as a double) and returns the sine.
double tan(double);
This function takes an angle (as a double) and returns the tangent.
double log(double);
This function takes a number and returns the natural log of that number.
double pow(double, double);
The first is a number you wish to raise and the second is the power you wish to raise it to.
double hypot(double, double);
If you pass this function the length of two sides of a right triangle, it will return the length of the hypotenuse.
double sqrt(double);
You pass this function a number and it returns its square root.
int abs(int);
This function returns the absolute value of an integer that is passed to it.
double fabs(double);
This function returns the absolute value of any decimal number passed to it.
double floor(double);
Finds the integer which is less than or equal to the argument passed to it.
Following is a simple example to show a few mathematical operations. To utilize these functions, you need to include the math header file <math.h> in your program in the same way you included stdio.h −
#include <stdio.h>
#include <math.h>

int main() {
   short  s;
   int    i;
   long   l;
   float  f;
   double d;

   printf( "sin(s): %f\n", sin(10));
   printf( "abs(i): %f\n", abs(1000));
   printf( "floor(f): %f\n", floor(230.47));
   printf( "sqrt(l): %f\n", sqrt(1000000));
   printf( "pow(d, 2): %f\n", pow(2.374, 2));
}
When the above program is executed, it produces the following result −
sin(s): -0.544021
abs(i): -0.544021
floor(f): 230.000000
sqrt(l): 1000.000000
pow(d, 2): 5.635876

Besides the above usage, you will use numbers in loop counting, flag representation, true or false values in C programming.
Following is the equivalent program written in Java. Java provides almost all the numeric data types available in C programming.
You can try to execute the following program to see the output, which is identical to the result generated by the above C example.
public class DemoJava {
   public static void main(String []args) {
      short  s;
      int    i;
      long   l;
      float  f;
      double d;

      s = 10;
      i = 1000;
      l = 1000000L;
      f = 230.47f;
      d = 30949.374;

      System.out.format( "s: %d\n", s);
      System.out.format( "i: %d\n", i);
      System.out.format( "l: %d\n", l);
      System.out.format( "f: %f\n", f);
      System.out.format( "d: %f\n", d);
   }
}
When the above program is executed, it produces the following result −
s: 10
i: 1000
l: 1000000
f: 230.470001
d: 30949.374000

Java also provides a full range of built-in functions for mathematical calculation and you can use them in the same way as you did in C programming.
Python is a little different from C and Java; it categorizes numbers in int, long, float and complex. Here are some examples of numbers in Python −
Following is the equivalent program written in Python −
s = 10
i = 1000
l = 1000000
f = 230.47
d = 30949.374

print "s: ", s
print "i: ", i
print "l: ", l
print "f: ", f
print "d: ", d
When the above program is executed, it produces the following result −
s:  10
i:  1000
l:  1000000
f:  230.47
d:  30949.374

Python also provides a full range of built-in functions for mathematical calculations and you can use them in the same way you have used them in C programming.

    107 Lectures 
     13.5 hours 

 Arnab Chakraborty

    106 Lectures 
     8 hours 

 Arnab Chakraborty

    99 Lectures 
     6 hours 

 Arnab Chakraborty

    46 Lectures 
     2.5 hours 

 Shweta

    70 Lectures 
     9 hours 

 Abhilash Nelson

    52 Lectures 
     7 hours 

 Abhishek And Pukhraj
 Print
 Add Notes
 Bookmark this page