Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))

Let's begin by writing our first C program that prints the message "Hello, world!" on the display console:

Hello, world!

Step 1: Write the Source Code: Enter the following source codes using a programming text editor (such as NotePad++ for Windows or gEdit for UNIX/Linux/Mac) or an Interactive Development Environment (IDE) (such as CodeBlocks, Eclipse, NetBeans or MS Visual Studio - Read the respective "How-To" article on how to install and get started with these IDEs).

Do not enter the line numbers (on the left panel), which were added to help in the explanation. Save the source file as "

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
4". A C source file should be saved with a file extension of "
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
5". You should choose a filename which reflects the purpose of the program.

1
2
3
4
5
6
7
8
9

Step 2: Build the Executable Code: Compile and Link (aka Build) the source code "

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
4" into executable code ("
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
7" in Windows or "
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
8" in UNIX/Linux/Mac).

  • On IDE (such as CodeBlocks), push the "Build" button.
  • On Text editor with the GNU GCC compiler, start a CMD Shell (Windows) or Terminal (Mac, Linux) and issue these commands:
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    where
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    9 is the name of GCC C compiler;
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    0 option specifies the output filename ("
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    7" for Windows or "
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    8" for UNIX/Linux/Mac); "
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    4" is the input source file.

Step 3: Run the Executable Code: Execute (Run) the program.

  • On IDE (such as CodeBlocks), push the "Run" button.
  • On Text Editor with GNU GCC compiler, issue these command from CMD Shell (Windows) or Terminal (UNIX/Linux/Mac):
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
Brief Explanation of the Program

/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the compiler. But they provide useful explanation and documentation to your readers (and to yourself three days later). There are two kinds of comments:

  1. Multi-line Comment: begins with
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    4 and ends with
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    5. It may span more than one lines (as in Lines 1-3).
  2. End-of-line Comment: begins with
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    6 and lasts until the end of the current line (as in Lines 4, 6, 7, 8, and 9).

#include
The "

// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
7" is called a preprocessor directive. A preprocessor directive begins with a
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
8 sign, and is processed before compilation. The directive "
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
7" tells the preprocessor to include the "
1
2
3
4
5
6
7
8
9
10
0" header file to support input/output operations. This line shall be present in all our programs. I will explain its meaning later.

int main() { ...... }
defines the so-called

1
2
3
4
5
6
7
8
9
10
1 function. The
1
2
3
4
5
6
7
8
9
10
1 function is the entry point of program execution.
1
2
3
4
5
6
7
8
9
10
1 is required to return an
1
2
3
4
5
6
7
8
9
10
4 (integer).

printf("Hello, world!\n");
We invoke the function

1
2
3
4
5
6
7
8
9
10
5 to print the string "Hello, world!" followed by a newline (
1
2
3
4
5
6
7
8
9
10
6) to the console. The newline (
1
2
3
4
5
6
7
8
9
10
6) brings the cursor to the beginning of the next line.

return 0;
terminates the

1
2
3
4
5
6
7
8
9
10
1 function and returns a value of 0 to the operating system. Typically, return value of 0 signals normal termination; whereas value of non-zero (usually 1) signals abnormal termination. This line is optional. C compiler will implicitly insert a "
1
2
3
4
5
6
7
8
9
10
9" to the end of the
1
2
3
4
5
6
7
8
9
10
1 function.

C Terminology and Syntax

Statement: A programming statement performs a piece of programming action. It must be terminated by a semi-colon (

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1) (just like an English sentence is ended with a period) as in Lines 7 and 8.

Preprocessor Directive: The

// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
7 (Line 4) is a preprocessor directive and NOT a programming statement. A preprocessor directive begins with hash sign (
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
8). It is processed before compiling the program. A preprocessor directive is NOT terminated by a semicolon - Take note of this rule.

Block: A block is a group of programming statements enclosed by braces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4. This group of statements is treated as one single unit. There is one block in this program, which contains the body of the
1
2
3
4
5
6
7
8
9
10
1 function. There is no need to put a semi-colon after the closing brace.

Comments: A multi-line comment begins with

// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
4 and ends with
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
5. An end-of-line comment begins with
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
6 and lasts till the end of the line. Comments are NOT executable statements and are ignored by the compiler. But they provide useful explanation and documentation. Use comments liberally.

Whitespaces: Blank, tab, and newline are collectively called whitespaces. Extra whitespaces are ignored, i.e., only one whitespace is needed to separate the tokens. But they could help you and your readers better understand your program. Use extra whitespaces liberally.

Case Sensitivity: C is case sensitive - a ROSE is NOT a Rose, and is NOT a rose.

The Process of Writing a C Program

Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))

