C Programming_Unit 1 Final Ppt
C Programming_Unit 1 Final Ppt
1956
Approved by AICTE, COA and BCI, New Delhi
B22CI0104
Programming with C
Discuss the use of structures, unions, and file operations for solving
C_Pgm_Unit_I 6
Problem
Algorithm
Program
Input Computer
Output
C_Pgm_Unit_I 7
Purpose of writing an algorithm
• It is written using English language so that it is easily understandable even by non-
programmers.
• Basically an algorithm is a finite steps that must be followed to solve any problem.
C_Pgm_Unit_I 8
Rules for writing an algorithm
It should terminate after a finite time.
Every step in the algorithm must be effective i.e. every step should do some work.
C_Pgm_Unit_I 9
Examples an algorithm
1. Addition of 2 numbers
2. Simple calculator
3.Exchange of 2 numbers by using temporary variable
4.Exchange of 2 numbers without using temporary variable
5.Simple interest and compound interest
6. Find the largest number among three numbers
7.Area of circle
8. Area of Rectangle
9. Find Roots of a Quadratic Equation : ax2 + bx + c = 0
C_Pgm_Unit_I 10
Add two numbers entered by the user
Step 1: Start
Step 3: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Stop
C_Pgm_Unit_I 11
Algorithm to calculate simple interest
Step 1: Start
si←(p * t * r) / 100
Step 4: Display si
Step 5: Stop
C_Pgm_Unit_I 12
Algorithm to calculate area of triangle
Step 1: Start
area←(0.5 * b * h )
Step 5: Stop
C_Pgm_Unit_I 13
Algorithm to calculate area of circle
Step 1: Start
area←(3.1412 * r * r)
Step 5: Stop
C_Pgm_Unit_I 14
Find the largest number among three 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 && a > c )
Display a is the largest number.
else if ( b > a && b>c)
Display b is the largest number.
else
Display c is the largest number.
Step 5: Stop
C_Pgm_Unit_I 15
Flow chart
They are widely used in multiple fields to document, study, plan, improve and
communicate often complex processes in clear, easy-to-understand diagrams.
C_Pgm_Unit_I 16
Notations used to write a flow chart
C_Pgm_Unit_I 17
Notations used to write a flow chart
C_Pgm_Unit_I 18
Notations used to write a flow chart
C_Pgm_Unit_I 19
Add two numbers
C_Pgm_Unit_I 20
Calculate profit and loss
C_Pgm_Unit_I 21
Largest among three different numbers
C_Pgm_Unit_I 22
Advantage of writing algorithm
It is easy to understand.
In Algorithm the problem is broken down into smaller pieces or steps hence, it is
C_Pgm_Unit_I 23
Advantages of using flowcharts
Communication: Flowcharts are better way of communicating the logic of a system
Effective analysis: With the help of flowchart, problem can be analysed in more
documentation, which is needed for various purposes, making things more efficient.
Efficient Coding: The flowcharts act as a guide or blueprint during the systems
flowchart becomes complex and clumsy. This will become a pain for the user,
Alterations and Modifications: If alterations are required the flowchart may require re-
Time consuming
C_Pgm_Unit_I 25
Difference between Flow Chart & Algorithm
C_Pgm_Unit_I 26
• C language & its features,
• C tokens,
• data types in C ,
• variables,
• constants,
• structure of C program with example,
• input / output functions
C_Pgm_Unit_I 27
STRUCTURE OF C PROGRAM
1. DOCUMENTATION / COMMENTS
• This section consists of the description of the program, the name of the
program, and the creation date and time of the program.
• It is specified at the start of the program in the form of comments.
• It is represented as follows
Or
2.Multi-line comment:
• Operations like declaration and execution are performed inside the open and close
curly braces of the main program.
• The return type of the main() function can be int as well as void too. void() main
tells the compiler that the program will not return any value.
• The int main() tells the compiler that the program will return an integer value.
• Brackets []: Opening and closing brackets are used as array element reference.
These indicate single and multidimensional subscripts.
• Parentheses (): These special symbols are used to indicate function calls and
function parameters.
• Braces {}: These opening and ending curly braces mark the start and end of a
block of code containing more than one executable statement.
• Pre-processor (#): The preprocessor is a macro processor that is used
automatically by the compiler to transform your program before actual
compilation.
VARIABLES IN C
A variable in simple terms is a storage place that has some memory allocated to
it. Basically, a variable is used to store some form of data.
Different types of variables require different amounts of memory.
Variable Declaration:
A typical variable declaration is of the form:
o Data type variable_name;
for multiple variables:
o Data type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers,
and the underscore ‘_’ character.
o int a, int A, int AB, int a_B
However, the name should not start with a number.
o int 9a, int 89a
Difference b/w variable declaration and definition / Variable initialization :
}
TYPES OF VARIABLES IN C
2. Global Variable:
o A variable that is declared outside the function or block is called a global
variable.
o Declared before main function.
o It is available for all functions.
o That is before
}
TYPES OF VARIABLES IN C
3. Static Variable:
o A variable that retains its value between multiple function calls is known as a static
variable.
o It is declared with the static keyword.
o we can use static variable as a counter. to count the number of times a function is
called.
4. Automatic Variable:
o All variables in C that are declared inside the block, are automatic variables by default.
Derived / Non Primitive data types: arrays, functions, structures and pointers.
1. Void Data types
• %d int
• %f float
• %lf double
• %c char
• %s string
Input and Output Functions
Formatted Input Function: scanf()
Input and Output Functions
Formatted Output Function: printf()
• It is a library function defined in stdio.h header file which will take data stored at
given location and prints it on monitor.
• This function prints all types of values (numeric, character, string) as input.
• getchar(): It read a character from the keyboard and store this into a memory
location. We have to press ENTER key after typing the character.
These functions allow us to take input or These functions do not allow to take input
1 display output in the user’s desired format. or display output in user desired format.
printf(), scanf() are examples of these getch(), getche(), gets() and puts(), are
5 functions. some examples of these functions.
C Program to print the size of each data type
#include <stdio.h>
#include<conio.h>
int main()
{
C_Pgm_Unit_I 66
C Program for arithmetic operation
#include <stdio.h>
#include<conio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
getch();
}
C_Pgm_Unit_I 67
C Program for arithmetic operation
#include<stdio.h>
#include<conio.h>
int main()
{
int a = 12, b = 3;
int addition, subtraction, multiplication, division, modulus;
4. Statically Type:
C programming language is a statically typed language.
Meaning the type of variable is checked at the time of compilation but not at run time.
This means each time a programmer type a program they have to mention the type of
variables used.
7. Libraries with rich Functions: Robust libraries and functions in C help even a
beginner coder to code with ease.
10. Easy to Extend: Programs written in C language can be extended means when a
program is already written in it then some more features and operations can be added to
it.
Introduction to
GitHub
C_Pgm_Unit_I 74
THANK YOU
C_Pgm_Unit_I 75