C -PROGRAMMING
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 1
INDEX
List of programs
Sl Particulars Page
No no
1 Program to illustrate the use of different data types 4-5
and verify their memory size.
2 Compute simple interest for given p,t,r. 6-7
3 compute compound interest for given p,t,r. 8-9
4 Compute the area of a circle 10
5 Compute the area of square 11
6 Compute area of rectangle. 12
7 Compute area of triangle. 13
8 Swap contents of two variables without using 14
intermediate variables.
9 Compute the largest of three numbers using ternary 15
operators.
10 Compute the result of student using nested if. 16-18
11 Compute factorial of a single digit number. 19
12 Compute the sum of digits of a given 3 digit number 20-21
reducing it to single digit.
13 Sort an array of numbers in Ascending order. 22-23
14 Sort an array of numbers in Desecending order. 24-25
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 2
15 Compute the length of a string and reverse the string 26
using string function.
16 Compute the sum of two matrices 27-28
17 Compute cube of a number using function. 29-30
18 Store the details of an employee using a structure and 31-32
print the details.
19 Given the resistance and tolerance, generate the color 33-36
bands of the resistor using switch statement.
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 3
Program-1
Program to illustrate the use of different data types and verify their
memory size.
#include<stdio.h>
#include<conio.h>
void main()
int a;
float b;
char c;
long int d;
double e;
short int f;
clrscr();
printf("size of int data type=%d bytes\n",sizeof (a));
printf("size of float data type=%d bytes\n",sizeof (b));
printf("size of char data type=%d bytes\n",sizeof (c));
printf("size of long int data type=%d bytes\n",sizeof (d));
printf("size of double data type=%d bytes\n",sizeof (e));
printf("size of short int data type=%d bytes\n",sizeof (f));
getch();
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 4
Output:
size of int data type=2 bytes
size of float data type=4 bytes
size of char data type=1 bytes
size of long int data type=4 bytes
size of double data type=8 bytes
size of short int data type=2 bytes
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 5
Program-2
Compute simple interest for given p,t,r.`
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
float p,t,r,si;
clrscr();
printf("enter the principal amount\n");
scanf("%f",&p);
printf("enter the time period\n");
scanf("%f",&t);
printf("enter the rate of interest\n");
scanf("%f",&r);
si=p*t*r/100;
printf("simple interest=%f\n",si);
getch();
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 6
Output:
enter the principal amount
1200
enter the time period
enter the rate of interest
5.4
simple interest=129.600006
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 7
Program-3
compute compound interest for given p,t,r.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
float p,t,r,ci;
clrscr();
printf("enter the principal amount\n");
scanf("%f",&p);
printf("enter the time period\n");
scanf("%f",&t);
printf("enter the rate of interest\n");
scanf("%f",&r);
ci=p*(pow((1+r/100),t));
printf(“compound interest =%f”,ci);
getch();
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 8
Output:
enter the principal amount
1200
enter the time period
enter the rate of interest
5.4
compound interest =1333.099243
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 9
Program-4
Compute the area of a circle.
#include<stdio.h>
#include<conio.h>
void main()
float r,area;
printf("enter the radius value\n");
scanf("%f",&r);
area=3.14*r*r;
printf("area of circle=%f",area);
getch();
Output:
enter the radius value
area of circle = 50.240002
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 10
Program-5
Compute the area of square.
#include<stdio.h>
#include<conio.h>
void main()
int s,area;
clrscr();
printf("enter the length of side\n");
scanf("%d",&s);
area=s*s;
printf("area of square=%d",area);
getch();
Output:
enter the length of side
area of square=25
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 11
Program-6
Compute area of rectangle.
#include<stdio.h>
#include<conio.h>
void main()
float l,b,area;
clrscr();
printf("enter the value for length\n");
scanf("%f",&l);
printf(“enter the value for breadth\n”);
scanf(“%f”,&b);
area=l*b;
printf("area of rectangle=%f",area);
getch();
Output:
enter the value of length
enter the value of breadth
area of rectangle = 8
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 12
Program-7
Compute area of triangle.
#include<stdio.h>
#include<conio.h>
void main()
float b,h,area;
clrscr();
printf("enter the base and height of triangle\n");
scanf("%f,%f",&b,&h);
area=0.5*b*h;
printf("area of triangle=%f",area);
getch();
Output:
enter the base and height of triangle
3,8
area of triangle=12.000000
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 13
Program-8
Swap contents of two variables without using intermediate variables.
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
clrscr();
printf("enter the number for a and b\n");
scanf("%d,%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping : a=%d,b=%d",a,b);
getch();
Output:
enter the number for a and b
76,56
after swapping : a=56,b=76
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 14
Program-9
Compute the largest of three numbers using ternary operators.
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c,largest;
clrscr();
printf("enter the number for a,b and c\n");
scanf("%d,%d,%d",&a,&b,&c);
largest=(a>b)?((a>c)?a:c):((b>c)?b:c);
printf("largest of 3 number =%d\n",largest);
getch();
Output:
enter the number for a,b and c
45,65,32
largest of 3 number=65
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 15
Program-10
Compute the result of student using nested if.
#include<stdio.h>
#include<conio.h>
void main()
int s1,s2,s3,s4;
float percentage;
clrscr();
printf("enter the marks for s1,s2,s3,s4 maximum marks is 100\n");
scanf("%d,%d,%d,%d",&s1,&s2,&s3,&s4);
if(s1>=35 && s2>=35 && s3>=35 && s4>=35)
percentage=(s1+s2+s3+s4)/4;
printf("percentage=%f\n",percentage);
if(percentage>=75)
printf("result=pass, grade:distinction\n");
else if(percentage>=60 && percentage<75)
printf("result=pass, grade :first class\n");
else if(percentage>=50 && percentage<60)
printf("result=pass, grade:second class\n");
else if(percentage>=40 && percentage<50)
printf("result=pass\n");
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 16
else
printf("result=fail");
getch();
Output 1:
enter the marks for s1,s2,s3,s4 maximum marks is 100
85,75,67,92
percentage=79.00000
result=pass, grade:distinction
Output 2:
enter the marks for s1,s2,s3,s4 maximum marks is 100
65,70,85,75
percentage=73.75
result=pass, grade:first class
Output 3:
enter the marks for s1,s2,s3,s4 maximum marks is 100
48,56,62,67
percentage=58.25
result=pass, grade:second class
Output 4:
enter the marks for s1,s2,s3,s4 maximum marks is 100
40,55,36,62
percentage=48.25
result=pass
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 17
Output 5:
enter the marks for s1,s2,s3,s4 maximum marks is 100
20,34,40,27
percentage=30.25
result=fail
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 18
Program-11
Compute factorial of a single digit number.
#include<stdio.h>
#include<conio.h>
void main()
int n,fact=1,i;
clrscr();
printf("enter the value for the n\n");
scanf("%d",&n);
for(i=n;i>=1;i--)
fact=fact*i;
printf("factorial of given number=%d",fact);
getch();
Output:
enter the value for the n
factorial of given number=24
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 19
Program-12
Compute the sum of digits of a given 3 digit number reducing it to
single digit.
#include<stdio.h>
#include<conio.h>
void main()
long int num;
int sum=0,remainder;
clrscr();
printf("enter 3 digit number\n");
scanf("%ld",&num);
while(num/10!=0)
sum=0;
while(num!=0)
remainder=num%10;
sum+=remainder;
num=num/10;
num=sum;
printf("sum of 3 digit number=%d\n",sum);
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 20
getch();
Output:
enter 3 digit number
999
sum of 3 digit number=9
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 21
Program-13
Sort an array of numbers in Ascending order.
#include<stdio.h>
#include<conio.h>
void main()
int a[5],i,temp,j;
clrscr();
printf("enter 5 datas\n");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
for(j=0;j<5-i-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
printf("data sorted in ascending order is\n");
for(i=0;i<=4;i++)
printf("%d\t",a[i]);
getch();
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 22
}
Output:
enter 5 datas
98 65 76 32 58
data stored in ascending order is
32 58 65 76 98
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 23
Program-14
Sort an array of numbers in Desecending order.
#include<stdio.h>
#include<conio.h>
void main()
int a[5],i,j,temp;
clrscr();
printf("enter 5 datas\n");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
for(j=0;j<5-i-1;j++)
if(a[j]<a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
printf("data sorted in descending order is\n");
for(i=0;i<=4;i++)
printf("%d\t",a[i]);
getch();
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 24
}
Output:
enter 5 datas
43 65 90 56 9
data sorted in descending order is
90 65 56 43 9
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 25
Program-15
Compute the length of a string and reverse the string using string
function.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str[40];
int p;
clrscr();
printf("enter the string\n");
scanf("%s",str);
p=strlen(str);
printf("length of string=%d\n",p);
printf("after reversing the string=%s",strrev(str));
getch();
Output:
enter the string
electronics
length of string=11
after reversing the string=scinortcele
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 26
Program-16
Compute the sum of two matrices.
#include<stdio.h>
#include<conio.h>
void main()
int a[3][4],b[3][4],c[3][4],i,j;
clrscr();
printf("enter the data for matrix a\n");
for(i=0;i<3;i++)
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
printf("enter the data for matrix b\n");
for(i=0;i<3;i++)
for(j=0;j<4;j++)
scanf("%d",&b[i][j]);
//to compute matrix c
for(i=0;i<3;i++)
for(j=0;j<4;j++)
c[i][j]=a[i][j]+b[i][j];
printf("the resulted matrix is\n");
for(i=0;i<3;i++)
{{
for(j=0;j<4;j++)
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 27
printf("%d\t",c[i][j]);
printf("\n");
getch();
Output:
enter the data for matrix a
6789
3454
2853
enter the data for matrix b
5673
3791
7320
the resulted matrix is
11 13 15 12
6 11 14 5
9 11 7 3
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 28
Program-17
Compute cube of a number using function.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int cube(int); //function declaration
void main()
int n,p;
clrscr();
printf("enter the number for n\n");
scanf("%d",&n);
p=cube(n); //function call
printf("cube of a number=%d\n",p);
getch();
//function defination
int cube(int a)
int b;
b=a*a*a;
return(b);
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 29
Output:
enter the number for n
cube of a number=64
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 30
Program-18
Store the details of an employee using a structure and print the details.
#include<stdio.h>
#include<conio.h>
struct employee
char name[20];
int id;
int salary;
char gender[10];
};
void main()
struct employee emp;
clrscr();
printf("enter the name \n");
scanf("%s",emp.name);
printf("enter the id\n");
scanf("%d",&emp.id);
printf("enter the salary\n");
scanf("%d",&emp.salary);
printf("enter the gender\n");
scanf("%s",emp.gender);
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 31
printf("employee details as following \n");
printf("name:%s\n",emp.name);
printf("id:%d\n",emp.id);
printf("salary:%d\n",emp.salary);
printf("gender:%s",emp.gender);
getch();
Output:
enter the name
vishnu
enter the id
enter the salary
70157
enter the gender
male
employee details as following
name: vishnu
id:8
salary:70157
gender: male
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 32
Program-19
Given the resistance and tolerance ,generate the color bands of the
resistor using switch statement.
#include<stdio.h>
#include<conio.h>
void main()
int digit[3],tol,i;
clrscr();
printf("Enter the first digit with in [0-9]:\n");
scanf("%d",&digit[0]);
printf("Enter the second digit with in [0-9]:\n");
scanf("%d",&digit[1]);
printf("Enter the multiplier with in [0-9]:\n");
scanf("%d",&digit[2]);
printf("Enter the tolerance in percentage [5 or 10 or 20]:\n");
scanf("%d",&tol);
printf("The resistance value entered is %d%dx10^%dohm\n",digit[0],digit[1],digit[2]);
printf("The color code for given resistance value is \n");
for(i=0;i<3;i++)
switch(digit[i])
case 0:printf("Black ");
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 33
break;
case 1:printf("Brown ");
break;
case 2:printf("Red ");
break;
case 3:printf("Orange ");
break;
case 4:printf("Yellow ");
break;
case 5:printf("Green ");
break;
case 6:printf("Blue ");
break;
case 7:printf("Voilet ");
break;
case 8:printf("Grey ");
break;
case 9:printf("White ");
break;
default:printf("unknown value");
break;
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 34
switch(tol)
case 5:printf("Gold");
break;
case 10:printf("Silver");
break;
case 20:printf("No color ");
break;
default:printf("Unknown value");
break;
getch();}
Output:
Enter the first digit with in [0-9]:
Enter the second digit with in [0-9]:
Enter the multiplier with in [0-9]:
Enter the tolerence in percentage [5 or 10 or 20]:
The resistence value entered is 12x10^3ohm
The color code for given resistance value is
brown red orange gold
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 35
ASSEMBLY LANGUAGE PROGRAM[ALP]
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 36
INDEX
LIST OF PROGRAMS
SL NO PARTICULARS PAGE NO
1 TO ILLUSTRATE DIFFERENT ADDRESSING MODES 39
2 MOVE BLOCK OF DATA FROM ONE SET OF MEMORY 40
LOCATION TOANOTHER SET OF MEMORY LOCATION
IN INTERNAL RAM
3 ALP TO EXCHANGE BLOCK OF DATA B/W INTERNAL 41-42
RAM & EXTERNAL RAM
4 ALP TO EVALUATE LOGICAL EXPRESSION Y=a&b|c^!d 43-44
5 ALP TO CONVERT PACKED BCD TO UNPACKED BCD 45
6 ALP TO C0NVERT UNPACKED BCD TO PACKED BCD 46
7 ALP TO EVALUATE ARITHMATIC EXPRESSION 47
Y=(((5*2)4+1)/3%2
8 ALP TO ADD 2 BCD NUMBERS AND CONVERT THE 48
ANSWER TO BCD
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 37
PROGRAM 1
TO ILLUSTRATE DIFFERENT ADDRESSING MODES
ORG 0000H
MOV R0,#50H ; R050H [immediate addressing]
MOV R1,#40H ; R140H [immediate addressing]
MOV A,60H ; A [60H] [direct addressing]
ADD A,R1 ; AA+R2 [register addressing]
MOV @R0,A ; [[R0]]A , i.e.[50H]A [indirect addressing]
HERE:SJMP HERE ; Short jump HERE
END ; stop
OUTPUT
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 38
PROGRAME 2
MOVE BLOCK OF DATA FROM ONE SET OF MEMORY LOCATION TO
ANOTHER SET OF MEMORY LOCATION IN INTERNAL RAM
ORG 0000H
MOV R0,#30H ; R030H [source pointer]
MOV R1,#40H ; R140H [destination pointer]
MOV R2,#05H ; R205h [counter]
BACK:MOV A,@ R0 ; A[[R0]], i.e A[30H]
MOV @R1,A ; [[R1]]A
INC R0 ; R0R0+1
INC R1 ; R1R1+1
DJNZ R2,BACK ; decrement R2 ,if it is not equal to 0 go to lable BACK
HERE:SJMP HERE ; short jump here
END ; stop
OUTPUT:
BEFORE EXECUTION
AFTER EXECUTION
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 39
PROGRAME 3
ALP TO EXCHANGE BLOCK OF DATA B/W INTERNAL RAM & EXTERNAL
RAM
ORG 0000H
MOV R1,#60H ;R160H source pointer
MOV DPTR,#9000H ;DPTR9000H[destination pointer to external
MOV R2,#05H ;R205H [counter]
BACK:MOVX A,@DPTR ;A[[DPTR]]
XCH A,@ R1 ;A[R1],A[60H]
MOVX @DPTR,A ; [[DPTR]]A
INC R1 ; R1R1+1
INC DPTR ; DPTRDPTR+1
DJNZ R2,BACK ; decrement R2 jump to lable BACK if it is not equal to 0
HERE:SJMP HERE ; short jump
END ; stop
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 40
OUTPUT
BEFORE EXECUTION:
AFTER EXECUTION
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 41
PROGRAME 4
ALP TO EVALUATE LOGICAL EXPRESSION Y=a&b|c^!d
ORG 0000H
MOV R0,#12H ; R012H [immediate data]
MOV R1,#02H ; R102H [immediate data]
MOV R2,#25H ` ; R225H [immediate data]
MOV R3,#03H ;
R3 [immediate data]
;
MOV A,R0
; AR0,i.e A12H
ANL A,R1
; A A and R1R1A,
MOV R1,A ; AR3,i.e A03H
MOV A,R3
CPL A ; compliment AAA xor R2 A A
XRL A,R2 ; or R1
; [30H]A
ORL A,R1
;
MOV 30H,A
HERE:SJMP HERE ; short jump here
END ; stop
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 42
OUTPUT
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 43
PROGRAME 5
ALP TO CONVERT PACKED BCD TO UNPACKED BCD
ORG 0000H
MOV R0,#98H ; R098H packed bcd number
MOV A,R0 ; AR0,A98H
ANL A,#0F0H ; AA AND F0H i.e A 90H
SWAP A ; nibble exchange 9 0 i.e
A09H
MOV 30H,A ; [30H]A i.e [30H]09H
MOV A,R0 ; AR0,A98H
ANL A,#0FH ; AA AND 0FH i.e A 08H
MOV 31H,A ; [31H]A i.e [31H]08H
HERE:SJMP HERE ; short jump here
END ; stop
OUTPUT
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 44
PROGRAME 6
ALP TO C0NVERT UNPACKED BCD TO PACKED BCD
ORG 0000H
MOV R0,#09H ; R009H [immediate data]
MOV R1,#08H ; R108H [immediate data]
MOV A,R0 ; AR0,A09H
SWAP A ; nibble exchange 0 9 i.e A90H
ADD A,R1 ; A98H i.e A90H+08H
MOV 30H,A ; [30H]A
HERE:SJMP HERE ; short jump here
END ; stop
OUTPUT
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 45
PROGRAME 7
ALP TO EVALUATE ARITHMATIC EXPRESSION Y=(((5*2)4+1)/3%2
ORG 0000H
MOV A,#04H ; A04H [immediate data]
MOV R1,#01H ; R101H [immediate data]
ADD A,R1 ; AA+R1 i,e A04H+01H
MOV R1,A ; R1A
MOV B,#05H ; B05H[immediate data]
MOV A,#02H ; A02H[immediate data]
MUL AB ; B:A05H*02H i.e B(hb):
A(lb)00H:0AH
SUBB A,R1 ; Subtract With Borrow AA-R1
MOV B,#03H ; B03H
DIV AB ; B:A0AH/03H i.e
B(rem):A(quo)=01H:03H
MOV B,#02H ; B02H
DIV AB ; B:A03H/02H i.e
B(rem):A(quo)=01H:01H
MOV 30H,B ; [30H]B i.e [30H]01H(rem)
HERE:SJMP HERE ; Short Jump here
END ; Stop
OUTPUT
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 46
PROGRAME 8
ALP TO ADD 2 BCD NUMBERS AND CONVERT THE ANSWER TO
BCD
ORG 0000H
MOV R0,#99H ; R099H [immidiate bcd data 1]
MOV A,#99H ; A99H [immidiate bcd data 2]
ADD A,R0 ; AA+R0
DA A ; Decimal adjust accumulator,cy=1
and A=98
HERE:SJMP HERE ; Short jump here
END ; Stop
OUTPUT
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 47
EMBEEDDED C- PROGRAMMING
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 48
INDEX
LIST OF PROGRAMS
SL NO PARTICUALRS PAGE
NO
1 Embedded- C program to convert Packed BCD to Unpacked BCD 51
2 Embedded- C program to convert Unpacked BCD to Packed BCD 52
3 Embedded- C program to search for an 8 bit number in an array of 53
N number
4 Embedded C program to Search for an 8 bit number in an array of N 54
number
5 Embedded C program to toggle a particular bit in an Internal RAM 55-56
using a delay subroutine
6 Embedded C program to toggle all the bits of port P0 with a delay 57-58
subroutine
7 Embedded C program to generate a square wave on P2.3 using 59-60
delay subroutine
8 Embedded C program to toggle LED using push button switch 61
9 ALP to demonstrate enable , disable and priority settings of 62-63
interrupts and verify it in IE and IP registers
10 Embedded C program to generate square wave using Timer to 64-66
create a delay
11 Embedded C program to generate Triangular Waveform 67-68
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 49
Program-1
Embedded- C program to convert Packed BCD to Unpacked BCD
#include<reg51.h>
void main()
unsigned char num=0x23;
unsigned char x,y;
x=num&0x0f0;
x=x>>4; //right shift by 4 bits
P0=x;
y=num&0x0f;
P1=y;
Input:
Packed BCD data – 23h
Output:
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 50
Program-2
Embedded- C program to convert Unpacked BCD to Packed BCD
#include<reg51.h>
void main()
unsigned char x=0x09;
unsigned char y=0x08;
unsigned char num;
x=x<<4; //left shift by 4 bits
num=x|y; //logical OR operation
P0=num;
Input:
Unpacked BCD data - 09h ,08h
Output:
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 51
Program-3
Embedded- C program to search for an 8 bit number in an array of
N number [successful search].
#include<reg51.h>
void main()
unsigned char data1[]={0x10,0x20,0x30,0x40,0x43,0x50};
unsigned char key=0x20; //key to be searched
unsigned char i,result=0;
for(i=0;i<5;i++)
if(key==data1[i])
result=0xaa;
break;}
P0=result;
Output: successful search
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 52
Program-4
Embedded C program to Search for an 8 bit number in an array of N
number [unsuccessful search].
#include<reg51.h>
void main()
unsigned char data1[]={0x10,0x20,0x30,0x40,0x40,0x50};
unsigned char key=0x60; //key to be searched
unsigned char i,result=0;
for(i=0;i<5;i++)
if(key==data1[i])
result=0xaa;
break;}
P0=result;
Output: unsuccessful search
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 53
Program-5
Embedded C program to toggle a particular bit in an Internal RAM using a
delay subroutine
#include<reg51.h>
void delay(unsigned int); // function declaration
void main()
bit b1;
while (1) //infinite loop
b1=1;
delay(3); //function call to create a delay of 3 ms
b1=0;
delay(3); //function call to create a delay of 3 ms
//function definition
void delay(unsigned int t)
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1275;j++); // 1 ms delay
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 54
Output:
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 55
Program-6
Embedded C program to toggle all the bits of port P0 with a delay subroutine
#include<reg51.h>
void delay(unsigned int); //function declaration
void main()
while (1) //infinite loop
P0=0x00;
delay(4); //function call to create 4 ms delay
P0=0xff;
delay(4);
//function definition
void delay(unsigned int t)
{unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1275;j++); //1 ms delay
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 56
Output:
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 57
Program-7
Embedded C program to generate a square wave on P2.3 using delay
subroutine
#include<reg51.h>
void delay(unsigned int); //function declaration
sbit b1=P2^3; //port bit
void main()
while (1) //infinite loop
{b1=0;
delay(2); // function call of 2ms
b1=1;
delay(2); // function call of 2ms
//function definition
void delay(unsigned int t)
{unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1275;j++); // 1ms delay
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 58
Output:
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 59
Program-8
Embedded C program to toggle LED using push button switch
#include<reg51.h>
sbit Switch=P3^2;
sbit led=P2^0;
void main() {Switch=1;
while(1)
{if (Switch==1)
led=1;
else
led=0;
OUTPUT:
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 60
Program-9
ALP to demonstrate enable , disable and priority settings of interrupts and
verify it in IE and IP registers
ORG 0000H
MOV IE,#96H //enabling serial interrupt,timmer 0,external hardware interupt1
HERE:SJMP HERE
END
Output:
Before execution: after execution:
Priority interrupt:
ORG 0000H
MOV IE,#04H // priority interrupt external hardware interrupt 1
HERE:SJMP HERE
End
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 61
OUTPUT:
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 62
Program-10
Embedded C program to generate square wave using Timer to
create a delay
#include<reg51.h>
void delay(void); //Function Declaration
sbit x = P1^2; //S-bit operation
void main()
TMOD=0X01; //Timer-0 in Mode-1
while(1)
TL0=0X1A; //Initial Count value to create a delay of 0.25ms
TH0=0X0ff;
x=~x;
delay ();
//Function Definition
void delay(void)
TR0=0X01; //Start the Timer-0
while(TF0==0); //Monitor TF0 to become 1
TR0=0X00; //Stop Timer-0
TF0=0x00; //Clear Flag bit
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 63
}
Output
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 64
Program-11
Embedded C program to generate Triangular Waveform
#include<reg51.h>
void main()
unsigned char i;
while (1) //Repeat Continuously
for(i=0;i<255;i++) //Increasing Ramp
P1=i;
for(i=255;i>0;i--) //Decreasing Ramp
P1=i;
Output
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 65
E & C DEPT 2021-2022, SJ(GOVT) POLYTECHINC,BENGALURU-560001 Page 66