0% found this document useful (0 votes)
18 views133 pages

PPS Unit1

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

PPS Unit1

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

MALLA REDDY ENGINEERING COLLEGE FOR WOMEN

(Autonomous Institution-UGC, Govt. of India)


Permanently affiliated to JNTUH,Approved by AICTE,ISO 9000:2015 Certified Institution
Accrediated by NBA and NAAC with 'A' Grade
NIRF,Indian Ranking -2018,Accepted by MHRD , Govt. of India
AAA+ Rated by careers 360 Magazine, National Ranking -Top 100 Rank Band by outlook

PROGRAMMING FOR PROBLEM SOLVING


(B.Tech I Year - I Sem)

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 Business and Industry

• 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

• Central Processing Unit (CPU)


• Memory Unit
• Input Devices
• Output Devices
• Secondary Storage Devices

8
• Definition:

• Introduction To Computer Software


• Operating system
• Problem solving Techniques
• Computer Prog ramming languages
• Computer Threats

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,

Computer Hardware is the one we can


see and touch

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,

Software is of two kinds:-


1. System software or operating system
2. Application software or ready-made programs.

Software is the one we cannot see.

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.

Arithmetic and Logic 16


ALU

Place where the actual execution of

the instructions take place during


the processing operations.
All calculations are performed here.
It consists of circuits that perform
arithmetic as well as logical
operations.
17
Control Unit
It act as a central
nervous system for the
other components of
the computer.
It manages and
coordinates the entire
computer system.
It get information from
the main memory
It issues signal to control
18
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).
Interpreter :
2. Interpreter :
Interpreter is a program that converts the high level language into
the bit format i.e. machine language. The function of the interpreter
and compiler is the same but the interpreter translates one line at a
time and executes it. No object code is produced so every time when
the program has to be run it is to be interpreted first.
Creating, Compiling and Executing a Program
1
Number
Systems
Number systems:

• Number systems are the technique to represent

numbers in the computer system architecture, every

value that you are saving or getting into/from

computer memory has a defined number system.


• Computer architecture supports following number
systems.
• Binary number system
• Octal number system
• Decimal number system
• Hexadecimal (hex) number system 25
• 1) Binary Number System
• A Binary number system has only two digits that are 0 and 1. Every
number (value) represents with 0 and 1 in this number system. The
base of binary number system is 2, because it has only two digits.
• 2) Octal number system
• Octal number system has only eight (8) digits from 0 to 7. Every
number (value) represents with 0,1,2,3,4,5,6 and 7 in this number
system. The base of octal number system is 8, because it has only 8
digits.
• 3) Decimal number system
• Decimal number system has only ten (10) digits from 0 to 9. Every
number (value) represents with 0,1,2,3,4,5,6, 7,8 and 9 in this
number system. The base of decimal number system is 10, because it
has only 10 digits.
• 4) Hexadecimal number system
• A Hexadecimal number system has sixteen (16) alphanumeric values
from 0 to 9 and A to F. Every number (value) represents with
0,1,2,3,4,5,6, 7,8,9,A,B,C,D,E and F.
Number Systems
CONVERSION
OF
NUMBERS
TOPICS

• Conversion between Binary &


Decimal.

• Conversion between Decimal &


Hexadecimal.

• Conversion between Hexadecimal &


Binary.

• Conversion between Decimal & Octal.

• Conversion between Octal &


Hexadecimal.

• Conversion between Binary & Octal.


Conversion Among
Bases
The
possibilities:

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

Convert each hexadecimal digit to a 4-bit

equivalent binary representation


Example

10AF16 = ?2

1 0 A F

0001 0000 1010 1111

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

1 011 010 111

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.

• The weight is the position of the


bit, starting from 0 on the right.

• Add the results


7248 =>4 x 0 = 4
8
2 x 1 = 16
8
7 x 2 = 448
8
(468)10
Example
Decimal to Octal

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

 The weight is the position of the bit,


starting from 0 on the right

 Add the results


Example
ABC16 => x 160 = 12 x 1 = 12
C
B x 161 = 11 x 16 = 176
A x 162 = 10 x 256 = 2560
274810
Decimal Octal

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

0001 1111 0000 1100

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:

If student's grade is greater than or equal to 60


Print "passed"
else
Print "failed"
Software Development Life Cycle
Introduction to C Language
• C is a procedural programming language.
• It was initially developed by Dennis Ritchie in the year 1972.
• It was mainly developed as a system programming language to
write an operating system.
• The main features of C language include
• Low-level access to memory
• A simple set of keywords
• Clean style
• Easy to learn
• Structured language
• It produces efficient programs.
• These features make C language suitable for system programming
like an operating system or compiler development.
Structure of a C Program
C Tokens

TOKEN is the smallest


individual unit in a 'C'
program.
Keywords
Keywords are those words whose meaning is already defined by
Compiler. There are 32 Keywords in C.
Constants
A constant is a value that can't be changed in the program, for
example: 10, 20, 'a', 3.4, "c programming" etc.
Identifiers
Identifiers are the names given to variables, constants, functions and
user-define data. These identifier are defined against a set of rules.

Rules for an Identifier

• An Identifier can only have alphanumeric characters(a-z , A-Z , 0-


9) and underscore(_).
• The first character of an identifier can only contain alphabet(a-z ,
A-Z) or underscore (_).
• Identifiers are also case sensitive in C. For example name and
Name are two different identifiers in C.
• Keywords are not allowed to be used as Identifiers.
• No special characters, such as semicolon, period, whitespaces,
slash or comma are permitted to be used in or as Identifier.
Variables
A variable is a name of the memory location. It is used to store data.
Its value can be changed, and it can be reused many times.

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.

Commonly occurred syntax errors are:

If we miss the parenthesis (}) while writing the code.


