PPS Unit 1
PPS Unit 1
I B.Tech I Semester
BY
Mr. V.SUNDARA RATNAM
Assistant Professor
Computer Science and Engineering
Malla Reddy Engineering College For Women
(Autonomous Institution-UGC, Govt. of India)
Accredited by NBA & NAAC with ‘A’ Grade UGC, Govt. of
India
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.
Components of a Computer System
Input and Output Devices of a Computer System
4
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.
Components of a Computer System
Introduction to Computer System
An Operating System (OS) is system software that manages
computer hardware, software resources, and provides common
services for computer programs.
Compiler
A compiler is a computer program that translates computer code
written in one programming language (the source language) into
another language (the target language).
Creating, Compiling and Executing a Program
Number Systems
Number System Conversions
Number System Conversions
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 among 3 numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c. 10 5 1
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 largest 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: Repeat the steps until i = n
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 among three numbers
Flowchart : Factorial of a number
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.
34
Operators in C
37
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
}
38
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.
39
Operators in C
40
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
42
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 – – ;
43
Operators in C
44
Operators in C
Conditional Operators
• The conditional expression can be used as shorthand for some if-else
statements. It is a ternary operator.
• This operator consist of two symbols: the question mark (?) and the
colon (:).
45
Operators in C
Example
void main()
{
int a , b, c; a = 10;
b = (a == 1) ? 20: 30;
printf( "Value of b is %d\n", b ); // 30
c = (a == 10) ? 20 : 30;
printf( "Value of c is %d\n", c ); // 20
}
46
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
47
Operators in C
Truth Table
48
Operators in C
Shift Operator:
Left Shift Operator (<<) :The left shift operator will shift
the bits towards left for the given number of times.
int a=2<<1;
printf(“%d”,a);// will print 4
If you left shift like 2<<2, then it will give the result as 8.
Therefore left shifting 1 time, is equal to multiplying the
value by 2.
Right shift Operator ( >>)
The right shift operator will shift the bits towards right for
the given number of times
int b=4>>1
printf(“%d”,b);// will print 2
Right shifting 1 time, is equivalent to dividing the value
by 2.
49
Operators in C
Special Operators
C supports some special operators such as:
• comma operator “,”
int a=5,b=6;
• size of operator “sizeof()”
• Address operator “&”
• pointer operator “*”
• member selection operator “. and -> ”
50
Operator Precedence and Associativity
Operator precedence: It dictates the order of evaluation of operators
in an expression.
int x = 5 - 17* 6;
51
Operator Precedence and Associativity
52
Expression Evaluation
53
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
54
Type Conversion
55
Type Conversion
56
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
57
Type Conversion
58
Type Conversion-Example
#include<stdio.h>
int main()
{
double da = 4.5;
double db = 4.6;
double dc = 4.9;
int result = (int)da + (int)db + (int)dc;
printf("result = %d", result);
return 0;
}
59
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;
}
60
Input / Output Functions
printf() function:
The printf() function is used for output. It prints the given statement
to the console.
printf("format string",argument_list);
The format string can be %d (integer), %c (character), %s (string),
%f (float) etc.
scanf() function:
The scanf() function is used for input. It reads the input data from the
console.
scanf("format string",argument_list);
61
Input / Output Functions Example
Program to print cube of given number
#include<stdio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
62
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.
63
THANK YOU