#include "stdio.h"
#include "conio.h"
void print(int,int); /* To print digit from 1 to 19 */
void print_place(int); /* To print place */
void print_digit(int); /* To print numbers 20,30,40,50,60,70,80,90 */
int arr[8] = {0}; /* Global Declaration and Initialization */
void main()
{
long int num,temp[8];
int i,j,k,digit,place = 0,rem,check;
clrscr();
printf("Please, Enter Number\n");
printf("The Number Should be Greater than One and Less than One Crore.\n>> ");
scanf("%ld",&num);
printf("\n>> ");
if(num < 10)
print(num,1);
if(num > 0 && num < 10000000)
{
i = 0;
do
{
digit = num % 10;
place++; /* To find the places of digit */
temp[i++] = digit; /* Copy to temporary array */
num /= 10;
}while(num != 0);
i--;
j = 0;
while(i >= 0)
arr[j++] = temp[i--]; /* Copy array temp to array arr reversly */
i = 0;
j = place-1;
k = place-1;
while(i < j)
{
if(place == 7) /* For Lakh */
{
check = 10 * arr[0] + arr[1]; /* If the Sum of first and second digit */
if(check > 9 && check < 20) /* is betwen 9 and 20 */
{
rem = check; /* copy check to rem */
print(rem,place); /* and print */
}
else
{
rem = 10 * arr[0]; /* else multiply the first number with 10 */
print_digit(rem); /* print the number */
print(arr[1],1); /* print the second digit */
print_place(place); /* print place */
}
i += 2; /* Increase i by 2 */
place -= 2; /* Decrease place by 2 */
}
if(place == 5) /* For Thousand */
{
check = 10 * arr[k - 4] + arr[k - 3]; /* Sum of third and fourth digit */
if(check > 9 && check < 20)
{
rem = check;
print(rem,5);
}
else
{
rem = 10 * arr[k - 4];
print_digit(rem);
print(arr[k - 3],1);
if(arr[k - 4] != 0 && arr[k - 3] != 0)
print_place(place);
}
i += 2;
place -= 2;
}
if(place < 3)
{
check = 10 * arr[k - 1] + arr[k];
if(check > 9 && check < 20)
{
rem = check;
print(rem,place);
}
else
{
rem = 10 * arr[k - 1];
print_digit(rem);
print(arr[k],1);
}
i++;
}
else
print(arr[i++],place--);
}
}
else
printf("Out of Range.");
getch();
}
void print(int num,int place)
{
static int pass = 1;
switch (num)
{
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
case 10:
printf("Ten ");
break;
case 11:
printf("Eleven ");
break;
case 12:
printf("Tweleve ");
break;
case 13:
printf("Thirteen ");
break;
case 14:
printf("Fourteen ");
break;
case 15:
printf("Fifteen ");
break;
case 16:
printf("Sixteen ");
break;
case 17:
printf("Seventeen ");
break;
case 18:
printf("Eighteen ");
break;
case 19:
printf("Nineteen ");
break;
}
if(arr[1] == 0 && arr[2] == 0 && arr[3] == 0 && arr[4] == 0 && arr[5] == 0 && arr[6] == 0)
{
if(pass == 1)
print_place(place);
pass++;
}
if(pass == 1)
print_place(place);
}
void print_digit(int num)
{
switch(num)
{
case 20:
printf("Twenty ");
break;
case 30:
printf("Thirty ");
break;
case 40:
printf("Forty ");
break;
case 50:
printf("Fifty ");
break;
case 60:
printf("Sixty ");
break;
case 70:
printf("Seventy ");
break;
case 80:
printf("Eighty ");
break;
case 90:
printf("Ninety ");
break;
}
}
void print_place(int place)
{
switch (place)
{
case 3:
printf("Hundred ");
break;
case 4:
case 5:
printf("Thousand ");
break;
case 6:
case 7:
printf("Lakh ");
break;
}
}
Sunday, September 20, 2009
PROGRAM TO CONVERT ENTERED NUMBER TO WORDS
Labels: Nice Programs
Posted by Unknown at 12:35 AM 0 comments
Friday, September 4, 2009
COMMENTS REMOVER
METHOD 1: Not suitable for all cases / Preferable for small programs
Simple Logic and Easy to Understand
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void main()
{
char str[10000],filename[15];
/* The size of string str is 10000 so the program will produce */
/* error if the sourcefile contains more than 10000 characters. */
/* You can edit the source code to remove such errors. */
int i;
FILE *source,*temp;
clrscr();
printf("**************************\n");
printf("* COMMENTS REMOVER \t *\n");
printf("*\tby Ankit Pokhrel *\n");
printf("**************************\n\n");
printf("Enter a filename with extension: ");
gets(filename);
source = fopen(filename,"r"); /* Open the source file */
if(source == NULL)
{
perror("Error ");
getch();
exit(0);
}
temp = fopen("temp.cpp","w"); /* Make a temporary file */
if(temp == NULL)
{
perror("Error ");
getch();
exit(0);
}
/* Copy all the contents of file to variable string str */
i = 0;
do
{
str[i] = fgetc(source);
} while(str[i++] != EOF);
str[i-1] = '\0'; /* Remove EOF character with end - of - string marker '\0' */
i = 0;
while(str[i] != '\0')
{
if(str[i] == '/' && str [i+1] == '*') /* If comment */
{
i += 2; /* Jump first two characters '/' and '*' */
do
{
i++; /* Continuously move the cursor of the file */
} while(str[i] != '*' || str[i+1] != '/'); /* until the end of comment is reached */
i += 2; /* Jumps the last two characters i.e, '*' and '/' */
}
else
fputc(str[i++],temp); /* Copy to temp */
}
fcloseall(); /* Close both files */
remove(filename); /* Remove original file */
rename("temp.cpp",filename); /* Rename temporary file to original filename */
printf("Comments removed successfully.");
getch();
}
METHOD 2: Suitable for all cases / Preferable for Large Programs
Complex Logic and Difficult to Understand
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void main()
{
char ch,filename[15];
FILE *source,*temp;
clrscr();
printf("**************************\n");
printf("* COMMENTS REMOVER \t *\n");
printf("*\tby Ankit Pokhrel *\n");
printf("**************************\n\n");
printf("Enter a filename with extension: ");
gets(filename);
source = fopen(filename,"r"); /* Open the source file */
if(source == NULL)
{
perror("Error ");
getch();
exit(0);
}
temp = fopen("temp.cpp","w"); /* Make a temporary file */
if(temp == NULL)
{
perror("Error ");
getch();
exit(0);
}
while(ch != EOF) /* Until end of file is reached */
{
ch = fgetc(source); /* Get a character */
if(ch == '/')
{
ch = fgetc(source); /* Get another character */
if(ch == '*')
{
do
{
ch = fgetc(source); /* Continuously get a character */
if(ch == EOF) /* If end of file is reached inside the loop */
break;
if(ch == '*')
{
ch = fgetc(source);
if(ch == '/') /* unless '/' is found */
break;
}
} while(1); /* Until breaking condition is not reached */
}
else
{
fputc('/',temp);
fputc(ch,temp);
}
}
else
{
if(ch != EOF) /* To exclude last EOF character */
fputc(ch,temp);
}
}
fcloseall(); /* Close both files */
remove(filename); /* Remove original file */
rename("temp.cpp",filename); /* Rename temporary file to original filename */
printf("Comments removed successfully.");
getch();
}
Labels: Nice Programs
Posted by Unknown at 1:18 AM 0 comments
A DIGITAL WATCH
#include "stdio.h"
#include "conio.h"
#include "graphics.h"
#include "dos.h"
void clear(int,int,int,int);
void main()
{
int gd = DETECT,gm,hour,min,sec,midx,midy,temp;
char buffer[20],ch;
struct time now;
initgraph(&gd,&gm,"c:\\tc\\bgi");
midx = getmaxx()/2;
midy = getmaxy()/2;
setcolor(RED);
rectangle(midx-210,midy-40,midx+198,midy+30);
outtextxy(midx-80,midy+190,"Press \"ESC\" to Exit");
gettime(&now);
if(now.ti_hour > 12)
{
setcolor(GREEN);
settextstyle(5,HORIZ_DIR,5);
outtextxy(midx+170,midy-20,"PM");
hour = now.ti_hour - 12;
ch = 'p';
}
else
{
setcolor(GREEN);
settextstyle(5,HORIZ_DIR,5);
outtextxy(midx+170,midy-20,"AM");
hour = now.ti_hour;
ch = 'a';
}
min = now.ti_min;
sec = now.ti_sec;
while(1)
{
if(kbhit())
{
if(getch() == 27)
break;
}
if(hour == 12 && min == 0 && sec == 0 && ch == 'a')
{
setcolor(BLACK);
settextstyle(5,HORIZ_DIR,5);
outtextxy(midx+170,midy-20,"AM");
setcolor(GREEN);
outtextxy(midx+170,midy-20,"PM");
}
if(hour == 12 && min == 0 && sec == 0 && ch == 'p')
{
setcolor(BLACK);
settextstyle(5,HORIZ_DIR,5);
outtextxy(midx+170,midy-20,"PM"); /* To Overwrite Previous Notation */
setcolor(GREEN);
outtextxy(midx+170,midy-20,"AM");
}
delay(1000);
if(sec >= 59)
{
clear(midx-12,midy-32,midx+32,midy+25);
min++;
if(min > 59)
{
min = 0;
hour++;
if(hour > 12)
hour -= 12;
clear(midx-144,midy-32,midx-101,midy+25);
if(hour > 9)
{
clear(midx-144,midy-32,midx-101,midy+25);
clear(midx-198,midy-32,midx-156,midy+25);
}
}
if(min%10 == 0)
clear(midx-67,midy-32,midx-25,midy+25);
sec = 0;
}
else
sec++;
if(sec%10 == 0)
clear(midx+65,midy-32,midx+107,midy+25);
clear(midx+120,midy-32,midx+162,midy+25); /* To clear seconds */
sprintf(buffer,"%02d:%02d:%02d",hour,min,sec);
setcolor(BROWN);
settextstyle(5,HORIZ_DIR,8);
outtextxy(midx-195,midy-70,buffer);
}
closegraph();
}
/* This function clears the specified area */
/* by glowing the pixel of that area in black color */
void clear(int x1,int y1,int x2,int y2)
{
int x,y;
for(x=x1;x<=x2;x++)
for(y=y1;y<=y2;y++)
putpixel(x,y,BLACK);
}
Labels: c graphics, Nice Programs
Posted by Unknown at 1:07 AM 0 comments
PROGRAM TO MAKE A PIECHART
#include "stdio.h"
#include "conio.h"
#include "process.h"
#include "graphics.h"
float angle(int,int);
void main()
{
int gd = DETECT,gm,midx,midy,temp,errorcode;
int comp,ele,civil,arc,start_angle,end_angle,total;
float percent;
char convert[6];
initgraph(&gd,&gm,"c:\\tc\\bgi");
errorcode = graphresult();
if (errorcode != grOk) /* An Error Occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* Terminate with an error code */
}
midx = getmaxx()/2;
midy = getmaxy()/2;
/* Input Data and Calculates Total */
do
{
cleardevice();
setcolor(RED);
outtextxy(15,10,"ENTER NUMBER OF STUDENTS ON FOLLOWING FACULTIES");
setcolor(BROWN);
outtextxy(15,38,"Computer :");
gotoxy(17,3);
scanf("%d",&comp);
outtextxy(15,53,"Electronics :");
gotoxy(17,4);
scanf("%d",&ele);
outtextxy(15,68,"Civil :");
gotoxy(17,5);
scanf("%d",&civil);
outtextxy(15,83,"Architect :");
gotoxy(17,6);
scanf("%d",&arc);
total = comp+ele+civil+arc;
/* Formats and Makes a Piechart with the help of */
/* function pieslice() and simple mathmatical calculations */
setcolor(RED);
rectangle(70,150,midx+260,midy+150);
setcolor(BLACK);
setfillstyle(SOLID_FILL,GREEN);
start_angle = 0;
end_angle = start_angle+angle(comp,total);
if(start_angle >= end_angle)
; /* Does Nothing */
else
pieslice(midx-120,midy+30,start_angle,end_angle,100); /* Draw a Sector */
start_angle = end_angle;
end_angle = start_angle+angle(ele,total);
if(start_angle >= end_angle)
;
else
{
setfillstyle(SOLID_FILL,BLUE);
pieslice(midx-120,midy+30,start_angle,end_angle,100);
}
start_angle = end_angle;
end_angle = start_angle+angle(civil,total);
if(start_angle >= end_angle)
;
else
{
setfillstyle(SOLID_FILL,BROWN);
pieslice(midx-120,midy+30,start_angle,end_angle,100);
}
start_angle = end_angle;
end_angle = 360;
if(start_angle >= end_angle)
;
else
{
setfillstyle(SOLID_FILL,CYAN);
pieslice(midx-120,midy+30,start_angle,end_angle,100);
}
/* Draw Bars */
setfillstyle(SOLID_FILL,GREEN);
bar(midx+50,midy-30,midx+60,midy-20);
setfillstyle(SOLID_FILL,BLUE);
bar(midx+50,midy-10,midx+60,midy);
setfillstyle(SOLID_FILL,BROWN);
bar(midx+50,midy+10,midx+60,midy+20);
setfillstyle(SOLID_FILL,CYAN);
bar(midx+50,midy+30,midx+60,midy+40);
/* Displays Informations with Percentage */
setcolor(LIGHTBLUE);
outtextxy(midx+75,midy-28,"Computer");
percent = ((float) comp/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy-28,convert);
outtextxy(midx+75,midy-8,"Electronics");
percent = ((float) ele/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy-8,convert);
outtextxy(midx+75,midy+13,"Civil");
percent = ((float) civil/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy+13,convert);
outtextxy(midx+75,midy+33,"Architect");
percent = ((float) arc/total)*100;
sprintf(convert,"(%.2f\%)",percent);
outtextxy(midx+165,midy+33,convert);
setcolor(RED);
outtextxy(midx-90,midy+200,"Press \"ESC\" to stop.");
} while(getch() != 27);
/* Clean Up */
closegraph();
}
float angle(int x,int total)
{
float degree;
degree = ((float)x/total) * 360; /* Converts the data to degree */
return degree;
}
Labels: c graphics, Nice Programs
Posted by Unknown at 1:06 AM 0 comments
Friday, June 26, 2009
PROGRAM TO EXCLUDE THE REPEATED ELEMENTS FROM AN ARRAY
#include "stdio.h"
#include "conio.h"
#include "alloc.h"
#define TRUE 1
#define FALSE 0
void main()
{
int *a1,*a2,*a3,i,j,size=0,repeated,num;
clrscr();
printf("How Many Numbers? ");
scanf("%d",&num);
a1=(int *) malloc(num*sizeof(int));
a2=(int *) malloc(num*sizeof(int));
a3=(int *) malloc(num*sizeof(int));
printf("\nEnter %d Numbers:\n",num);
for(i=0;i
scanf("%d",&a1[i]);
/* Copying the elements of array a1 to another array a2 */
a2[i]=a1[i];
repeated=FALSE;
for(j=0;j
if(a1[i]==a2[j])
{
repeated=TRUE;
break;
}
if(!repeated)
a3[size++]=a1[i];
}
printf("\nRepeated Numbers Excluded.");
printf("\nPrinting Third Array...\n");
for(i=0;i
free(a1);
free(a2);
free(a3);
getch();
}
Labels: Arrays, Loops, Nice Programs
Posted by Unknown at 12:48 AM 0 comments