Displaying the value of a variable without its declaration.
If we miss the semicolon (;) at the end of the statement.
Syntax and Logical Errors
The logical error is an error that leads to an undesired output. These
errors produce the incorrect output. The occurrence of these errors
mainly depends upon the logical thinking of the developer.
Example.
#include <stdio.h>
void main()
{
int sum=0; // variable initialization
int k=1;
for(int i=1;i<=10;i++); // logical error, as we put the semicolon
{
sum=sum+k;
k++;
}
printf("The value of sum is %d", sum);
Operators in C

•C supports rich set of operators.

• An operator is a symbol that tells the compiler to perform certain


mathematical or logical manipulations.

• Operators are used in programs to manipulate data and variables.

91
Operators in C

Types of ‘C’ operators


1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and Decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
92
Operators in C
Arithmetic Operators
An arithmetic operator performs mathematical operations such as
addition, subtraction and multiplication on numerical values
(constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
remainder after
%
93 division( modulo division)
Operators in C
Relational Operators
Relational operators are used to find the relation between two
variables. i.e. to compare the values of two variables in a C program.

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;

Example Left side must be a variable that


a + = 3; can receive a value
a=a + 3;
Both are same
98
Operators in C
Shorthand Assignment operators

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 &

• The output of bitwise AND is 1 if the corresponding bits of two


operands is 1. If either bit of an operand is 0, the result of
corresponding bit is evaluated to 0.
• Let us suppose the bitwise AND operation of two integers 10 and
5.
• 10 = 1010 (In Binary)
• 5 = 0101 (In Binary)

Bit Operation of 10 and 5


1010
& 0101
________
0000 = 0 (In decimal)
Bitwise OR operator |
• The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.
• 10 = 1010 (In Binary)
• 5 = 0101 (In Binary)

Bit Operation of 10 and 5


1010
| 0101
________
1111 = 15 (In decimal)
Bitwise XOR (exclusive OR)
operator ^

• The result of bitwise XOR operator is 1 if the corresponding


bits of two operands are opposite. It is denoted by ^.

• 10 = 1010 (In Binary)


• 2 = 0010 (In Binary)

Bit Operation of 10 and 2


1010
^ 0010
________
1 0 0 0 = 8(In decimal)
Bitwise complement operator ~

• Bitwise compliment operator is an unary operator


(works on only one operand). It changes 1 to 0 and
0 to 1. It is denoted by ~.
• 10 = 0000 1010 (In Binary)

• Bitwise complement Operation of 10


• ~ 00001010
________
• 11110101
Twist in bitwise complement operator

• For any integer n, bitwise complement of n will be -(n+1). To understand


this, you should have the knowledge of 2's complement.
• 2's Complement
• Two's complement is an operation on binary numbers. The 2's
complement of a number is equal to the complement of that number
plus 1. For example:

Decimal Binary 2's complement


• 0 00000000 -(11111111+1) = -00000000 = -0(decimal)
• 1 00000001 -(11111110+1) = -11111111 = -256(decimal)
• 12 00001100 -(11110011+1) = -11110100 = -244(decimal)
• 220 11011100 -(00100011+1) = -00100100 = -36(decimal)
Shift Operators in C programming
• There are two shift operators in C programming:
• Right shift operator
• Left shift operator.
• Right Shift Operator
• Right shift operator shifts all bits towards right by
certain number of specified bits. It is denoted by >>.
X=a>>1 a=5
5 0101 (In binary)
0000 0101
000 0010 1
X=2
• Left Shift Operator
• Left shift operator shifts all bits towards left by a
certain number of specified bits. The bit positions that
have been vacated by the left shift operator are filled
with 0. The symbol of the left shift operator is <<.

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

• It is similar to the if-else statement. The

if-else statement takes more than one line of the

statements, but the conditional operator finishes

the same task in a single statement. The conditional

operator in C is also called the ternary

operator because it operates on three operands.


• The operands may be an expression, constants
or variables. It starts with a condition, hence it
is called a conditional operator.

Syntax:-

expression1 ? expression2 : expression3;


#include<stdio.h>
int main()
{
float num1, num2, max;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
max = (num1 > num2) ? num1 : num2;
printf("Maximum of %f and %f = %f", num1, num2, max);
return 0;
}
• Output:-
Enter two numbers: 12.5 10.5

Maximum of 12.50 and 10.50 = 12.50


Operator Precedence and Associativity
Operator precedence: It dictates the order of evaluation of operators
in an expression.

Associativity: It defines the order in which operators of the same


precedence are evaluated in an expression. Associativity can be either
from left to right or right to left.
Example:

int x = 5 - 17* 6;

In C, the precedence of * is higher than - and =.


Hence, 17 * 6 is evaluated first. Then the expression involving - is
evaluated as the precedence of - is higher than that of =.

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

Implicit Type Conversion


This type of conversion is usually performed by the compiler when
necessary without any commands by the user. Thus it is also called
"Automatic 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

Standard File File Pointer Device

Standard input stdin Keyboard

Standard output stdout Screen

Standard error stderr Your screen


The getchar() and putchar() Functions

#include <stdio.h>
int main( ) {

int c;
EXPECTED OUTPUT:

printf( "Enter a value :"); Enter a value : this is test


c = getchar( ); You entered: t

printf( "\nYou entered: ");


putchar( c );

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

printf( "Enter a value :");


gets( str );

printf( "\nYou entered: ");


puts( str );

return 0;
}
The scanf() and printf() Functions
#include <stdio.h>
int main( ) {

char str[100];
int i;

printf( "Enter a value :");


Enter a value :
scanf("%s %d", str, &i); seven 7
You entered:
printf( "\nYou entered: %s %d ", str, i); seven 7

return 0;
}

You might also like