0% found this document useful (0 votes)
32 views54 pages

Sit 112 Notes

Uploaded by

brianmaingi533
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views54 pages

Sit 112 Notes

Uploaded by

brianmaingi533
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

SIT 112: INTROUCTION TO PROGRAMMING

Course code: SIT 112 Course Title: Introduction to


Programming and Logic
Semester: 1 Academic year: 2024/25

LESSON 1: INTRODUCTION TO COMPUTER PROGRAMMING

Computer programming is a process of developing and implementing various sets of instructions


to enable a computer to do a certain task. Better still, it can also be defined as the act of writing
computer programs, which are a sequence of instructions written using a Computer Programming
Language to perform a specified task by the computer. Before getting into computer programming,
let us first understand computer programs and what they do. A computer program is a sequence
of instructions written using a Computer Programming Language to perform a specified task when
executed by a computer. A computer requires programs to function and typically executes the
program's instructions in a central processing unit. The two important terms that we have used in
the above definition are −

• Sequence of instructions
• Computer Programming Language

To understand these terms, let's go back and try to understand a computer program, which is a
sequence of instructions written in a Computer Language to perform a specified task by the
computer. Following is a simple program written in C programming Language −

printf( "Hello, World!" );

The above computer program instructs the computer to print "Hello, World!" on the computer
screen. A computer program is also called computer software, which can range from two lines to
millions of lines of instructions. Computer program instructions are also called program source
code and computer programming is also called program coding. A computer without a
computer program is just a dump box; it is programs that make computers active.

Page 1 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Programming Process

Here we discuss the actual process of writing a given program. It involves the following steps:
• Write a program.
• Compile the program.
• Run the program.
• Debug the program.
• Repeat the whole process until the program is finished.

1. Write a program: This means write the steps needed to perform the task, using the programming
language you know. You'll do the typing in a programming environment (an application program
which lets you write programs). Incidentally, the stuff you type to create a program is usually
called source code (a text listing of commands to be compiled or assembled into an executable
computer program), or just code. Programmers also sometimes call programming coding. A
common programming environment is CodeWarrior and another common one is InterDev, but you
don't need to worry about those just yet. Some programming environments are free, and some you
have to buy just like any other application program.
2. Compile the program: In order to use a program, you usually have to compile it first. When you
write a program (in a programming language, using a programming environment, as we mentioned
a moment ago), it's not yet in a form that the computer can use. This isn't hard to understand, given
that computers actually only understand lots of 1s and 0s in long streams.
So many people can't accurately write programs using only vast amounts of 1s and 0s, so you
write it in a more easily-understood form (a programming language), then you convert it to a form
that the computer can actually use. This conversion process is called compiling, or compilation
and it is done by a program called a compiler.
3. Run the program: Now that you've compiled the program into a form that the computer can
use, you want to see if it works: you want to make the computer perform the steps that you
specified. This is called running the program, or sometimes executing it.
4. Debug the program: It refers to the act of fixing errors and problems with your program. Once
again, your programming environment will help you to debug your programs.

Page 2 of 54
SIT 112: INTROUCTION TO PROGRAMMING

5. Repeat the whole process until the program is finished: And then you repeat the whole process
until you're happy with the program.
In computing, several computer-programming languages have been developed to provide
instructions to the computer (i.e., to write computer programs). We will see some computer
programming languages in the subsequent chapters. If you understood what a computer program
is, then we will say: the act of writing computer programs is called computer programming.
There are hundreds of programming languages, which can be used to write computer programs
and following are a few of them: Java, C, C++, Python, PHP, Perl, Ruby, Visual basic etc

Computer Programming Terminologies

This section lists various terms related to programming and computers in general, but only those
which are in some way relevant to programming.

1. API: Stands for Application Programming Interface. It is a kind of predefined standard on how
you should write certain types of programs, and commonly refers to a standard for how to write
programs which interact with and extend other programs.

2. Computer Programmer: This is a person who can write computer programs or in other words,
someone who can do computer programming. They are named based on computer programming
language expertise eg C Programmer, C++ Programmer, Java Programmer etc.

3. Algorithm: It is a step-by-step procedure to resolve any problem. An algorithm is an effective


method expressed as a finite set of well-defined instructions. Thus, a computer programmer lists
down all the steps required to resolve a problem before writing the actual code. Following is a
simple example of an algorithm for adding two numbers:
i. Start.
ii. Accept Number one.
iii. Accept Number two.
iv. Add both the numbers.
v. Print the result.
vi. End
4. Application: A program designed to let you accomplish a specific task or a sort of template to
help you make applications.

Page 3 of 54
SIT 112: INTROUCTION TO PROGRAMMING

5. Linker: A special program which links together all the bits of object code produced by a
compiler and its usually included with a compiler.
6. Object Code: A program which has been compiled into a form suitable for the computer to use.
7. Object Orientation: A modern programming concept where the programmer creates "objects"
like real-life objects, with both properties and abilities. Popular modern object oriented
programming languages include Java and C++
8. Operating system: The special and very important program which makes your computer work.
9. Programming: The art of writing computer programs (and it is indeed an art).
10. Programming Language: Any of countless special languages used to write programs. Usually
not difficult to learn, and including several English words like "end" and "repeat" and "if".
11. Programming Environment: A special program (or set of programs) which help people to
write
12. RAD: Rapid Application Development. A concept where you create application programs very
quickly by visually dragging controls like buttons and checkboxes and so forth into windows, to
create the user interface for your application.
13. Run: To make a computer perform the steps you've written in your program; to make the
program do whatever it does. Sometimes also called "executing" a program.
14. SDK: A Software Development Kit. Typically it is a package of sample code, documentation
and other items to help you create certain kinds of programs.
15. Software: Any computer program. It's called software because it's not tangible; not "hard".
16. User interface: The appearance of your program to the person using it. The windows, menus,
buttons and so on are all collectively called the user interface.
17. Windows: One of several operating systems from the company called Microsoft. Collectively
they are the most commonly used operating systems in the world.

TASK:

Uses of Computer Programs

