Computer Programming - Numbers

Every programming language provides support for manipulating different types of numbers like simple whole integer, floating point number. The programming languages like C, Java and Python categorize these numbers in several categories based on their nature.
Let's go back and check data types chapter, where we listed down core data types related to numbers:
TypeKeywordValue range which can be represented by this data type
Numberint-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
Small Numbershort-32,768 to 32,767
Long Numberlong-2,147,483,648 to 2,147,483,647
Decimal Numberfloat1.2E-38 to 3.4E+38 till 6 decimal places
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 type.
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>

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 number of digits after decimal to be printed. When above program is executed, it produces the following result:
s: 10
i: 1000
l: 1000000
f: 230.470
d: 30949.374

Math Operations on Numbers

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 square root of a number for example, 2304, then you have built-in function available to calculate square root for this number.
S.N.Function & Purpose
1double cos(double);
This function takes an angle (as a double) and returns the cosine.
2double sin(double);
This function takes an angle (as a double) and returns the sine.
3double tan(double);
This function takes an angle (as a double) and returns the tangent.
4double log(double);
This function takes a number and returns the natural log of that number.
5double pow(double, double);
The first is a number you wish to raise and the second is the power you wish to raise it to.
6double hypot(double, double);
If you pass this function the length of two sides of a right triangle, it will return you the length of the hypotenuse.
7double sqrt(double);
You pass this function a number and it gives you this square root.
8int abs(int);
This function returns the absolute value of an integer that is passed to it.
9double fabs(double);
This function returns the absolute value of any decimal number passed to it.
10double floor(double);
Finds the integer which is less than or equal to the argument passed to it.
Following a simple example to show few of the mathematical operations. To utilize these functions, you need to include the math header file <math.h> header file in your program in similar way you have included stdio.h:
#include <stdio.h>
#include <math.h>

main()
{
   short  s;
   int    i;
   long   l;
   float  f;
   double d;
   
   s = 10;      
   i = 1000;    
   l = 1000000; 
   f = 230.47;  
   d = 2.374;
   
   printf( "sin(s): %f\n", sin(s));
   printf( "abs(i): %f\n", abs(i));
   printf( "floor(f): %f\n", floor(f));
   printf( "sqrt(f): %f\n", sqrt(f));
   printf( "pow(d, 2): %f\n", pow(d, 2));

}
When above program is executed, it produces the following result:
sin(s): -0.544021
abs(i): -0.544021
floor(f): 230.000000
sqrt(f): 15.181238
pow(d, 2): 5.635876
Other than above usage, you will use numbers in loop counting, flag representation, true or false values in C programming.

Numbers in Java

Following is the equivalent program written in Java programming language. Java programming language also provides almost all 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 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 very similar way you have used them in C programming.

Numbers in Python

Python is little different from C and Java and categorize numbers in int, long, float and complex. Here are some examples of numbers in Python:
intlongfloatcomplex
1051924361L0.03.14j
100-0x19323L15.2045.j
-7860122L-21.99.322e-36j
0800xDEFABCECBDAECBFBAEl32.3+e18.876j
-0490535633629843L-90.-.6545+0J
-0x260-052318172735L-32.54e1003e+26J
0x69-4721885298529L70.2-E124.53e-7j
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 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 calculation and you can use them in very similar way you have used them in C programming.
It was easy to learn about numbers in computer because we are playing with numbers from childhood. Numbers are simple 1, 2, 3.....10.20, 300.345, etc.
It's even further easy to learn about characters in computer programming because you are playing with characters even before you started playing with numbers. Yes these are simple alphabets like a, b, c, d....A, B, C, D, .....but with an exception that in computer programming any single digit number like 0, 1, 2,....and special characters like $, %, +, -.... etc., are also treated as characters and to assign them in a character type variable you simply need to put them inside a single quotes. For example, following statement defines a character type variable ch and we assign a value 'a' to it:
char ch = 'a';
Here, ch is a variable of character type which can hold a character of the implementation's character set and 'a' is called character literal or a character constant. Not only a, b, c,....but when any number like 1, 2, 3.... or any special character like !, @, #, #, $,.... is kept inside a single quotes, then they will be treated as a character literal and can be assigned to a variable of character type, so following is a valid statement:
char ch = '1';
A character data type consumes 8 bits of memory which means you can store anything in a character whose ASCII value lies in between -127 to 127, so in total it can hold one of 256 different values. Bottom-line is that a character data type can store any of the characters available on your keyboard including special characters like !, @, #, #, $, %, ^, &, *, (, ), _, +, {, }, etc.
It's worth to explain it little further that you can keep only a single alphabet or single digit number inside single quotes and more than one alphabets or digits are not allowed inside single quotes. So following statements are invalid in C programming:
char ch1 = 'ab';
char ch2 = '10';
Following is a simple example, which shows how to define, assign and print characters in C Programming language:
#include <stdio.h>

