PPS Unit1
PPS Unit1
1
“Unit-I”
2
Introduction to Computer System
A Computer is an electronic device that receives input, stores or
processes the input as per user instructions and provides output in
desired format.
Input-Process-Output Model
Computer input is called data and the output obtained after processing
it, based on user’s instructions is called information.
History
In 1833 AD, Charles Babbage, an
English Professor of Mathematics
proposed a calculating machine which
he called the Analytical Machine. If
developed, it would have punched card
input, a memory unit, a unit which
would perform the arithmetic, control
program, give automatic printout.
4
The Role of Computers
• In Publication Field
• In Education Field
• In Government Organizations
• In Medical Field
• In Science Field
• In Entertainment Field
5
Why is a Computer so Powerful?
Speed
Reliability
Accuracy
Storage
Communications
6
Basic components of a
computer are:
7
Basic hardware of a PC system
8
• Definition:
9
Hardware
The different physical parts of a computer consisting of a
input devices, CPU, the output devices & the secondary
storage together are called the Hardware.
In other words,
10
Hardware
The three essential parts of a computer are:-
CENTRAL PROCESSING
INPUT UNIT OUTPUT UNIT
UNIT
•KEYBOARD •DISPLAY
•MOUSE ARITHMETIC AND
MONITOR
•SCANNER LOGICAL UNIT •TV, VIDEO
•LIGHTPEN •PRINTER
CONTROL UNIT
•JOYSTIC •MAG. TAPE
•MAGNETIC TAPE •MAG. DISK
•MAGNETIC DISK RAM •PAPER TAPE
MEMORY •VOICE
ROM RESPONSE
11
Software
The instructions we feed into the computer to order it to
perform certain desired work is called Software.
In other words,
12
Components of a Computer System
Input and Output Devices of a Computer System
13
Components of a Computer System
Components of a Computer System
The Central Processing Unit (CPU) is responsible for executing
instructions such as arithmetic calculations, comparisons among data,
and movement of data inside the system.
Processor or CPU
The CU and ALU are jointly
known as CPU.
Brain of the computer.
Perform data processing
operations.
Control the working of the
entire system.
Decimal Octal
Binary Hexadecimal
Binary to Decimal
Decimal Octal
Binary Hexadecimal
Binary to
Decimal
• Technique
–Multiply each bit by 2n, where n is the
“weight” of the bit.
–The weight is the position of the bit,
starting from 0 on the right.
–Add the results.
EXAMPLE
Bit “0”
(101011)2 => 1 x 20 = 1
1 x 21 2
=
0 x 22 0
= 32
=
(43)10
0
1 x 23
Decimal to
Binary
Decimal Octal
Binary Hexadecimal
Example
Exampl
e
Binary to Hexadecimal
Decimal Octal
Binary Hexadecimal
Technique
› Group bits in fours, starting
on right
› Convert to hexadecimal
digits
10101110112 = ?16
10 1011 1011
2 B B
10101110112 = 2BB16
Hexadecimal to Binary
Decimal Octal
Binary Hexadecimal
Hexadecimal to Binary
Technique
10AF16 = ?2
1 0 A F
10AF16 =
00010000101011112
Binary to Octal
Decimal Octal
Binary Hexadecimal
Binary to Octal
• Technique
– Group bits in threes, starting on right
– Convert to octal digits
Example
10110101112 = ?8
3
10110101112 = 13278
2
Decimal Octal
Binary Hexadecimal
Octal to Decimal
Decimal Octal
Binary Hexadecimal
Octal to Decimal
Technique
• Multiply each bit by 8n, where n is
the “weight” of the bit.
Decimal Octal
Binary Hexadecimal
Decimal to
Octal
Technique
Divide by 8
Keep track of the remainder
Decimal to Octal
• Example
• convert 27 into octal
• 26 divide by 8 , remainder is 2
• so its equivalent is 32
Example
Decimal Octal
Binary Hexadecimal
Technique
Multiply each bit by 16n, where
n is the “weight” of the bit
Binary Hexadecimal
Techniqu
e
Divide by 16
Keeptrack of the
remainder
Decimal Octal
Binary Hexadecimal
Technique
Use binary as an
intermediary
1076 8 ?16
1 0 7 6
=
001 000 111 110
2 3 E
10768 = 23E16
Decimal Octal
Binary Hexadecimal
Technique
Use binary as an
intermediary
1F0C16 = ?8
1 F 0 C
1 7 4 1 4
1F0C16 = 174148
Algorithm
An algorithm is a mathematical process to solve a problem using a
finite number of steps.
Properties of Algorithms
• Input -An algorithm has input values from a specified set.
• Output -From each set of input values an algorithm produces
output values from a specified set.
• Definiteness -The steps of an algorithm must be defined precisely.
• Correctness -An algorithm should produce the correct output
values for each set of input values.
• Finiteness -An algorithm should produce the desired output after a
finite (but perhaps large) number of steps for any input in the set.
• Effectiveness -It must be possible to perform each step of an
algorithm exactly and in a finite amount of time.
• Generality -The procedure should be applicable for all problems
of the desired form, not just for a particular set of input values.
Algorithm : Addition of two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Algorithm : largest of two numbers
Algorithm:
Step 1: Start
Step 2: Read a, b . /* a, b two numbers */
Step 3: If a>b then /*Checking */
Display “a is the largest number”.
Otherwise
Display “b is the largest number”.
Step 4: Stop.
Algorithm : Largest among 3 numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Algorithm : Factorial of a number
Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i←1
Step 4: Read value of n
Step 5: if i<=n then repeat 5.1 and 5.2
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop
Flowchart
A flowchart can also be defined as a diagrammatic/graphical
representation of an algorithm.
Flowchart : Addition of two numbers
Flowchart : Largest of two numbers
Flowchart : Largest of Three numbers
Pseudocode
Pseudocode is an artificial and informal language that helps
programmers develop algorithms.
Example:
Syntax:
type variable_list;
Example:
int a;
float b;
char c;
Here, a, b, c are variables.
The int, float, char are the data types.
Datatypes
A data type specifies the type of data that a variable can store such
as integer, floating, character, etc.
Datatypes
Syntax and Logical Errors
Syntax errors are also known as the compilation errors as they
occurred at the compilation time.
These errors are mainly occurred due to the mistakes while typing or
do not follow the syntax of the specified programming language.
Example:
If we want to declare the variable of type integer,
int a; // this is the correct form
Int a; // this is an incorrect form.
91
Operators in C
94
Operators in C
Example
#include<stdio.h>
void main()
{
int i=7,j=1,k=0;
printf(“%d \n”,i==j); 0
printf(“%d \n”,i<j); 0
printf(“%d \n”,i>j); 1
printf(“%d \n”,i<=j); 0
printf(“%d \n”,i>=j); 1
printf(“%d \n”,i!=j); 1
}
95
Operators in C
Logical Operators
• Allow a program to make a decision based on multiple conditions.
• Each operand is considered a condition that can be evaluated to a true
or false value.
96
Operators in C
97
Operators in C
Assignment Operators
• Assignment operators are used to assign the result of an
expression to a variable.
• C has a set of ‘shorthand’ assignment
operator :
variable name =expression;
Simple Assignment
Operator Shorthand Operator
a = a+1 a + =1
a = a-1 a - =1
a = a* (m+n) a * = m+n
a = a / (m+n) a / = m+n
a = a %b a %=b
99
Operators in C
Increment and Decrement Operators
Increment operators are used to increase the value of the variable
by one and decrement operators are used to decrease the value of
the variable by one.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
100
Operators in C
101
Operators in C
Bitwise Operators
•In the C programming language, operations can be performed on a
bit level using bitwise operators.
•Following are the bitwise Operators
102
Operators in C
Truth Table
103
• Operators Meaning of operators
•& Bitwise AND
•| Bitwise OR
•^ Bitwise XOR
•~ Bitwise complement
• << Shift left
• >> Shift right
Bitwise AND operator &
X=a<<1 a=5
5 0101 (In binary)
0000 0 1 0 1
0000 1 0 1 0
X=10
Sizeof() operator
• The sizeof operator is the most common operator in
C. It is a compile-time unary operator and used to
compute the size of its operand. It returns the size of
a variable. It can be applied to any data type
The output can be different on different machines like
a 32-bit system can show different output while a 64-
bit system can show different of same data types.
• #include <stdio.h>
• int main()
• {
• int a = 16;
• printf("Size of variable a : %d\n",sizeof(a));
• printf("Size of int data type : %d\n",sizeof(int));
• printf("Size of char data type : %d\n",sizeof(char));
• printf("Size of float data type : %d\n",sizeof(float));
• printf("Size of double data type : %d\n",sizeof(double));
• return 0;
• }
• Output
• Size of variable a : 4
• Size of int data type : 4
• Size of char data type : 1
• Size of float data type : 4
• Size of double data type : 8
Conditional Operator
Syntax:-
int x = 5 - 17* 6;
117
Operator Precedence and Associativity
118
Expression Evaluation
119
Type Conversion
The type conversion process in C is basically converting one type of
data type to other to perform some operation.
The conversion is done only between those datatypes wherein the
conversion is possible ex – char to int and vice versa.
There are two types of conversions:
1. Implicit Type Conversion
2. Explicit Type Conversion
120
Type Conversion
121
Type Conversion
122
Type Conversion
Example 1
int a = 20;
double b = 20.5;
a + b; -----40.5
Example 2
char ch='a';
int a =13;
a + c; ------110
123
Type Conversion
124
Type Conversion-Example
#include<stdio.h>
int main()
{
double a = 4.5;
double b = 4.6;
double c = 4.9;
int result = (int)a + (int)b + (int)c;
printf("result = %d\n", result);
return 0;
}
125
typedef
typedef is a keyword used in C language to assign alternative names to
existing datatypes.
Its mostly used with user defined datatypes.
Following is the general syntax for using typedef,
typedef <existing_name> <alias_name>
Example:
#include <stdio.h>
int main()
{
typedef unsigned int unit;
unit i,j;
i=10,j=20;
printf("Value of i is :%d",i);
printf("\nValue of j is :%d",j);
return 0;
}
126
Input / Output Functions
printf() function:
The printf() function is used for output. It prints the given statement
to the console.
Syntax:
printf("format string",argument_list);
The format string can be %d (integer), %c (character), %s (string),
%f (float) etc.
Example: printf(“sum=%d\n”, sum);
scanf() function:
The scanf() function is used for input. It reads the input data from the
console.
scanf("format string",argument_list);
Example: scanf(“%d%d”, &a, &b);
127
Input / Output Functions Example
Program to print cube of given number
#include<stdio.h>
int main()
{
int n;
printf("enter a number:");
scanf("%d",&n);
printf("cube of number is:%d ", n*n*n);
return 0;
}
128
Introduction to stdin, stdout and stderr
STDIN and STDOUT are the file pointers that are automatically
defined when a program executes and provide access to the keyboard
and screen.
Stdin: By default stdin accesses the keyboard. Functions that read
stdin include...
• gets
• getchar
• scanf
Stdout: stdout sends data to the screen. Functions that write to stdout
include....
• printf
• puts
• Putchar
stderr is an output stream typically used by programs to output error
messages or diagnostics to the console.
129
C - Input and Output
#include <stdio.h>
int main( ) {
int c;
EXPECTED OUTPUT:
return 0;
}
The gets() and puts() Functions
#include <stdio.h>
int main( ) {
Enter a value : this is test
char str[100]; You entered: this is test
return 0;
}
The scanf() and printf() Functions
#include <stdio.h>
int main( ) {
char str[100];
int i;
return 0;
}