Step 1: Write the source codes (

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
5) and header files (
The sum of 55 and 66 is 121.
0).

Step 2: Pre-process the source codes according to the preprocessor directives. The preprocessor directives begin with a hash sign (

// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
8), such as
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
7 and
The sum of 55 and 66 is 121.
3. They indicate that certain manipulations (such as including another file or replacement of symbols) are to be performed BEFORE compilation.

Step 3: Compile the pre-processed source codes into object codes (

The sum of 55 and 66 is 121.
4,
The sum of 55 and 66 is 121.
5).

Step 4: Link the compiled object codes with other object codes and the library object codes (

The sum of 55 and 66 is 121.
6,
The sum of 55 and 66 is 121.
7)to produce the executable code (
The sum of 55 and 66 is 121.
8).

Step 5: Load the executable code into computer memory.

Step 6: Run the executable code.

C Program Template

You can use the following template to write your C programs. Choose a meaningful filename for you source file that reflects the purpose of your program with file extension of "

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
5". Write your programming statements inside the body of the
1
2
3
4
5
6
7
8
9
10
1 function. Don't worry about the other terms for the time being. I will explain them later.

1
2
3
4
5
6
7
8
9
10

Let's Write a Program to Add a Few Numbers

Example: Adding Two Integers

Let's write a C program called "

int integer1, integer2, sum;
1" to add two integers as follows:

Add2Integers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
The sum of 55 and 66 is 121.
Dissecting the Program

int integer1;
int integer2;
int sum;
We first declare three

1
2
3
4
5
6
7
8
9
10
4 (integer) variables:
int integer1, integer2, sum;
3,
int integer1, integer2, sum;
4, and
int integer1, integer2, sum;
5. A variable is a named storage location that can store a value of a particular data type, in this case,
1
2
3
4
5
6
7
8
9
10
4 (integer). You can declare one variable in one statement. You could also declare many variables in one statement, separating with commas, e.g.,

int integer1, integer2, sum;

integer1 = 55;
integer2 = 66;
sum = integer1 + integer2;
We assign values to variables

int integer1, integer2, sum;
3 and
int integer1, integer2, sum;
4; compute their sum and store in variable
int integer1, integer2, sum;
5.

printf("The sum of %d and %d is %d.\n", integer1, integer2, sum);
We use the

1
2
3
4
5
6
7
8
9
10
5 function to print the result. The first argument in
1
2
3
4
5
6
7
8
9
10
5 is known as the formatting string, which consists of normal texts and so-called conversion specifiers. Normal texts will be printed as they are. A conversion specifier begins with a percent sign (
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2), followed by a code to indicate the data type, such as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
3 for decimal integer. You can treat the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4 as placeholders, which will be replaced by the value of variables given after the formatting string in sequential order. That is, the first
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4 will be replaced by the value of
int integer1, integer2, sum;
3, second
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4 by
int integer1, integer2, sum;
4, and third
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4 by
int integer1, integer2, sum;
5. The
1
2
3
4
5
6
7
8
9
10
6 denotes a newline character. Printing a
1
2
3
4
5
6
7
8
9
10
6 bring the cursor to the beginning of the next line.

Example: Prompting User for Inputs

In the previous example, we assigned fixed values into variables

int integer1, integer2, sum;
3 and
int integer1, integer2, sum;
4. Instead of using fixed values, we shall prompt the user to enter two integers.

