Introduction to Programming with Reference to C programming

ashwani406084 14 views 34 slides Aug 13, 2024
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

Introduction to Programming with Reference to C programming
Data Types
keywords
instructions


Slide Content

Fundamentals of Programming languages
(With Reference to C Programming)
Ashwani Kumar
Assistant Professor
PG Department of Computer Science
Baring Union Christian College, Batala(Punjab)

Contents
•Learning Outcome
•Programming
•Language
•Programming Language
•Types of Programming Languages
•Basic concepts of C
•Instructions & its types
•Challenges of learning Programming

Learning Outcomes
Student will able to
Identify the problem
Analyze the problem
Find the possible solutions(Choose one)
Develop the code
Verify and Implement

What is Programming Language?
Programming Language is a language in
which we can write programs to solve some
sort of problem. Here I have used the Word
“program” , program is nothing ,it is just a
group of instructions given to the computer
for solving specific problem.

 
Types of Programming
Languages
Mainly we have two types of Programming
Languages :
1 High level Programming Languages
2. Low level Programming Languages

 
Exercise

Comparison of High and Low
Level Language
S. No.High level Languages Low level Languages
1
English like languageLanguage of 0’s and 1’s
2
Close to programmer Close to machine
3
Faster program developmentFaster program execution
4
Better programming
efficiency
Better machine efficiency
5
eg, Fortran, basic, pascal,c+
+,java,c#.
Machine language and
Assembly language

After the above discussion it is clear that there are
two factors on which Programming Language depends :
•Program execution efficiency
•Program development efficiency
We have a language that has the features of High level
as well as low level Programming Language. It is C
Language, the language of my Reference
 

Comparison of C with English
ENGLISH C LANGUAGE
 
Alphabets Alphabets, Digits & Special Symbols
 
 
Words Constants, variable & Keywords
 
 
Sentences Instructions
 
 
Paragraph Programs

Alphabets: A to Z, a to z
Digits : 0 to 9
Special Symbols:~,! ,@, # , $ , %, ^ ,
&, * , ( , ) ,etc.
Constants: 3x+4y=12
Here 3, 4 and 12 are constants because we
cannot change the value.
x and y are the variable because we can
change the value of x and y.
 

S. No.Constants Range Memory
Occupied
Example
1. int (Integer)-32,768 to 32,7672 bytes 2,23,456
2. float(Real)-3.4e38 to +3.4e384 bytes 2.0,24.6
3. char
(Character)
-128 to 127 1 byte ‘A’,’a’,’d’,’1’
Types Of Constants
(Restricted to only three)

Variables
,
They are the name given to the location in the memory where
different values are stored
float rate_of_interest;
int amount;
Keywords
,
These are the words whose meaning is already known to the
Compiler.
There are 32 keywords used in C language
int, char, void , for, if, else, do, switch, main, etc.

Instruction
It is a statement which consists of variable, constants, special
symbols and keywords.
There are four types of instructions in C:
1. Type Declaration Instruction
2. Input/output Instruction
3. Arithmetic Instruction
4. Control Instruction

Type Declaration Instruction
It is used to declare the variables in the program.
Any variable in a C program must be declared before
using it. These types of instruction must be written at
the beginning of the program.
int amount;
char ch;

Input/output Instructions
Input: These instructions are used to give input
to the program
Output: These Instructions are used to receive
output from the program.

Arithmetic Instructions
The instructions in which arithmetic operations can be
performed are called arithmetic instructions.
eg. rate_of_interest=(p*r*t)/100;
•Arithmetic operation between integer and integer gives integer result.
•Arithmetic operation between integer and float gives float result.
• Arithmetic operation between float and float gives float result.
Note: Operator and Operand
For example
5/2
5.0/2
2/5
5.0/2.0
Answers: a) 2 b) 2.5 c) 0 d) 2.5

Arithmetic Instructions
a) int c=5/2
b) float c=5/2
c) float c=2/5
d) int c=5.0/2.0
e) float c=9.0/2
f) float c=9/2.0
g) int c=9/2.0;
Answers: a) 2 b) 2.0 c) 0.0 d) 2 e) 4.5 f) 4.5 g)4

Control Instructions
S. No Instruction Type Use
1. Sequence Control
Instruction
Insure the sequential execution
2. Decision Control
Instruction
Allow the Compiler to take the
decision, which instruction is to be
executed next
3. Control Instruction Help the compiler to execute the no.
of instruction repeatedly
4. Case Control InstructionAllow the Compiler to take the
decision, which instruction is to be
executed next

Rules for writing C program
•All the statements are written in small case because
C is case sensitive language.
•Every program must be started from main function
because C is a functional language and must have at
least one function i.e main().
•To improve the readability, blank space must be
inserted between two words.
•Every statement ends with a semicolon(;). Some
exceptions are there, I will explain where it
needed.

