Programming in C - Franklin's Lectures 2
Programming in C - Franklin's Lectures 2
• 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
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.
Numarical Character
‘\b’ backspace
‘\n’ newline
‘\\’ backslash
C
B 30
20
Data types
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
<,<=,>,>=,==,!=
Logical Operators
&&
!!
!
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.
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()
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>
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).
Syntax:-
destination_datatype = (target_datatype)variable;
Eg:- int a;
float x,y;
…….
……..
a=(int)x/y;
Decision Making and Branching
2. Switch 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.
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”);