PromptAdd2Integers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Enter first integer: 55
Enter second integer: 66
The sum of 55 and 66 is 121.
Disecting the Program

int integer1, integer2, sum;
We first declare three

1
2
3
4
5
6
7
8
9
10
4 (integer) variables:
int integer1, integer2, sum;
3,
int integer1, integer2, sum;
4, and
int integer1, integer2, sum;
5 in one statement.

printf("Enter first integer: ");
We use the

1
2
3
4
5
6
7
8
9
10
5 function to put out a prompting message.

scanf("%d", &integer1);
We then use the

1
2
3
4
5
6
7
8
9
00 function to read the user input from the keyboard and store the value into variable
int integer1, integer2, sum;
3.The first argument of
1
2
3
4
5
6
7
8
9
00 is the formatting string (similar to
1
2
3
4
5
6
7
8
9
10
5).The
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4 conversion specifier provides a placeholder for an integer, which will be substituted by variable
int integer1, integer2, sum;
3. Take note that we have to place an ampersand sign (
1
2
3
4
5
6
7
8
9
06), which stands for address-of operator, before the variable, I shall explain its significance later. It is important to stress that missing ampersand (
1
2
3
4
5
6
7
8
9
06) in
1
2
3
4
5
6
7
8
9
00 is a common error, which leads to abnormal termination of the program.

Reading Multiple Integers

You can read multiple items in one

1
2
3
4
5
6
7
8
9
00 statement as follows:

In the

1
2
3
4
5
6
7
8
9
00, the first
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4 puts the first integer entered into variable
int integer1, integer2, sum;
3, and the second
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
4 puts into
int integer1, integer2, sum;
4. Again, remember to place an ampersand (
1
2
3
4
5
6
7
8
9
06) before the variables in
1
2
3
4
5
6
7
8
9
00.

Exercises
  1. Print each of the following patterns. Use one
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    5 statement for each line of outputs. End each line by printing a newline (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    6).
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0
  2. Print the above patterns using ONE
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    5 statement.
  3. Write a program to prompt user for 5 integers and print their sum. Use five
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4 variables
    int integer1, integer2, sum;
    3 to
    1
    2
    3
    4
    5
    6
    7
    8
    9
    22
    1
    2
    3
    4
    5
    6
    7
    8
    9
    23 to store the five integers.
  4. Write a program to prompt user for 5 integers and print their product. Use an
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4 variable
    1
    2
    3
    4
    5
    6
    7
    8
    9
    25 to store the product and operator
    1
    2
    3
    4
    5
    6
    7
    8
    9
    26 for multiplication.

What is a Program?

Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))

A program is a sequence of instructions (called programming statements), executing one after another - usually in a sequential manner, as illustrated in the previous example and the following flow chart.

Example (Sequential): The following program (

1
2
3
4
5
6
7
8
9
27) prompts user for the radius of a circle, and prints its area and circumference. Take note that the programming statements are executed sequentially - one after another in the order that they are written.

1
2
3
4
5
6
7
8
9
1
1
2
3
4
5
6
7
8
9
2
Dissecting the Program

double radius, circumference, area;
double pi = 3.14159265;
We declare three

1
2
3
4
5
6
7
8
9
28 variables called
1
2
3
4
5
6
7
8
9
29,
1
2
3
4
5
6
7
8
9
30 and
1
2
3
4
5
6
7
8
9
31. A
1
2
3
4
5
6
7
8
9
28 variable, unlike
1
2
3
4
5
6
7
8
9
10
4, can hold real number (or floating-point number) such as 1.23 or 4.5e6. We also declare a
1
2
3
4
5
6
7
8
9
28 variable called
1
2
3
4
5
6
7
8
9
35 and initialize its value to 3.1416.

printf("Enter the radius: ");
scanf("%lf", &radius);
We use

