Professional Documents
Culture Documents
CPDS UNIT I Final
CPDS UNIT I Final
C Programming
UNIT I
Programming Language?
A programming language is the communication bridge between a programmer and computer,
which is designed to communicate instructions to a machine, particularly a computer.
We have three types of programming languages
1.Low level language(Machine level language)
2.Mid-level language(Assembly level language)
Example:
LD R1 7; LOAD 1st VALUE in r1
LD R2 10; LOAD 2nd VALUE in r2
ADD R1, R2; R1=R1+R2
3.High level language
Translators?
High level and Mid-level languages require translators for communicating
the instructions to the computer. We have three types of translators.
1. Assembler
2. Interpreter
3. Compiler
Source Code: Source code is the code that is input to a translator.
Executable code: Executable code is the code that is output from the
translator.
Assembler
Assembler: An Assembler converts an assembly program into machine code.
Interpreter:
An Interpreter is also a program that translates high-level source code into
Executable code.
Compiler:
A Compiler is a program that translates a high level language into machine code.
Algorithm:
Algorithm is nothing but step-by-step solution of a problem which is written in a normal language
is called “Algorithm”.
An algorithm must possess the following properties:
1. Input: An Algorithm must take zero or more inputs (or) may not take inputs.
Output: An Algorithm should provide at least one output.
2. Finiteness: If we trace out the instructions of an algorithm, then for all cases the algorithm will
Terminate after a finite number of steps (An Algorithm should give results in a finite number of
steps).
3. Definiteness: An Algorithm should not contain ambiguous statements (or) instructions (Each
instruction must be clear and unambiguous).
4. Effective: Each statement in an algorithm is easily converted into an instruction and it can be
executed in a finite amount of time.
Examples: #1
1.Algorithm to read the name and print the name
Algorithm: Step 1 : Start
Step 2 : Read input name
Step 3 : Print name
Step 4 : Stop
Algorithm:
Step1: start
Step2: Read number num.
Step3: Compute the modulus operation i.e.a=num%2.
Step4: If a=0 then
Display the given number is even
Step5: If a=1 then
Display the given number is odd
Step6: stop
Flowchart:
Graphical or symbolic representation of an algorithm is “Flow chart”
Advantages of Flowchart:
1. Conveys Better meaning.
2. Analyses the problem effectively:
3. Effective Coding:
4. Systematic Debugging
Symbols:
Ex 1: Read two numbers and produce the sum of those numbers.
Ex 2:Flowchart to find the average of three numbers
Ex 3: Flowchart to calculate area of square
Ex 4: Read two numbers & print the greater number.
Program Control Structures:
The structure of an algorithm(or)Flowchart(or)program decides the order of
execution of the steps.
The steps in an algorithm can be divided into three categories
Sequence
Selection
Iteration
Sequence:
The steps described in an algorithm are performed successively one by one
without skipping any step.
Ex: Algorithm:
Step 1 : Start
Step 2 : Read input name
Step 3 : Print name
Step 4 : Stop
Selection:
If the failure occurs in the sequence, there must be some alternative to handle. The selection statement is
If (Condition)
Statement1;
Else
Statement 2;
Example:Algorithm:
Step1:start
Step2:Read two numbers (A,B)
Step3:If A>B , then goto step4 otherwise goto step5
Step4:print ‘A’ as maximum otherwise
Step5:Print ‘B’ is maximum.
Step6:Stop.
Iteration:#1
§ In a program it is sometimes necessary to perform the same action for number of times.
§ If the statement is written repetitively, it will increase the program code.
§To avoid this problem, iteration mechanism is applied.
§The statements used are for and while.
1. for loop:
for (expression1; expression2; expression3)
{
Statement;
}
Iteration:#2
2. While loop:
-expression1;
While (expression2)
{
Statement;
expression3;
}
Example:
Software Development Method:
The Following are the steps required for developing the software.
1. Specify the Problem requirements
2. Analyze the Problem
3. Design the algorithm to solve the problem
4. Implement the algorithm
5. Test and verify the program
6. Maintain and update the program
Contents
1. What is C?
2. Features of C program.
3. Rules for Writing, Compiling and Executing the C program.
4. Structure of C program.
5. Library and IDE.
6. C Tokens.
7. Control characters (Escape sequences).
8. Receiving input values from keyboard.
9. Control Structures: Conditional and Iterative Statements
What is C?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972 by
Dennis Ritchie.
Any programming Language can be divided in to two categories.
Problem oriented (High level language)
Machine oriented (Low level language)
But C is considered as a Middle level Language.
C is modular, portable, reusable.
Pre-processor directives
Global declarations
main()
{
Local variable declaration
Statement sequences
Function invoking
}
C Input and Output(I/O)
As we all know the three essential functions of a computer are reading, processing and writing
data.
In C programming you can use scanf() and printf() predefined function to read and print data.
The above program scanf() is used to take input from the user, and respectively printf() is used
to display output result on the screen.
C Preprocessor Directives
The object like macros is an identifier that is replaced by value.it is widely used to represent
numeric constants.
Example: #define PI 3.1415
Here, PI is the macro name which will be replaced by the value 3.14. Let's see an example of
Object-like Macros :
Output:3.1415
Function like macros
Here, MIN is the macro name. Let's see an example of Function-like Macros :
printf() clrscr()
scanf() getch()
graphics.h math.h
sqrt()
Setcolor()
pow()
Various parts of the C program
/* Comments */ Comments are a way of explaining what makes a program..
#include<stdio.h> stdio is standard for input / output, this allows us to use some commands which includes
a file called stdio.h.or
This is a preprocessor command.
int/void main() int/void is a return value,
main() The main() is the main function where program execution begins. Every C program must
contain only one main function.
Braces Two curly brackets "{...}" are used to group all statements.or
Curly braces which shows how much the main() function has its scope.
String Constant Constant which stores string value “hello”, “sample”, ”good”
How to Declare Constant in C :
Declaration Explanation
read as “a is an integer which is
const int a = 1;
constant”
int const a = 1; read as “a is a constant integer”
Strings
Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus,
a null-terminated string contains the characters that comprise the string followed by a null.
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in C/C++ −
How to declare and initialize a string?
? (question mark)
] (right bracket)
~ (tilde) / (division)
/ division
Arithmetic expressions
◦ switch(a++ + b + 1)…
(Character value is allowed because it can use the ASCII value of the character as input to match the
cases.)
Example Programs
1.Write a C program to print day of week name using switch case.
2.Write a C program print total number of days in a month using switch case.
3.Write a C program to check whether an alphabet is vowel or consonant using
switch case.
4.Write a C program to find maximum between two numbers using switch case.
5.Write a C program to check whether a number is even or odd using switch case.
6.Write a C program to check whether a number is positive, negative or zero using
switch case.
7.Write a C program to create Simple Calculator using switch case.
ITERATIVE STATEMENTS
Loops
ØA Loop executes the sequence of statements many times until the stated condition becomes false. A loop consists of two
parts, a body of a loop and a control statement.
ØThe purpose of the loop is to repeat the same code a number of times.
Types of Loops in C
1. Entry controlled loop: In an entry controlled loop, a condition is checked before executing the body of a loop. It is
also called as a pre-checking loop.
2. Exit controlled loop: In an exit controlled loop, a condition is checked after executing the body of a loop. It is also
called as a post-checking loop.
ØIn while loop, if the condition is not true, then the body of a
loop will not be executed, not even once;
Example Programs on while loop
1.Program to print the values from 1 to 100 using while loop.
2.Program to print multiplication table.
3.Program to find a factorial of a number.
4.Program to find the sum of first 10 natural numbers.
5.Program to print all the even numbers from 1 to 50 using while loop.
6.Program to check prime or not using while loop.
7.Program to print all the numbers divisible by both 3 and 5 from 1 to 100 using while loop.
8.Program to check Armstrong number or not.
9.Program to check perfect number or not.
11.Program to print the alphabets from a to z.
12.Program to print capital alphabet and ASCII value.
13. Program to check palindrome or not
Do While loop in C
A do...while loop in C is similar to the while loop except that
the condition is always executed after the body of a loop. It is
also called an exit-controlled loop.
Syntax of do...while loop in C programming language is as
follows:
Syntax of Do-While Loop in C:
do
{
Statements
} while (expression);
Example Programs on do-while loop
1. Write a program to print the values in a given range
2. Write a C program to find sum of all odd numbers between 1 to n.
3. Write a C program to count number of digits in a number.
4. Write a C program to find first and last digit of a number and also sum of first and last digit.
5. Write a C program to calculate sum and products of digits of a number.
6. Write a C program to enter a number and print its reverse.
For loop
A for loop is a more efficient loop structure in 'C'
programming. The general structure of for loop syntax in C is
as follows:
Syntax of For Loop in C:
for (initial value; condition; incrementation or
decrementation)
{
statements;
}
Example Programs on for loop
1. Program to print numbers from 50 to 1.
2. Write a C program to find square of a number from 1 to 10 using for loop.
3. C program to find it square root for a number from 1 to 10.
4. Program to print the prime number series.
5. Program to print the Armstrong number series.
6. to print all Strong numbers between 1 to n. Write a C program
7. Write a C program to print Fibonacci series up to n terms.
8. Write a C program to print lucas series up to n terms.
The Lucas numbers are in the following integer sequence:
2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123 …………..
Example programs on for loop(patterns)
1.C program to print half pyramid star pattern.
2.C program to print inverted half pyramid star pattern.
3.C program to print half pyramid number pattern.
4.C program to print half pyramid alphabet pattern.
5.C program to print full pyramid of star pattern.
Log10 (to count number of digits)
#include<stdio.h>
#include<math.h>
int main()
{
int n=45677345,a;
a=log10(n)+1;
printf("%d",a);
return 0;
}
Difference between while(non-zero) and
while(0)
üwhile(0) is treated as while with false condition. So this kind of loop is useless. It will never
execute the inner statement as 0 is treated as false.
üThe while(1) or while(any non-zero value) is used for infinite loop. There is no condition for
while. As 1 or any non-zero value is present, then the condition is always true. So what are
present inside the loop that will be executed forever. To come out from this infinite loop, we have
to use conditional statement and break statement.
Jump Statements
Break
The break is a keyword in C which is used to bring the program
control out of the loop.
The break statement in C can be used in the following two
scenarios:
ØWith switch case
ØWith loop
Syntax:
//loop or switch case
break;
Example programs on break
1. C program to take input from the user until he/she enters zero.
2. Program to calculate the sum of numbers (10 numbers max) // If the user enters a negative
number, the loop terminates
3. program asks a user input a integer number x. If x is divisible by 5, this causes the exit from
the loop.
Continue
The continue statement in C language is used to bring the program control to the beginning of
the loop.
The continue statement skips some lines of code inside the loop and continues with the next
iteration.
It is mainly used for a condition so that we can skip some code for a particular condition.
Syntax:
//loop statements
continue;
//some lines of the code which is to be skipped
Example programs on continue
1. C program to print sum of odd numbers between 0 and 10
2. Program to calculate the sum of numbers (10 numbers max) . If the user enters a negative
number, it’s should not be added to the result
3. write a program that will print 1 to 25, excluding the multiples of three.
Enumeration data type(enum)
ØThe enum in C is also known as the enumerated type. It is a user-defined data type that consists of
integer values, and it provides meaningful names to these values.
ØThe enum is defined by using the enum keyword.
ØThe enum is used when we want our variable to have only a set of values.
The following is the way to define the enum in C:
enum name{integer_const1, integer_const2,.....integter_constN};
For example:
enum fruits{mango, apple, strawberry, papaya};
The default value of mango is 0, apple is 1, strawberry is 2, and papaya is 3.
If we want to change these default values, then we can do as given below:
enum fruits{ mango=2, apple=1, strawberry=5, papaya=7};
Example
#include <stdio.h>
enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
int main()
{
printf("The value of Wednesday is %d",Wednesday);
return 0;
}
Output:
The value of Wednesday is 4
Topics covered
Control Structures
ØConditional Statements
(Simple if, if-else, Nested -if-else, Switch).
ØIterative Statements
(for, While, Do-While)
ØJump Statements
(break, Continue).
End of unit 1