This section contains programs, some of these are written by me and some were found on internet or some good books of C like a to z of C/ C programming a moden approach etc.
I am also sharing these programs so that you may find alll of them at a place, thanks to all those great programmer who wrote awesom codes in C.
To print currancy in Railway Style using Recursion ?
#include<stdio.h>
void func(int n)
{
int r;
if(n==0)
return;
else
{
r=n%10;
func(n/10);
}
switch(r)
{
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;
default:printf(" Zero");
}
}
void main()
{
int n;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
func(n);
}
Program to generate pascal triangle with recursion ?
int func(int r,int c)
{
int f;
if(c==1||r==c)
return 1;
else
f=func(r-1,c)+func(r-1,c-1);
return f;
}
void main()
{
int f,j,r,i;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)
printf(" ");
for(j=1;j<=i;j++)
{
f=func(i,j);
printf("%d ",f);
}
printf("\n");
}
}
Program to convert Currancy to Indian Style ?
#include<stdio.h>
#include<conio.h>
char one[20][10]= {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninteen"};
char tens[10][10]={"","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninty"};
void display(int n)
{
int r;
if(n<20)
printf(" %s ",one[n]);
else
{
r=n%10;
n=n/10;
printf(" %s %s ",tens[n],one[r]);
}
}
void main()
{
long n,m;
int a[5];
int c=0;
clrscr();
printf("Enter number : ");
scanf("%ld",&n);
m=n;
while (n>0)
{
if (c==1)
{
a[c]=n%10;
n=n/10;
}
else
{
a[c]=n%100;
n/=100;
}
c++;
}
c--;
if (m>=10000000L)
{
display (a[c]);printf("Crore");c--;
if (a[c]>0)
{
display (a[c]);printf("Lack");
}c--;
if (a[c]>0)
{
display(a[c]);printf("Thousand");
}c--;
if (a[c]>0)
{
display(a[c]);printf("Hundred");
}c--;
if (a[c]>0)
display(a[c]);
}
else if(m>=100000L)
{
display (a[c]);printf("Lack");c--;
if (a[c]>0)
{
display(a[c]);printf("Thousand");
}c--;
if (a[c]>0)
{
display(a[c]);printf("Hundred");
}c--;
if (a[c]>0)
display(a[c]);
}
else if(m>=1000)
{
display(a[c]);printf("Thousand");c--;
if (a[c]>0)
{
display(a[c]);printf("Hundred");
}c--;
if (a[c]>0)
display(a[c]);
}
else if(m>=100)
{
display (a[c]);printf("Hundred");c--;
if (a[c]>0)
display(a[c]);
}
else
display(a[c]);
getch();
}
Write a program to connect C with MySql database?
#include<stdio.h>
#include<mysql.h>
#include<stdlib.h>
#include<string.h>
int main()
{
MYSQL *cn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server="localhost";
char *database="Student";
char *user="root";
char *password="123456";
char login[20],pass[20];
cn=mysql_init(NULL);
if(!mysql_real_connect(cn,server,user,password,database,0,NULL,0))
{
printf("\n%s",mysql_error(cn));
exit(1);
}
if(mysql_query(cn,"Select * from Login"))
{
printf("\n%s",mysql_error(cn));
exit(1);
}
res=mysql_use_result(cn);
row=mysql_fetch_row(res);
printf("\nLogin : ");
gets(login);
printf("\nPassword : ");
gets(pass);
if(strcmp(login,row[0])==0&&strcmp(pass,row[1])==0)
{
printf("Welcome to the world of Database Programming");
}
else
printf("\nInvalid Login Id or Password");
mysql_free_result(res);
mysql_close(cn);
printf("\n");
}
Note : to run this program you need a command on linux, i wrote my name, you replace the file name with yours.
gcc -o Piyush $(mysql_config --cflags) Piyush.c $(mysql_config --libs)
Write a program to swap two numbers?
#include <stdio.h>
#define swap(x,y) (x ^= y ^= x ^= y)
#define print(a,b) printf("\na = %d\tb = %d",a,b)
int main()
{
int a = 3 , b=4;
print(a,b);
swap(a,b);
print(a,b);
}
Note : ^ operator works for integers so for swaping two floating numbers use 3 variable concept.
Write a program to display a number in Roman style ?
#include <stdio.h>
void InRoman( int n )
{
int i, num[ ] = { 1, 4, 5, 9, 10, 40, 50, 90, 100,400, 500, 900, 1000, 9999 };
char *r[ ] = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C","CD", "D", "CM", "M" };
while ( n )
{
for( i=0 ; num[i]<=n ;++i )
;
--i;
n -= num[i];
printf( "%s", r[i] );
}
}
int main( void )
{
int n;
printf( "Enter the number: " );
scanf( "%d", &n );
printf( "%d in romain : ",n );
InRoman( n );
return(0);
}
Calulate the day on a date?
#include <stdio.h>
int DayOfWeek( int y, int m, int d )
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
int main()
{
char *day[] = {"Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thrusday" , "Friday" , "Saturday"};
int dd,mm,yy;
printf("\nYour D.O.B : ");
scanf("%d%*c%d%*c%d", &dd, &mm, &yy);
printf("Hey you were born on : %s\n", day[DayOfWeek(yy,mm,dd)]);
}
Write a program to display calender of a month?
#include <stdio.h>
#include <stdlib.h>
int Days_Tbl[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
char *Month_Tbl[12] = {
"January", "February", "March", "April", "May",
"June", "July", "August", "September",
"October", "November", "December"
};
int FirstDayOfMonth( int m, int y );
void PrintCalendar( int m, int y );
int FirstDayOfMonth( int m, int y )
{
int i, leap;
long d;
if ( y>1752 ) /* for Gregorian Calendar */
{
leap = ( y%4==0&&y%100!=0 || y%400==0 );
d = 365L*1752 + 1752/4;
d += 365L*(y-1752-1) + (y-1752-1)/4 - (y-1752-1)/100
+ (y-1752-1)/400 + 6;
}
else
{
leap = ( y%4==0 );
d = 365L*(y-1) + (y-1)/4 + 6;
}
for( i=1; i<m; ++i )
d += Days_Tbl[leap][i-1];
if ( y>1752 || (y==1752 && m>9) )
d -= 11;
return( d % 7 );
}
void PrintCalendar( int m, int y )
{
int i, leap, firstdayofmonth;
firstdayofmonth = FirstDayOfMonth( m, y );
leap = ( y>1752 ) ? ( y%4==0&&y%100!=0 || y%400==0 ) : ( y%4==0 );
printf( "%13s - %d\n", Month_Tbl[m-1], y );
printf( "Sun Mon Tue Wed Thu Fri Sat\n" );
for ( i=0; i<firstdayofmonth ; ++i )
printf( " " );
for ( i=1 ; i<=Days_Tbl[leap][m-1] ; ++i )
{
printf( "%3d ", i );
if ( (firstdayofmonth + i)%7 == 0 )
printf("\n");
if (y==1752 && m==9 && i==2)
{
i += 11;
firstdayofmonth += 3;
}
}
printf( "\n" );
}
int main( int argc, char *argv[ ] )
{
int m, y;
switch( argc )
{
case 1:
printf( "Syntax: cal [month] year \n" );
break;
case 2:
y = atoi( argv[1] );
for ( m=1 ; m<=12 ; ++m )
{
PrintCalendar( m, y );
printf( "Press <ENTER>....\n" );
getchar( );
}
break;
case 3:
m = atoi( argv[1] );
y = atoi( argv[2] );
PrintCalendar( m, y );
}
return(0);
}
I am also sharing these programs so that you may find alll of them at a place, thanks to all those great programmer who wrote awesom codes in C.
To print currancy in Railway Style using Recursion ?
#include<stdio.h>
void func(int n)
{
int r;
if(n==0)
return;
else
{
r=n%10;
func(n/10);
}
switch(r)
{
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;
default:printf(" Zero");
}
}
void main()
{
int n;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
func(n);
}
Program to generate pascal triangle with recursion ?
int func(int r,int c)
{
int f;
if(c==1||r==c)
return 1;
else
f=func(r-1,c)+func(r-1,c-1);
return f;
}
void main()
{
int f,j,r,i;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)
printf(" ");
for(j=1;j<=i;j++)
{
f=func(i,j);
printf("%d ",f);
}
printf("\n");
}
}
Program to convert Currancy to Indian Style ?
#include<stdio.h>
#include<conio.h>
char one[20][10]= {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninteen"};
char tens[10][10]={"","","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninty"};
void display(int n)
{
int r;
if(n<20)
printf(" %s ",one[n]);
else
{
r=n%10;
n=n/10;
printf(" %s %s ",tens[n],one[r]);
}
}
void main()
{
long n,m;
int a[5];
int c=0;
clrscr();
printf("Enter number : ");
scanf("%ld",&n);
m=n;
while (n>0)
{
if (c==1)
{
a[c]=n%10;
n=n/10;
}
else
{
a[c]=n%100;
n/=100;
}
c++;
}
c--;
if (m>=10000000L)
{
display (a[c]);printf("Crore");c--;
if (a[c]>0)
{
display (a[c]);printf("Lack");
}c--;
if (a[c]>0)
{
display(a[c]);printf("Thousand");
}c--;
if (a[c]>0)
{
display(a[c]);printf("Hundred");
}c--;
if (a[c]>0)
display(a[c]);
}
else if(m>=100000L)
{
display (a[c]);printf("Lack");c--;
if (a[c]>0)
{
display(a[c]);printf("Thousand");
}c--;
if (a[c]>0)
{
display(a[c]);printf("Hundred");
}c--;
if (a[c]>0)
display(a[c]);
}
else if(m>=1000)
{
display(a[c]);printf("Thousand");c--;
if (a[c]>0)
{
display(a[c]);printf("Hundred");
}c--;
if (a[c]>0)
display(a[c]);
}
else if(m>=100)
{
display (a[c]);printf("Hundred");c--;
if (a[c]>0)
display(a[c]);
}
else
display(a[c]);
getch();
}
Write a program to connect C with MySql database?
#include<stdio.h>
#include<mysql.h>
#include<stdlib.h>
#include<string.h>
int main()
{
MYSQL *cn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server="localhost";
char *database="Student";
char *user="root";
char *password="123456";
char login[20],pass[20];
cn=mysql_init(NULL);
if(!mysql_real_connect(cn,server,user,password,database,0,NULL,0))
{
printf("\n%s",mysql_error(cn));
exit(1);
}
if(mysql_query(cn,"Select * from Login"))
{
printf("\n%s",mysql_error(cn));
exit(1);
}
res=mysql_use_result(cn);
row=mysql_fetch_row(res);
printf("\nLogin : ");
gets(login);
printf("\nPassword : ");
gets(pass);
if(strcmp(login,row[0])==0&&strcmp(pass,row[1])==0)
{
printf("Welcome to the world of Database Programming");
}
else
printf("\nInvalid Login Id or Password");
mysql_free_result(res);
mysql_close(cn);
printf("\n");
}
Note : to run this program you need a command on linux, i wrote my name, you replace the file name with yours.
gcc -o Piyush $(mysql_config --cflags) Piyush.c $(mysql_config --libs)
Write a program to swap two numbers?
#include <stdio.h>
#define swap(x,y) (x ^= y ^= x ^= y)
#define print(a,b) printf("\na = %d\tb = %d",a,b)
int main()
{
int a = 3 , b=4;
print(a,b);
swap(a,b);
print(a,b);
}
Note : ^ operator works for integers so for swaping two floating numbers use 3 variable concept.
Write a program to display a number in Roman style ?
#include <stdio.h>
void InRoman( int n )
{
int i, num[ ] = { 1, 4, 5, 9, 10, 40, 50, 90, 100,400, 500, 900, 1000, 9999 };
char *r[ ] = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C","CD", "D", "CM", "M" };
while ( n )
{
for( i=0 ; num[i]<=n ;++i )
;
--i;
n -= num[i];
printf( "%s", r[i] );
}
}
int main( void )
{
int n;
printf( "Enter the number: " );
scanf( "%d", &n );
printf( "%d in romain : ",n );
InRoman( n );
return(0);
}
Calulate the day on a date?
#include <stdio.h>
int DayOfWeek( int y, int m, int d )
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
int main()
{
char *day[] = {"Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thrusday" , "Friday" , "Saturday"};
int dd,mm,yy;
printf("\nYour D.O.B : ");
scanf("%d%*c%d%*c%d", &dd, &mm, &yy);
printf("Hey you were born on : %s\n", day[DayOfWeek(yy,mm,dd)]);
}
Write a program to display calender of a month?
#include <stdio.h>
#include <stdlib.h>
int Days_Tbl[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
char *Month_Tbl[12] = {
"January", "February", "March", "April", "May",
"June", "July", "August", "September",
"October", "November", "December"
};
int FirstDayOfMonth( int m, int y );
void PrintCalendar( int m, int y );
int FirstDayOfMonth( int m, int y )
{
int i, leap;
long d;
if ( y>1752 ) /* for Gregorian Calendar */
{
leap = ( y%4==0&&y%100!=0 || y%400==0 );
d = 365L*1752 + 1752/4;
d += 365L*(y-1752-1) + (y-1752-1)/4 - (y-1752-1)/100
+ (y-1752-1)/400 + 6;
}
else
{
leap = ( y%4==0 );
d = 365L*(y-1) + (y-1)/4 + 6;
}
for( i=1; i<m; ++i )
d += Days_Tbl[leap][i-1];
if ( y>1752 || (y==1752 && m>9) )
d -= 11;
return( d % 7 );
}
void PrintCalendar( int m, int y )
{
int i, leap, firstdayofmonth;
firstdayofmonth = FirstDayOfMonth( m, y );
leap = ( y>1752 ) ? ( y%4==0&&y%100!=0 || y%400==0 ) : ( y%4==0 );
printf( "%13s - %d\n", Month_Tbl[m-1], y );
printf( "Sun Mon Tue Wed Thu Fri Sat\n" );
for ( i=0; i<firstdayofmonth ; ++i )
printf( " " );
for ( i=1 ; i<=Days_Tbl[leap][m-1] ; ++i )
{
printf( "%3d ", i );
if ( (firstdayofmonth + i)%7 == 0 )
printf("\n");
if (y==1752 && m==9 && i==2)
{
i += 11;
firstdayofmonth += 3;
}
}
printf( "\n" );
}
int main( int argc, char *argv[ ] )
{
int m, y;
switch( argc )
{
case 1:
printf( "Syntax: cal [month] year \n" );
break;
case 2:
y = atoi( argv[1] );
for ( m=1 ; m<=12 ; ++m )
{
PrintCalendar( m, y );
printf( "Press <ENTER>....\n" );
getchar( );
}
break;
case 3:
m = atoi( argv[1] );
y = atoi( argv[2] );
PrintCalendar( m, y );
}
return(0);
}