Today computer programs are being used in almost every field, household, agriculture, medical,
entertainment, defense, communication, etc. Listed below are a few applications of computer
programs −

Page 4 of 54
SIT 112: INTROUCTION TO PROGRAMMING

• In entertainment sector for playing games


• MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are examples of
computer programs.
• Computer programs are being used to develop graphics and special effects in movie
making.
• Computer programs are being used to perform Ultrasounds, X-Rays, and other medical
examinations.
• Computer programs are being used in our mobile phones for SMS, Chat, and voice
communication.

Page 5 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 2: PROGRAMMING LANGUAGES

SEE ATTACHED PDF DOCUMENT ON LESSON 2

Page 6 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 3: COMPUTER PROGRAMMING ENVIRONMENT

Environment refers to the collection of hardware and software tools a system developer uses to
build software systems. In computer program and software product development, the development
environment is the set of processes and programming tools used to create the program or software
product. The term may sometimes also imply the physical environment. Thus, we need to have
the required software setup, i.e., installation on our PC which will be used to write computer
programs, compile, and execute them. For example, if you need to browse Internet, then you need
the following setup on your machine −

• A working Internet connection to connect to the Internet


• A Web browser such as Internet Explorer, Chrome, Safari, etc.

Similarly, you will need the following setup to start with programming using any programming
language.

• A text editor to create computer programs.


• A compiler to compile the programs into binary format.
• An interpreter to execute the programs directly.

In case you don’t have sufficient exposure to computers, you will not be able to set up either of
these software. I, we suggest you take the help from any technical person around you to set up the
programming environment on your machine from where you can start. But for you, it is important
to understand what these items are.

1. Text Editor

A text editor is a type of program used for editing plain text files and in computer programming it
refers to software that is used to write computer programs. A text editor in Windows is Notepad
and you can launch it by following these steps: Start Icon → All Programs → Accessories →
Notepad → Mouse Click on Notepad

It will launch Notepad with the following window −

Page 7 of 54
SIT 112: INTROUCTION TO PROGRAMMING

2. Compiler

A compiler is a special program that processes statements written in a particular programming


language and turns them into machine language or "code" that a computer understands. You write
your computer program using your favorite programming language and save it in a text file called
the program file. Actually, the computer cannot understand your program directly given in the
text format, so we need to convert this program in a binary format, which can be understood by
the computer. The conversion from text program to binary file is done by software called Compiler
and this process of conversion from text formatted program to binary format file is called program
compilation. Finally, you can execute binary file to perform the programmed task. The following
flow diagram gives an illustration of the process –

Page 8 of 54
SIT 112: INTROUCTION TO PROGRAMMING

So, if you are going to write your program in any such language, which needs compilation like C,
C++, Java and Pascal, etc., then you will need to install their compilers before you start
programming.

3. Interpreter

An interpreter is a computer program that is used to directly execute program instructions written
using one of the many high-level programming languages. It translates a program line-by-line
(statement-by-statement) and carries out the specified actions in sequence. We just discussed about
compilers and the compilation process. Compilers are required in case you are going to write your
program in a programming language that needs to be compiled into binary format before its
execution. There are other programming languages such as Python, PHP, and Perl, which do not
need any compilation into binary format, rather an interpreter can be used to read such programs
line by line and execute them directly without any further conversion. So, if you are going to write
your programs in PHP, Python, Perl, Ruby, etc., then you will need to install their interpreters
before you start programming. The following flow diagram gives an illustration of the process:

Page 9 of 54
SIT 112: INTROUCTION TO PROGRAMMING

4. Debugger

A debugger or debugging tool is a computer program that is used to test and debug other programs
(the "target" program). Debugging is the routine process of locating and removing computer
program bugs, errors or abnormalities, which is methodically handled by software programmers
via debugging tools. Debugging checks, detects and corrects errors or bugs to allow proper
program operation according to set specifications.

Page 10 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 4: COMPUTER PROGRAMMING - BASIC SYNTAX

In computer science, the syntax of a computer language is the set of rules that defines the
combinations of symbols that are considered to be a correctly structured document or fragment in
that language. Every spoken language has a general set of rules for how words and sentences
should be structured. These rules are collectively known as the language syntax. In computer
programming, syntax serves the same purpose, defining how declarations, functions, commands,
and other statements should be arranged. Many computer programming languages share similar
syntax rules, while others have a unique syntax design. For example, C and Java use a similar
syntax, while Perl has many characteristics that are not seen in either the C or Java languages.

A program's source code must have correct syntax in order to compile correctly and be made into
a program or else the program will fail to compile and produce a "syntax error."

Syntax errors are also called compile-time errors, since they can prevent a program from
compiling. Errors that occur in a program after it has been compiled are called runtime errors,
since they occur when the program is running. A syntax error can be as simple as a missing
parenthesis or a forgotten semicolon at the end of a statement. Even these small errors will keep
the source code from compiling. Fortunately, most integrated development environments (IDEs)
include a parser, which detects syntax errors within the source code. Modern parsers can even
highlight syntax errors before a program is compiled, making it easy for the programmer to locate
and fix them.

Let’s start with a little coding, which will really make you a computer programmer. We are going
to write a single-line computer program to write Hello, World! on your screen. Let’s see how it
can be written using different programming languages.

Hello World Program in C


#include <stdio.h>
main() {
/* printf() function to write Hello, World! */
printf( "Hello, World!" );
}

Page 11 of 54
SIT 112: INTROUCTION TO PROGRAMMING

This little Hello World program will help you understand various basic concepts related to C
Programming. Try to change the content inside printf(), i.e., type anything in place of Hello
World! and then check its result. It just prints whatever you keep inside the two double quotes

Program Entry Point

