[go: up one dir, main page]

0% found this document useful (0 votes)
9 views64 pages

PPS Unit 1

The document provides an introduction to computer systems, programming concepts, and the C programming language. It covers topics such as algorithms, data types, operators, error handling, and input/output functions. Additionally, it explains the structure of a C program and the use of various programming constructs to solve problems.

Uploaded by

thallashruthi158
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)
9 views64 pages

PPS Unit 1

The document provides an introduction to computer systems, programming concepts, and the C programming language. It covers topics such as algorithms, data types, operators, error handling, and input/output functions. Additionally, it explains the structure of a C program and the use of various programming constructs to solve problems.

Uploaded by

thallashruthi158
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/ 64

Programming For Problem Solving

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:

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.

34
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
35
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
%
36 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.

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;

Example Left side must be a variable that


a + = 3; can receive a value
a=a + 3;
Both are same
41
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

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.

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

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

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

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.

The syntax of printf() function is given below:

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

You might also like