Program for computing Area, Volume and Perimeter of Rrectangle.

#include

void inputoutput ()
{
int comp,ans;
clrscr ();
printf ("choose please: 1=perimeter,2=area,3=volume] ?: ");
scanf ("%d",&comp);
if (comp==1)
{
int le, wi;
printf ("Enter the length: ");
scanf ("%d",≤);
printf ("Enter the width: ");
scanf ("%d",&wi);
printf ("P=%d",perimeter(le,wi));
}
else if (comp==2)
{
int le, wi;
printf ("Enter the length: ");
scanf ("%d",≤);
printf ("Enter the width: ");
scanf ("%d",&wi);
printf ("A=%d", area(le,wi));
}
else if (comp==3)
{
int length,width,height;
printf ("Enter lenght: ");
scanf ("%d",&length);
printf ("Enter width: ");
scanf ("%d",&width);
printf ("Enter height: ");
scanf ("%d",&height);
printf ("V=%d",volume (length,width,height));
}

else inputoutput ();
printf ("
Do you want to continue? [Yes=1/No=0]: ");
scanf ("%d",&ans);
if (ans==1)
inputoutput ();
else printf ("GOOD Day");
}
int perimeter (int l, int w)
{
int per;
per=(l*2)+(w*2);
return (per);

}
int area (int le, int wi)
{
int are;
are=le*wi;
return (are);
}
int volume (int length, int width, int height)
{
int vol;

vol=(length*width*height);
return (vol);
}



main ()
{
clrscr ();
inputoutput ();
getch ();
}

Program to correct rudimentary syntax errors

#include
# include
#define NULL 0
FILE *fpt;
void main()
{
int c1=0,c2=0,c3=0,c4=0,c5=0;
char c,name[20],z;
clrscr();
printf("Enter the name of file to be checked :- ");
gets(name);
fpt=fopen(name,"r");
if (fpt==NULL)
printf("
ERROR - can/'t open file %s",name);
else
{
while ((c=getc(fpt))!=EOF)
{
if (c=='(')
c1=c1+1;
if (c==')')
c1=c1-1;
if (c=='[')
c2=c2+1;
if (c==']')
c2=c2-1;
if (c=='
')
{
if (c1!=0)
printf("
ERROR - Unbalanced parenthesis ()");
if (c2!=0)
printf("
ERROR - Unbalanced brackets []");
}
if (c=='{')
c3=c3+1;
if (c=='}')
c3=c3-1;
if ((int)c==39)
{
if (c1!=0)
{
if (c4==0)
c4=c4+1;
else
c4=c4-1;
}
else
printf("
ERROR - Unbalanced ' ");
}
if ((int)c==34)
{
if (c1!=0)
{
if (c5==0)
c5=c5+1;
else
c5=c5-1;
}
else
{
z=(char)34;
printf("
ERROR - Unbalanced %c ",z);
}
}
}
}

if (c1!=0)
printf("
ERROR - Unbalanced parenthesis ()");
if (c2!=0)
printf("
ERROR - Unbalanced brackets []");
if (c3!=0)
printf("
ERROR - Unbalanced braces {}");
if (c4!=0)
printf("
ERROR - Unbalanced ' ");
if (c5!=0)
printf("
ERROR - Unbalanced " ");

if (c1==0 && c2==0 && c3==0 && c4==0 && c5==0)
printf("
Program is up to date. WELL DONE!");
fclose(fpt);
getch();
}

Invoke function without main in C Language

#include
#include

#define FIRST 0
#define SECOND 1
#define THIRD 2


//Variables
int a,b,c,ch;
float d;
//Function Prototype
void Read();
void Operation();
void Display();

#pragma startup Read 0 //First_Priority
#pragma startup Operation 1 //Second_Priority
#pragma exit Display //Third_Priority

void main()
{
printf("

Enter to main() ");
getch();
printf("Exit From main() ");
getch();
}

void Read()
{
clrscr();
printf("
Enter the value of a : ");
scanf("%d",&a);
printf("
Enter the value of b : ");
scanf("%d",&b);
}

void Operation()
{
printf("ArithMetic Operations");
printf("---------------------");
printf("1 -> Addition ");
printf("2 -> Subtraction");
printf("3 -> Multiplication");
printf("---------------------");
scanf("%d",&ch);
switch(ch)
{
case 1:
c = a+b;
break;
case 2:
c = a-b;
break;
case 3:
c = a*b;
break;
}
}

void Display()
{
switch(ch)
{
case 1:
printf("The Result (Addition) : %d",c);
break;
case 2:
printf("

The Result (Subtraction): %d",c);
break;
case 3:
printf("

The Result (Multiplication): %d",c);
break;
}
getch();
}

Program to convertion of number to letters

#include
#include
#include
char tens[15][15]={{""},{""},{"Twenty "},{"Thirty "},{"Fourty "},{"Fifty
"},{"Sisty "},{"Seventy "},{"Eighty "},{"Ninty "}};
char one[25][15]={{""},{"One "},{"Two "},{"Three "},{"Four "},{"Five
"},{"Six "},{"Seven "},{"Eight "},{"Nine "},{"Ten "},{"Eleven "},{"Twelve
"},{"Thirteen "},{"Fourteen "},{"Fifteen "},{"Sisteen "},{"Seventeen
"},{"Eighteen "},{"Nineteen "}};
char out[150];
int n[10]={0},i,temp1=0,temp;
long int num,f;
void convert(int i)
{

if(i==7)
{
strcat(out,one[n[i]]);
strcat(out,"Crore ");
}
if(i==6||i==4||i==1)
{
if(n[i]==1)
temp1=n[i]*10;
if(i==1&&n[0]!=0&&f>1)
strcat(out," And ");
strcat(out,tens[n[i]]);
}
if((i==5||i==3||i==0||i==2))
{
if(temp1!=0)
{
temp1+=n[i];
if(i==0&&n[i+1]!=0&&n[i+2]==0&&f>1)
strcat(out," And ");
strcat(out,one[temp1]);
}
else
{
if(i==0&&n[i+1]!=0&&n[i+2]==0&&f>1)
strcat(out," And ");
strcat(out,one[n[i]]);
}
if(n[i]!=0||temp1!=0||n[i+1]!=0)
switch(i)
{
case 5:strcat(out,"Lakh ");break;
case 3:strcat(out,"Thousand ");break;
case 2:strcat(out,"Hundred ");
if(n[i]==0)
strcat(out," And ");
}
temp1=0;
}

}

void main()
{
int i=0,j;
char c[2];
clrscr();
c[1]='

CPU Scheduling algorithm implementation

#include
#include
#include
#include
#include


union REGS i, o ;
int x1,y1,click;
int count=1;
char *comments[]={
" ",
"Opps ?...Try it again!",
"C give yourself a chance!",
"Yes!! You can do it !",
"Think about 1 Million!",
"Don't give up Friend !",
"Ohhh... LOOSER..!!",
"Sorry..You loose !"
};

void main()
{

int x=50,y=50,gdriver = 9, gmode=2;
initgraph(&gdriver, &gmode, "c:\tc\bgi");
setcolor(LIGHTRED);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
outtextxy(240,200,"Developed by :");
outtextxy(240,250,"WWW");
rectangle(210,170,420,300);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(200,400,"Press any key to continue.........");
getch();
cleardevice();


showmouseptr();
setbkcolor(9); //sets the backgroundcolour.
setcolor(YELLOW);
outtextxy(20,445,"CLICK ON THE BUTTON "IGCT"");
unpressed(290,430,"EXIT");
unpressed(x,y,"IGCT");

while(1)//infinite loop.
{

getmousepos();

if(x1 >= x && x1 <= x+50 && y1 >= y && y1 <= y+30 )//checks if the
mouse pointer is over the button.
{ //where
x,x+50,y,y+50 are ((x,y))top,(x,y)bottom)co-ordinates.
hidemouseptr();
erasebutton(x,y);
x=random(600);
y=random(400);
//checks whether count=1 so as to display the button on the new
position for only once.

setcolor(9);
outtextxy(380,445,comments[count-1]);//to erase previousely written
things
setcolor(YELLOW);
outtextxy(380,445,comments[count]);
count++;

if(count==9)
{
setcolor(9);
outtextxy(380,445,comments[8]);//to erase previousely written
things
setcolor(YELLOW);
outtextxy(380,445,comments[1]);
count=2;
}

unpressed(x,y,"IGCT");
showmouseptr();


}
if(x1>=290&&x1<=340&&y1>=430&&y1<=460 && click==1)//checks if the mouse
pointer is over the "EXIT" button.
{ //for click==1 refer
to mousepos.h.
hidemouseptr();
pressed(290,430,"EXIT");
showmouseptr();
sound(1200);
delay(100);
nosound();
while(click!=0)
getmousepos();
break;
}

}

closegraph();
restorecrtmode();
}

unpressed(int x,int y,char *string) //display's an unpressed button.

{

setfillstyle(SOLID_FILL,CYAN);//sets the fill pattern and colour.
bar(x, y, x+50, y+30);//creates a box and the box is automatically
filled with the chosen colour.
setcolor(LIGHTCYAN);
line(x,y,x+50,y);//creates a white line just over the box.
line(x,y,x,y+30);

setcolor(EGA_DARKGRAY);
line(x,y+30,x+50,y+30);//creates a dark line just below the box.
line(x+2,y+31,x+50,y+31);
line(x+50,y,x+50,y+30);
line(x+51,y,x+51,y+31);
setcolor(BLUE);
outtextxy(x+10,y+12,string);//displays text on the box.
return 0;
}


pressed(int x,int y,char *string)

{
setfillstyle(SOLID_FILL,CYAN);
bar(x, y, x+50, y+30);
setcolor(EGA_DARKGRAY);
line(x,y,x+50,y);
line(x-1,y-1,x+51,y-1);
line(x,y,x,y+30);
line(x-1,y-1,x-1,y+30);

setcolor(LIGHTCYAN);
line(x,y+30,x+50,y+30);
line(x-1,y+31,x+50,y+31);
line(x+50,y,x+50,y+30);
line(x+51,y-1,x+51,y+31);

setlinestyle(DOTTED_LINE,1,NORM_WIDTH);
setcolor(EGA_DARKGRAY);
rectangle(x+4,y+4,x+46,y+26);//creates a dotted rectangle over the
box.
setcolor(BLUE);
outtextxy(x+10,y+12,string);
return 0;
}

erasebutton(int x,int y)//erases button.

{


setfillstyle(SOLID_FILL,getbkcolor());//gets the background colour


bar(x, y-1, x+51, y+31); // and fills the box with the
SOLID_FILL pattern.
return 0;
}

showmouseptr( ) //shows the mouse-pointer.

{

i.x.ax = 1 ;

int86 ( 0x33, &i, &o ) ;
return 0;
}
hidemouseptr()//hides the mouse pointer.
{
i.x.ax=2;
int86(51,&i,&o);//note:-0x33(hex)÷51(dec).
return 0;
}

getmousepos() //finds the mouse-pointer position.

{


i.x.ax = 3 ;

int86 ( 0x33, &i, &o ) ;

click = o.x.bx ;// if the left click button is pressed then click=1.

x1 = o.x.cx ; //x co-ordinate.

y1 = o.x.dx ; //y co-ordinate.

return 0;
}

Decimal to Binary, Octal and HEX converter

#include
#include
#define MAX 79
#define ESC 27
void main()
{
char res,r;
int i,k=0,y;
long j=0,l=0,o=0;
textcolor(15);
for(i=0;i<5000;i++)
cprintf(" ");
while(1)
{
clrscr();
printf("Enter Any Number To Be Converted : ");
scanf("%ld",&j);
l=j;
o=l;
k=0;
if(j>2145555550)
{
textcolor(4);
gotoxy(15,5);
cprintf("The Number Is Greater Than Range");
textcolor(15);
getch();
goto end;
}
else
{
gotoxy(1,6);
printf("Binary Equivalent:-");
y=MAX;
while(j>0)
{
k=j%2;
j=j/2;
gotoxy(y,6);
y--;
printf("%d",k);
}
gotoxy(1,8);
printf("Hexadecimal Equivalent:- ");
y=MAX;
while(l>0)
{
k=l%16;
l=l/16;
gotoxy(y,8);
y--;
if(k<=9)
{
printf("%d",k);
}
if(k==10)
{
printf("A");
}
if(k==11)
{
printf("B");
}
if(k==12)
{
printf("C");
}
if(k==13)
{
printf("D");
}
if(k==14)
{
printf("E");
}
if(k==15)
{
printf("F");
}
}
gotoxy(1,10);
printf("Octal Equivalent:- ");
y=MAX;
while(o>0)
{
k=o%8;
o=o/8;
gotoxy(y,10);
y--;
printf("%d",k);
}
}
gotoxy(15,25);
printf("Press Any Key To Continue , ESC To Exit");
res=getch();
if(res==ESC)
{
break;
}
}
end:
textcolor(7);
for(i=0;i<5000;i++)
cprintf(" ");
clrscr();
}

Calendar Program

#include

int day(int m1,int y1)
{
int d;
if(m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12)
d=31;
else if(m1==4 || m1==6 || m1==9 || m1==11)
d=30;
else if((y1%100!=0 && y1%4==0) || y1%400==0)
d=29;
else
d=28;
return d;
}

void main()
{
long unsigned int t;
unsigned int y,y1,m,m1,d,da,i,j,k;
char
a[12][20]={"January","February","March","April","May","June","July","Augus
t","September","October","November","December"};
clrscr();
textcolor(CYAN);
cprintf("Enter the year: ");
scanf("%4u",&y);
if(y<0)
y=-y;
cprintf("
Enter the month: ");
scanf("%2u",&m);
if(m<=0 || m>=13)
m=1;
clrscr();
gotoxy(32,2);
cprintf("Calendar");
y1=0;
t=0;
while(y1
{
if((y1%100!=0 && y1%4==0) || y1%400==0)
t=t+366;
else
t=t+365;
y1++;
}
m1=1;
while(m1
{
d=day(m1,y);
t=t+d;
m1++;
}
d=t%7;
printf("

Year: '%u'",y);
printf("
Month: '%s'

",a[m-1]);

printf("%6s%6s%6s%6s%6s%6s%6s

","Sun","Mon","Tue","Wed","Thu","Fri","Sa
t");
textcolor(GREEN);
k=1;
for(i=1;i<=day(m,y);i++,k++)
{
if(i==1)
{
if(d==0)
{
for(j=1;j<7;j++,k++)
printf("%6s","");
}
else
{
for(j=1;j
printf("%6s","");
}
}
cprintf("%6d",i);
if(k%7==0)
printf("

");
}
gotoxy(27,22);
cprintf("www");

getch();
}

Calender Program in C

#include
#include
#include
#include

unsigned long days=0;
//stores the days elapsed since 01.01.1899
void display(int n)
//contains the number of days to display
{
int i, column, k, flag=0, j;
printf("Sun Mon Tues Wed Thur Fri Sat
");
for(i=1; i<=n; i++)
{
k=days%7;//remainder gives the starting day of each month
if(flag==0)
{
for(j=1; j<=k; j++)//controls tabs of first week
printf(" ");
flag=1;//ensures that block is only executed once
column=k;
}
printf("%d ", i);
column++;
if(column%7==0)//prints new line at the end of each week
printf(" ");
}
printf(" Press any key to continue");
getch();
}

void calculate(int year)
//function calculates no. of days elapsed since 1899
{
int i, month;
for(i=1899; i
//1899 chosen because Jan 1, 1899 is a Sunday
{
if((i%400==0)?1:((i%100==0)?0:((i%4==0)?1:0)))

days+=366;
else
days+=365;
}
for(month=1; month<=12; month++)
{
printf("


");
switch(month)
/*switch case used to display each month and
increment no. of days*/
{
case 1: printf(" JANUARAY %d", year);
display(31);
days+=31;
break;
case 2: printf(" FEBURARY %d", year);
if((year%400==0)?1:((year%100==0)?0:((year%4==0)?1:0)))
{
display(29);
days+=29;
}
else
{
display(28);
days+=28;
}
break;
case 3: printf(" MARCH %d", year);
display(31);
days+=31;
break;
case 4: printf(" APRIL %d", year);
display(30);
days+=30;
break;
case 5: printf(" MAY %d", year);
display(31);
days+=31;
break;
case 6: printf(" JUNE %d", year);
display(30);
days+=30;
break;
case 7: printf(" JULY %d", year);
display(31);
days+=31;
break;
case 8: printf(" AUGUST %d", year);
display(31);
days+=31;
break;
case 9: printf(" SEPTEMBER %d", year);
display(30);
days+=30;
break;
case 10: printf(" OCTOBER %d", year);
display(31);
days+=31;
break;
case 11: printf(" NOVEMBER %d", year);
display(30);
days+=30;
break;
case 12: printf(" DECEMBER %d", year);
display(31);
days+=31;
break;
}
}
}

void main()
{
char ch[10];
int i, year, choice;
do
{
clrscr();
days=0;
printf("Enter the year in 'yyyy' format:
");
scanf("%s", ch);//stores input first as a string
for(i=0; i
if(ch[i]<'0' || ch[i]>'9')//checks for invalid inputs
{
printf("

Invalid Year!");
printf("

END OF PROGRAM");
getch();
exit(0);
}
year = atoi(ch);
//converts the year from string to integer datatype
clrscr();
printf("

Calender for Year %d", year);
printf("
**********************

");
calculate(year);
//calls function to calculate no. of days elapsed

printf("
*******************************************
");
printf("

Press 1 to continue, 2 to exit
");
scanf("%d", &choice);
}while(choice==1);
clrscr();
printf("

END OF PROGRAM");
getch();
}

Calendar of Thousands of Years

#include
#include
static char *months[]={"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
void main()
{
static int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
long int ndays,ldays,tydays,tdays;
int d,i,m,fday,y;
char ch;
textcolor(LIGHTGREEN);
textbackground(LIGHTBLUE);
clrscr();
printf("Please Enter year(1900 onwards) & month(number):");
scanf("%d%d",&y,&m);
while(1)
{
ndays=(y-1)*365l;
ldays=(y-1)/4-(y-1)/100+(y-1)/400;
tdays=ndays+ldays;//total days
//check for the leap year
if((y%100==0 && y%400==0)||(y%4==0 && y%100!=0))
days[1]=29;
else
days[1]=28;
d=days[m-1];
tydays=0;
for(i=0;i<=m-2;i++)
tydays=tydays+days[i];
tdays=tydays+tdays;
fday=tdays%7;
cal(y,m,fday,d);
ch=getche();
switch(ch)
{
case 77:
if(m==12)
{
y++;
m=1;
}
else
m++;
break;
case 72:
y++;
continue;
case 75:
if(m==1)
{
y--;
m=12;
}
else
m--;
break;
case 80:
y--;
continue;
case 27:
exit(0);
}}}
cal(int yr,int mo,int fd,int da)
{
int i,r,c;
char a;
clrscr();
gotoxy(25,2);
printf("%s %d",months[mo-1],yr);
textcolor(LIGHTGREEN);
gotoxy(5,5);
printf("____________________________________________________");
gotoxy(10,6);
printf("MON TUE WED THU FRI SAT SUN");
gotoxy(5,7);
printf("____________________________________________________");
r=9;
c=11+(6*fd);
for(i=1;i<=da;i++)
{
gotoxy(c,r);
if(c==47)
textcolor(RED);
else
textcolor(LIGHTGREEN);
cprintf("%d",i);
if(c<=41)
c=c+6;
else
{
c=11;
r=r+1;
}
}
textcolor(LIGHTGREEN);
gotoxy(5,15);
printf("____________________________________________________");
gotoxy(11,17);
printf("UP-Next Year DOWN-Prev Year");
gotoxy(11,18);
printf("RIGHT-Next Month LEFT-Prev Month");
gotoxy(27,20);
printf("Esc-Exit");
return 0;
}

system programming in C

// understanding IVT table
#include
#include
main()
{
unsigned long far *address = (unsigned long far *)0x00000000;
unsigned long intadd[256];
unsigned int segment, offset;
int i;
FILE *fp;
fp = fopen("IVT.txt", "wb");
for(i = 0; i <>
{
intadd[i] = *(address++);
segment = FP_SEG(intadd[i]);
offset = FP_OFF(intadd[i]);
fprintf(fp, "interrupt %3X : vector %Fp(hex) : %lu(dec)",i, intadd[i], (unsigned long)segment * 16 + offset);
}
fclose(fp);
}

Convert decimal to binary

#include

void dec_bin(int number);

int main(void)
{
int input = 0;

printf("Digit (0-255): ");
scanf("%d", &input);

(input >= 0) && (input < 256) ? dec_bin(input) : exit(1);

return 0;
}

void dec_bin(int number)
{
int x, y;
x = y = 0;

for(y = 7; y >= 0; y--)
{
x = number / (1 << y);
number = number - x * (1 << y);
printf("%d", x);
}

printf("\n");
}

program to find out total marks & Percentage of three subjects

void main ()

{

int m1,m2,m3;

float tm,per;

clrscr ();

printf ("Enter M1: ");

scanf ("%d",&m1);

printf ("Enter M2: ");

scanf ("%d",&m2);

printf ("Enter M3: ");

scanf ("%d",&m3);

tm=m1+m2+m3;

per=(tm/300*100);

printf ("\nTotal Marks are %.2f",tm);

printf ("\nPercentage is %.2f",per);

getch ();

}

Program to SWAP the three digit number

void main ()

{

int b,r,n,r1,r2;

clrscr ();

printf ("Enter the Value: ");

scanf ("%d",&n);

r=n%10;

n=n/10;

r1=n%10;

n=n/10;

r2=n%10;

n=n/10;

b=(r*100)*(r2*10)+(r2);

printf ("%d%d%d",r,r1,r2);

getch ();

}

Program To find sum of five elements of an array

void main ()

{

int no[5],i,sum;

clrscr ();

for (i=0;i<=4;i++)

{

printf ("Enter Element: ");

scanf ("%d",&no[i]);

}

sum=no[0]+no[1]+no[2]+no[3]+no[4];

printf ("\nSum of five Elements: %d",sum);

getch ();

}

A bubblesort routine

# include
# include
void bubblesort(int array[],int size);
void main()
{
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);
}
void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}

This program calculates an average of the numbers entered

#include
int Average(int i);

int main()
{
int num;
do
{
printf("Enter numbers.\n");
scanf("%d",&num);
if(num != -1)
printf("The average is %d", Average(num));
printf("\n");

}while(num>-1);

return 0;
}

int Average(int i)
{
static int sum = 0, count = 0;
sum = sum + i;
count++;
return sum / count;
}

Basic example showing constants usage in C

#include
/*constants for bonus rates and sales*/
#define BONUSRATE1 0.1
#define BONUSRATE2 0.15
#define BONUSRATE3 0.2
#define SALES1 2000
#define SALES2 5000
#define SALES3 10000
int main()
{
int sales;
double commission;
/*get employees sales*/
printf("Please enter your total sales to the nearest dollar.\n");
scanf("%d", &sales);
/*calculate employees bonus based on info*/
if(sales <=2000)
{
commission = sales * BONUSRATE1;
printf("%g\n" , commission);
}
else if(sales > 2000 && sales <=5000)
{
commission = sales * BONUSRATE2;
printf("%g\n" , commission);
}
else
{
commission = sales * BONUSRATE3;
printf("%g\n" , commission);
}

return 0;
}

A simple example showing some comparison operators

#include
int main()
{
int number1 , number2;
printf("Enter the number1 number to compare.\n");
scanf("%d",&number1);
printf("Enter the number2 number to compare.\n");
scanf("%d",&number2);
printf("number1 > number2 has the value %d\n", number1 > number2);
printf("number1 < number2 has the value %d\n", number1 < number2);
printf("number1 == number2 has the value %d\n", number1 == number2);
return 0;
}

count occurrences of values in an array.

#include
void print_arr(int grades[], int elements);
int count_passes(int grades[], int elements,int value);

int main(void)
{
int grades[10] = {70,80,95,65,35,85,54,78,45,68};
int result;
print_arr(grades,10);
result = count_passes(grades,10,70);
if(result == 1)
printf("There was %d pass.\n",result);
else
printf("There were %d passes.\n",result);
return 0;
}

void print_arr(int grades[], int elements)
{
int i;

for(i = 0;i < elements;i++)
{
printf("%d ",grades[i]);
}
printf("\n");
}

int count_passes(int grades[], int elements,int value)
{
int i ,passes = 0 ;
for(i = 0;i < elements;i++)
{
if(grades[i] >= value)
passes++;
}
return(passes);
}

Program to Open a file, read a file and write to a file

#include
int main()
{
float sales , commission;
FILE *fin, *fout;
fin = fopen("c:\\pop.dat","r");
fout = fopen("c:\\pop2.dat","w");
while (fscanf(fin,"%f",&sales) != EOF)
{
fprintf(fout,"Your sales for the year were %8.2f \n",sales);
if(sales < 30000)
commission = sales / 100 * 5;
else
commission = sales / 100 * 10;
fprintf(fout,"Your commission is %8.2f",commission);
}
return 0;
}

Program to find whether a number is odd or even

#include
int main()
{
int number ;
printf("Enter a whole number\n");
scanf("%d",&number);
if(number % 2 == 0)
printf("number is even.\n");
else
printf("number is odd.\n");

return 0;
}

calculate the power in watts

#include
int main()
{
float power,voltage,current;
voltage = current = 0;

printf("Power calculator.\n");
printf("This will calculate the power in watts , ");
printf("when you input the voltage and current.");
/*get the voltage*/
printf("Enter the voltage in volts.\n");
scanf("%f",&voltage);
/*get the current*/
printf("Enter the current in amps.\n");
scanf("%f",¤t);
/*calculate the power*/
power = voltage * current;
printf("The power in watts is %.2f watts\n",power);

return 0;
}

Ohms law example In C

#include
#include
#include
int main()
{
char ch;
float voltage , current , resistance , result;
printf("Ohms law calculator.\n");
printf("Please choose from following calculcations.\n");
printf("1. choose 1 to calculate the voltage.\n");
printf("2. choose 2 to calculate the current.\n");
printf("3. choose 3 to calculate the resistance.\n");
printf("Anything else to quit.\n");
scanf("%c",&ch);
switch(ch)
{
case '1' :
printf("please enter the current in amps.\n");
scanf("%f",¤t);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = current * resistance;
printf("The voltage is %0.2f volts.\n",result);
break;
case '2' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = voltage / resistance;
printf("The current is %0.2f amps.\n",result);
break;
case '3' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the current in amps.\n");
scanf("%f",¤t);
result = voltage / current;
printf("The resistance is %0.2f ohms.\n",result);
break;
default :
exit(0);
break;
}
return 0;
}

Search an array

#include
void print_arr(int myArray[], int elements);
int search_arr(int myArray[], int elements, int number);

int main(void)
{
int myArray[10] = {12,23,56,35,18,65,12,87,73,9};
int result,number;
print_arr(myArray,10);
number = 65;
result = search_arr(myArray,10,number);
if(result == -1)
printf("%d was not found.\n",number);
else
printf("Found %d\n",result);
return 0;
}

void print_arr(int myArray[], int elements)
{
int i;

for(i = 0;i < elements;i++)
{
printf("%d ",myArray[i]);
}
printf("\n");
}

int search_arr(int myArray[], int elements, int number)
{
int i;
for(i = 0;i < elements;i++)
{
if(myArray[i] == number)
return(number);
}
return(-1);
}

program to calculate sum all of the elments in an array

#include
void print_arr(int myArray[], int elements);
int sum_arr(int myArray[], int elements);

int main(void)
{
int myArray[5] = {78,34,25,98,12 };
int sum;
printf("Array info: ");
print_arr(myArray,5);
sum = sum_arr(myArray,5);
printf("The sum of the array is : %d\n",sum);
return 0;
}

void print_arr(int myArray[], int elements)
{
int i;
for(i = 0;i < elements;i++)
{
printf("%d ",myArray[i]);
}
printf("\n");
}

int sum_arr(int myArray[], int elements)
{
int i, sum = 0;
for(i = 0;i < elements;i++)
{
sum = sum + myArray[i];
}
return(sum);
}

Square Root of a number by using simple calculations

#include
#include
main()
{
float a,b,e=0.00001,p,k;
clrscr();
textcolor(GREEN);
do {
printf("*******************************************************");
printf(" PROGRAM TO FIND SQUARE ROOT OF A NUMBERxDB
");
printf("*********************************************************");
cprintf("ENTER A NUMBER(-1 to Quit) :");
scanf("%f",&k);

a=k;p=a*a;
while(p-k>=e)
{
b=(a+(k/a))/2;
a=b;
p=a*a;
}
printf("SQUARE ROOT IS = %f",a);
getch();
clrscr();
}
while(k!=-1);
getch();
}

Program for conversion of Decimal to Roman Number

#include

main()
{
int a,b,c,d,e;
clrscr();
printf("Input a number (between 1-3000):");
scanf("%d",&e);
while (e==0||e>3000)
{
printf ("ERROR: Invalid Input!");
printf ("Enter the number again:");
scanf ("%d",&e);
}
if (e>3000)
printf("Invalid");
a = (e/1000)*1000;
b = ((e/100)%10)*100;
c = ((e/10)%10)*10;
d = ((e/1)%10)*1;

if (a ==1000)
printf("M");
else if (a ==2000)
printf("MM");
else if (a ==3000)
printf("MMM");

if (b == 100)
printf("C");
else if (b == 200)
printf("CC");
else if (b == 300)
printf("CCC");
else if (b == 400)
printf("CD");
else if (b ==500)
printf("D");
else if (b == 600)
printf("DC");
else if (b == 700)
printf("DCC");
else if (b ==800)
printf("DCCC");
else if (b == 900)
printf("CM");


if (c == 10)
printf("X");
else if (c == 20)
printf("XX");
else if (c == 30)
printf("XXX");
else if (c == 40)
printf("XL");
else if (c ==50)
printf("L");
else if (c == 60)
printf("LX");
else if (c == 70)
printf("LXX");
else if (c ==80)
printf("LXXX");
else if (c == 90)
printf("XC");

if (d == 1)
printf("I");
else if (d == 2)
printf("II");
else if (d == 3)
printf("III");
else if (d == 4)
printf("IV");
else if (d ==5)
printf("V");
else if (d == 6)
printf("VI");
else if (d == 7)
printf("VII");
else if (d ==8)
printf("VIII");
else if (d == 9)
printf("IX");
getch();
}

Progam that gives length of side of a Triangle

//Progam that gives all details of a Triangle given the lengths of its sides
#include
#include
#include
#include

main()
{
clrscr();
float a,b,c,S,D,A,B,C,Area,R;
printf("Enter the lengths of the three sides of the triangle :");
scanf("%f%f%f",&a,&b,&c);

S = (a+b+c)/2.0; // S is the semiperimeter of the triangle
D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
if(D<=0)
{
printf("The triangle cannot be formed");
getch();
exit(0);
}

if((a==b || b==c || c==a) && !(a==b && b==c && c==a))
// this complex logic is to eliminate interpretting a triangle with all
three
// sides equal as both isosceles and equilateral.
printf("The triangle is ISOSCELES");
if(a==b && b==c && c==a)
printf("The triangle is EQUILATERAL Type");
if(a!=b && b!=c && c!=a)
printf("The triangle is SCALENE");

Area = sqrt(D);
R = (a*b*c)/(4.0*Area);
printf("PERIMETER = %.2f units",(2.0*S));
printf("AREA = %.2f sq.units",Area);
printf("CIRCUM RADIUS = %.2f units",R);
// using sine rule,we get...
A = (180.0/3.1415926)*asin(a/(2.0*R));// value of pi should be upto 7
B = (180.0/3.1415926)*asin(b/(2.0*R));// decimal places of accuracy and
also
C = (180.0/3.1415926)*asin(c/(2.0*R));// note that the 7th decimal place
// 6 and not 7 as it had to be if were
if(A==90.0 || B==90.0 || C==90.0)
// approximated to 7 decimalplaces
printf("The triangle is RIGHT ANGLED");
if(A<90.0 && B<90.0 && C<90.0)
printf("The triangle is ACUTE ANGLED");
if(A>90.0 || B>90.0 || C>90.0)
printf("The triangle is OBTUSE ANGLED");

printf("The angles are as follows :");
printf("A = %.2f degrees",A);
printf("B = %.2f degrees",B);
printf("C = %.2f degrees",C);
printf("Where A,B,C stand for angles opposite to sides%.2f,%.2f,%.2f",a,b,c);
printf(" respectively");


getch();
return 0;
}

Program to construct a pyramid of any input numbers

main()
{
int n,row=1,col=40,i=0,j,k=0,count=1;
int a[10];
clrscr();
i=n-1;
printf("Pyramid of how many numbers? ");
scanf("%d",&n);
for (j=0;j\<=n;j++)
{
printf("Enter no.:- ");
scanf("%d",&a[j]);
}
clrscr();
for (row=n;row>=1;row--)
{
k=0;
k=40-(4*(row-1));
i=row-1;
for (col=40;col>=k;col=(col-4))
{
gotoxy(col,row);
printf("%d",a[i]);
--i;
}
}

for (count=n;count>=1;count--)
{
k=0;
k=40+(4*(count-1));
i=count-1;
for (col=40;col<=k;col=(col+4))
{
gotoxy(col,count);
printf("%d",a[i]);
--i;
}
}
getch();
}

Program to compute difference between two dates

#include "stdio.h"
#include "math.h"
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter first day, month, year");
scanf("%d%d%d",&day1,&mon1,&year1);
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2ref = year2;
dd1=0;
dd1=func1(mon1);
for(i=ref;i{
if(i%4==0)
dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
printf("No. of days of first date fronm the Jan 1 %d= %d",year1,dd1);
/* Count for additional days due to leap years*/
dd2=0;
for(i=ref;i{
if(i%4==0)
dd2+=1;
}
dd2=func1(mon2)+dd2+day2+((year2-ref)*365);
printf("No. of days from the reference year's first Jan = %d",dd2);
printf("Therefore, diff between the two dates is %d",abs(dd2-dd1));

getch();
}




int func1(x) //x for month y for dd
{ int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120;break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: printf("Error encountered"); exit(1);
}
return(y);
}

Program for rotating circles using maths Function

#include "stdio.h"
#include "graphics.h>"
#include "conio.h>"
#include "dos.h"
#include "stdlib.h"
#include "math.h"
#include "iostream.h"
main()
{
int gd=DETECT,gm,x=295,y=222,a,j,i;
initgraph(&gd,&gm,"c:\tc\bgi");
setcolor(14);
outtextxy(x-10,y,"POP");
while(!kbhit())
{
i++;

delay(1);
setcolor(16);
circle(x+(200*cos(i)),y+(200*sin(i)),8);
setcolor(10);
circle(x+(200*cos(i+40)),y+(200*sin(i+40)),8);
//set2
setcolor(16);
circle(x+(160*sin(i)),y+(160*cos(i)),7); //anti clockwise:-sin,cos
interchanged
circle(x+(160*sin(i)),y+(160*cos(i)),5);
setcolor(i);
circle(x+(160*sin(i+40)),y+(160*cos(i+40)),7);
circle(x+(160*sin(i+40)),y+(160*cos(i+40)),5);

setcolor(16);
circle(x+(120*sin(i)),y+(120*cos(i)),6);//anti clockwise:-sin,cos
interchanged
setcolor(12);
circle(x+(120*sin(i+40)),y+(120*cos(i+40)),6);

setcolor(16);
circle(x+(90*cos(i)),y+(90*sin(i)),5);
setcolor(i);
circle(x+(90*cos(i+40)),y+(90*sin(i+40)),5);
}
getch();
return 0;
}

Print a double pyramid

void main(void)
{
clrscr();
int i,j,k,l,b,n;
printf("Enter the value of N:");
scanf("%d",&n);
for(i=0;i{
printf("");
for(l=0;l printf(" ");
for(j=i+1;j<=n;j++)
printf("%d",j);
for(k=n-1;k>i;k--)
printf("%d",k);
}
b=n-1;
for(i=0;i{
printf("");
for(l=n-2;l>i;l--)
printf(" ");
for(j=b;j<=n;j++)
printf("%d",j);
for(k=n-1;k>=b;k--)
printf("%d",k);
b--;
}
getch();
}

Factorial Function In C

#include "stdio.h"
#include "conio.h"
long int factorial(int n);
void main()
{
int n,i;
float s,r;
char c;
clrscr();
repeat : printf("You have this series:- 1/1! + 2/2! + 3/3! + 4/4!");
printf("To which term you want its sum? ");
scanf("%d",&n);
s=0;
for (i=1;i<=n;i++)
{
s=s+((float)i/(float)factorial(i));
}
printf("The sum of %d terms is %f",n,s);
fflush(stdin);
printf ("Do you want to continue?(y/n):- ");
scanf("%c",&c);
if (c=='y')
goto repeat;
getch();
}

long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}

ARRANGE THE ELEMENTS IN ARRAY IN DESSENDING ORDER

main()
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{
for(j=i+1;j{
if(a[i]{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}

SEARCHING OF THE ELEMENTS OF BINARY NOUMBER

main()
{
int a[100],i,n,j,search,mid,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{
for(j=i+1;j{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
printf("\n enter search element");
scanf("%d",&search);
mid=n/2;
if(a[mid]==search)
{
printf("\n location of searched element %d ",mid);
}
if(search>a[mid])
{
for(i=mid+1;i{
if(a[i]==search)
{
printf("\n location of sarched element is %d",i);
}
else
for(i=0;i{
if(a[i]==search)
printf("\n location of searched element %d",i+1);
}xc
getch();
}
}
}

PROGRAM TO ARRANGE THE ELEMENTS IN ARRAY IN ASSENDING ORDER

/*ARRANGE THE ELEMENTS IN ARRAY IN ASSENDING ORDER*/
#include "stdio.h"
#include "conio.h"
main()
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{
for(j=i+1;j{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}

FIND THE SUM OF DIGIT THREE Numbers

/* FIND THE SUM OF DIGIT THREE NO'S*/
#include "math.h"
main()
{
int d,d1,d2,d3,r1,r2,sum;
clrscr();
printf("\n enter any three digit no's");
scanf("%d",&d);
d1=d/100;
r1=d%100;
if(r1!=0)
{
d2=r1/10;
r2=r1%10;
if(r2!=0)
d3=r2;
else
d3=0;
}
else
d2=0;
d3=0;
}
sum=d1+d2+d3;
printf("\n sum of 3 digit no is %d",sum);
getch();
}
#include "stdio.h"
#include "conio.h"
main()
{
int a[3][3],b[3][3],i,j;
clrscr();
printf("\n Enter the value of n = ");
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
scanf("%d",&a[i][j]);
}
printf("\n =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
printf("\n THE MATRIX OF A\n");
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
printf("%3d",a[i][j]);
printf("\n\n");
}
printf("\n THE TRANSPOSE OF MATRIX B\n");
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
b[i][j]=a[j][i];
printf("%3d",b[i][j]);
}
printf("\n =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
}
getch();
}


*****************************************************************

Output :

Enter the value of n = 1 2 3 4 5 6 7 8 9

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
THE MATRIX OF A
1 2 3

4 5 6

7 8 9


THE TRANSPOSE OF MATRIX B
1 4 7
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
2 5 8
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
3 6 9
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Matrix Multiplication

void main()
{
int row1=0,
col1=1,
row2=0,
col2=0,
**matrix1,
**matrix2,
**result;

clrscr();
printf(" Enter number of row for first matrix ");
scanf("%d",&row1);

while (col1!=row2)
{
printf(" Enter number of column for first matrix ");
scanf("%d",&col1);

printf(" Enter number of row for second matrix ");
scanf("%d",&row2);

if (col1!=row2)
{
clrscr();
printf("Column number of first matrix must be same as the row number of second matrix");
}


}


printf(" Enter number of column for second matrix ");
scanf("%d",&col2);

matrix1=init(matrix1,row1,col1);
matrix2=init(matrix2,row2,col2);
/* setting values in matrix */
printf("First matrix \n");
set(matrix1,row1,col1);
printf("Second matrix \n");
set(matrix2,row2,col2);
/* printint matrix */
clrscr();
printf(" [ First matrix ]\n");
get(matrix1,row1,col1);
printf(" [ Second matrix ]\n");
get(matrix2,row2,col2);

printf(" [ Multiplication Result ]\n");
result=mul(matrix1,matrix2,row1,col2,col1);
get(result,row1,col2);
printf("\n\t\t Thanks from debmalya jash");
getch();
free(matrix1);
free(matrix2);
fress(result);


} /* end main */


/* to initialize matrix */
int** init(int** arr,int row,int col)
{
int i=0,
j=0;

arr=(int**)malloc(sizeof(int)*row*col);

for(i=0;i {
for(j=0;j {
*((arr+i)+j)=(int*)malloc(sizeof(int));
*(*(arr+i)+j)=0;
}
}
return arr;
}

/* to set value in matrix */
int** set(int** arr,int row,int col)
{
int i=0,
j=0,
val=0;

for(i=0;i {
for(j=0;j {
printf("Enter value for row %d col %d :",(i+1),(j+1));
scanf("%d",&val);
*(*(arr+i)+j)=val;
}
}
return arr;
}


/* print values of the passed matrix */
void get(int** arr,int row,int col)
{
int i=0,
j=0;

for(i=0;i {
for(j=0;j {
printf("%d\t",*(*(arr+i)+j));
}
printf("\n");
}
}

/* mutiply two matrices and return the resultant matrix */
int** mul(int** arr1,int** arr2,int row,int col,int col1)
{
int **result,
i=0,
j=0,
k=0;

result=init(result,row,col);

for(i=0;i {
for(j=0;j {
for(k=0;k {
printf("%dX%d(%d)",*(*(arr1+i)+k),*(*(arr2+k)+j),(*(*(arr1+i)+k))*(*(*(arr2+k)+j)));
*(*(result+i)+j)+=(*(*(arr1+i)+k))*(*(*(arr2+k)+j));

if (k!=(col1-1))
printf("+");
}
printf("\t");
}
printf("\n");
}
return result;