So let’s start our first C program for
explaining
Sequence control instructions
/* PROGRAM TO CALCULATE SIMPLE INTEREST*/
#include<conio.h> //HEADER FILE
#include<stdio.h>
main()
{
int p,t; //DECLARATION INSTRUCTIONS
float r,si; //DI
clrscr();
printf("Enter the values of p: "); //OUTPUT INSTRUCTION
scanf("%d",&p); //INPUT INSTRUCTION
printf("\nEnter the values of r & t: ");
scanf("%f%d",&r,&t);
si=(p*r*t)/100;
printf("\nSimple interest is: %f",si);
getch();
}
OUTPUT IS AS FOLLOW:
 
 
Enter the values of p: 100
 
Enter the values of r & t: 10 2
 
Simple interest is: 20.000000
 

DECISION CONTROL INSTRUCTIONS
These instructions help the compiler to take decision which
statements are to be executed next.
There are three types of decision control instructions:
 
1. if eg. if(condition is true)
execute this statement;
 
2. if else eg. if(condition is true)
execute this statement;
else
execute this statement;
 

Working of if statement
/*PROGRAM TO CALCULATE SIMPLE INTEREST USING IF*/
#include<conio.h>
#include<stdio.h>
main()
{
int p,t;
float r,si,tds;
clrscr();
p=1000; //DIRECT METHOD FOR INPUT
t=2;
r=10.0;
si=p*r*t/100;
if(si>100)
{
tds=si*10/100;
si=si-tds;
}
printf("Net Simple Interest Is: %f",si);
getch();
}
OUTPUT IS AS FOLLOW:
 
 
NET SIMPLE INTEREST IS: 180.000000

Working of if - else statement
/* PROGRAM TO PRINT THE RESULT OF A STUDENT*/
#include<conio.h>
#include<stdio.h>
main()
{
int roll_no;
float marks;
printf("\nENTER THE ROLL NO AND MARKS OF STUDENT: ");
scanf("%d%f",&roll_no,&marks);
if(marks>=40)
printf("\nStudent of roll no %d is pass",roll_no);
else
printf("\nStudent of roll no %d is fail",roll_no);
getch();
}

OUTPUT IS AS FOLLOW:
 
ENTER THE ROLL NO AND MARKS OF STUDENT: 1100 56
 
Student of roll no 1100 is pass
ENTER THE ROLL NO AND MARKS OF STUDENT: 1102 39
 
Student of roll no 1102 is fail

Word of caution
int i=4;
if(i=5)
printf(“HELLO”);
else
printf(“HI”);
int i=5;
if(i=5)
printf(“HELLO”);
else
printf(“HI”);
int i=4;
if(i= =5);
printf(“HELLO”);
printf(“\nHI”);
int i=4;
if(i= =5)
printf(“HELLO”);
printf(“HI”);
else
printf(“GOOD”);
HELL
O
ERRORHELLO
HI
HELLO

Let us consider the following problem to check our
understanding about conditional statements
“A company insures its driver in the following cases
a) If the driver is married
b) If the driver is unmarried , Male and above 30 years
of age
c) If the driver is unmarried ,Female and above 25
years of age”
Here we have to take decision at 3 levels depending
upon the following factors:
1. Marital status
2. Gender
3. Age

Loop control instructions
There are three types of loops:
1. For loop
2. While loop
3. Do while loop
Every loop control statement has three parts:
1. Initialization
2. Condition
3. Update

FLOW CHART OF SIMPLE INTEREST CALCULATION

The following example shows the
working of For Loop
/* PROGRAM TO CALCULATE SIMPLE INTEREST*/
#include<conio.h>
#include<stdio.h>
main()
{
int p,t,count;
float r,si;
clrscr();
for(count=1;count<=3;count++) //LOOP CONTROL INSTRUCTION OR FOR LOOP
{
printf("\nEnter the values of p: ");
scanf("%d",&p);
printf("\nEnter the values of r & t: ");
scanf("%f%d",&r,&t);
si=(p*r*t)/100;
printf("\nSimple interest is: %f",si);
}
getch();
}

ENTER THE VALUES OF P: 100
 
ENTER THE VALUES OF R & T: 2 2
 
SIMPLE INTEREST IS: 4.000000
ENTER THE VALUES OF P: 200
 
ENTER THE VALUES OF R & T: 2 2
 
SIMPLE INTEREST IS: 8.000000
ENTER THE VALUES OF P: 300
 
ENTER THE VALUES OF R & T: 2 5
 
SIMPLE INTEREST IS: 30.000000
Output for above program is as follow:

Challenges of learning
Programming
One of the most difficult disciplines to master
Tight Deadlines
Have to Learn the whole Life Cycle
Rapid Change in Technology
Rapid Change in Behavior of clients

Closure
Reinforcement
Questioning
Illustration
Skill of explaining
Blackboard writing