computer
• C-Commonly
• O-Operated
• M-Machine
• P-Particularly
• U-Used for
• T-Technology
• E-Education and
• R-Research
ALGORITHM
It is the step by step instructions or procedure of solving a
problem is called algorithm
To write a logical step by step method to solve the problem is called
algorithm , in other words An algorithm is a procedure for solving problems. In
order to solve a mathematical or computer problem , this is the 1st step of the
procedure . An algorithm includes calculations , reasoning and data processing .
Algorithms can be presented by natural languages , pseudo code and flow chart
Characteristics
→Clear and Unambiguous
→Well- Defined Inputs And Outputs
→Finiteness
→Feasible
→Language Independent
Characteristics
• Each an every instruction should be clear
• Each instruction should be performed a finite number of times
• The algorithm should ultimately terminate
• When the algorithm terminate the decided result should be obtained
1. Write an algorithm for adding 2 numbers
Step 1: Start
Step 2: read a,b
Step 3: c=a+b
Step 4: print c
Step 5: Stop
2. Write an algorithm to find the biggest of 2
numbers.
Step 1: Start
Step 2: read a,b
Step 3: if a>b
print a is biggest
else
print b is biggest
Step 4: Stop
FLOW CHART
Pictorial/graphical representation of algorithm is
called flowchart
Symbols
Start/stop
Input/Output
Process/operations
Decision
making/condition
Data flow
Connection
1.Draw a flow chart for adding 2 numbers
start
Input a,b
C=a+b
Print c
stop
2. Draw a flow chart to find the biggest of 2 numbers.
start
Input a,b
If a>b
Print a is
Print b is biggest
biggest
stop
Eg:
1. Write an algorithm and flow chart to find biggest of 3
numbers.
2. To check a given number is even or odd
PROGRAM
The collection of detailed expression that you supply when you
want your computer to perform a specific task is known as a program.
Set of instruction is called program.
→Father of C :- Dennis Ritchie
→Developed at AT & T’S Bell laboratories of USA in 1972
Features of C programming
1. Procedural Language
2. Fast and Efficient
3. Modularity
4. Statically Type
5. General-Purpose Language
6. Rich set of built-in Operators
7. Libraries with Rich Functions
8. Middle-Level Language
9. Portability
10.Easy to Extend
The Character Set
1. Letters
2. Digits
3. Special Characters
4. White Space
Eg:- ‘A’ ’a’ ’4’ ’%’ ’’ ’-’
Tokens in C
Tokens in C
Basic build-in block of C programming is called
Tokens.
• Keywords
• Identifiers
• literals/Constants
• Data types
• Operators
• Special symbols
Keywords
keywords are reserved words that have a specific meaning .
Eg:-
auto ,double, int, struct
break, else, long, switch,
Case, enum, register, typedef,
Char, extern, return, union,
const , float, short…etc
Identifiers
Identifiers in C are used for naming variables, functions, arrays,
structures, etc. Identifiers in C are the user-defined words
Rules for constructing identifiers in C are given
below:
1. First character must be an alphabets or underscore.
2. Must consist of only alphabets, digits or underscore.
3. Only 31 character are significant .
4. Keywords not permitted.
5. Wide space not allowed.
Constants/Literals
Constants in c refers of fixed values that that do not change during the
execution.
Constant
Numarical Character
Integer floating Singular String
Decimal Octal HexaDecimal
Back slash character constants
‘\a’ alert
‘\b’ backspace
‘\f’ form feed
‘\n’ newline
‘\t’ horizontal tab
‘\r’ carriage return
‘\v’ vertical tab
‘\\’ backslash
‘\’ ’ single quote
‘\" ’ double quote
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.
For eg:
A=10
B=20
C=A+B A
10
C
B 30
20
Data types
Primitive /Basic/built-in/fundamental Derived user defined
Eg: Eg: Eg:
Int →2 byte, Array Structure
Float →4 byte, Function Union
Double →8 byte, pointer
Char→ 1 byte,
Void→---
Declaring a variable
Syntax:-
Data type variableName;
Eg:-
Int a;
Operator
Operator is a symbol that tells the compiler to perform specific
mathematical or logical functions.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Conditional Operator
• Assignment Operators
• Increment & decrement operator
• Bitwise Operators
• Special operator
Arithmetic Operators
+→Addition
-→Subtraction
*→Multiplication
/→Division
%→Mode
Relational Operators
Also called Comparison operators,
<,<=,>,>=,==,!=
Logical Operators
&&
!!
!
a>b && a>c
a>b !! a>c
Conditional Operator
• The conditional operator is also known as a ternary operator. The
conditional statements are the decision-making statements which
depends upon the output of the expression. It is represented by two
symbols, i.e., '?' and ':'.
• As conditional operator works on three operands, so it is also known
as the ternary operator.
Syntax of a conditional operator
Expression1? expression2: expression3;
a>b ? big=a : big=b;
Assignment Operators
Operator are used to assign a value to a variable.
=
A=10;
Short hand assignment operator
+=,-=,*=,/=
A+=10 A=A+10
A-=20 A=A-20
Increment & decrement operator
++
--
i++ i=i+1
i-- i=i-1
++A A++
--A A--
Unary Operator
++a, --a, a++,a—
Binary Operator
+,-,*,/
eg:-
a+b
Ternary Operator
conditional operator(?:)
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation.
& Bitwise AND
! Bitwise OR
^ Bitwise XOR
<< Bitwise left shift
>> Bitwise right shift
Special operator
1.Comma Operator(,)
2.Size of Operator
1.Comma operator
It can be used to like the related expressions together.
2.Size of operator
It returns the number of bit the operant occupies.
Eg:
Int a,b;
b=Size of(a);
Output
b=2
Operator Precedence
Special /Punctuation symbols
Eg: #,@.....etc
I/O Operations
For input For out put
Scanf() printf()
Getchar() putchar()
Getch() putch()
Syntax of scanf()
scanf(“Control string ”, & variable);
eg:-
sacnf(“%d”,&a);
Control strings
%d integer
%f float
%s string
%c character
%lf double
%ld long
Syntax of printf
printf(“text for print”);
Eg:
printf(“this is eg for printf”);
Header File In C
A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source
files. There are two types of header files: the files that the programmer
writes and the files that comes with your compiler.
Include Syntax
#include <file>
Eg:- #include <stdio.h>
1. Write a c programme to print your name,
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“ your name”);
getch();
}
Output
your name
2.Write a c programme to add two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
a=10;
b=20;
c=a+b;
printf(“ the sum is %d”,c);
getch();
}
Output
the sum is 30
3→Write a c programme to find addition, Subtraction , multiplication
and division two numbers(a=20,b=30).
4→Write a c programme to find area of a rectangle.(A=l*b)
5. Write a c programme to add two numbers (value read by key board).
#include<stdio.h>
#include<conio.h>
void main()
{ Output
int a,b,c; Enter the 1st number 5
clrscr(); Enter the 2nd number 10
the sum is 15
printf(“Enter the 1st number”);
scanf(“%d”,&a);
printf(“Enter the 2nd number”);
scanf(“%d”,&b);
c=a+b;
printf(“ the sum is %d”,c);
getch();
}
6→ Write a C programme to find addition, Subtraction ,
multiplication and division any two numbers
7→ Write a C programme to find area of a rectangle(A=l*b), circle,
triangle and cube.
? Write a program to find ASCII Value of a Character.
#include<stdio.h>
int main()
{
Char c;
printf(“Enter the character :”);
scanf(“%c”,&c);
prntf(“ASCII Value of %c = %d”, c,c);
return 0;
}
Type Conversion and Type
Casting
1.Type Conversion/Implicit
2. Casting/Explicit
Type Conversion
In type conversion, a data type is automatically
converted into another data type by a compiler at the
compiler time.
Convert low size data type to higher.
Int → float
Char → int
Type Casting:
In typing casting, a data type is converted into another
data type by the programmer using the casting operator during
the program design.
Convert higher size data type to low.
Syntax:-
destination_datatype = (target_datatype)variable;
Eg:- int a;
float x,y;
…….
……..
a=(int)x/y;
Decision Making and Branching
Decision Making statements
1. If statements
2. Switch statements
3. Conditional operator statements
4. Jump statements
If statements
1. Simple if
2. if…….else
3. if else ladder
4. Nested if
1. Simple if
if(condition)
{
Body of if;
}
Eg:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a,b;
Printf(“enter the 2 numbers”);
Scanf(“%d%d”,&a,&b);
If(a>b)
{
Printf(“largest number is %d”,a);
}
getch();
}
1. Write a c programme to divide two numbers using simple if
B!=0
2. if…….else
if(condition)
{
body of if;
}
else
{
body of else;
}
Eg:
#include<stdio.h>
else
#include<conio.h> {
Void main() printf(“largest number is %d”,b);
{
}
Int a,b; getch();
}
Printf(“enter the 2 numbers”);
Scanf(“%d%d”,&a,&b);
If(a>b)
{
Printf(“largest number is %d”,a);
}
Write a c programme to check a number is even or odd
3. if else ladder
if(condition 1)
{ …………………..
Statement 1; ………………….
} Else if(condition n)
{
Else if(condition 2)
Statement n;
{ }
Statement 2; Else
} {
Statement else;
Else if(condition 3)
}
{
Statement 3;
}
Eg:
#include <stdio.h>
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Write a c programme to find grade of a student
90---100 A
80---90 B
4.Nested if
if(test condition 1)
{
{
if(test condition 2)
Statement 3;
{
statement 1;
}
}
……….
else ……….
{ Statement x;
statement 2;
}
}
else
Eg:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a , b, c;
Clrscr();
Printf(“enter the 3 numbers”);
else
{
Scanf(“%d%d%d”,&a,&b,&c);
printf(“ c is bigg %d”,c);
If(a>b)
}
{
}
If(a>c)
{
Printf(“ a is bigg %d”,a);
}
else if(b>c)
{
Printf(“b is bigg %d”,b);
}
else
{
printf(“ c is bigg %d”,c);
}
getch();
}
Switch Statement
Switch( expression )
{
Case value N:
case value1:
block N;
block1; ………….
………… ………….
………… break;
break; default:
Case value2: default block;
……………
block2:
……………
………… }
…………
break; Statement X;
……………………..
……………………..
………………………
Eg: 1-Monday 4-Thursday 7-Sunday
2-Tuesday 5-Friday
3-Wednesday 6-Saturday
#include<stdio.h>
#include<conio.h>
Void main() Case 2:
{ printf(“Tuesday”);
Int n; break;
Clrscr(); Case 3:
Printf(“ enter the day number “); printf(“Wednesday”);
Scanf(“%d”,&n); break;
Switch(n)
{
Case 1:
printf(“Monday”);
break;
Case 4: Case 7:
printf(“Sunday”);
printf(“Thursday”);
break;
break; default:
Case 5: printf(“invalid day number”);
break;
printf(“Friday”); }
break; getch();
}
Case 6:
printf(“Saturday”);
break;
Write a c programme to make a simple calculator using switch
case.
Write a c programme to check given character is vowel or not.
Conditional operator statements
?:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a,b,c,big;
Clrscr();
Printf(“enter 3 numbers”);
Scanf(“%d%d%d”,&a,&b,&c);
big=a>b ? a>c : b>c;
printf(“bigg number is %d”,big);
getch();
}
Looping Statements
#include <stdio.h>
int main() Output:
Hello World
{
Hello World
printf( "Hello World\n"); Hello World
printf( "Hello World\n"); Hello World
Hello World
printf( "Hello World\n"); Hello World
printf( "Hello World\n"); Hello World
Hello World
printf( "Hello World\n"); Hello World
printf( "Hello World\n"); Hello World
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
return 0;
Looping Statements are:-
1.For loop
2. while loop
3. do while loop
1.For loop
Syntax
for( initialization ; Condition ; Updation)
{
body of for loop;
}
Eg:
int i ;
for( i=1 ; i<=10 ; i++)
{
printf(“%d”,i);
}
Write a c programme to display 20 to 30
Write a c programme to display leap years with in a range
For(…….)
{
If( i%4==0)
2. while loop
Syntax
Initialization
While(condition)
{
Body of the loop;
Updation;
}
i=1;
While(i<=10 )
{
printf(“%d”,i);
i++;
}
Write a c programme to print even numbers up to 20.
#include<stdio.h>
#include<conio.h>
void main()
{
int n=2;
clrscr();
while(n<=20)
{
printf(“%d”,n);
n=n+2;
}
getch();
}
3. do while loop
Syntax
initialization
do
{
body of the loop;
updation;
} while(condition);
i=1;
do
{
printf(“%d”,i);
i++;
} While(i<=10 );
Write a c programme to print even numbers up to 20.
#include<stdio.h>
#include<conio.h>
void main()
{
int n=2;
clrscr();
do
{
printf(“%d”,n);
n=n+2;
} while(n<=20);
getch();
}
goto statement
The goto statement can transfer the program control to
anywhere in the function. The target destination of a goto statement is
marked by a label, which is an identifier.
int i=1;
start:
printf(“%d”,i);
i++;
if ( i<= 50)
goto start;
Break statement
It takes the program control outside the immediate enclosing loop
(for, while, do...while) or switch statement.
for (i=1; i<=10; ++i)
{
if (i==6)
break;
printf(“%d”,i)
printf(“\t”);
}
This code gives the following output:
12345
continue statement
Used for skipping over a part of the code within the loop-body and forcing
the next iteration.
for (i=1; i<=10; ++i)
{
if (i==6)
continue;
printf(“%d”,i)
printf(“\t”);
This code gives the following output:
1 2 3 4 5 7 8 9 10