20BCS010 Anzil ESProg7ii

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

20BCS010

ANZIL MUFTI
PROGRAM 7

AIM: Write a program to take a number as binary input and display


its factorial on the LCD screen.
OUTCOME: Students will learn how to use Keil compiler for writing programs
and FlashMagic to build the program in PC.  
HARDWARE USED: Microcontroller kit, Power supply, connecting wires.  
SOFTWARE USED: Keil µVision 4, Flash Magic. 1.

SOURCE CODE:

#include <LCD.h>

unsigned int count = 0;


unsigned char a[16];

void input()
{
unsigned int val = 0;
if (INT0 == 0)
val += 8;

if (INT1 == 0)
val += 4;
if (T0 == 0)
val += 2;

if (T1 == 0)
val += 1;
if (val != 0)
count = val;
}

long fact(int n)
{
long factorial = 1;
while (n)
{
factorial *= n;
n--;
}
return factorial;
}

void intToChar(long n)
{
int i = 0, j = 0;
for (i = 0; i < 16; i++)
{
a[i] = ' ';
}
i = 0;
while (n != 0)
{
a[i] = (n % 10) + '0';
i++;
n = n / 10;
}
i--;
while (j <= i)
{
char temp = a[i];
a[i] = a[j];
a[j] = temp;
i--;
j++;
}
}
void main(void)
{
int i = 0;
LCD_INIT();

while (1)
{
input();
if (count == 0)
{
LCD_WRITE("Enter a number", 0, 0);
}
else
{
LCD_WRITE("Fact of ", 0, 0);
delay(20);
intToChar(count);
LCD_WRITE(a, 0, 8);
delay(20);

LCD_WRITE("is ", 1, 0);


intToChar(fact(count));
LCD_WRITE(a, 1, 4);
delay(20);
count = 0;
}
}
}

SNAPS:

You might also like