Computer Fundamentals
Computer Fundamentals
Computer Fundamentals
Computer Science
Chapter – 1
Programming Fundamentals
WHAT IS A COMPUTER PROGRAM?
A program is a set of instructions that a computer follows in order to perform a particular task.
WHAT IS A PROGRAMMING LANGUAGE?
A programming language is a formal language, which comprises a set of instructions
that produce various kinds of output.
Programming languages are used in
computer programming to implement
algorithms. Most programming languages consist of instructions for computers.
Ada Lovelace is considered as the first programmer, who worked with Sir Charles Babbage
on the Analytical Engine ( First Generation general purpose mechanical computer partly
built by English inventor Charles Babbage).
1|Page
Types of Programming Languages
Machine Language
It is known as machine code or object code, machine language is
a collection of binary digits or bits that the computer reads and interprets.
Machine language is the only language a computer is capable of understanding.
Machine language example
Below is an example of machine language (binary) for the text "Hello World".
01001000 01100101 01101100 01101100 01101111
00100000 01010111 01101111 01110010 01101100
01100100
Assembly Language
An assembly language is a low-level programming language.
It uses Mnemonic codes to write the program (i.e.) short form of
the command. Ex: mov, load, add etc.
The assembly language program is compiled by a compiler called an Assembler.
2|Page
Activity
Write your name using the upper-case letters of Binary code.
1 A 00001 14 N 01110
2 B 00010 15 O 01111
3 C 00011 16 P 10000
4 D 00100 17 Q 10001
5 E 00101 18 R 10010
6 F 00110 19 S 10011
7 G 00111 20 T 10100
8 H 01000 21 U 10101
9 I 01001 22 V 10110
10 J 01010 23 W 10111
11 K 01011 24 X 11000
12 L 01100 25 Y 11001
13 M 01101 26 Z 11010
3|Page
Difference between high level and low level language
S.no High Level Language Low Level Language
1 It is programmer friendly language. It is a machine friendly language.
2 High level language is less memory efficient. Low level language is high memory
efficient.
3 It is easy to understand It is tough to understand
4 It is simple to debug. It is complex to debug comparatively.
5 It is simple to maintain. It is complex to maintain comparatively.
6 It is portable. It is non-portable.
7 It can run on any platform. It is machine-dependent.
8 It needs compiler or interpreter for translation. It needs assembler for translation.
9 It is used widely for programming. It is not commonly used now-a-
days in programming.
Windows Operating System is written in C++, C#, Java Script and Visual Basic
Language Processor
A language processor is a software program designed or used to perform
tasks such as processing program code to machine code.
Types of Language
4|Page
Compiler
Compiler
A compiler is a translator which transforms source language (high-
level language) into object language (machine language).
Interpreter
5|Page
Example algorithm -Going to the market to purchase a pen.
1. Get dressed to the market.
2. Check your wallet for money.
3. If there is no money in the wallet, replenish it.
4. Go to the shop.
5. Ask for your favorite brand of pen.
6. If pen is not available ,go to step 10 else go to step 7.
7. Give money to the shopkeeper.
8. Keep the purchased pen safely.
9. Go back home.
10. Ask for any other brand of pen. 11.Go to step 7.
Exercise
Create an algorithm to check whether a number is positive or negative.
6|Page
Write an algorithm to find the sum of two integers.
1. Start
2. Read num
3. Is num>0
4. Disply “ the given number is positive”
5. Else
6. Disply “ the given number is negative”
7. Stop
Flowchart
Flowchart is a diagrammatic representation of sequence of
logical steps of a program. Flowcharts use simple geometric
shapes to depict processes and arrows to show relationships and process/data flow.
Flowchart symbols
7|Page
Flowchart to calculate the average of two numbers.
8|Page
Step-2 : Input temperature in Celsius say C Step-3 : F = (9.0/5.0 x C) + 32
Step-4 : Display Temperature in Fahrenheit F Step-5 : Stop
Exercise
Draw the flowchart to check whether a number is positive or negative.
Draw the flowchart to find the sum of two integers.
Draw the flowchart for finding the perimeter of a triangle.
IPO CYCLE
The entire process that involves input and output action is said to be IPO cycle.
9|Page
10 | P a g e
Activity
Collect information about the following.
1. Input devices
2. Output devices
3. CPU
4. Storage devices
CHAPTER 2
NUMBER SYSTEM
Definition of Number System in Computers
In computers, Number System is defined as a writing system to represent the
numbers in different ways i.e. we are using different symbols and notations to represent
numbers.
There are four ways we can represent the number.
That is, there are four types of Number System – Binary, Decimal, Octal, Hexadecimal.
11 | P a g e
The modern binary number system is credited to Gottfried Leibniz in 1679, a
German mathematician.
Binary has been used in nearly everything electronic; from calculators to
supercomputers. Machine code is binary digits.
Binary 2 0 or 1 (1010111)2
12 | P a g e
Decimal 10 0,1,2,3,4,5,6,7,8,9 (104)10
13 | P a g e
Convert the following decimal numbers into binary.
1. (99)10
2. (123) 10
3. (48) 10
14 | P a g e
Octal to Decimal
Decimal to Hexadecimal
Hexadecimal to Decimal
15 | P a g e
Binary game
https://games.penjee.com/binary-bonanza/
Functional Components of a Computer
Input Unit
The input unit consists of input devices that are attached to the computer.
These devices take input and convert it into binary language that the
computer understands.
Some of the common input devices are keyboard, mouse, joystick, scanner etc.
16 | P a g e
Thereafter CPU executes or performs the required computation and then either stores the
output or displays on the output device.
The CPU has three main components which are responsible for different functions –
1. Arithmetic Logic Unit (ALU)
2. Control Unit (CU)
3. Memory Unit
Arithmetic and Logic Unit (ALU)
The ALU, as its name suggests performs mathematical calculations and takes
logical decisions.
Arithmetic calculations include addition, subtraction, multiplication and division.
Logical decisions involve comparison of two data items to see which one is larger
or smaller or equal.
Control Unit
The Control unit coordinates and controls the data flow in and out of CPU and
also controls all the operations of ALU, memory registers and also input/output units.
It is also responsible for carrying out all the instructions stored in the program.
It decodes the fetched instruction, interprets it and sends control signals to input/output
devices until the required operation is done properly by ALU and memory.
Processor
Memory Unit
Memory attached to the CPU is used for storage of data and instructions and is called
internal memory .
The internal memory is divided into many storage locations, each of which can store data
or instructions.
Each memory location is of the same size and has an address.
With the help of the address, the computer can read any memory location easily without
having to search the entire memory.
when a program is executed, it’s data is copied to the internal memory answer is stored in
the memory till the end of the execution.
The internal memory is also called the Primary memory or Main memory.
17 | P a g e
This memory is also called as RAM, i.e. Random Access Memory.
Random Access Memory
Output Unit
The output unit consists of output devices that are attached with the computer.
It converts the binary data coming from CPU to human understandable form.
The common output devices are monitor, printer, plotter etc.
Tech Fact
There are 4 types of DVDs based on the size.
DVD 5: Also called a single-sided single – layered. It has the capacity of 4.7 GB.
DVD 10: Also called a Double-sided single-layered. It has the capacity of 8.75 GB.
DVD 9: Also called a single-sided Dual-layered. It has the capacity of 7.95 GB.
DVD 18: Also called a Double-sided Dual-layered. It has the capacity of 17.95 GB.
Output devices
18 | P a g e
CHAPTER 3
INTRODUCTION TO “GO Lang”
Introduction
Go language is a programming language initially developed at Google in the year
2007 by Robert Griesemer, Rob Pike, and Ken Thompson.
It is a statically-typed language having syntax similar to that of C.
Go was created with the goal of:
The simplicity of use while increasing productivity.
Providing a high level of code efficiency.
Advanced performance for businesses.
Here are some notable companies that have shifted to using Golang:
Google
Apple
Facebook
Docker
The New York Times
BBC
19 | P a g e
package main
import "fmt"
func main() {
/* This is my first sample program. */ fmt.Println("Hello, World!")
}
1. The first line of the program package main defines the package name in which this
program should lie. It is a mandatory statement, as Go programs run in packages.
2. The next line import "fmt" is a preprocessor command which tells the Go compiler to
include the files lying in the package fmt.
3. The next line func main() is the main function where the program execution begins.
4. The next line /*...*/ is ignored by the compiler and it is there to add comments in the
program. Comments are also represented using // similar to Java or C++ comments.
5. The next line fmt.Println(...) is another function available in Go which causes the message
"Hello, World!" to be displayed on the screen. Here fmt package has
exported Println method which is used to display the message on the screen.
6. Notice the capital P of Println method. In Go language, a name is exported if it starts with
capital letter.
Go Data Types
Data types specify the type of data that a valid Go variable can hold. In Go language,
the type is divided into four categories which are as follows:
Basic type: Numbers, strings, and Booleans.
20 | P a g e
Data Type
Unlike humans, a computer does not know the difference between "1234" and "abcd."
A data type is a classification that dictates what a variable or object can hold in computer
programming.
Strings
The string data type represents a sequence of Unicode code points.
Or in other words, we can say a string is a sequence of immutable bytes, means once
a string is created you cannot change that string. A string may contain arbitrary
data, including bytes with zero value in the human-readable form.
Number
They are again arithmetic types and they represents
a. integer types or
b. b) floating point
values throughout the program.
Booleans
The Boolean data type represents only one bit of information either true or false.
The values of type Boolean are not converted implicitly or explicitly to any other type.
21 | P a g e
Variables
Here're some important things to know about Golang variables:
Golang is statically typed language, which means that when variables are declared, they
either explicitly or implicitly assigned a type even before your program runs.
Golang requires that every variable you declare inside main() get used somewhere in
your program.
You can assign new values to existing variables, but they need to be values of the same
type.
Naming rules in Golang
A name must begin with a letter, and can have any number of additional letters and
numbers.
A variable name cannot start with a number.
A variable name cannot contain spaces.
If a name consists of multiple words, each word after the first should be capitalized like
this: empName, EmpAddress, etc.
variable names are case-sensitive (car, Car and CAR are three different variables).
Variable declaration
Declaring a single variable
var name type is the syntax to declare a single variable.
Ex:
package main
import "fmt"
func main() {
var age int // variable declaration
fmt.Println("My age is", age)
}
A variable can be assigned to any value of its type. In the above program, age can be assigned any
integer value.
package main
import "fmt"
func main() {
var age int // variable declaration
fmt.Println("My age is", age)
22 | P a g e
age = 29 //assignment
fmt.Println("My age is", age)
age = 54 //assignment
fmt.Println("My new age is", age)
}
Multiple variable declaration
Multiple variables can be declared using a single statement.
Syntax
var name1, name2 type = initialvalue1, initialvalue2
Example
package main
import "fmt"
func main() {
var width, height int = 100, 50 //declaring multiple variables
fmt.Println("width is", width, "height is", height)
}
The type can be removed if the variables have an initial value. Since the above program
has initial values for variables, the int type can be removed.
Ex
package main
import "fmt"
func main() {
var width, height = 100, 50 //"int" is dropped
fmt.Println("width is", width, "height is", height)
}
If the initial value is not specified for width and height, they will have 0 assigned as their
initial value.
Ex
package main
import "fmt"
func main() {
var width, height int
23 | P a g e
fmt.Println("width is", width, "height is", height)
width = 100
height = 50
fmt.Println("new width is", width, "new height is", height)
}
There might be cases where we would want to declare variables belonging to different types in a
single statement.
Syntax
var (
name1 = initialvalue1
name2 = initialvalue2
)
package main
import "fmt"
func main() {
var (
name = "naveen"
age = 29
height int
)
fmt.Println("my name is", name, ", age is", age, "and height is", height)
}
Short hand declaration
Go also provides another concise way to declare variables. This is known as short
hand declaration and it uses := operator.
name := initialvalue
The following program uses the short hand syntax to declare a
variable count initialized to 10. Go will automatically infer that count is of type int since it
has been initialized with the integer value 10.
24 | P a g e
import "fmt"
func main() {
count := 10
fmt.Println("Count =",count)
}
Short hand declaration……
package main
import "fmt"
func main() {
name, age := "naveen", 29 //short hand declaration
fmt.Println("my name is", name, "age is", age)
}
Go - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations.
Go language is rich in built-in operators and provides the following types of operators −
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Miscellaneous Operators
Arithmetic Operators
These are used to perform arithmetic/mathematical operations on operands in Go language:
Addition: The ‘+’ operator adds two operands. For example, x+y.
Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
25 | P a g e
Modulus: The ‘%’ operator returns the remainder when first operand is divided by the second. For
example, x%y
// Multiplication
result3:= p * q
// Division
result4:= p / q
// Modulus
result5:= p % q
Output:
Result of p + q = 54
Result of p - q = 14
Result of p * q = 680
Result of p / q = 1
Result of p % q = 14
26 | P a g e
Relational Operators
Relational operators are used for comparison of two values. Let’s see them one by one:
== is the equality operator. This returns true if both the operands are referring to the same
object, otherwise false.
!= is for non-equality operator. ...
< is less than operator.
> is greater than operator.
<= is less than or equal to operator.
> = is greater than or equal to operator.
‘=='(Equal To) operator checks whether the two given operands are equal or not.
If so, it returns true. Otherwise it returns false.
For example, 5==5 will return true.
‘!='(Not Equal To) operator checks whether the two given operands are equal or not. If not, it
returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator.
For example, 5!=5 will return false.
‘>'(Greater Than)operator checks whether the first operand is greater than the second operand. If
so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
‘<‘(Less Than)operator checks whether the first operand is lesser than the second operand. If so, it
returns true. Otherwise it returns false. For example, 6<5 will return false.
‘>='(Greater Than Equal To)operator checks whether the first operand is greater than or equal to
the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return
true.
‘<='(Less Than Equal To)operator checks whether the first operand is lesser than or equal to the
second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return
true.
27 | P a g e
fmt.Println(result1)
// ‘!='(Not Equal To)
result2:= p != q
fmt.Println(result2)
// ‘<‘(Less Than)
result3:= p < q
fmt.Println(result3)
// ‘>'(Greater Than)
result4:= p > q
fmt.Println(result4)
}
Output:
false
true
false
true
true
false
Logical Operators
They are used to combine two or more conditions/constraints or to complement the evaluation of
the original condition in consideration.
28 | P a g e
Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are
satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true
(i.e. non-zero).
Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is
satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-
zero). Of course, it returns true when both a and b are true.
29 | P a g e
Logical multiplication
Ex
(p>q) && (p!=q) ex:p=35 and q=20
T && T
T
Logical or - ||
Logical addition
(p>q) || (p==q)
T || F
T
NOT
p>q - True
!(p>q)= !(True) = False
if(p!=q || p<=q){
fmt.Println("True")
}
30 | P a g e
if(!(p==q)){
fmt.Println("True")
}
}
Output:
True
True
True
31 | P a g e