1
2
3
4
5
6
7
8
9
36 to put up a prompt message, and
1
2
3
4
5
6
7
8
9
00 to read the user input into variable
1
2
3
4
5
6
7
8
9
29. Take note that the
1
2
3
4
5
6
7
8
9
39 conversion specifier for
1
2
3
4
5
6
7
8
9
28 (
1
2
3
4
5
6
7
8
9
41 stands for long float). Also remember to place an ampersand (
1
2
3
4
5
6
7
8
9
06) before
1
2
3
4
5
6
7
8
9
29.

area = radius * radius * pi;
circumference = 2.0 * radius * pi;
perform the computation.

printf("The radius is %lf.\n", radius);
printf("The area is %lf.\n", area);
printf("The circumference is %lf.\n", circumference);
Again, we use

1
2
3
4
5
6
7
8
9
39 conversion specifier to print a
1
2
3
4
5
6
7
8
9
28.

Take note that the programming statements inside the

1
2
3
4
5
6
7
8
9
10
1 are executed one after another, sequentially.

Exercises
  1. Follow the above example, write a program to print the area and perimeter of a rectangle. Your program shall prompt the user for the length and width of the rectangle, in
    1
    2
    3
    4
    5
    6
    7
    8
    9
    28s.
  2. Follow the above example, write a program to print the surface area and volume of a cylinder. Your program shall prompt the user for the radius and height of the cylinder, in
    1
    2
    3
    4
    5
    6
    7
    8
    9
    28s.

What is a Variable?

Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))

Computer programs manipulate (or process) data. A variable is used to store a piece of data for processing. It is called variable because you can change the value stored.

More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value of that type.

  • A variable has a name (or identifier), e.g.,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    29,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    31,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    51,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    52. The name is needed to uniquely identify and reference a variable, so as to assign a value to the variable (e.g.,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    53), and retrieve the value stored (e.g.,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    54).
  • A variable has a type. Examples of type are:
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      4: for integers (whole numbers) such as
      1
      2
      3
      4
      5
      6
      7
      8
      9
      56 and
      1
      2
      3
      4
      5
      6
      7
      8
      9
      57;
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      28: for floating-point or real numbers, such as
      1
      2
      3
      4
      5
      6
      7
      8
      9
      59,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      60,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      61,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      62,
      1
      2
      3
      4
      5
      6
      7
      8
      9
      63 having a decimal point and fractional part, in fixed or scientific notations.
  • A variable can store a value of the declared type. It is important to take note that a variable is associated with a type, and can only store value of that particular type. For example, a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4 variable can store an integer value such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    56, but NOT real number such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    66, nor texts such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    67. The concept of type was introduced into the early programming languages to simplify interpretation of data.

The above diagram illustrates 2 types of variables:

1
2
3
4
5
6
7
8
9
10
4 and
1
2
3
4
5
6
7
8
9
28. An
1
2
3
4
5
6
7
8
9
10
4 variable stores an integer (whole number). A
1
2
3
4
5
6
7
8
9
28 variable stores a real number.

To use a variable, you need to first declare its name and type, in one of the following syntaxes:

Take note that:

  • Each declaration statement is terminated with a semi-colon (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    1).
  • In multiple-variable declaration, the names are separated by commas (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    73).
  • The symbol
    1
    2
    3
    4
    5
    6
    7
    8
    9
    74, known as the assignment operator, can be used to assign an initial value (of the declared type) to the variable.

For example,

Once a variable is declared, you can assign and re-assign a value to a variable, via the assignment operator "

1
2
3
4
5
6
7
8
9
74". For example,

Take note that:

  • Each variable can only be declared once.
  • You can declare a variable anywhere inside the program, as long as it is declared before it is being used.
  • Once the type of a variable is declared, it can only store a value belonging to this particular type. For example, an
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4 variable can hold only integer such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    56, and NOT floating-point number such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    78 or text string such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    67.
  • The type of a variable cannot be changed inside the program.
x=x+1?

