Cse101 Till Mid Term
Cse101 Till Mid Term
Cse101 Till Mid Term
Computer Programming
Lecture #0
• Text Book
▪ “PROGRAMMING IN C”
by
ASHOK N. KAMTHANE
PEARSON, 2nd Edition
• “PROGRAMMING IN ANSI C”
By E. BALAGURUSAMY
McGraw Hill Education
▪ Attendance 5
▪ Academic Task 25
▪ MTE 20
▪ ETE 50
• Total 100
Pick Brush
Brush
Rinse
• A step cannot be skipped Brush
Stop
Go!!!
Where
Class To Movie
Go?
Stop Stop
© LPU :: CSE101 C Programming
So what does this mean?
• There has to be a
LANGUAGE to communicate
with the logic machine
Otherwise….
© LPU :: CSE101 C Programming
Diving deeper…
•Microsoft Excel
•Oracle Database
•MySql
•Linux
•Unix
•Android
•Google
© LPU :: CSE101 C Programming
MNCs
Top rated Companies which has a dearth of C programmers
After MTE
✓ Arrays and Strings
✓ Pointers
✓ Dynamic Memory Allocation
✓ Derived Data Types- Structures
and Union
© LPU :: CSE101 C Programming
Tools
Offline Compiler Code Blocks:
• http://www.codeblocks.org/downloads
Primary Memory
Loader
Loader puts program in memory.
Disk ..
..
..
Primary Memory
Phase 6: CPU executes the Phase 5: A loader loads Phase 4: A linker links
program one instruction at the executable image the object code with the
a time. The load process in into memory (RAM). code in library or other
Windows OS is just input places to produce an
the name of the executable image.
executable file.
• Step1. Start.
• Step2. Take the two numbers.
• Step3. Add them.
• Step4. Print the result.
• Step5. Stop.
START
TAKE TWO
NUMBERS A and B
FIND SUM=A+B
PRINT SUM
STOP
©LPU CSE101 C Programming
Flow Chart: Greater than two Numbers
a) I/O
b) Flow
c) Terminal
d) Decision
Output:
Car is under process
A B C D …… X Y Z Characters
Uppercase Alphabets A, B, C, … Y, Z
Lowercase Alphabets a, b, c, … y, z
Digits 0, 1, 2, 3, … 9
~‘!@#%^&*()_-+=|\{} []
Special Symbols
:;"'<>,.?/
White spaces Single space, tab, new line.
©LPU CSE101 C Programming
Token
• Every single element in a C Program is Token
do if static while
dist_travelled = 6.28 * 15
dist_travelled = 94.2 Ans. 94.2 Real (float in C)
Data
Type
©LPU CSE101 C Programming
List of Data Types
Type Size Minimal range
(bytes)
char 1 -128 to 127
unsigned char 1 0 to 255
int 4 -32768 to 32767
unsigned int 4 0 to 65535
short int 2 -32768 to 32767
unsigned short int 2 0 to 65535
long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float 4 3.4e-38 to 3.4e+38 with 6 digits of
precision
double 8 1.7e-308 to 1.7e+308 with 15 digits
of precision
long double 10 3.4e-4932 to 1.1e+4932 with 20
digits of precision
15
%d
Program 90
%d
3.14
%d
15
%d
Program 94.2
%f
3.14
%f
• Floating Constant
– Constants that contains number with decimal
points
Example : 3.14;
309.89
Variables to be declared as
• Speed, s1, speed_of_car
• Distance, d1, dist
• Time, t1, time_of_travel
Arithmetic Operators
These are binary operators i.e. expression requires two operands
Operator Description Example (a=4 and b=2)
+ Addition of two operands a+b=6
- Subtraction of two operands a–b=2
* Multiplication of two operands a*b=8
/ Division of two operands a/b=2
% Modulus gives the remainder a%b=0
after division of two operands
++count increments count by 1 and then uses its value as the value of the
expression. This is known a prefix operator.
count++ uses count as the value of the expression and then increments
count by 1. This is known as postfix operator.
1. Wake up;
2. Get ready;
3. If you have enough time, then
eat breakfast;
4. Go to school.
©LPU CSE101 C Programming
Control Statements
• The C language programs until now follows a
sequential form of execution of statements.
• C language provides statements that can alter the
flow of a sequence of instructions. These statements
are called control statements.
• These statements help to jump from one part of the
program to another. The control transfer may be
conditional or unconditional.
Go!!!
Where
Class To Movie
Go?
Stop Stop
©LPU CSE101 C Programming
if statement
if (expression)
statement;
or
if (expression)
{
block of statements;
}
yes no
Clouds?
No rain
Raining
#include <stdio.h>
void main()
{
int x = 5;
if (x < 1)
printf("hello");
if (x == 5)
printf("hi");
else
printf("no");
}
a) hi b)hello c) no d) error
Grab
something to
eat along
MESSAG
E
DISPLAY
A.if
B.if-else
C.if-else-if
D.All of the above
if ( condition )
do this ;
if ( condition ) else if ( condition )
do this ; do this ;
else else {
do this ; do this ;
and this ;
©LPU CSE101 C Programming }
#include<stdio.h>
void main() Program to
{
float marks;
scanf(“%f”, &marks);
print grades
if (marks>90){
printf(“Grade A”);
of students
}
else if (marks>80) {
marks.
printf(“Grade B”);
}
else if(marks>70){
printf(“Grade C”);
}
else if (marks >60) {
printf(“Grade D”);
}
}
66.70
Grade D
or
78.00
Grade C C Programming
©LPU CSE101
Forms of if
Decision control Syntax Description
statements
if if (condition){ In these type of statements, if condition is
Statements;} true, then respective block of code is
executed.
if…else if (condition){ In these type of statements, group of
Statement1; statements are executed when condition is
Statement2;} true. If condition is false, then else part
else { statements are executed.
Statement3;
Statement4;}
Nested if if (condition1){ If condition 1 is false, then condition 2 is
Statement1;} checked and statements are executed if it
else is true. If condition 2 also gets failure, then
if(condition2){ else part is executed.
Statement2;}
else
©LPU CSE101 C ProgrammingStatement 3;
break statement
• break is a keyword.
• break allows the programmer to terminate
the loop.
• A break statement causes control to transfer
to the first statement after the loop or block.
• The break statement can be used in nested
loops. If we use break in the innermost loop
then the control of the program is terminated
only from the innermost loop.
Day= No Day=
Monday Sunday
Yes
• Condition – is used to
control the iteration –
either to continue or stop
iterating.
Repeated_Actions False
#include<stdio.h>
int main()
{
int i = 4;
while(i == 4--)
printf("Loop ");
return 0;
}
A. Loop Loop Loop Loop B. Loop Loop loop
C. Compilation Error D. Prints Nothing
n=n-1;
} True
} Print
n
False
10 9 8 7 6 5 4 3 2 1 n = n -1
for (initialization-expression;
loop-repetition-condition;
update-expression){
statement;
}
• The initialization-expression set the initial value of the
loop control variable.
• The loop-repetition-condition test the value of the
loop control variable.
• The update-expression update the loop control
variable.
©LPU CSE101 C Programming
for statement
True
Repeated_Actions
False
Updating
} Print
n
False
n = n -1
10 9 8 7 6 5 4 3 2 1
Do TEN push ups = for
count=1; count<=10;
count++
©LPU CSE101 C Programming
©LPU CSE101 C Programming
Nested Loops
• Nested loops consist of an outer loop with one
or more inner loops.
• Eg:
Outer loop
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){ Inner loop
…
}
}
• The above loop will run for 100*50 iterations.
Enter a number
4
The tables from 1 to 4
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12CSE101
©LPU 16 20 24 28 32 36 40
C Programming
#include<stdio.h>
#include<conio.h> Program to
void main() display a
{
int i,j; pattern.
printf(“Displaying right angled triangle for 5
rows”);
for(i=1 ; i<=5 ; i++) {
for(j=1 ; j<=i ; j++)
printf(“* ”);
printf(“\n”);
}
}
5-23
©LPU CSE101 C Programming
do…while statement
do
{
Repeated_Actions; True Repeated_Actions
} while (Condition);
Condition
False
n=n-1;
}while (n>0); n = n -1
is n>0?
10 9 8 7 6 5 4 3 2 1
False
10 9 8
10 8 6 4 2
n=10;
A:
n=10 printf(“%d “, n);
n = n -1;
A
Print if (n>0)
n
goto A;
n = n -1
Output:
is n>0? True A
10 9 8 7 6 5 4 3 2 1
False
enter a number: 18
18 is even
Example:
char c;
c = getchar();
©LPU CSE101 C Programming
#include<stdio.h>
void main()
{
char c;
printf(“enter a character”);
c=getchar();
printf(“c = %c ”,c);
}
Enter a character k
c = k
enter a character: r
r
getche();
Syntax
char str[length of string in number];
gets(str);
puts(str);