main()
{
   char  ch1;
   char  ch2;
   char  ch3;
   char  ch4;
   
   ch1 = 'a';      
   ch2 = '1';
   ch3 = '$';
   ch4 = '+';  

   printf( "ch1: %c\n", ch1);
   printf( "ch2: %c\n", ch2);
   printf( "ch3: %c\n", ch3);
   printf( "ch4: %c\n", ch4);
}
Here, we used %c to print a character data type. When above program is executed, it produces the following result:
ch1: a
ch2: 1
ch3: $
ch4: +

Escape Sequences:

Many programming languages support a concept called Escape Sequence. So when a character is preceded by a backslash (\), then it is called an escape sequence and has special meaning to the compiler. For example, following is a valid character and it is called new line character:
char ch = '\n';
Here, character n has been preceded by a backslash (\), so now it has special meaning which is a new line but keep in mind that backslash (\) has special meaning with few characters only, so following will not have any meaning in C programming and it will be assumed as an invalid statement:
char ch = '\1';
Following table shows correct escape sequences available in C programming language:
Escape SequenceDescription
\tInserts a tab in the text at this point.
\bInserts a backspace in the text at this point.
\nInserts a newline in the text at this point.
\rInserts a carriage return in the text at this point.
\fInserts a form feed in the text at this point.
\'Inserts a single quote character in the text at this point.
\"Inserts a double quote character in the text at this point.
\\Inserts a backslash character in the text at this point.
Following is a simple example which shows how the compiler interprets an escape sequence in a print statement:
#include <stdio.h>

main()
{
   char  ch1;
   char  ch2;
   char  ch3;
   char  ch4;
   
   ch1 = '\t';      
   ch2 = '\n';


   printf( "Test for tabspace %c and a newline %c will start here", ch1, ch2);
}
When above program is executed, it produces the following result:
Test for tabspace     and a newline 
 will start here

Characters in Java

Following is the equivalent program written in Java programming language. Java programming language handles character data type in similar way as we have seen in C programming language. Though Java provides additional support for character manipulation which you will to know when you will drill down this programming language.
You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.
public class DemoJava
{    
   public static void main(String []args) 
   {

      char  ch1;
      char  ch2;
      char  ch3;
      char  ch4;
   
      ch1 = 'a';      
      ch2 = '1';
      ch3 = '$';
      ch4 = '+';  

      System.out.format( "ch1: %c\n", ch1);
      System.out.format( "ch2: %c\n", ch2);
      System.out.format( "ch3: %c\n", ch3);
      System.out.format( "ch4: %c\n", ch4);
    
   }
}
When above program is executed, it produces the following result:
ch1:  a
ch2:  1
ch3:  $
ch4:  +
Java also supports escape sequence in very similar way you have used them in C programming.

Characters in Python

Python does not support any character data type but all the characters are treated as string, which is a sequence of characters and we will study strings in a separate chapter. But you do not need to have any special arrangement while using a single character in Python.
Following is the equivalent program written in Python:
ch1 = 'a';      
ch2 = '1';
ch3 = '$';
ch4 = '+'; 

print "ch1: ", ch1
print "ch2: ", ch2
print "ch3: ", ch3
print "ch4: ", ch4
When above program is executed, it produces the following result:
ch1:  a
ch2:  1
ch3:  $
ch4:  +
Python also supports escape sequence in very similar way you have used them in C programming.
Consider a situation, where we need to store 5 integer numbers. If we use programming's simple variable and data type concepts, then we need 5 variables of int data type and program will be something as follows:
#include <stdio.h>