Assignment (

1
2
3
4
5
6
7
8
9
74) in programming is different from equality in Mathematics. e.g., "
1
2
3
4
5
6
7
8
9
81" is invalid in Mathematics. However, in programming, it means compute the value of
1
2
3
4
5
6
7
8
9
82 plus 1, and assign the result back to variable
1
2
3
4
5
6
7
8
9
82.

"x+y=1" is valid in Mathematics, but is invalid in programming. In programming, the RHS of "

1
2
3
4
5
6
7
8
9
74" has to be evaluated to a value; while the LHS shall be a variable. That is, evaluate the RHS first, then assign to LHS.

Some languages uses

1
2
3
4
5
6
7
8
9
85 as the assignment operator to avoid confusion with equality.

Basic Arithmetic Operations

The basic arithmetic operators are:

OperatorMeaningExample
1
2
3
4
5
6
7
8
9
86Addition
1
2
3
4
5
6
7
8
9
87
1
2
3
4
5
6
7
8
9
88Subtraction
1
2
3
4
5
6
7
8
9
89
1
2
3
4
5
6
7
8
9
26Multiplication
1
2
3
4
5
6
7
8
9
91
1
2
3
4
5
6
7
8
9
92Division
1
2
3
4
5
6
7
8
9
93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2Modulus (Remainder)
1
2
3
4
5
6
7
8
9
95
1
2
3
4
5
6
7
8
9
96Increment by 1 (Unary)
1
2
3
4
5
6
7
8
9
97 or
1
2
3
4
5
6
7
8
9
98
1
2
3
4
5
6
7
8
9
99Decrement by 1 (Unary)
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
00 or
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
01

Addition, subtraction, multiplication, division and remainder are binary operators that take two operands (e.g.,

1
2
3
4
5
6
7
8
9
87); while negation (e.g.,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
03), increment and decrement (e.g.,
1
2
3
4
5
6
7
8
9
98,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
00) are unary operators that take only one operand.

Example

The following program (

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
06) illustrates these arithmetic operations.

1
2
3
4
5
6
7
8
9
3
1
2
3
4
5
6
7
8
9
4
Dissecting the Program

int number1, number2;
int sum, difference, product, quotient, remainder;
declare all the

1
2
3
4
5
6
7
8
9
10
4 (integer) variables
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
08,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
09,
int integer1, integer2, sum;
5,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
11,
1
2
3
4
5
6
7
8
9
25,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
13, and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
14, needed in this program.

printf("Enter two integers (separated by space): ");
scanf("%d%d", &number1, &number2);
prompt user for two integers and store into

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
08 and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
09, respectively.

sum = number1 + number2;
difference = number1 - number2;
product = number1 * number2;
quotient = number1 / number2;
remainder = number1 % number2;
carry out the arithmetic operations on

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
08 and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
09. Take note that division of two integers produces a truncated integer, e.g.,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
19,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
20, and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
21.

printf("The sum, difference, product, quotient and remainder of %d and %d are %d, %d, %d, %d, %d.\n",
   number1, number2, sum, difference, product, quotient, remainder);
prints the results of the arithmetic operations, with the appropriate string descriptions in between.

++number1;
--number2;
illustrate the increment and decrement operations. Unlike

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
22,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
23,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
24,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
25 and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
26, which work on two operands (binary operators),
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
27 and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
28 operate on only one operand (unary operators).
1
2
3
4
5
6
7
8
9
97 is equivalent to
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
30, i.e., increment x by 1. You may place the increment operator before or after the operand, i.e.,
1
2
3
4
5
6
7
8
9
97 (pre-increment) or
1
2
3
4
5
6
7
8
9
98 (post-increment). In this example, the effects of pre-increment and post-increment are the same. I shall point out the differences in later section.

Exercises
  1. Introduce one more
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4 variable called
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    34, and prompt user for its value. Print the sum and product of all the three integers.
  2. In Mathematics, we could omit the multiplication sign in an arithmetic expression, e.g.,
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    35. In programming, you need to explicitly provide all the operators, i.e.,
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    36. Try printing the sum of
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    37 times of
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    08 and
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    39 times of
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    09 and
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    41 time of
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    34.