Every C program starts with main(), which is called the main function, and then it is followed by
a left curly brace. The rest of the program instruction is written in between and finally a right curly
brace ends the program. The coding part inside these two curly braces is called the program body.
The left curly brace can be in the same line as main(){ or in the next line like it has been mentioned
in the above program.

Functions

Functions are small units of programs and they are used to carry out a specific task. For example,
the above program makes use of two functions: main() and printf(). Here, the function main()
provides the entry point for the program execution and the other function printf() is being used to
print an information on the computer screen. You can also write your own functions though C
programming itself provides various built-in functions like main(), printf(), etc., which we can use
in our programs based on our requirement.

Comments

A C program can have statements enclosed inside /*.....*/. Such statements are called comments
and these comments are used to make the programs user friendly and easy to understand. The good
thing about comments is that they are completely ignored by compilers and interpreters. So you
can use whatever language you want to write your comments.

Whitespaces

When we write a program using any programming language, we use various printable characters
to prepare programming statements. These printable characters are a, b, c,......z, A, B, C,.....Z, 1,
2, 3,...... 0, !, @, #, $, %, ^, &, *, (, ), -, _, +, =, \, |, {, }, [, ], :, ;, <, >, ?, /, \, ~. `. ", '. Hope I'm
not missing any printable characters from your keyboard. Apart from these characters, there are

Page 12 of 54
SIT 112: INTROUCTION TO PROGRAMMING

some characters which we use very frequently but they are invisible in your program and these
characters are spaces, tabs (\t), new lines(\n). These characters are called whitespaces. These three
important whitespace characters are common in all the programming languages and they remain
invisible in your text document –

Whitespace Explanation Representation

New Line To create a new line \n

Tab To create a tab. \t

Space To create a space. empty space

A line containing only whitespace, possibly with a comment, is known as a blank line, and a C
compiler totally ignores it. Whitespace is the term used in C to describe blanks, tabs, newline
characters, and comments. So you can write printf("Hello, World!" ); as shown below. Here all
the created spaces around "Hello, World!" are useless and the compiler will ignore them at the
time of compilation.

#include <stdio.h>

main() {

/* printf() function to write Hello, World! */

printf( "Hello, World!" );

If we make all these whitespace characters visible, then the above program will look like this and
you will not be able to compile it −

Page 13 of 54
SIT 112: INTROUCTION TO PROGRAMMING

#include <stdio.h>
main()
{
printf("Hello");
printf(", ");
printf("world!");
printf("\n");
printf("Hello\nworld\nagain\n");
}
Output will be:

#include <stdio.h>
main()
{
printf("Hello");
printf(", ");
printf("world!");
printf("\n");
printf("Hello\t\tworld\t\tagain\t");
}
Semicolons

Every individual statement in a C Program must be ended with a semicolon (;), for example, if
you want to write "Hello, World!" twice, then it will be written as follows −

#include <stdio.h>

main() {
/* printf() function to write Hello, World! */
printf( "Hello, World!\n" );
printf( "Hello, World!" );

Page 14 of 54
SIT 112: INTROUCTION TO PROGRAMMING

This program will produce the following result −

Hello, World!
Hello, World!

Program Explanation

Here, when we execute the binary a.out file, the computer enters inside the program starting from
main() and encounters a printf() statement. Keep a note that the line inside /*....*/ is a comment
and it is filtered at the time of compilation. So printf() function instructs the computer to print the
given line at the computer screen. Finally, it encounters a right curly brace which indicates the end
of main() function and exits the program.

Syntax Error

If you do not follow the rules defined by the programing language, then at the time of compilation,
you will get syntax errors and the program will not be compiled. From syntax point of view, even
a single dot or comma or a single semicolon matters and you should take care of such small syntax
as well. In the following example, we have skipped a semicolon, let's try to compile the program

#include <stdio.h>
main() {
printf("Hello, World!")
}

This program will produce the following result −

main.c: In function 'main':


main.c:7:1: error: expected ';' before '}' token
}
^

Page 15 of 54
SIT 112: INTROUCTION TO PROGRAMMING

So the bottom-line is that if you are not following proper syntax defined by the programming
language in your program, then you will get syntax errors. Before attempting another compilation,
you will need to fix them and then proceed.

Hello World Program in Java

Following is the equivalent program written in Java. This program will also produce the same
result Hello, World!.

public class HelloWorld {


public static void main(String []args) {
/* println() function to write Hello, World! */
System.out.println("Hello, World!");
}
}

Hello World Program in Python

Following is the equivalent program written in Python. This program will also produce the same
result Hello, World!.

# print function to write Hello, World! */


print "Hello, World!"

Hope you noted that for C and Java examples, first we are compiling the programs and then
executing the produced binaries, but in Python program, we are directly executing it. As we
explained in the previous chapter, Python is an interpreted language and it does not need an
intermediate step called compilation. Python does not require a semicolon (;) to terminate a
statement, rather a new line always means termination of the statement.

Page 16 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 5: COMPUTER PROGRAMMING DATA TYPES

In programming, a data type is a classification of data which tells the compiler or interpreter how
the programmer intends to use the data. Within a programing language, a data type of an object
determines what type of values an object can have and what operations can be performed on the
object. The most common data types, including strings, numbers, Booleans, lists and arrays.

• Strings

One of the most widely used data types is a string. A string consists of one or more characters,
which can include letters, numbers, or other types of characters. You can think of a string as plain
text. A string represents alphanumeric data. This means that a string can contain many different
characters, but they are all considered as if they were text, even if the characters are numbers. A
string can also contain spaces.

For example:
print 'Hello World!'
address = '123 Central Avenue'

• Numeric Data Types

An integer is a numeric value without a decimal. Integers are whole numbers and can be positive
or negative. Sometimes a distinction is made between short and long integers, referring to how
much data storage is used for the number. A short integer is typically stored using 16 bits, which
means you can store up to 2^16, or 65,536, unique values. For any numbers larger than that, you
would need to use a long integer, which uses 32 bits or more.

A number with a decimal is referred to as a decimal, a float or a double. The terminology varies
somewhat with the programming language being used. The term 'float' comes from floating point,
which means you can control where the decimal point is located. The term 'double' refers to using
double the amount of storage relative to a float. Working with numbers in code is a little bit like
using a calculator. Here is an example of how numeric values are used in code, in this case using
a multiplication character:

Page 17 of 54
SIT 112: INTROUCTION TO PROGRAMMING

result = 3 * 117.89

The value stored in the 'result' would be 353.67.

• Boolean Data

The Boolean data type can only represent two values: true or false. Typically, a 1 is used to
represent True, and a 0 is used to represent False. Consider the following example where a user
inputs two values and the program determines whether the first one is smaller than the second one
or not.

In this example the first value is in fact not smaller than the second one, and the program therefore
results in a Boolean value of False. The Boolean type is the primary results of conditional
statements, which are used to control workflow in program. For example, if a particular condition
is true, then do this - if the condition is false, then do something else.

• Composite Data Types

The data types covered so far are often referred to as primitive data types. A composite data type
is obtained by combining more than one primitive data type. These are also referred to as data
structures. Common examples of composite data types are lists and arrays. A list contains elements
of one particular data type. For example, a list could contain strings. An example would be the
names of all players on a soccer team. Each name is a string, but when you organize all the names
together, they form a list. A list is the simplest data structure.

For example, a list of strings could look like this:

('John', 'Paul', 'George', 'Ringo')

A list of numbers could look like this:


Page 18 of 54
SIT 112: INTROUCTION TO PROGRAMMING

(67, 84, 92, 52, 81, 75)

Each element in a list is identified by a specific index. All elements in a list are ordered in a
particular sequence. This sequence does not change unless you actively change elements or sort
the sequence. Each element has an index, which tells you at what position in the sequence that
element is located. Typically the index value of the first position is zero (0). In the example above,
the number 67 is located at index position 0, and the number 81 is located as index position 4.

An array is a data type where the elements are identified by one or more indices. An array is
similar to a list, but an array can have multiple dimensions. A one-dimensional array is the same
as a list: a linear sequence of elements that are all of the same type.

• C Data Types

A few common data types supported by programming languages −

Type Keyword Value range which can be represented by this data type

Character char -128 to 127 or 0 to 255

Number int -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647

Small Number short -32,768 to 32,767

Long Number long -2,147,483,648 to 2,147,483,647

Decimal Number float 1.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
complex data types, which are called user-defined data type, for example a string will be a
sequence of characters.

EXAMPLES:

#include <stdio.h>

Page 19 of 54
SIT 112: INTROUCTION TO PROGRAMMING

int main()
{
char ch = 'A';
char str[20] = "facebook.com";
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf("Character is %c \n", ch);
printf("String is %s \n" , str);
printf("Float value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;
}
Output:

Explanation:

PRINTF() FUNCTION IN C LANGUAGE:

printf() and scanf() functions are inbuilt library functions in C programming language which are
available in C library by default. These functions are declared and related macros are defined in
“stdio.h” which is a header file in C language.

We have to include “stdio.h” file as shown in below C program to make use of these printf() and
scanf() library functions in C language.

In C programming language, printf() function is used to print the “character, string, float, integer,
octal and hexadecimal values” onto the output screen.

We use printf() function with %d format specifier to display the value of an integer variable.

Page 20 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for
double and %x for hexadecimal variable.

To generate a newline, we use “\n” in C printf() statement.

Note:

C language is case sensitive. For example, printf() and scanf() are different from Printf() and
Scanf(). All characters in printf() and scanf() functions must be in lower case.

2. SCANF() FUNCTION IN C LANGUAGE:

In C programming language, scanf() function is used to read character, string, numeric data from
keyboard.

Consider below example program where user enters a character. This value is assigned to the
variable “ch” and then displayed.

Then, user enters a string and this value is assigned to the variable “str” and then displayed.

Example:

#include <stdio.h>
int main()
{
char ch;
char str[100];
printf("Enter any character \n");
scanf("%c", &ch);
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
scanf("%s", &str);
printf("Entered string is %s \n", str);
}
Explanation:
• The format specifier %c is used in scanf() statement. So that, the value entered is received
as a character and %s for string.
• Ampersand is used before variable name “ch” in scanf() statement as &ch.
• It is just like in a pointer which is used to point to the variable

Page 21 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 6: COMPUTER PROGRAMMING VARIABLES

Variables are the names you give to computer memory locations which are used to store values
in a computer program. The variable's name represents what information the variable contains.
They are called variables because the represented information can change but the operations on
the variable remain the same. Variables represent all kinds of data, including booleans, names,
integers, arrays, pictures, sounds, scalars, strings, or any object or class of objects depending on
the programming language that supports them. The symbolic names of variables are replaced with
the actual data location by compilers and interpreters. Data in locations changes during execution
while locations and names are fixed.

Variable Properties

There are 6 properties associated with a variable. The first three are very important as you start to
program. The last three are important as you advance and improve your skills (and the complexity
of the programs you are creating).

1. Name: The name is Symbolic. It represents the "title" of the information that is being
stored with the variable. The name is perhaps the most important property to the
programmer, because this is how we "access" the variable. Every variable must have a
unique name!
2. Type: The type represents what "kind" of data is stored with the variable. In C, the type of
a variable must be explicitly declared when the name is created.
3. Value: A variable, by its very name, changes over time. Thus if the variable is jims_age
and is assigned the value 21. At another point, jims_age may be assigned the value 27.
4. Scope: Good programs are "Chopped" into small self-contained sections (called functions)
much like a good novel is broken into chapters, and a good chapter is broken into
paragraphs, etc. A variable that is seen and used in one function is NOT available in another
section. This allows us to reuse variable names, such as age. In one function 'age' could
refer to the age of a student, and in another function 'age' could refer to the vintage of a fine
wine.

Page 22 of 54
SIT 112: INTROUCTION TO PROGRAMMING

5. Life Time: The life time of a variable is strongly related to the scope of the variable. When
a program begins, variables "come to life" when the program reaches the line of code where
they are "declared". Variables "die" when the program leaves the "Scope" of the variable.
6. Location (in Memory): Luckily, we don't have to worry too much about where in the
computer hardware the variable is stored. But you should be aware that a "Bucket" or
"Envelope" exists in the hardware for every variable you declare. In the case of an array, a
"bunch of buckets" exist. Every bucket can contain a single value.

• Rules for naming Variables

1. A variable names must be different from keywords


2. A valid identifier can have letters (both uppercase and lowercase letters), digits and
underscores.
3. The first letter of an identifier should be either a letter or an underscore.
4. There is no rule on length of an identifier. However, the first 31 characters of identifiers
are discriminated by the compiler.
5. Give meaningful name to an identifier

Variable Vocabulary

1. "Access" a variable: It means to "read" (or assign) the information inside the variable. To
access a variable, you simply type its name. For example: total_age = jims_age + jens_age;
2. “Assign" a value to a variable: When you assign a value to a variable, you replace the
old value with a new one. The old value is gone forever. You can, and often do, replace the
old value of a variable based on the current value of the variable
3. "Declare" a variable: To declare a variable is to create the variable. In C and Java you
declare a variable by writing its TYPE followed by its name and assigning it a value. (e.g.,
'int jims_age = 21;').
4. "Initialize" a variable: Initializing a variable is another way to say, "declare a variable",
but further, implies giving it a base value. For example, if we are plotting something on the
X/Y axes, and we want to start at zero for Z, we would say ("x = 0;"). Thus we have
"initialized" the x variable to the value 0.

Page 23 of 54
SIT 112: INTROUCTION TO PROGRAMMING

5. "Stored" in a variable: The information "stored" in a variable, is the last piece of


information assigned to that variable name. After assigning into a variable, all previous
information is lost. Note, we often use the statement "store the value 5 in X" to mean the
same as "assign the value 5 to X"

Illustration

For example, assume you want to store two values 10 and 20 in your program and at a later stage,
you want to use these two values. Let's see how you will do it. Here are the following three simple
steps −

• Create variables with appropriate names.


• Store your values in those two variables.
• Retrieve and use the stored values from the variables.

Creating variables

Creating variables is also called declaring variables in C programming. Different programming


languages have different ways of creating variables inside a program. For example, C
programming has the following simple way of creating variables −

#include <stdio.h>

main() {
int a;
int b;
}

The above program creates two variables to reserve two memory locations with names a and b.
We created these variables using int keyword to specify variable data type which means we want
to store integer values in these two variables. Similarly, you can create variables to store long,
float, char or any other data type. For example −

/* variable to store long value */

Page 24 of 54
SIT 112: INTROUCTION TO PROGRAMMING

long a;

/* variable to store float value */


float b;

You can create variables of similar type by putting them in a single line but separated by comma
as follows −

#include <stdio.h>

main() {
int a, b;
}

Store Values in Variables

You have seen how we created variables in the previous section. Now, let's store some values in
those variables −

#include <stdio.h>

main() {

int a;
int b;

a = 10;
b = 20;

The above program has two additional statements where we are storing 10 in variable a and 20 is
being stored in variable b. Almost all the programming languages have similar way of storing

Page 25 of 54
SIT 112: INTROUCTION TO PROGRAMMING

values in variable where we keep variable name in the left hand side of an equal sign = and
whatever value we want to store in the variable, we keep that value in the right hand side.

Now, we have completed two steps, first we created two variables and then we stored required
values in those variables. Now variable a has value 10 and variable b has value 20. In other words
we can say, when above program is executed, the memory location named a will hold 10 and
memory location b will hold 20.

Access stored values in variables

If we do not use the stored values in the variables, then there is no point in creating variables and
storing values in them. We know that the above program has two variables a and b and they store
the values 10 and 20, respectively. So let's try to print the values stored in these two variables.
Following is a C program, which prints the values stored in its variables −

#include <stdio.h>

main() {

int a;
int b;

a = 10;
b = 20;

printf( "Value of a = %d\n", a );


printf( "Value of b = %d\n", b );

When the above program is executed, it produces the following result −

Value of a = 10

Page 26 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Value of b = 20

You must have seen printf() function in the previous chapter where we had used it to print "Hello,
World!". This time, we are using it to print the values of variables. We are making use of %d,
which will be replaced with the values of the given variable in printf() statements. We can print
both the values using a single printf() statement as follows −

#include <stdio.h>

main() {

int a;
int b;

a = 10;
b = 20;

printf( "Value of a = %d and value of b = %d\n", a, b );


}

When the above program is executed, it produces the following result −

Value of a = 10 and value of b = 20

If you want to use float variable in C programming, then you will have to use %f instead of %d,
and if you want to print a character value, then you will have to use %c. Similarly, different data
types can be printed using different % and characters.

Page 27 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Computer Programming Keywords

In programming, a keyword is a word that is reserved by a program because the word has a special
meaning and can be commands or parameters. Every programming language has a set of keywords
that cannot be used as variable names. Keywords are sometimes called reserved names. So far, we
have covered two important concepts called variables and their data types. Different programming
languages provide different set of reserved keywords, but there is one important & common rule
in all the programming languages that we cannot use a reserved keyword to name our variables,
which means we cannot name our variable like int or float rather these keywords can only be used
to specify a variable data type. For example, if you will try to use any reserved keyword for the
purpose of variable name, then you will get a syntax error.

#include <stdio.h>
main() {
int float;
float = 10;
printf( "Value of float = %d\n", float);
}
When you compile the above program, it produces the following error −
main.c: In function 'main':
main.c:5:8: error: two or more data types in declaration specifiers
int float;
......

Let's now give a proper name to our integer variable, then the above program should compile and
execute successfully −

#include <stdio.h>
main() {
int count;
count = 10;
printf( "Value of count = %d\n", count);

Page 28 of 54
SIT 112: INTROUCTION TO PROGRAMMING

C Programming Reserved Keywords

Here is a table having almost all the keywords supported by C Programming language −

Auto Else long switch

Break Enum register typedef

Case Extern return union

Char Float short unsigned

Const For signed void

Continue Goto sizeof volatile

Default If static while

Do Int struct _Packed

Double

Page 29 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 7: COMPUTER PROGRAMMING OPERATORS

An operator in a programming language is a symbol that tells the compiler or interpreter to


perform specific mathematical, relational or logical operation and produce final result. This lesson
will explain the concept of operators and it will take you through the important arithmetic and
relational operators available in C.

Arithmetic Operators

Computer programs are widely used for mathematical calculations. We can write a computer
program which can do simple calculation like adding two numbers (2 + 3) and we can also write
a program, which can solve a complex equation like P(x) = x4 + 7x3 - 5x + 9. In first expression 2
and 3 are operands and + is an operator. Similar concepts exist in Computer Programming. Take
a look at the following two examples −

2+3
P(x) = x4 + 7x3 - 5x + 9.

These two statements are called arithmetic expressions in a programming language and plus,
minus used in these expressions are called arithmetic operators and the values used in these
expressions like 2, 3 and x, etc., are called operands. In their simplest form, such expressions
produce numerical results. Similarly, a programming language provides various arithmetic
operators. The following table lists down a few of the important arithmetic operators available in
C programming language. Assume variable A holds 10 and variable B holds 20, then −

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

Page 30 of 54
SIT 112: INTROUCTION TO PROGRAMMING

/ Divides numerator by de-numerator B / A will give 2

% This gives remainder of an integer division B % A will give 0

Following is a simple example of C Programming to understand the above mathematical operators


#include <stdio.h>
main() {
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf( "Value of c = %d\n", c);
c = a - b;
printf( "Value of c = %d\n", c);
c = a * b;
printf( "Value of c = %d\n", c);
c = b / a;
printf( "Value of c = %d\n", c);
c = b % a;
printf( "Value of c = %d\n", c);
}

When the above program is executed, it produces the following result −

Value of c = 30
Value of c = -10
Value of c = 200
Value of c = 2
Value of c = 0

Page 31 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Relational Operators

Consider a situation where we create two variables and assign them some values as follows −

A = 20
B = 10

Here, it is obvious that variable A is greater than B in values. So, we need the help of some symbols
to write such expressions which are called relational expressions. If we use C programming
language, then it will be written as follows −

(A > B)

Here, we used a symbol > and it is called a relational operator and in their simplest form, they
produce Boolean results which means the result will be either true or false. Similarly, a
programming language provides various relational operators. The following table lists down a few
of the important relational operators available in C programming language. Assume variable A
holds 10 and variable B holds 20, then −

Operator Description Example

Checks if the values of two operands are equal or


== (A == B) is not true.
not, if yes then condition becomes true.

Checks if the values of two operands are equal or


!= not, if values are not equal then condition becomes (A != B) is true.
true.

Checks if the value of left operand is greater than the


> value of right operand, if yes then condition (A > B) is not true.
becomes true.

Page 32 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Checks if the value of left operand is less than the


< value of right operand, if yes then condition (A < B) is true.
becomes true.

Checks if the value of left operand is greater than or


>= equal to the value of right operand, if yes then (A >= B) is not true.
condition becomes true.

Checks if the value of left operand is less than or


<= equal to the value of right operand, if yes then (A <= B) is true.
condition becomes true.

Here, is one example of C Programming which makes use of if conditional statement. Though
this statement will be discussed later in a separate chapter, but in short, we use if statement to
check a condition and if the condition is true, then the body of if statement is executed, otherwise
the body of if statement is skipped.

#include <stdio.h>
main() {
int a, b;
a = 10;
b = 20;
/* Here we check whether a is equal to 10 or not */
if( a == 10 ) {
/* if a is equal to 10 then this body will be executed */
printf( "a is equal to 10\n");
}
/* Here we check whether b is equal to 10 or not */
if( b == 10 ) {
/* if b is equal to 10 then this body will be executed */
printf( "b is equal to 10\n");
}

Page 33 of 54
SIT 112: INTROUCTION TO PROGRAMMING

/* Here we check if a is less b than or not */


if( a < b ) {
/* if a is less than b then this body will be executed */
printf( "a is less than b\n");
}
/* Here we check whether a and b are not equal */
if( a != b ) {
/* if a is not equal to b then this body will be executed */
printf( "a is not equal to b\n");
}
}

When the above program is executed, it produces the following result −

a is equal to 10
a is less than b
a is not equal to b

Logical Operators

Logical operators are very important in any programming language and they help us take decisions
based on certain conditions. Suppose we want to combine the result of two conditions, then logical
AND and OR logical operators help us in producing the final result. The following table shows all
the logical operators supported by the C language. Assume variable A holds 1 and variable B holds
0, then –

Operator Description Example

Called Logical AND operator. If both the operands


&& (A && B) is false.
are non-zero, then condition becomes true.

Called Logical OR Operator. If any of the two


|| (A || B) is true.
operands is non-zero, then condition becomes true.

Page 34 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Called Logical NOT Operator. Use to reverses the


! logical state of its operand. If a condition is true then !(A && B) is true.
Logical NOT operator will make false.

Try the following example to understand all the logical operators available in C programming
language −

#include <stdio.h>
main() {
int a = 1;
int b = 0;
if ( a && b ) {
printf("This will never print because condition is false\n" );
}
if ( a || b ) {
printf("This will be printed print because condition is true\n" );
}
if ( !(a && b) ) {
printf("This will be printed print because condition is true\n" );
}
}

When you compile and execute the above program, it produces the following result −

This will be printed print because condition is true


This will be printed print because condition is true

EXAMPLES:

1. C Program to Calculate the Simple Interest

#include <stdio.h>

Page 35 of 54
SIT 112: INTROUCTION TO PROGRAMMING

void main()

float principal_amt, rate, simple_interest;

int time;

printf("Enter the values of principal_amt, rate and time \n");

scanf("%f %f %d", &principal_amt, &rate, &time);

simple_interest = (principal_amt * rate * time) / 100.0;

printf("Amount = Ksh. %f\n", principal_amt);

printf("Rate = Ksh. %f%\n", rate);

printf("Time = %d years\n", time);

printf("Simple interest = %.2f\n", simple_interest);

2. Volume and surface area of a cylinder

#include <stdio.h>

int main()

float radius, height;

float surface_area, volume;

printf("Enter value for radius and height of a cylinder : \n");

scanf("%f%f", &radius, &height);

surface_area = 2 * (22 / 7) * radius * (radius + height);

Page 36 of 54
SIT 112: INTROUCTION TO PROGRAMMING

volume = (22 / 7) * radius * radius * height;

printf("Surface area of cylinder is: %.3f", surface_area);

printf("\n Volume of cylinder is : %.3f", volume);

return 0;

TASK:

1. Write a C program code to calculate compound interest


2. Write a C program code to calculate area of trapezium

Page 37 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 8: LOGIC

Problem Algorithm

An Algorithm is a logical sequence of discrete steps that describe a complete solution to a given
problem in a finite amount of time independently of the software or hardware of the computer. It
is the set of rules that define how a particular problem can be solved in finite number of steps.
Algorithms are very essential as they instructs the computer what specific steps it needs to perform
to carry out a particular task or solve a problem.

Every algorithm should have the following five characteristics: Input, Output, Definiteness,
Effectiveness and Termination. An Algorithm has the following properties;

 It must be precise and unambiguous


 It must give the correct solution in all cases
 It must eventually end.
Flow Charts
A flow chart is a traditional means of showing in diagrammatic form, the sequence of steps to be
undertaken in solving a problem. Flowcharts or flow diagrams are important tools in writing a
computer program. A flowchart allows you to plan the sequence of steps in a program before
writing it. The flowchart serves as a visual representation which many programmers find
indispensable in planning any program of at least moderate complexity.

Elements of a Flowchart.

A flowchart consists of a set of boxes, the shapes of which indicate specific operations. The
separate boxes are connected with arrows to show the sequences in which the various operations
are performed.

Task: Describe the standard symbols with their names and description.

Pseudo code

An alternative method of representing an Algorithm to the flowcharts. Pseudo code is halfway


between English and programming language and is based upon a few simple grammatical
construction which avoid the ambiguities of English but which can be easily converted into

Page 38 of 54
SIT 112: INTROUCTION TO PROGRAMMING

computer programming language. Pseudo code is an informal high-level description of a computer


programming algorithm, which omits details that are not essential for human understanding of the
algorithm, is easier for humans to understand than conventional programming language code, is
compact and environment-independent description of the key principles of an algorithm and
resembles skeleton programs including dummy code and can be compiled without errors.

COMPUTER PROGRAMMING DECISION

Decision making is about deciding the order of execution of statements based on certain conditions
or repeat a group of statements until certain specified conditions are met. Decision making is
critical to computer programming. It requires that the programmer specifies one or more conditions
to be evaluated or tested by the program, along with a statement or statements to be executed if
the condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false. There will be many situations when you will be given two or
more options and you will have to select an option based on the given conditions. For example,
we want to print a remark about a student based on his secured marks. Following is the situation

Assume given marks are x for a student:


If given marks are more than 80, then
Student is brilliant
If given marks are less than 30, then
Student is poor
If given marks are less than 80 and more than 30, then
Student is average

Now, the question is how to write a programming code to handle such situations. Almost all the
programming languages provide conditional statements that work based on the following flow
diagram −

Page 39 of 54
SIT 112: INTROUCTION TO PROGRAMMING

The following is a C program with the help of if conditional statements to convert the above
given situation into a programming code −

#include <stdio.h>
main() {
int x = 45;
if( x > 80)
{
printf( "Student is brilliant\n");
}
if( x < 30)
{
printf( "Student is poor\n");
}
if( x < 80 && x > 30 )
{
printf( "Student is average\n");
}
}

When the above program is executed, it produces the following result −

Page 40 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Student is average

The above program uses if conditional statements. Here, the first if statement checks whether
the given condition i.e., variable x is greater than 80 or not and if it finds the condition is true, then
the conditional body is entered to execute the given statements. Here we have only one printf()
statement to print a remark about the student. Similarly, the second if statement works. Finally,
the third if statement is executed, here we have the following two conditions −

• First condition is x > 80


• Second condition is x < 30

The computer evaluates both the given conditions and then, the overall result is combined with the
help of the binary operator &&. If the final result is true, then the conditional statement will be
executed, otherwise no statement will be executed. Different programming languages provide
different types of decision-making statements, but the basic concept remains the same.

1. if...else statement

An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false. The syntax of an if...else statement in C programming language is −

if(boolean_expression) {

/* Statement(s) will execute if the boolean expression is true */


}
else {

/* Statement(s) will execute if the boolean expression is false */


}

The above syntax can be represented in the form of a flow diagram as shown below −

Page 41 of 54
SIT 112: INTROUCTION TO PROGRAMMING

An if...else statement is useful when we have to take a decision out of two options. For example,
if a student secures more marks than 80, then the student is brilliant, otherwise no such situation
can be coded, as follows −

#include <stdio.h>
main() {
int x = 45;
if( x > 80) {
printf( "Student is brilliant\n");
}
else {
printf( "Student is average\n");
}
}

When the above program is executed, it produces the following result −

Page 42 of 54
SIT 112: INTROUCTION TO PROGRAMMING

Student is average

2. if...elseif...else statement

An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions. While using if, else if, else statements, there are a few points to keep in mind

• An if can have zero or one else's and it must come after an else if.
• An if can have zero to many else…if's and they must come before the else.
• Once an else…if succeeds, none of the remaining else…if's or else's will be tested.

The syntax of an if...else if...else statement in C programming language is −

if(boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3) {
/* Executes when the boolean expression 3 is true */
}
else {

/* Executes when the none of the above condition is true */


}

Now with the help of if...elseif...else statement, the very first program can be coded as follows −

#include <stdio.h>
main() {
int x = 45;
if( x > 95) {

Page 43 of 54
SIT 112: INTROUCTION TO PROGRAMMING

printf( "Student is brilliant\n");


}else if( x < 30) {
printf( "Student is poor\n");
}else if( x < 95 && x > 30 ) {
printf( "Student is average\n");
}
}

When the above program is executed, it produces the following result −

Student is average

2. The Switch Statement

A switch statement is an alternative of if statements which allows a variable to be tested for


equality against a list of values. Each value is called a case, and the variable being switched on is
checked for each switch case. It has the following syntax −

switch(expression){
case ONE :
statement(s);
break;
case TWO:
statement(s);
break;
......
default :
statement(s);
}

The expression used in a switch statement must give an integer value, which will be compared
for equality with different cases given. Wherever an expression value matches with a case value,
the body of that case will be executed and finally, the switch will be terminated using a break

Page 44 of 54
SIT 112: INTROUCTION TO PROGRAMMING

statement. If no break statements are provided, then the computer continues executing other
statements available below to the matched case. If none of the cases matches, then the default case
body is executed. The above syntax can be represented in the form of a flow diagram as shown
below −

Now, let's consider another example where we want to write the equivalent English word for a
given number. Then, it can be coded as follows −

#include <stdio.h>
main() {
int x = 2;
switch( x ){
case 1 :
printf( "One\n");
break;
case 2 :

Page 45 of 54
SIT 112: INTROUCTION TO PROGRAMMING

printf( "Two\n");
break;
case 3 :
printf( "Three\n");
break;
case 4 :
printf( "Four\n");
break;
default :
printf( "None of the above...\n");
}
}

When the above program is executed, it produces the following result −

Two
EXAMPLE:

#include <stdio.h>

main() {

int x;

printf("enter marks\n",x);

scanf("%d", &x);

switch( x/10 ){

case 10:

case 9:

case 8:

case 7:

Page 46 of 54
SIT 112: INTROUCTION TO PROGRAMMING

printf( "A\n");

break;

case 6:

printf( "B\n");

break;

case 5 :

printf( "C\n");

break;

case 4 :

printf( "D\n");

break;

default :

printf( "FAILED\n");

Page 47 of 54
SIT 112: INTROUCTION TO PROGRAMMING

LESSON 9: COMPUTER PROGRAMMING LOOPS

Computer Programming - Loops

In computer programming, a loop is a sequence of instructions that is continually repeated until a


certain condition is reached. Typically a certain process is done, such as getting an item of data
and changing it and the some condition is checked such as whether a counter has reached a
prescribed number. Let's consider a situation when you want to print Hello, World! five times.
Here is a simple C program to do the same −

#include <stdio.h>
main() {
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
}

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

It was simple, but again, let's consider another situation when you want to write Hello, World! a
thousand times. We can certainly not write printf() statements a thousand times. Almost all the
programming languages provide a concept called loop, which helps in executing one or more
statements up to a desired number of times. All high-level programming languages provide various
forms of loops, which can be used to execute one or more statements repeatedly. Let's write the
above C program with the help of a while loop.

Page 48 of 54
SIT 112: INTROUCTION TO PROGRAMMING

#include <stdio.h>
main() {
int i = 0;
while ( i < 5 )
{
printf( "Hello, World!\n");
i = i + 1;
}
}

When the above program is executed, it produces the following result −

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

The above program makes use of a while loop, which is being used to execute a set of
programming statements enclosed within {....}. Here, the computer first checks whether the given
condition, i.e., variable "i" is less than 5 or not and if it finds the condition is true, then the loop
body is entered to execute the given statements. Here, we have the following two statements in the
loop body −

• First statement is printf() function, which prints Hello World!


• Second statement is i = i + 1, which is used to increase the value of variable i

After executing all the statements given in the loop body, the computer goes back to while( i < 5)
and the given condition, (i < 5), is checked again, and the loop is executed again if the condition
holds true. This process repeats till the given condition remains true which means variable "i" has
a value less than 5.

Page 49 of 54
SIT 112: INTROUCTION TO PROGRAMMING

To conclude, a loop statement allows us to execute a statement or group of statements multiple


times. Given below is the general form of a loop statement in most of the programming languages

The while Loop

A while loop available in C Programming language has the following syntax −

while ( condition ) {
/*....while loop body ....*/
}

The above code can be represented in the form of a flow diagram as shown below −

Page 50 of 54
SIT 112: INTROUCTION TO PROGRAMMING

The following important points are to be noted about a while loop −

• A while loop starts with a keyword while followed by a condition enclosed in ( ).


• Further to the while() statement, you will have the body of the loop enclosed in curly braces
{...}.
• A while loop body can have one or more lines of source code to be executed repeatedly.
• If the body of a while loop has just one line, then its optional to use curly braces {...}.
• A while loop keeps executing its body till a given condition holds true. As soon as the
condition becomes false, the while loop comes out and continues executing from the
immediate next statement after the while loop body.
• A condition is usually a relational statement, which is evaluated to either true or false. A
value equal to zero is treated as false and any non-zero value works like true.

Page 51 of 54
SIT 112: INTROUCTION TO PROGRAMMING

The For Loop Statement

The for loop is an entry-controlled loop that provides a more concise loop control structure. The
general
form for the for loop is

for (initialization ; test-condition; increment)


{
body of the loop
}

The execution of the for statement is as follows;

1. Initialization of the control variables is done first using assignment statements such as i =1 and
count = 0;. The i and count are called the loop control variables.

2. The value of the control variable is tested using the test-condition. The test-condition is a
relational expression such as i<10 that determines when the loop will exit. If the condition is
true, the body of the loop is executed; otherwise the loop is terminated and the execution
continues with the statement that immediately follows the loop.

When the body of the loop is executed, the control is transferred back to the for statement after
evaluating the last statement in the loop. The control is incremented using an assignment statement
such as i = i + 1 and the new value of the control variable is again tested to see whether it satisfies
the loop condition.

FOR LOOP EXAMPLE

1. Printing digits 0 to 9

#include<stdio.h>

main()

Page 52 of 54
SIT 112: INTROUCTION TO PROGRAMMING

int x;

for (x =0; x<=9; x= x+1)

printf("%d", x);

printf("\n");

Output:

2. Printing multiple table for an integer

#include <stdio.h>

int main()

int n, i;

printf("Enter an integer: ");

scanf("%d",&n);

for(i=1; i<=10; ++i)

printf("%d * %d = %d \n", n, i, n*i);

Page 53 of 54
SIT 112: INTROUCTION TO PROGRAMMING

return 0;

Output

Note: the three sections in the for ( ) statement must be separated by semicolons(;). There is no (;)
after the increment section (x = x+1)

Page 54 of 54

You might also like