main()
{
   int  number1;
   int  number2;
   int  number3;
   int  number4;
   int  number5;
   
   number1 = 10;      
   number2 = 20;   
   number3 = 30;   
   number4 = 40; 
   number5 = 50;     

   printf( "number1: %d\n", number1);
   printf( "number2: %d\n", number2);
   printf( "number3: %d\n", number3);
   printf( "number4: %d\n", number4);
   printf( "number5: %d\n", number5);
}
It was simple, because we had to store just 5 integer numbers. Now let's assume we have to store 5000 integer numbers, so what is next? Are we going to use 5000 variables?
To handle such situation, almost all the programming languages provide a concept called the array. An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
So instead of declaring individual variables, such as number1, number2, ..., and number99, you just declare one array variable number of integer type and use number1[0], number1[1], and ..., number1[99] to represent individual variables. Here, 0, 1, 2, .....99 are index associated with var variable and they are being used to represent individual elements available in the array.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Arrays in C

Create Arrays

To create an array variable in C, a programmer specifies the type of the elements and the number of elements to be stored in that array. Following is a simple syntax to create an array in C programming:
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, now to declare a 10-element array called number of type int , use this statement:
int number[10];
Now, number is a variable array, which is sufficient to hold up to 10 integer numbers.

Initializing Arrays

You can initialize array in C either one by one or using a single statement as follows:
int number[5] = {10, 20, 30, 40, 50};
The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:
int number[] = {10, 20, 30, 40, 50};
You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array:
number[4] = 50;
The above statement assigns element number 5th in the array with a value of 50. All arrays have 0 as the index of their first element which is also called base index and last index of an array will be total size of the array minus 1. Following is the pictorial representation of the same array we discussed above:
Array Presentation

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example:
int var = number[9];
The above statement will take 10th element from the array and assign the value to var variable. Following is an example, which will use all the above-mentioned three concepts viz. creation, assignment and accessing arrays:
#include <stdio.h>
 
int main ()
{
   int number[10]; /* number is an array of 10 integers */
   int i = 0;
 
   /* Initialize elements of array n to 0 */         
   while( i < 10 )
   {
      /* Set element at location i to i + 100 */
      number[ i ] = i + 100;
      i = i + 1;
   }
   
   /* Output each array element's value */
   i = 0;
   while( i < 10 )
   {
      printf("number[%d] = %d\n", i, number[i] );
      i = i + 1;
   }
 
   return 0;
}
When the above code is compiled and executed, it produces the following result:
number[0] = 100
number[1] = 101
number[2] = 102
number[3] = 103
number[4] = 104
number[5] = 105
number[6] = 106
number[7] = 107
number[8] = 108
number[9] = 109

Arrays in Java

Following is the equivalent program written in Java programming language. Java programming language also supports array, but there is a little difference to create them in different ways using new operator available in Java programming language.
You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.
public class DemoJava
{    
   public static void main(String []args) 
   {
      int[] number = new int[10];
      int i = 0;
      
      while( i < 10 )
      {
         number[ i ] = i + 100;
         i = i + 1;
      }

      i = 0;
      while( i < 10 )
      {
         System.out.format( "number[%d] = %d\n", i, number[i] );
         i = i + 1;
      }
   }
}
When above program is executed, it produces the following result:
number[0] = 100
number[1] = 101
number[2] = 102
number[3] = 103
number[4] = 104
number[5] = 105
number[6] = 106
number[7] = 107
number[8] = 108
number[9] = 109

Arrays (Lists) in Python

Python does not have a concept of Array, instead Python provides another data structure called list, which provides similar functionality as arrays in any other language.
Following is the equivalent program written in Python:
# Following defines an empty list.
number = []
i = 0

while i < 10:
   # Appending elements in the list
   number.append(i + 100)
   i = i + 1

i = 0
while i < 10:
   # Accessing elements from the list
   print "number[", i,  "] = ", number[ i ]
   i = i + 1
When above program is executed, it produces the following result:
number[ 0 ] =  100
number[ 1 ] =  101
number[ 2 ] =  102
number[ 3 ] =  103
number[ 4 ] =  104
number[ 5 ] =  105
number[ 6 ] =  106
number[ 7 ] =  107
number[ 8 ] =  108
number[ 9 ] =  109