What If Your Need To Add a Thousand Numbers? Use a Loop!

Suppose that you want to add all the integers from 1 to 1000. If you follow the previous examples, you would require a thousand-line program! Instead, you could use a loop in your program to perform a repetitive task, that is what the dumb computers are good at.

Example

Try the following program

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
43, which sums all the integers from 1 to an upperbound provided by the user, using a so-called while-loop.

1
2
3
4
5
6
7
8
9
5
1
2
3
4
5
6
7
8
9
6
Dissecting the Program

int sum = 0;
declares an

1
2
3
4
5
6
7
8
9
10
4 variable named
int integer1, integer2, sum;
5 and initializes it to 0. This variable will be used to accumulate numbers over the steps in the repetitive loop.

printf("Enter the upperbound: ");
scanf("%d", &upperbound);
prompt user for an upperbound to sum.

int number = 1;
while (number <= upperbound) {
   sum = sum + number;
   ++number;
}
This is the so-called while-loop. A while-loop takes the following syntax:

Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))
1
2
3
4
5
6
7
8
9
7

As illustrated in the flow chart, the initialization statement is first executed. The test is then checked. If the test is true, the body is executed. The test is checked again and the process repeats until the test is false. When the test is false, the loop completes and program execution continues to the next statement after the loop.

In our program, the initialization statement declares an

1
2
3
4
5
6
7
8
9
10
4 variable named
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
47 and initializes it to 1. The test checks if
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
47 is equal to or less than the
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
49. If it is true, the current value of
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
47 is added into the
int integer1, integer2, sum;
5, and the statement
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
52 increases the value of
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
47 by 1. The test is then checked again and the process repeats until the test is false (i.e.,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
47 increases to
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
55), which causes the loop to terminate. Execution then continues to the next statement (in Line 22).

In this example, the loop repeats

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
49 times. After the loop is completed, Line 22 prints the result with a proper description.

Exercises
  1. Modify the above program to sum all the number between a lowerbound and an upperbound provided by the user.
  2. Modify the above program to sum all the odd numbers between
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    57 to an upperbound. (Hint: Use "
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    58".)
  3. Modify the above program to sum all the numbers between
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    57 to an upperbound that are divisible by
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    60. (Hint: Use "
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    61")
  4. Modify the above program to find the sum of the square of all the numbers from
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    57 to an upperbound, i.e.
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    63
  5. Modify the above program to compute the product of all the numbers from
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    57 to
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    65. (Hint: Use a variable called
    1
    2
    3
    4
    5
    6
    7
    8
    9
    25 instead of
    int integer1, integer2, sum;
    5 and initialize
    1
    2
    3
    4
    5
    6
    7
    8
    9
    25 to 1. Ans:
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    69.) Based on this code, write a program to display the factorial of n, where n is an integer between
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    57 to
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    71.

Conditional (or Decision)

What if you want to sum all the odd numbers and also all the even numbers between 1 and 1000? There are many way to do this. You could declare two variables:

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
72 and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
73. You can then use a conditional statement to check whether the number is odd or even, and accumulate the number into the respective sum. The program
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
74 is as follows:

1
2
3
4
5
6
7
8
9
8
1
2
3
4
5
6
7
8
9
9
Dissecting the Program

int sumOdd = 0;
int sumEven = 0;
declare two

1
2
3
4
5
6
7
8
9
10
4 variables named
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
72 and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
73 and initialize them to 0, for accumulating the odd and even numbers, respectively.

if (number % 2 == 0) {
   sumEven = sumEven + number;
} else {
   sumOdd = sumOdd + number;
}
This is a conditional statement. The conditional statement can take one these forms: if-then or if-then-else.

Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))
Which of the following is a function from A=(1, 2, 3, 4,5 to B=(a, b, c, d))

For a if-then statement, the true-body is executed if the test is true. Otherwise, nothing is done and the execution continues to the next statement. For a if-then-else statement, the true-body is executed if the test is true; otherwise, the false-body is executed. Execution is then continued to the next statement.

