Computer Fundamentals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Class-7

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

High Level Language


 A high-level language (HLL) is
a programming languages such as C, FORTRAN, or Pascal that enables a programmer to
write programs that are more or less independent of a particular type
of computer.
 Such languages are considered high-level because they are closer to human languages.

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

An interpreter is a computer program that is used to directly execute


program instructions written using one of the many high-level programming languages.

Algorithms and Flow chart


 A finite set of steps that must be followed to solve any problem is called an algorithm.
 Algorithm is generally developed before the actual coding is done.
 It is written using English like language so that it is easily understandable even by non-
programmers.
 Sometimes algorithms are
written using pseudo codes, i.e. a language similar to the programming language to be us
ed.
Advantages of algorithm
 It is a step-wise representation of a solution to a given problem, which makes it easy
to understand.
 An algorithm uses a definite procedure.
 It is not dependent on any programming language, so it is easy to
understand for anyone even without programming knowledge.
 Every step in an algorithm has its own logical sequence so it is easy to debug

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.

Algorithm & Flowchart to convert temperature from Celsius to Fahrenheit

C : temperature in Celsius F : temperature Step-1 : Start

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.

Binary Number System


 Binary Number System is a number system in which we represent the numbers by
using only two symbols i.e. 0 or 1.
 0 represents OFF and 1 represents ON.
 Hence, the base value of binary numbers is 2.
 Binary number system is used when there are only two possibilities or outcomes.
For examples, Electric bulb can be On or Off.

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.

 What is Octal Number System?


Octal numbers can be represented using 8 different values or digits from 0 to 7.
 Since we can represent the number using 8 different digits the base of Octal
Number System is 8.
 Also, there is a relationship between Binary and Octal Number system. The group
of 3 binary digits makes one octal digit. Since, 2 3 = 8.
What is Decimal Number System?
 In decimal number system we have 10 digits – 0 to 9 to represent the numbers. Hence,
the base value of the Decimal Number system is 10.
 Decimal number system is used when there are 10 possible outputs of a system.
For example, top 10 students from a class.
 Hexadecimal Number System
In Hexadecimal number system, we add 6 more digits to Decimal number system.
 Which means hexadecimal number system is a number system in which we use 16
different values to represent the numbers
 That is 0 to 9 and A to F. A, B, C, D, E, F represents 10, 11, 12, 13, 14, 15 respectively.
Number System Table – Binary, Octal, Decimal, Hexadecimal

Number System Base Digits used Examples

Binary 2 0 or 1 (1010111)2

Octal 8 0,1,2,3,4,5,6,7 (480)8

12 | P a g e
Decimal 10 0,1,2,3,4,5,6,7,8,9 (104)10

Hexadecimal 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F (AF)16

How do you measure computer memory?

Decimal to binary conversion

13 | P a g e
Convert the following decimal numbers into binary.

1. (99)10
2. (123) 10
3. (48) 10

Binary to decimal conversion

Convert the following binary number into decimal number


1. (101101)2
2. (11100) 2
3. (1100111) 2
Decimal to Octal

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.

Central Processing Unit (CPU)


 Once the information is entered into the computer by the input device, the processor
processes it.
 The CPU is called the brain of the computer because it is the control centre of the
computer.
 It first fetches instructions from memory and then interprets them so as to know what is to
be done.
 If required, data is fetched from memory or input device.

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

Activity – Group discussion


Discuss the importance of computer memory and other storage devices in the present era.

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

Minimum structure of Go program


Hello World Example
A Go program basically consists of the following parts −
1. Package Declaration
2. Import Packages
3. Functions
4. Variables
5. Statements and Expressions
6. Comments
First program

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.

A variable is a name that represents a data stored in the computer's memory.

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.

Short hand variable declaration


package main

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

fmt.Printf("\nResult of p * q = %d", result3)

// Division

result4:= p / q

fmt.Printf("\nResult of p / q = %d", result4)

// Modulus

result5:= p % q

fmt.Printf("\nResult of p % q = %d", result5)

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.

// Go program to illustrate the


// use of relational operators
package main
import "fmt"
func main() {
p:= 34
q:= 20
// ‘=='(Equal To)
result1:= p == q

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)

// ‘>='(Greater Than Equal To)


result5:= p >= q
fmt.Println(result5)

// ‘<='(Less Than Equal To)


result6:= p <= q
fmt.Println(result6)

}
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.

Logical NOT:The ‘!’ operator returns true the condition in consideration is


not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
Logical operators

Logical and- &&

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

!(p<q)= !(False)= True

// Go program to illustrate the


// use of logical operators
package main
import "fmt"
func main() {
var p int = 23
var q int = 60

if(p!=q && p<=q){


fmt.Println("True")
}

if(p!=q || p<=q){
fmt.Println("True")
}

30 | P a g e
if(!(p==q)){
fmt.Println("True")
}
}
Output:
True
True
True

Input statement in Golang


var storageVariable variableType fmt.Scan(&storageVariable) //assuming fmt is imported

Sum of two numbers


package main
import "fmt"
func main() {
var a, b, c int
fmt.Println("enter first number")
fmt.Scan(&a) // input statement
fmt.Println("enter second number")
fmt.Scan(&b)
c=a+b
fmt.Println("the sum is", c)
}
Programs for practice
1. Write a Go program to find the volume of a sphere.
2. Write a Go program to convert Celsius into Fahrenheit and vice versa.

31 | P a g e

You might also like