In our program, we use the remainder operator

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
78 to compute the remainder of
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
47 divides by
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
80. We then compare the remainder with
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
81 to test for even number.

Comparison Operators

There are six comparison (or relational) operators:

OperatorMeaningExample
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
82Equal to
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
83
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
84Not equal to
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
85
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
86Greater than
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
87
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
88Greater than or equal to
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
89
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
90Less than
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
91
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
92Less than or equal to
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
93

Take note that the comparison operator for equality is a double-equal sign

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
94; whereas a single-equal sign
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
95 is the assignment operator.

Combining Simple Conditions

Suppose that you want to check whether a number

1
2
3
4
5
6
7
8
9
82 is between
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
57 and
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
98 (inclusive), i.e.,
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
99. There are two simple conditions here,
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
00. In programming, you cannot write
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
99, but need to write
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
02, where "
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
03" denotes the "
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
04" operator. Similarly, suppose that you want to check whether a number
1
2
3
4
5
6
7
8
9
82 is divisible by 2 OR by 3, you have to write
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
06 where "
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
07" denotes the "
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
08" operator.

There are three so-called logical operators that operate on the boolean conditions:

OperatorMeaningExample
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
03Logical AND
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
02
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
07Logical OR
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
12
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
13Logical NOT
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
14

For examples:

Exercises
  1. Write a program to sum all the integers between 1 and 1000, that are divisible by 13, 15 or 17, but not by 30.
  2. Write a program to print all the leap years between AD1 and AD2010, and also print the number of leap years. (Hints: use a variable called
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    15, which is initialized to zero. Increment the
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    15 whenever a leap year is found.)

Type double & Floating-Point Numbers

Recall that a variable in C has a name and a type, and can hold a value of only that particular type. We have so far used a type called

1
2
3
4
5
6
7
8
9
10
4. A
1
2
3
4
5
6
7
8
9
10
4 variable holds only integers (whole numbers), such as
1
2
3
4
5
6
7
8
9
56 and
1
2
3
4
5
6
7
8
9
57.

In programming, real numbers such as

1
2
3
4
5
6
7
8
9
59 and
1
2
3
4
5
6
7
8
9
60 are called floating-point numbers, and belong to a type called
1
2
3
4
5
6
7
8
9
28. You can express floating-point numbers in fixed notation (e.g.,
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
24,
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
25) or scientific notation (e.g.,
1
2
3
4
5
6
7
8
9
62,
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
27) where
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
28 or
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
29 denote the exponent of base 10.

Example
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
0
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
1

Mixing int and double, and Type Casting

Although you can use a

1
2
3
4
5
6
7
8
9
28 to keep an integer value (e.g.,
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
31), you should use an
1
2
3
4
5
6
7
8
9
10
4 for integer. This is because
1
2
3
4
5
6
7
8
9
10
4 is far more efficient than
1
2
3
4
5
6
7
8
9
28, in terms of running times and memory requirement.

At times, you may need both

1
2
3
4
5
6
7
8
9
10
4 and
1
2
3
4
5
6
7
8
9
28 in your program. For example, keeping the sum from
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
57 to
// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
98 (
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
39) as an
1
2
3
4
5
6
7
8
9
10
4, and their average
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
41 as a
1
2
3
4
5
6
7
8
9
28. You need to be extremely careful when different types are mixed.

It is important to note that:

  • Arithmetic operations (
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    22,
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    23,
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    24,
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    25) of two
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4's produce an
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4; while arithmetic operations of two
    1
    2
    3
    4
    5
    6
    7
    8
    9
    28's produce a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    28. Hence,
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    21 (take note!) and
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    52.
  • Arithmetic operations of an
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    4 and a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    28 produce a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    28. Hence,
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    56 and
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    57.

You can assign an integer value to a

1
2
3
4
5
6
7
8
9
28 variable. The integer value will be converted to a double value automatically, e.g.,
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
59. For example,

However, if you assign a

1
2
3
4
5
6
7
8
9
28 value to an
1
2
3
4
5
6
7
8
9
10
4 variable, the fractional part will be lost. For example,

Some C compilers signal a warning for truncation, while others do not. You should study the "warning messages" (if any) carefully - which signals a potential problem in your program, and rewrite the program if necessary. C allows you to ignore the warning and run the program. But, the fractional part will be lost during the execution.

Type Casting Operators

If you are certain that you wish to carry out the type conversion, you could use the so-called type cast operator. The type cast operation returns an equivalent value in the new-type specified.

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
2

For example,

Similarly, you can explicitly convert an

1
2
3
4
5
6
7
8
9
10
4 value to
1
2
3
4
5
6
7
8
9
28 by invoking type-casting operation too.

Example

Try the following program and explain the outputs produced:

// Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
> gcc -o Hello.exe Hello.c
 
// UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
$ gcc -o Hello Hello.c
3

The first average is incorrect, as

// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
64 produces an
1
2
3
4
5
6
7
8
9
10
4 (of
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
66).

For the second average, the value of

int integer1, integer2, sum;
5 (of
1
2
3
4
5
6
7
8
9
10
4) is first converted to
1
2
3
4
5
6
7
8
9
28. Subsequently,
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
70 produces
1
2
3
4
5
6
7
8
9
28.

For the third average,

// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
72 produces
1
2
3
4
5
6
7
8
9
28.

For the fourth average,

// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
64 produces an
1
2
3
4
5
6
7
8
9
10
4 (of
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
66), which is then casted to
1
2
3
4
5
6
7
8
9
28 (of
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
78) and assigned to
// Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
> Hello
Hello, world!
 
// UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
$ ./Hello
Hello, world!
79 (of
1
2
3
4
5
6
7
8
9
28).

Exercises
  1. Write a program called
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    81 to compute the sum of a harmonic series
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    82, where
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    83. Your program shall prompt user for the value of
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    84. Keep the sum in a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    28 variable, and take note that
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    86 gives
    // Windows (CMD shell) - Build "Hello.c" into "Hello.exe"
    > gcc -o Hello.exe Hello.c
     
    // UNIX/Linux/Mac (Bash shell) - Build "Hello.c" into "Hello"
    $ gcc -o Hello Hello.c
    81 but
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    88 gives
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    89.
    Try computing the sum for
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    90
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    91,
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    92,
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    93,
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    94,
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    95.
    Hints:
  2. Write a program called
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    96 to compute the sum of a geometric series
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    97. You program shall prompt for the value of
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    84. (Hints: Use post-processing statement of
    // Windows (CMD shell) - Run "Hello.exe" (.exe is optional)
    > Hello
    Hello, world!
     
    // UNIX/Linux/Mac (Bash shell) - Run "Hello" (./ denotes the current directory)
    $ ./Hello
    Hello, world!
    99.)

Summary

I have presented the basics for you to get start in programming. To learn programming, you need to understand the syntaxes and features involved in the programming language that you chosen, and you have to practice, practice and practice, on as many problems as you could.

Which of the following is not a function from 1 2 3 4 to 1 2 3?

Answer: {(1,2), (1,4), (2,5), (3,8)} is not a function. Step-by-step explanation: A function is a relation in which every x value is associated with exactly one y value.

What is a function f from A to B?

A function f : A −→ B consists of: (1) the set A, which is called the domain of f, (2) the set B, which is called the range of f, (3) a rule which assigns to every element a ∈ A an element b ∈ B, which we denote by f(a).

What is the formula for function of a function?

Functions are generally represented as y = f(x) and it states the dependence of y on x, or we say that y is a function of x. Functions formulas define the mathematical rules to connect one set of elements to another set of elements.

What are the 4 types of function?

There are 4 types of functions:.
Functions with arguments and return values. This function has arguments and returns a value: ... .
Functions with arguments and without return values. ... .
Functions without arguments and with return values. ... .
Functions without arguments and without return values..