C program for Binary search operations

C program for Binary search operations


#define MAX_LEN 10

/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}

/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}

void read_list(int l[],int n)
{
int i;
printf("\nEnter the elements:\n");
for(i=0;i scanf("%d",&l[i]);
}

void print_list(int l[],int n)
{
int i;
for(i=0;i printf("%d\t",l[i]);
}

/*main function*/
void main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;

clrscr();
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);

if(ch<=2 & ch>0)
{
printf("\nEnter the number of elements : ");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);


switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
getch();
break;

case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}


Related Links :

C Program to print Pascal Traingle

C Program to print Pascal Traingle


void main()
{
int num,x,y,a[50][50];
clrscr();
fflush(stdin);

printf("Enter the number of rows: ");
scanf("%d",&num);

printf("\n\t\t Pascal's Triangle of Order %d\n\n",num);

for(x=0;x{
for(y=0;y<=x;y++)
{
if(x==y || y==0)
a[x][y]=1;
else
a[x][y]=a[x-1][y-1]+a[x-1][y];
printf("%4d",a[x][y]);
}
printf("\n\n");
}

getch();
}



Related Links :

VHDL multiplexer using case statements

VHDL multiplexer using case statements


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity multiplexer4_1 is
port (
      i0 : in std_logic;
      i1 : in std_logic;
      i2 : in std_logic;
      i3 : in std_logic;
     sel : in std_logic_vector(1 downto 0);
     bitout : out std_logic
     );
end multiplexer4_1;

architecture Behavioral of multiplexer4_1 is
begin

process(i0,i1,i2,i3,sel)
begin
case sel is
  when "00" => bitout <= i0;
  when "01" => bitout <= i1;
  when "10" => bitout <= i2;
  when others => bitout <= i3;
end case;
end process;

end Behavioral;

The testbench code used for testing the code is given below:

  LIBRARY ieee;
  USE ieee.std_logic_1164.ALL;

  ENTITY testbench IS
  END testbench;

  ARCHITECTURE behavior OF testbench IS
          SIGNAL i0,i1,i2,i3,bitout :  std_logic:='0';
          SIGNAL sel :  std_logic_vector(1 downto 0):="00";
  BEGIN
    UUT : entity work.multiplexer4_1 port map(i0,i1,i2,i3,sel,bitout);

     tb : PROCESS
     BEGIN
            i0<='1';
            i1<='0';
            i2<='1';
            i3<='0';
            sel <="00";
            wait for 2 ns;
            sel <="01";
            wait for 2 ns;
            sel <="10";
             wait for 2 ns;
             sel <="11";
              wait for 2 ns;
            --more input combinations can be given here.
     END PROCESS tb;

  END;


Related Links :

Railway Reservation System Project In C

Railway Reservation System Project In C


#include conio.h
#include stdio.h
#include graphics.h
#include stdlib.h
#include dos.h
#include string.h
#include math.h

/***************Global Variable Declarations****************/

union REGS i,o;
int xmax,ymax;
int gdriver = DETECT, gmode, errorcode,x,y;
int count=0,no_pass=0;
FILE *ptr1;
char d[8][3]={{"S1"},{"S2"},{"S3"},{"S4"},{"S5"},{"S6"},{"S7"},{"S8"}};
char x1[2];

/****************************Global Structures declared**********/

struct travel
{
char tno[5];
//char doj[10];
int d1,m1,y1;
char from[4];
char to[4];
char passen[6][20];
char sex[6][2];
char age[6][3];
int seat_no[6];
char coano[3];
   // float pnr;
long int pnr;
float bill;
}passenger;
struct
{
// float pnr;
long int pnr;
}pass;
struct
{
int seat_no;
char coano[3];
char status[1];
}berth;

struct
{
char tno[5];
char tname[20];
}train;


/***********************User Defined Functions***********/

void firstscreen();
void mainmenu();
void reserv();
void clear();
void setgraph();
void clearline();
void drawbox();
void cancel();
void enquiry();
void color(int,int,int);
void printtic(struct travel);

/*****************Main Function*****************************/
void main()
{
int i;
FILE *ptr,*tr;
/***********loop for writing train nos***************/
if((tr=fopen("tra.dat","r"))==NULL)
{
tr=fopen("tra.dat","w");
strcpy(train.tno,"3143");
strcpy(train.tname,"Darjeeling Mail");
fwrite(&train,sizeof(train),1,tr);
strcpy(train.tno,"3147");
strcpy(train.tname,"Uttarbanga Exp");
fwrite(&train,sizeof(train),1,tr);
fclose(tr);
}

/************loop for writing train berth nos*********/
if((ptr=fopen("berths3.dat","r"))==NULL)
{
   ptr=fopen("berths3.dat","wb");
   strcpy(berth.coano,d[0]);
   strcpy(berth.status,"V");
   for(i=1;i<=73;i++)
   {
  if(i>72)
  {
count++;
strcpy(berth.coano,d[count]);
i=1;
  }
  if(count==8)
  break;
  berth.seat_no=i;
  fwrite(&berth,sizeof(berth),1,ptr);

   }
}
fclose(ptr);
if((ptr=fopen("berths7.dat","r"))==NULL)
{
   ptr=fopen("berths7.dat","wb");
   strcpy(berth.coano,d[0]);
   strcpy(berth.status,"V");
   for(i=1;i<=73;i++)
   {
  if(i>72)
  {
count++;
strcpy(berth.coano,d[count]);
i=1;
  }
  if(count==8)
  break;
  berth.seat_no=i;
  fwrite(&berth,sizeof(berth),1,ptr);

   }
     }
     fclose(ptr);
textcolor(WHITE);
textbackground(BLACK);
clrscr();
firstscreen();
mainmenu();
reserv();
cancel();

getch();
}
void firstscreen()
{
int r,c;
for(r=4;r<=20;r++)
{
gotoxy(20,r);
printf("*");
gotoxy(60,r);
printf("*");
}
for(c=20;c<60;c++)
{
gotoxy(c,4);
printf("*");
gotoxy(c,20);
printf("*");
}

gotoxy(36,6);
textattr(LIGHTRED);
cprintf("PROJECT");

gotoxy(39,8);
textcolor(BLUE);
cprintf("ON");

gotoxy(32,10);
textcolor(BROWN);
cprintf("Railway Reservation");

gotoxy(25,13);
textcolor(YELLOW);
putch(1);

textcolor(CYAN);
cprintf(" DEVELOPERS : ");


gotoxy(25,17);
textcolor(YELLOW);
putch(2);

textcolor(CYAN);
cprintf("RRP");
textcolor(CYAN);
cprintf("popsys");
gotoxy(28,23); textcolor(MAGENTA + BLINK);
cprintf("Press a key to continue");
getch();
}

void mainmenu()
{
char choice;
setgraph();
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 5);
outtextxy(xmax,45,"Railway Reservation");
/* draw a rectangle */
rectangle(100,150,500,350);
gotoxy(17,12);
printf("1. For reservation press  1
");
gotoxy(17,14);
printf("2. For cancellation press 2
");
gotoxy(17,16);
printf("3. For enquiry press 3
");
gotoxy(17,18);
printf("4. For closing the program press 4
");
gotoxy(48,25);
printf("Created by SOFTECH SOLUTIONS Ltd.");
gotoxy(20,20);
printf("Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
reserv();
break;
case 2:
closegraph();
clrscr();
cancel();
break;
case 3:
closegraph();
enquiry();
break;
case 4:
exit(1);
default:
gotoxy(29,20);
printf("Not a valid choice");
getch();
main();
}
}
int pass_name(int i,int x, int y)
{
int flag,pos;
do
{
flag=1;
gotoxy(x,y);
fflush(stdin);
gets(passenger.passen[i]);
fflush(stdin);
strupr(passenger.passen[i]);
if (strlen(passenger.passen[i]) == 0)
{
if(i>0)
{
return(7);
}
gotoxy(10,24);
printf("aPassenger Name should not be left BLANK");
flag=0;
//i--;
getch();
clearline(24);
}
else if (strlen(passenger.passen[i]) >20)
{
//gotoxy(10,20);
gotoxy(10,24);
printf("aPassenger Name must be less than 20 characters");
flag=0;
getch();
clearline(24);
}
}while(flag==0);
gotoxy(x,y);
puts(passenger.passen[i]);
i++;
return(i);
}
int pass_sex(int j,int x,int y)
{
int flag,i;
char ch[1][1];
do
{
flag=1;
gotoxy(x,y);
fflush(stdin);
gets(passenger.sex[j]);
fflush(stdin);
strupr(passenger.sex[j]);
if (strlen(passenger.sex[j]) == 0)
{
gotoxy(10,24);
printf("aSex should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if(strlen(passenger.sex[j])!=1)
{
gotoxy(10,24);
printf("aPassenger's Sex must be 1 characters long");
flag=0;
getch();
clearline(24);
}
}while (flag==0);
gotoxy(x,y);
puts(passenger.sex[j]);
j++;
return(j);
}

int pass_age(int k,int x,int y)
{
int flag,i,j;
do
{
flag=1;
gotoxy(x,y);
if(flag==1)
no_pass++;
gets(passenger.age[k]);
fflush(stdin);
strupr(passenger.age[k]);
if (strlen(passenger.age[k]) == 0)
{
gotoxy(10,24);
printf("aPassenger's Age should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.age[k])!=2)
{
gotoxy(10,24);
printf("aPassenger's Age must be equal to 2 characters");
flag=0;
// k--;
getch();
clearline(24);
}
else if(strcmp(passenger.age[k],"00")==0)
{
gotoxy(10,24);
printf("aPassenger's Age cannot be less than 1 year");
flag=0;
getch();
clearline(24);
}
for(i=k;i
{
for(j=0;j<2;j++)
{
if(!isdigit(passenger.age[i][j]))
{
gotoxy(10,24);
printf("aEnter a valid age");
flag=0;
getch();
clearline(24);
}
}
}
}while (flag==0);
gotoxy(x,y);
k++;
puts(passenger.age[k]);
return(k);
}

showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
int restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);

i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}

void clear()
{
int i,j;
for(i=0;i<=80;i++)
{
for(j=0;j<=25;j++)
{
printf("%c",' ');
gotoxy(i,j);
}
}
gotoxy(1,1);
}
void setgraph()
{

clrscr();

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\tc\bgi");

/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
  printf("Graphics error: %s
", grapherrormsg(errorcode));
  printf("Press any key to halt:");
  getch();
  exit(1);
}

xmax = getmaxx()/2;
ymax = getmaxy()/2;
}
void clearline(int yco)
{
int i,j;
gotoxy(5,yco);
for(i=0;i<70;i++)
{
printf("%c",' ');
}
}
void drawbox()
{
int xr,xc;
int la,ra;
gotoxy(0,0);
for(xr=1;xr<=80;xr++)
printf("Ä");
xc=2;
for(la=1;la<=23;la++)
{
gotoxy(1,xc);
printf("³");
gotoxy(80,xc);
printf("³");
xc++;
}
gotoxy(0,25);
for(xr=1;xr<80;xr++)
printf("Ä");
gotoxy(1,1);
printf("Ú");
gotoxy(80,24);
printf("³");
}


void reserv()
{
int i,flag,j,k,test,found,set_no,count1=0,year;
struct date d;
float pnrno;
char tno[3],ans;
char a[1],c,age[3],ch;
char *p;
FILE *ptr,*pno,*tr;
closegraph();
highvideo();
//Design Part
clear();
drawbox();
gotoxy(3,2);
for(i=0;i<74;i++)
printf("Ä");

j=3;
for(i=0;i<4;i++)
{
gotoxy(3,j);
printf("³");

gotoxy(77,j);
printf("³");
j++;
}
gotoxy(3,6);
for(i=0;i<75;i++)
printf("Ä");
gotoxy(6,2);
printf(" Journey Details ");
color(15,18,3);
gotoxy(5,3);
printf("Train No : ");
color(32,46,3);
gotoxy(20,3);
printf("Train Name :");
color(67,68,3);
color(71,72,3);
color(75,76,3);
gotoxy(49,3);
printf("Date of Booking :");
gotoxy(67,3);
getdate(&d);
printf("%d",d.da_day);
gotoxy(69,3);
putch('-');
gotoxy(71,3);
printf("%d",d.da_mon);
putch(' -');
printf("%d",d.da_year);
color(12,14,5);
gotoxy(5,5);
printf("From :");
color(27,29,5);
gotoxy(20,5);
printf("To :");
color(67,68,5);
color(71,72,5);
color(75,76,5);
gotoxy(49,5);
printf("Date Of Journey :");
gotoxy(3,8);
for(i=0;i<=74;i++)
printf("Ä");
gotoxy(7,8);
printf("Passenger Details");
j=9;
for(i=0;i<14;i++)
{
gotoxy(3,j);
printf("³");

gotoxy(77,j);
printf("³");
j++;
}
gotoxy(3,23);
printf(" ");
gotoxy(3,23);
for(i=0;i<74;i++)
printf("Ä");
gotoxy(4,10);
for(i=0;i<73;i++)
printf("Ä");
color(8,31,11);
gotoxy(22,9);
printf("NAME");

gotoxy(46,9);
printf("SEX");
gotoxy(53,9);
printf("AGE");
gotoxy(65,9);
printf("REMARKS");
j=9;
for(i=0;i<14;i++)
{
gotoxy(44,j);
printf("³");

gotoxy(51,j);
printf("³");

gotoxy(58,j);
printf("³");
j++;
}

for(i=0;i<6;i++)
{
color(5,38,(11+i));
color(46,48,(11+i));
color(53,56,(11+i));
color(5,38,(16+i));
color(46,48,(16+i));
color(53,56,(16+i));
}

//User input part
gotoxy(15,3);
do
{
int len;
fflush(stdin);
//name input
gets(passenger.tno);
if(len=(strlen(passenger.tno))!=4)
{
gotoxy(10,24);
printf("Train No Must be 4 digits long");
getch();
clearline(24); //clears the line
gotoxy(15,3);
}
tr=fopen("tra.dat","r");
fread(&train,sizeof(train),1,tr);
while(!feof(tr))
{
if((strcmp(passenger.tno,train.tno))==0)
{
found=0;
flag=0;
break;
}
else
found=2;
fread(&train,sizeof(train),1,tr);
}
if(found==2)
{
gotoxy(8,24);
printf("This Train No does not exist");
getch();
clearline(24);
gotoxy(15,3);
flag=1;
}
}while(strlen(passenger.tno)!=4||flag==1);
if(found==0)
{
gotoxy(32,3);
puts(train.tname);
found=1;
}

fflush(stdin);
do
{
flag=1;
gotoxy(12,5);
fflush(stdin);
scanf("%[^
]",passenger.from);
fflush(stdin);
strupr(passenger.from);
if (strlen(passenger.from) == 0)
{
gotoxy(10,24);
printf("aFrom field should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.from) != 3)
{
gotoxy(10,24);
printf("aFrom should be equal to 3 characters");
flag=0;
getch();
clearline(24);
}
for(i=0;i<3;i++)
{
ch=passenger.from[i];
if(isdigit(ch))
{
gotoxy(10,24);
printf("aEnter a valid Boarding Place");
flag=0;
getch();
clearline(24);
break;
}
}
}while (flag==0);
gotoxy(12,5);
puts(passenger.from);

do
{
flag=1;
gotoxy(27,5);
fflush(stdin);
scanf("%[^
]",passenger.to);
fflush(stdin);
strupr(passenger.to);
if (strlen(passenger.to) == 0)
{
gotoxy(10,24);
printf("aTo field should not be left BLANK");
flag=0;
getch();
clearline(24);
}
else if (strlen(passenger.to) != 3)
{
gotoxy(10,24);
printf("aTo should be equal to 3 characters");
flag=0;
getch();
clearline(24);
}
for(i=0;i<3;i++)
{
ch=passenger.to[i];
if(isdigit(ch))
{
gotoxy(10,24);
printf("aEnter a valid Destination Place");
flag=0;
getch();
clearline(24);
break;
}
}
}while (flag==0);
gotoxy(27,5);
puts(passenger.to);
i=j=k=0;

gotoxy(67,5);
while(c!=13)
{
   fflush(stdin);
   c=getch();
   if((c<'0'||c>'9')&&c!=13&&c!=8)
   {
if(c==27)
 return;
gotoxy(5,24);
printf("Please Enter Valid Date.");
getch();
gotoxy(67,5);
printf("  ");
gotoxy(5,24);
clearline(24);
gotoxy(67,5);
count1=0;
c='a';
   }
   else if(c==8)
   {
  if(wherex()>69)
  {
 gotoxy(wherex()-1,15);
 putch(' ');
 count1--;
 gotoxy(wherex()-1,15);
  }
   }
   else if(c!=13)
   {
  if(wherex()<69)
  {
 putch(c);
 age[count1]=c;
 count1++;
  }
  else
  {
 gotoxy(5,24);
 printf("Date Can Be Only 2 Digits.");
 getch();
 clearline(24);
 gotoxy(67,5);
  }
   }
else
{
age[count1]='



Related Links :

C Program to read text File on Disk using FREAD function

C Program to read text File on Disk using FREAD function 


Its Basic C Program to read any text from disk using FRead Function which belongs to stdio.h, using this function with file pointers you can read the TEXT contents of any file. 

#include 
#include 

int main()
{
    int cur_char;
    FILE *out_file;

    out_file = fopen("test.txt", "w");
    if (out_file == NULL) {
        fprintf(stderr,"Can not open output file\n");
        exit (8);
    }

    for (cur_char = 0; cur_char < 128; ++cur_char) {
        fputc(cur_char, out_file);
    }
    fclose(out_file);
    return (0);
}




Related Links :

C Program accept 10 values and sort them

C Program accept 10 values and sort them


#include
#include
 void main()
 {
  int num[10],i,j,temp;
  clrscr();
  printf("\n Enter 10 values of an array...........\n");
  for(i=0;i<10;i++)
   scanf("%d",&num[i]);

 printf("\n array before sort .........\n");
  for(i=0;i<10;i++)
   printf("%d\t",num[i]);

 for(i=0;i<9;i++)
 {
  for(j=i+1;j<10;j++)
  {
   if(num[i]<num[j])
   {
    temp=num[i];
    num[i]=num[j];
    num[j]=temp;
   }
  }
 }

 printf("\n array after sort .........\n");
  for(i=0;i<10;i++)
   printf("%d\t",num[i]);

   getch();
  }




Related Links :

C Program to find Sum of Series using Function

C Program to find Sum of Series using Function



#include
#include
long int sum_sqr(int);
main()
 {
  int n;
  clrscr();
  printf("\n Enter any number : ");
  scanf("%d",&n);
  printf("\n Sum of Series is %ld",sum_sqr(n));
  printf("\n\n Press any key to exit. . .");
  getch();
}

 long int sum_sqr(int m)
 {
  if(m<=1)
   return 1;
  else
   return (m*m+sum_sqr(m-1));
  }

Related Links :

C Program to calculate Sum and Average of Marks and validate also

C Program to calculate Sum and Average of Marks and validate also


#include
#include
main()
{
 int m[5],i,sum=0,avg;
 clrscr();
 printf("Enter the marks of five subjects=");
 for(i=0;i<=4;i++)
 {
  scanf("%d",&m[i]);
 }
 for(i=0;i<=4;i++)
 {
  if(m[i]>100)
  {
   printf("Invalid\n");
   break;
  }
  else
  {
   sum=sum+m[i];
  }
 }
 printf("Sum=%d\n",sum);
 avg=sum/5;
 printf("Avg=%d",avg);
 getch();
}


Related Links :

C While Loop Program to show Difference Between Pre-Increment and Post-Increment

C While Loop Program to show Difference Between Pre-Increment  and Post-Increment 



#include

 int main(void) {
  int i;
 
  i = 0;
  while(i++ < 5) {
   printf("%d\n", i);
  }
  printf("\n");
  i = 0;
  while(++i < 5) {
   printf("%d\n", i);
  }
  return 0;
 }



Related Links :

C Program to show Example of While .. Continue

C Program to show Example of While .. Continue 




#include

 int main()
 {
  int i;

  i = 0;
       while ( i < 20 )
       {
            i++;
            
            printf(" to see\n");
continue;
printf("Nothing to see\n");
       }
       return 0;
 }


Related Links :

C Program to print 1 to 10 Numbers using While and Break

C Program to print 1 to 10 Numbers using While and Break


#include

 int main()
 {
       int i;

  i = 0;
  while ( i < 20 )
       {
            i++;
            if ( i == 10)
                 break;
       }
       return 0;
 }


Related Links :

C Program to Print Number Pattern

C Program to Print Number Pattern


Triangle of Numbers in C, C Program to print Number RightAngle, Pattern of Number in C

1
2 3
4 5 6
7 8 9 10



#include
#include
void main()
{
int i,j,n,a=1;
clrscr();
printf("enter the range\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",a);
a=a+1;
}
printf("\n");
}
getch();
}


Related Links :

Program to print fibonacci series in c language

Program to print fibonacci series in c language


Fibonacci series in C Programming, Fibonacci series printing in C, C assignment to print fibonacci series


#include "stdio.h"
#include "conio.h"
void main()
{
 int a,b,c,i,n;
 clrscr();
 a=0;
 b=1;
 printf("\n Please enter n for how many times generate series");
 scanf("%d",&n);
 printf("\n FIBONACCI SERIES in C Language \n");
 printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
     c=a+b;
     a=b;
     b=c;
     printf("\t%d",c);
 }
 getch();
}

Related Links :

c program to Find GCD of two number

c program to Find GCD of two number


Find G.C.D. of two numbers in C, C Assignment to find GCD of 2 numbers, Write a c program for finding gcd (greatest common divisor) of two given numbers

#include
int main () 
{
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y; 
else
m=x; 
for (i=m;i>=l;i--) 
{
if(x%i==0&&y%i==0)
{
printf("\nHCF of two number is : %d",i) ;
break; 
} 
} 
return 0;
}


Related Links :

C program to find given number is prime or not

C program to find given number is prime or not


Prime Number Program in C, C assignment to find Prime Number


#include 
#include 
int main()
{
int num,i,count=0; 
printf("Enter a number: "); 
scanf("%d",&num); 
for(i=2;i<=num/2;i++)
{ 
if(num%i==0)
{ 
count++; 
break; 
} 
} 
if(count==0 && num!= 1)
{
printf("%d is a prime number",num); 
}
else
{
printf("%d is not a prime number",num); 
}
getch();
return 0; 
}



Related Links :

C program to swap a two value without using 3rd variable, arithmetic operator

C program to swap a two value without using 3rd variable, arithmetic operator


#include 
#include 
void main()
{
int a,b;
clrscr();
printf("\n Enter Value For First Number : -");
scanf("%d",&a);
printf("\n Enter Value For Second Number: -");
scanf("%d",&b);
aA=bA=aA=b;
printf("\n\n First Number = %d",a); 
printf("\n Second Number = %d",b); 
getch();
}


Related Links :

C Program to calculate sum of an array elements using pointers

C Program to calculate sum of an array elements using pointers


Addition of array using pointers in C, C example to add array using pointers, Pointers in C


#include
#include
void main()
{
 int a[10];
 int i,sum=0;
 int *ptr;
 
 printf("Please Enter 10 Array elements:\n");
 
 for(i=0;i<10;i++)
    scanf("%d",&a[i]);
    
 ptr = a;           /* a=&a[0]  assing array to pointer variable*/

 for(i=0;i<10;i++)
    {
    sum = sum + *ptr;    //*p=content pointed by 'ptr'
    ptr++;
    }
    
 printf("\nThe sum of array elements is %d",sum); 

/* Finally printing Sum of array elements using pointers */
}


Related Links :

C Program to Print Square of given Matrix

C Program to Print Square of given Matrix


Square of Matrix in C , C Multidimensional Array Example to find Square of matrix, Matrix multiplication in C


#include
#include

#define MAX_ROWS 3
#define MAX_COLS 4

void print_square(int [ ] );    

void main (void)
{
 int row;
 int num [MAX_ROWS][MAX_COLS] = {
                                {0,1,2,3},
                {4,5,6,7},
                {8,9,10,11} 
                };

    for(row=0; row< MAX_ROWS; row++)
            print_square(num[row]);
}
void print_square(int x[ ])
{
    int col;
    for (col = 0; col< MAX_COLS; col++)
        printf ("%d\t", x[col] * x[col]);
    printf("\n");
}



Related Links :

C Program to create triangle of ABCD

C Program to create triangle of ABCD

Pyramid of Characters in C | C Assignment to create right triangle of ABCD

#include
#include

void main()
{
   int i,j;
   clrscr();
   for(i=65;i<=68;i++)
   {
      for(j=65;j<=i;j++)
      {
  printf("%c",j);
      }
      printf("\n");
   }
   getch();
}


Related Links :

C Program to Find Leap Year

C Program to Find Leap Year


C Assignment to Find given year leap year or not, Leap Year Program in C



#include
 
main()
{
      int year;
      clrscr();
 
      printf("Enter a year to check if it is a leap year\n");
      scanf("%d", &year);
 
      if ( year%400 == 0)
         printf("%d is a leap year.\n", year);
      else if ( year%100 == 0)
         printf("%d is not a leap year.\n", year);
      else if ( year%4 == 0 )
         printf("%d is a leap year.\n", year);
      else
         printf("%d is not a leap year.\n", year);   
      getch();
      return 0;
}

Related Links :

Circular queue in Data Structure

Circular queue in Data Structure

The queue that we implemented using an array suffers from one limitation. In that implementation there is a possibility that the queue is reported as full (since rear has reached the end of the array), even though in actuality there might be empty slots at the beginning of the queue. To overcome this limitation we can implement the queue as a circular queue . Here as we go on adding elements to the queue and reach the end of the array, the next element is stored in the first slot of the array (provided it is free). 
More clearly, suppose an array arr of n elements is used to implement a circular queue. Now if we go on adding elements to the queue we may reach arr(n-l). We cannot add any more elements to the queue since we have reached the end of the an-ay. Instead of reporting the queue as full, if some elements in the queue have been deleted then there might be empty slots at the beginning of the queue. In such a case these slots would be filled by new elements being added to the queue. In short just because we have reached the end of the array the queue would not be reported as full. The queue would be reported as full only when all the slots in the array stand occupied.

Related Links :

Queue as an array - Data Structure

Queue as an array - Data Structure 


Queue, being a linear data structure can be represented in various ways such as arrays and linked lists. Representing a queue as an array would have the same problem that we discussed in case of stacks. An array is a data structure that can store a fixed number of elements. The size of an array should be fixed before using it Queue, on the other hand keeps on changing as we remove elements from the front end or add new elements at the rear end. Declaring an array with a maximum size would solve this problem. The maximum size should be large enough for a queue to expand or shrink.

Related Links :

Stack as an array Data Structure

Stack as an array Data Structure

Stack contains an ordered collection of elements. An array is used to store ordered list of elements. Hence, it would be very easy to manage a stack if we represent it using an array. However, the problem with an array is that we are required to declare the size of the array before using it in a program. This means the size of an array should be fixed. Stack on the other hand does not have any fixed size. It keeps on changing, as the elements in stack are popped or pushed.

Though an array and a stack are totally different data structures, an array can be used to store the elements of a stack. We can declare the array with a maximum size large enough to manage a stack. As a result, the stack can grow or shrink within the space reserved for it.

Related Links :

C Program to show bubble sorting Numbers from random Array

C Program to show bubble sorting Numbers from random Array


bubble sorting Method Example in C | Sort Numbers using bubble sorting method | Random Number sorting using bubble sorting technique.


#include  
 
#define MAX 10 
 
int a[MAX]; 
int rand_seed=10; 
 
int rand() /* returns random number between 0 and 32767.*/  
{    
    rand_seed = rand_seed * 1103515245 +12345;    
    return (unsigned int)(rand_seed / 65536) % 32768;  
}
  
void bubble_sort(int m)  
{    
    int x,y,t; 
     for (x=0; x < m-1; x++)      
        for (y=0; y < m-x-1; y++)        
            if (a[y] > a[y+1])        
            {  
                t=a[y];  
                a[y]=a[y+1];  
                a[y+1]=t;        
            } 
}
  
void main()  
{    
    int i,t,x,y; 
    /* fill array */    
    for (i=0; i < MAX; i++)    
    {      
        a[i]=rand();      
        printf("%d\n",a[i]);    
    } 
    bubble_sort(MAX); 
    /* print sorted array */    
    printf("--------------------\n");    
    for (i=0; i < MAX; i++)      
        printf("%d\n",a[i]); 
}



Related Links :

C Program to create Random Number Array & Sort it

C Program to create Random Number Array & Sort it


Array fills an array with random numbers, sorts them using a bubble sort, and then displays the sorted list.

#include   
 
#define MAX 10 
 
int a[MAX]; 
int rand_seed=10; 
 
int rand()  
/* from K&R - produces a random number between 0 and 32767.*/  
{    
    rand_seed = rand_seed * 1103515245 +12345;    
    return (unsigned int)(rand_seed / 65536) % 32768;  
} 
 
void main()  
{    
    int i,t,x,y;
  
    /* fill array */    
    for (i=0; i < MAX; i++)    
    {      
        a[i]=rand();      
        printf("%d\n",a[i]);    
    }
  
    /* bubble sort the array */    
    for (x=0; x < MAX-1; x++)      
        for (y=0; y < MAX-x-1; y++)        
            if (a[y] > a[y+1])        
            {  
                t=a[y];  
                a[y]=a[y+1];  
                a[y+1]=t;        
            } 
 
    /* print sorted array */    
    printf("--------------------\n");    
    for (i=0; i < MAX; i++)     
        printf("%d\n",a[i]); 
} 


Related Links :

C Program to Reverse given number by Recursion

C Program to Reverse given number by Recursion 


Reverse Digit using recursion | WAP to Reverse given Number in C Language

#include
int rev(int,int);

int main()
{
 int a;
 printf("Type a value : ");
 scanf("%d",&a);
 printf("Reverse: %d",rev(a,0));
 return 0;
}

int rev(int i,int r)  
{
 if(i > 0)  
  return rev(i/10,(r*10)+(i%10));  
 return r;  
}




Related Links :

C Programs to Count characters of file

C Programs to Count characters of file 


Count characters in File, C Assignment to Calculate characters in given File in C

#include
int main()
{
 FILE *fp;
 char a[10];
 long cnt=0;
 int c;

 printf("Enter file name : ");
 gets(a);
 
 if((fp=fopen(a,"r"))==NULL)
  printf("File dosen't exist.");
 else
 {
  while(1)
  {
   c=fgetc(fp);
   if(feof(fp)) break;
   cnt++;
  }
  printf("\nfile have %ld characters",cnt);
 }
 fclose(fp);
 return 0;
}




Related Links :

Transpose the matrix In C

Transpose the matrix In C 


C Program to Transpose the matrix | Transpose the matrix in C Language | C Assignment to Transpose the matrix | Example of Multidimensional array of Transpose the matrix
#include

int main()
{
 int a[4][4],i,j,b;

 for(i=0;i<4;i++)
 {
   printf("\nEnter elements of %d row of Matrix: ",i+1);
   for(j=0;j<4;j++)
   scanf("%d",&a[i][j]);
 }
 
 for(i=0;i<4;i++)
 {
  for(j=i+1;j<4;j++)
  {
   b=a[i][j];
   a[i][j]=a[j][i];
   a[j][i]=b;
  }
 }

 printf("\n Transposed Matrix:\n\n");
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
   printf("%4d",a[i][j]);
  printf("\n");
 }
 return 0;
}

Related Links :

C Program to Convert File to upper case using command line arguments

C Program to Convert File to upper case using command line arguments

Command Line Arguments in C | Passing Parameters From Command Line in C | Convert File to Uppercase | Capitalize File in C
#include
int main(int n,char *a[])
{
 int c;
 FILE *fr,*fw;

 if(n!=3)
 {
  printf("Sorry..!! Invalid numbers of arguments.");
  return 1;
 }

 if((fr=fopen(a[1],"r"))==NULL)
 {
  printf("File can't be open.");
  return 1;
 }
 if((fw=fopen(a[2],"r+"))==NULL)
 {
  printf("File can't be open.");
  fclose(fr);
  return 1;
 }
 while(1)
 {
  c=fgetc(fr);
  if(feof(fr)) break;
  c=~c;
  fputc(c,fw);
 }
 fclose(fr);
 fclose(fw);
 return 0;

Related Links :

Pattern of Numbers in C Language

Pattern of Numbers in C Language


Pyramid of Numbers in C, Triangle of Numbers in C
#include
#include
void main()
{
int i,j,n,k;
clrscr();
printf("enter the range\n");
scanf("%d",&n);
k=n+1;
for(i=1;i<=n;i++)
{
for(j=1,k=k-i;j<=i;j++)
{
printf("%d",k);
k++;
}
printf("\n");
}
getch();
}


enter the range 5 5 45 345 2345 12345

Related Links :

C Program to find nth term of the series using recursion

C Program to find nth term of the series using recursion

#include
int get_term(int x);

int main()
{
    int n,p;
    printf("Enter which term do you want?\n");
    scanf("%d",&n);
    p=get_term(n);
    printf("T(%d) = %d\n",n,p);
    return 0;
}

int get_term(int x)
{
    int term;
    if(x==1)
      term=2;
    else
      term= get_term(x-1) + 1;
    return term;
}

Related Links :

C Program to create Diamond of Asterisk (*)

C Program to create Diamond of Asterisk (*)


Diamond Shape in C Language | C Program to create Diamond of Dollar or Asterisk| Using for loop create Asterisk Diamond Shape 

#include
main()
{
    int j,i,k,l;
    char c;
    do
    {
        printf("Enter the number of stars in the base..\n");
        scanf("%d",&k);
        for (i=1;i<=k;i++)
        {
            for(l=0;l<(k-i);l++)
                printf(" ");
            for (j=0;j<i;j++)
                printf ("$ ");
            printf("\n");
        }
        for (i=1;i<=k;i++)
        {
            for(l=0;l<i;l++)
                printf(" ");
            for (j=1;j<=k-i;j++)
                printf ("$ ");
            printf("\n");
        }
        printf("\n Enter y to try more...press any other key exit program...\t");
        c=getchar();
        c=getchar();
    }
    while(c=='y');
} 


Related Links :

C Program to create ABCD triangle

C Program to create ABCD triangle 


Triangle of ABCD in C Language using Loop Control | Triangle of Alphabets in C language |
#include
main()
{
    char z;
    int j,i,k;
    printf("Enter the number of rows..(Between 1 to 26)\n");
    scanf("%d",&k);
    if(k<1||k>26)
    {
        printf("\n The number entered was not in range of 1 to 26\n"); //For Validation
        printf("exiting...\n");
        exit(0);
    }
    //If Proper Input given by user...
    printf("\n\n");
    for (i=1;i<=k;i++)
    {
        z = 'A';
        for (j=0;j<i;j++)
        {    
            printf ("%c  ",z);
            z++;
        }
    printf("\n\n");
    }
}

Related Links :

C++ program to Insert and Delete element of an array

Write a program to Insert and Delete element of an array


C++ program to Insert and Delete element of an array | Add Elements in Given Array | Run time Array Size Input 



    #include
    #include
    void main()
    {
    clrscr();
    int a[100],i,pos,num,item,size;
    cout<<"How many element: ";
    cin>>size;
    cout<<"Enter the elements of array: "<>a[i];
    cout<>num;
    cout<<"Enter position number: ";
    cin>>pos;
    cout<=pos;i--)
    a[i+1]=a[i];
    a[pos]=num;
    size++;
    cout<<"New Array: "<>pos;
    --pos;
    item=a[pos];
    for(i=pos;i


Related Links :

C program to find area of a circle

C++ program to find area of a circle 


Area of Circle in C++ | C Assignment to Find area of Circle | Calculate area of Circle when radius is given as input in C++ language



    #include 
    #include 
    void main()
    {
    clrscr();
    float r,a;
    cout<<"enter radius ";
    cin>>r;
    a=3.14*r*r;
    cout<<"The area is "<

Related Links :

Digital clock in VHDL

Digital clock in VHDL  | VHDL Digital Clock Program 


Program to create digital clock in VHDL, Source Code to create digital clock in VHDL



library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity digi_clk is
port (clk1 : in std_logic;
      seconds : out std_logic_vector(5 downto 0);
      minutes : out std_logic_vector(5 downto 0);
      hours : out std_logic_vector(4 downto 0)
     );
end digi_clk;

architecture Behavioral of digi_clk is
signal sec,min,hour : integer range 0 to 60 :=0;
signal count : integer :=1;
signal clk : std_logic :='0';
begin
seconds <= conv_std_logic_vector(sec,6);
minutes <= conv_std_logic_vector(min,6);
hours <= conv_std_logic_vector(hour,5);

 --clk generation.For 100 MHz clock this generates 1 Hz clock.
process(clk1)
begin
if(clk1'event and clk1='1') then
count <=count+1;
if(count = 50000000) then
clk <= not clk;
count <=1;
end if;
end if;
end process;

process(clk)   --period of clk is 1 second.
begin

if(clk'event and clk='1') then
sec <= sec+ 1;
if(sec = 59) then
sec<=0;
min <= min + 1;
if(min = 59) then
hour <= hour + 1;
min <= 0;
if(hour = 23) then
hour <= 0;
end if;
end if;
end if;
end if;

end process;

end Behavioral;

Related Links :

C program of Newton Raphson Method

C program of Newton Raphson Method



#include
#include
#include
#include

int bigval,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1=0,x2=0,t=0;
float fx1=0,fdx1=0;

int main()
{

   
    printf("\n\n\t C PROGRAM FOR THE NEWTON RAPHSON METHOD");
    printf("\n\n\n\t PLEASE ENTER THE MAXIMUM POWER OF X = ");
    scanf("%d",&bigval);

    for(i=0;i<=bigval;i++)
    {
        printf("\n\t x^%d = ",i);
        scanf("%d",&coef[i]);
    }

    printf("\n");

    printf("\n\t So, THE POLYNOMIAL IS "= ");
    for(i=bigval;i>=0;i--)/*printing coefficients*/
    {
        printf(" %dx^%d",coef[i],i);
    }

    printf("\n\n\tFirst approximation x1 ----> ");
    scanf("%f",&x1);

 
     printf("\n ITERATION \t x1 \t F(x1) \t \tF'(x1)  ");


    do
    {
            cnt++;
            fx1=fdx1=0;
            for(i=bigval;i>=1;i--)
            {
                fx1+=coef[i] * (pow(x1,i)) ;
            }
            fx1+=coef[0];
            for(i=bigval;i>=0;i--)
            {
                fdx1+=coef[i]* (i*pow(x1,(i-1)));
            }
            t=x2;
            x2=(x1-(fx1/fdx1));

            x1=x2;

            printf("\n\t %d \t%.3f \t %.3f\t\t%.3f ",cnt,x2,fx1,fdx1);

    }
 while((fabs(t - x1))>=0.0001);
    printf("\n\n\n\t AND.... THE ROOT OF EQUATION IS = %f",x2);
    getch();
}



Related Links :

String concatenation in c without using strcat Function

String concatenation in c without using strcat Function 



void stringConcat(char[],char[]); 
int main()
{
char strl[100],str2[100]; 
int compare;
printf("\nEnter first string: \n"); 
scanf("%s",strl);
printf("\nEnter second string: "); 
scanf("%s",str2);

stringConcat(strl,str2);
printf("\nString after concatenation: %s",strl);
return 0; 
}


void  stringConcat(char   strl[],char   str2[])
{ 
int   i=0,j=0;
while(strl[i]!='\0')
{
i++; 
}
while(str2[j] ! = '\0') 
{
strl[i] = str2[j] ;
i++;
j++;
}

strl[i] = ' \0';
}

Related Links :

C Program to convert string into ASCII values

C Program to convert string into ASCII values

 

#include
int main()
{
char str[100]; 
int i=0;
printf("Enter any string: \n"); 
scanf("%s",str);
printf("\nASCII values of each characters of given string: "); 
while(str[i])
printf("%d ",str[i++]);
return 0; 
}

Related Links :

C Program to Calculating Area of Circle using Pointer

C Program to Calculating Area of Circle using Pointer



#include

void areaperi ( int r, float *a, float *p )
{
*a = 3.14 * r * r ;
*p = 2 * 3.14 * r ;
}

void main( )
{
int radius ;
float area, perimeter ;

printf ( "\n Enter radius of a circle " ) ;
scanf ( "%d", &radius ) ;

areaperi ( radius, &area, &perimeter ) ;

printf ( "The Area is = %f", area ) ;
printf ( "\n The Perimeter = %f", perimeter ) ;
}

Related Links :

C Program to Calculate Area of Equilateral Triangle

C Program to Calculate Area of Equilateral Triangle

  • Equilateral triangle is a triangle in which all three sides are equal .
     
  • All angles are of measure 60 degree
     
  • A three-sided regular polygon
#include
#include
#include
void main()
{
int side;
float area,r_4;      // r_4 = root 3 / 4
clrscr();  // Clear Screen

r_4 = sqrt(3) / 4 ;

printf("\n Please Enter the Length of Side : "); 
scanf("%d",&side); 

area = r_4 * side * side ;

printf("\n The Area of Equilateral Triangle : %f",area);
getch();
}

Related Links :

C Program To find equivalent capacitance of series combination of capacitive circuit

C Program To find equivalent capacitance of series combination of capacitive circuit


#include
#include

void main()
{
float c[10],num,Cs=0;
int i;
clrscr();

printf("Enter the number of Capacitors : ");
scanf("%f",&num);

printf("\nEnter Value of Each Capacitor : \n");
for(i=0;i<num;i++)
   {
   printf("\nC%d : ",i+1);
   scanf("%f",&c[i]);
   }

for(i=0;i<num;i++)
   {
   Cs = Cs + (1.0/c[i]);
   }
   Cs = 1.0 / Cs;

printf("\nEquivalent Series Capacitance : %f mFarad",Cs);

getch();
}



Related Links :

C Program to Calculate Area of Rectangle

C Program to Calculate Area of Rectangle


#include
#include

void main()
{
int length,breadth,side;
clrscr();  // Clear Screen

printf("\n Enter the Length of Rectangle : "); 
scanf("%d",&length); 

printf("\n Enter the Breadth of Rectangle : "); 
scanf("%d",&breadth); 

area = length * breadth;

printf("\n Area of Rectangle : %d",area);
getch();
}


Related Links :

C program to find Hexadecimal equivalent using pointer

C program to find Hexadecimal equivalent using pointer



#include < stdio.h >
#include < conio.h > 
#include < alloc.h>

void main()
{
 int num,rem[20],*ptr;
 clrscr();
 printf("\nEnter number:");
 scanf("%d",&num);
 ptr=rem;
 while(num!=0)
 {
  *ptr=num%16;
  num=num/16;
  ptr++;
 }
 ptr--;
 while(ptr>=rem)
 {
  if(*ptr < 10)
   printf("%d",*ptr);
  else
   printf("%c",*ptr+55);
   ptr--;
 }
}


Related Links :

C Program to Check whether given string is palindrome or not

C Program to Check whether given string is palindrome or not


If we reverse of any string is equivalent to the original string then it is called palindrome String. 

#include < stdio.h >
#include < conio.h > 

int palin(char str[],int i,int j);
void main()
{
 char str[100];
 int i,j,reply; 
 clrscr();
 printf("\n Enter a string \n");
 gets(str);
 j = 0;
 while ( str[j] != '\0')
 {
  j++;
 } // while
 j--;
 i=0;
 reply = palin(str,i,j);
 if( reply == 0 )
  printf("\n %s is not palindrome",str);
 else
  printf("\n %s is palindrome",str);
}

int palin(char str[],int i,int j)
{
 if (i < j )
 {
  if ( str[i] != str[j])
   return(0);
  else
   return(palin(str,i+1,j-1));
 }
 else
  return(1);
} 


Related Links :

C Program to Convert binary number to decimal

C Program to Convert binary number to decimal 


binary number to decimal number conversion in C, C assignment to convert Binary to Decimal, Number convention in C language.


#include < stdio.h >
#include < conio.h > 

void main()
{
 long n,dec,mult,r,base; 
 clrscr();
 printf("\n Enter a base : ");
 scanf("%ld",&base);
 printf("\n Enter a number :");
 scanf("%ld",&n); 
 dec = 0;
 mult=1;
 printf("\n Decimal equivalent of base %ld number %ld is ",base,n);
 while ( n != 0 )
 {
  r = n % 10;
  n /= 10;
  r = r * mult;
  dec = dec + r;
  mult *= base;
 } // while
 printf("%ld",dec);
}
 

Related Links :

C Program to Reverse number using pointer initialization

C Program to Reverse number using pointer initialization



#include < stdio.h >
#include < conio.h > 
#include < alloc.h >

void main()
{
    long *np,*revp,n,rev,*np1,n1;
    clrscr();
    np=&n;
    revp=&rev;
    np1=&n1;
    printf("\nEnter value for n:");
    scanf("%ld",np);
    *revp=0;
    *np1=*np;
    while(*np!=0)
    {
        *revp=*revp*10+(*np%10);
        *np=*np/10;
    }
    printf("\n Reverse of %ld is %ld",*np1,*revp);
}

Related Links :

C program to Find the GCD (Greatest Common Divisor) of two numbers

C program to Find the GCD (Greatest Common Divisor) of two numbers

GCD of Numbers in C, C Assignment to find GCD in C, C Language program to find GCD




#include < stdio.h >
#include < conio.h >

void main()
{
int x,y;
clrscr();
printf("\nEnter two numbers :");
scanf("%d %d",&x,&y);
if(x==0 && y==0)
printf("\n No GCD");
else
if(x==0 && y!=0)
printf("GCD is %d",y);
else
if(x!=0 && y==0)
printf("GCD is %d",x);
else
{
while(x!=y)
{
if(x>y)
x=x-y;
else
if(x < y)
y=y-x;
}
printf("\n GCD is %d",x);
}
}

Related Links :

C Program to Find the maximum element of array

C Program to Find the maximum element of array | C Program to find largest element from array | Program to Find highest value from given array in C language




#include < stdio.h >
#include < conio.h >

void main()
{
int a[100],max,i,n;
clrscr();
printf("Enter the size of an array\n");
scanf("%d",&n);
printf("Enter the elements %d for array\n", n);
for(i=0;i < n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=0;i < n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
printf("The max of the given array is %d",max);
getch();
}



Related Links :

C Program to calculate sum of diagonal elements of a matrix

C Program to calculate sum of diagonal elements of a matrix | C Program to add Numbers diagonally | Diagonally add Elements of array in C





#include < stdio.h >
#include < conio.h >

void main()
{
int a[10][10],i,j,sum=0,r,c;
clrscr();
printf("\n Enter the number of rows and column ");
scanf("%d%d",&r,&c);
printf("\nEnter the %dX%d matrix",r,c);
for(i=0;i < r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}//for
}//for
for(i=0;i <r;i++)
{
for(j=0;j < c;j++)
{
if(i==j)
{
sum+=a[i][j];
}
}//for
}//for
printf("\nThe sum of diagonal elements is %d",sum);
}

Related Links :

C program to add value in Spiral matrix Format

C program to add value in Spiral matrix Format



#include < stdio.h >
#include < conio.h >

void main()
{
int m[20][20],i,j;
int p,q,r,hr,r,c,cnt;
clrscr();
printf("\nEnter r & c :");
scanf("%d %d",&r,&c);
cnt = 1;
r = 0; p = 0;
hr = r - 1;
q = c - 1;
while ( r <=hr && p <= q )
{
i = r;
for(j=p;j <= q;j++)
{
m[i][j] = cnt++;
j = q;
for(i=r+1;i<=hr;i++)
{
m[i][j] = cnt++;
if( r != hr )
{
i = hr;
for(j=q-1;j>=p;j--)
m[i][j] = cnt++;
}
if ( p != q )
{
j = p;
for(i=hr-1;i>r;i--)
m[i][j] = cnt++;
}
}
}
r++;p++;
hr--;q--;
}
printf("\nSpirally filled matrix is\n");
for(i=0;i < r;i++)
{
for(j=0;j<c;j++)
{
printf("%4d",m[i][j]);
printf("\n");
}
}

Related Links :

C Program to Check perfect number

C Program to Check perfect number





#include < stdio.h >
#include < conio.h >

void main()
{
int n,d,tot;

clrscr();
printf("\n Enter value for n : ");
scanf("%d",&n);
tot = 0;

d = 1;
while ( d<= n/2)
{
if ( n % d == 0 )
{
tot = tot + d;
}
d++;
} // while
if ( tot == n )
{
printf("\n Yes... %d is perfect",n);
}
else
{
printf("\n Ohhhh... %d is not perfect",n);
}
}

Related Links :

C program for multiplication of two mXn matrix

C program for multiplication of two mXn matrix

multiplication of Matrix in C, C assignment to multiplication of matix M x N in C, C assignment to solve multiplication of matrix.



#include
#include
#define ROW 20
#define COL 20
void main()
{
int a[ROW][COL],b[ROW][COL],c[ROW][COL];
int i=0,j=0,k,sum=0,m,n;
clrscr();
printf("\n enter the value for m\n");
scanf("%d",&m);
printf("\n enter the value for n\n");
scanf("%d",&n);
printf("\n enter the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the matrix A elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
printf("\n enter the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n the matrix B elements ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
printf("\n the product of matrices is as follows \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}




Related Links :

C Program to Calculate the volume and area of cube

C Program to Calculate the volume and area of cube


#include
main()
{
float side,surfacearea,volume;
clrscr();
printf("\n Please enter the side of cube...\n ");
scanf("%f",&side);
surfacearea=6*side*side;
volume=side*side*side;
printf("\n The surface area of cube=%f",surfacearea);
printf("\n The volume of cube=%f",volume);
getch();
}




Related Links :

c program to calculate the length of the string without using library function

c program to calculate the length of the string without using library function




#include
#include
void main()
{
int i=0,cnt=0;
char str[50];
clrscr();
printf("\n Enter Any string=");
gets(str);
while(str[i]!=NULL)
{
i++;
}
printf("\n length of string is =%d",i);
getch();
}

Related Links :

write a C program to display Fibonacci series upto n terms

write a C program to display Fibonacci series upto n terms

Fibonacci series in C++ language | C++ Program to print Fibonacci series | Fibonacci series upto n numbers, Fibonacci series upto given number in C++





#include
#include
void main()
{
clrscr();
int a=0,b=1,c=0,n;
cout<<"Enter the number of terms you wanna see: ";
cin>>n;
cout<<a<<" "<<b<<" ";
for(int i=1;i<=n-2;i++)
{
c=a+b;
a=b;
b=c;
cout<<c<<" ";
}
getch();
}




Related Links :

C Program To find equivalent resistance of Parallel combination of resistive circuits

C Program To find equivalent resistance of Parallel combination of resistive circuits | C assignment to find equivalent resistance in C language | Program to find equivalent resistance when value of Resistance is given




#include
#include

int main()
{
int r[10],num,i,Rs=0;
clrscr();

printf("Enter the number of Resistances : ");
scanf("%d",&num);

printf("\nEnter Value of Each Resistance : \n");
for(i=0;i<num;i++)
{
printf("\nR%d : ",i+1);
scanf("%d",&r[i]);
}

for(i=0;i<num;i++)
{
Rs = Rs + r[i];
}

printf("\nEquivalent Series Resistance : %d Kohm",Rs);

getch();
}


Related Links :

C Program to add number using Command line Arguments Parameters

C Program to Add two numbers using Command Line Arguments Parameters | C Program to add number using Command line Arguments Parameters | Command Line Arguments in C Language | Passing parameters by Command Line Arguments in C language






#include

void main(int argc , char * argv[])
{
int i,sum=0;

if(argc!=3)
{
printf("Oops....!! \n you have forgot to type numbers. \n");
exit(1);
}

printf("The sum is : ");

for(i=1;i<argc; i++)
sum = sum + atoi(argv[i]);

printf("%d",sum);

}







How Run Program?

Step 1 : Write a Program

Step 2 : Open Command Prompt inside Borland C/C++.

Step 3 : Click on DOS Shell.

Step 4 : Inside Command Prompt type this command.

C:\TC\BIN>add 10 20

Step 5 : Hit Enter , You will get following Output.



C:\TC\BIN>add 10 20

The sum is : 30

C:\TC\BIN>




Related Links :

Incrementing Array of Float Pointer in C

Incrementing Array of Float Pointer in C



#include

int main(){

float var[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];

ptr=&var;
printf("Value inside ptr : %u",ptr);

ptr=ptr+1;
printf("Value inside ptr : %u",ptr);

return 0;
}








Output :


Value inside ptr : 1000
Value inside ptr : 1020

Related Links :

Creating Triangle of Numbers in C

Creating Triangle of Numbers in C


1
2 1
1 2 3 2 1
2 3 4 3 2 1
1 2 3 4 5 4 3 2 1



# include 
# include 
void main ( )
{
clrscr ( );
int n,i,j,k;
printf ("\n Enter no. of rows :");
scanf ("%d",&n);
printf ("\n\n");
for (i =1;i < = n;i++)
{
for (j = 1;j < = n-i;j++)
printf (" ");
for (j = i;j > =1;j--)
printf ("%d",j);
for (j = 2;j < = i;j++)
printf ("%d",j);
printf ("\n");
}
getch ( );
}


Related Links :

C Program Decimal number to Octal Conversion | Decimal to Octal Conversion in C

C Program Decimal number to Octal Conversion | Decimal to Octal Conversion in C | C Language assignment to convert Decimal Value to Octal Value 



#include
#include
#include

void dec_oct(long int num)   // Function Decl.
{
long int rem[50],i=0,length=0;
while(num>0)
 {
 rem[i]=num%8;
 num=num/8;
 i++;
 length++;
 }
printf("\n Converted Octal number is : ");
     for(i=length-1;i>=0;i--)
             printf("%ld",rem[i]);
}





void main()
{
long int num;
clrscr();
 
 printf("Enter the decimal number : ");
 scanf("%ld",&num);

    dec_oct(num);   // Calling function

 getch();
}


Related Links :

C Program to Convert Decimal number into Hexadecimal | Decimal to Hexadecimal Conversion in C

C Program to Convert Decimal number into Hexadecimal | Decimal to Hexadecimal Conversion in C | C Language assignment to convert Decimal Number to Hexadecimal 



#include
#include
#include

void dec_hex(long int num)   // UDF
{
long int rem[50],i=0,length=0;

while(num>0)
   {
      rem[i]=num%16;
      num=num/16;
      i++;
      length++;
   }

printf("\n Hexadecimal number : ");
for(i=length-1;i>=0;i--)
  {
    switch(rem[i])
    {
      case 10:
          printf("A");
          break;
      case 11:
          printf("B");
          break;
      case 12:
          printf("C");
          break;
      case 13:
          printf("D");
          break;
      case 14:
          printf("E");
          break;
      case 15:
          printf("F");
          break;
      default :
         printf("%ld",rem[i]);
    }
  }
}
//================================================
void main()
{
long int num;
clrscr();

 printf("Enter the decimal number : ");
 scanf("%ld",&num);

    dec_hex(num);   // Calling function

 getch();
}



Related Links :

C Program to Convert Binary to Decimal number | Binary to Decimal Conversion in C

C Program to Convert Binary to Decimal number | Binary to Decimal Conversion in C | C Language Assignment to convert Binary Value to Decimal Value 



#include
#include
#include

void bin_dec(long int num)   // Function declaration
{
long int rem,sum=0,power=0;
while(num>0)
 {
 rem = num%10;
 num = num/10;
 sum = sum + rem * pow(2,power);
 power++;
 }

printf("\n Converted Decimal number is : %d",sum);
}



// END of Function




void main()
{
long int num;
clrscr();

printf(" Please Enter the Binary number (0 and 1 only): \n");
scanf("%ld",&num);

bin_dec(num);

getch();
}


Related Links :

C Program to Convert Decimal to Binary using Bitwise and operator | C Program to Convert to Decimal to binary

C Program to Convert Decimal to Binary using Bitwise and operator | C Program to Convert to Decimal to binary | Decimal to binary conversion in C language



#include
#include

void binary(unsigned int); 

void main()
{
unsigned int num;
printf(" Please Enter Any Decimal Number : ");
scanf("%u",&num);
binary(num);   // Function Call
getch();
}




// Function Starts 
void binary(unsigned int num)
{
unsigned int mask=32768;   //mask = [1000 0000 0000 0000]
printf("\n The Binary Eqivalent is : ");

while(mask > 0)
   {
   if((num & mask) == 0 )
         printf("0");
   else
         printf("1");
  mask = mask >> 1 ;  // Right Shift
   }
}



Related Links :

C Program Reverse all words not string | Reverse Words only from given String in C

C Program Reverse all words not string | Reverse Words only from given String in C | C assignment to reverse Word only

C program to accept a string from user and reverse all words but not string using pointer.



/*c program for reverse all words but not string using pointer*/

#include
#include
int main()
{
 char str[30];
 char *p,*tmp;
 printf(" Enter any string : \n");
 gets(str);
 for(p=str; *p!='\0'; p++)
 {
  if(*(p+1)==' ' || *(p+1)=='\0')
  {
   for(tmp=p; *tmp!=' ' && tmp>=str; tmp--)
     printf("%c",*tmp);
  }
  printf(" ");
 }
 getch();
 return 0;
}

OUTPUT :
Enter Any String : 
Life is Good


efiL si dooG

Related Links :

c program to print all ASCII Values using loop

c program to print all ASCII Values using loop | Printing ASCII Values in C | Printing All 255 Ascii Characters in C


#include
#include
int main()
{
 int i;
 printf("ASCII table & its equivalent values with numbering: \n");
 for(i=1; i<=255; i++)
 printf("\nValue: %d -> ASCII character: %c",i,i);
 getch();
 return 0;
}



Related Links :

C Program to Diagonally Add elements | Diagonal addition of elements of matrix in C

C Program to Addition of Diagonal Elements in Matrix | C Program to Diagonally Add elements | Diagonal addition of elements of matrix in C



#include
#include

void main()
{
int i,j,a[10][10],sum,m,n;

/* m - Number of rows 
   n - Number of Columns   */

printf("\n Enter the number of Rows : ");
scanf ("%d",&m);

printf("\n Enter the number of Columns : ");
scanf ("%d",&n);

/* Accept the Elements in m x n Matrix */

for(i=0;i<m;i++ )
       for(j=0;j<n;j++)
       {
       printf("Enter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       } 

/* Addition of all Diagonal Elements */

sum = 0;

for(i=0;i<m;i++ )
       for(j=0;j<n;j++)
       {
        if ( i == j )   
        sum = sum + a[i][j]; 
       } 

/*  Print out the Result */

printf("\nSum of All Diagonal Elements in Matrix : %d",sum); 

getch();

}



Enter the number of Rows : 2

Enter the number of Columns : 2

Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 1
Enter the Element a[1][0] : 1
Enter the Element a[1][1] : 1

The Addition of All Elements in the Matrix : 2

Related Links :

C Program to Addition of All Elements in Matrix | Summation of Elements of matrix in c

C Program to Addition of All Elements in Matrix | Summation of Elements of matrix in c



#include
#include

void main()
{
int i,j,a[10][10],sum,m,n;

/* m - Number of rows 
   n - Number of Columns */

printf("\n Enter the number of Rows : ");
scanf ("%d",&m);

printf("\n Enter the number of Columns : ");
scanf ("%d",&n);

/* Accept the Elements in m x n Matrix */

for(i=0;i<m;i++)
       for(j=0;j<n;j++ )
       {
       printf(" \n Enter the Element a[%d][%d] : ", i , j);
       scanf("%d",&a[i][j]);
       } 

/* Addition of all Elements */

sum = 0;

for(i=0;i<m;i++ )
       for(j=0;j<n;j++ )
       {
       sum = sum + a[i][j]; 
       } 

/* Print out the Result */
printf("\nThe Addition of All Elements in the Matrix : %d",sum); 
getch();
}





Enter the number of Rows : 2
Enter the number of Columns : 2

Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 1
Enter the Element a[1][0] : 1
Enter the Element a[1][1] : 1

The Addition of All Elements in the Matrix : 4


Related Links :

Reading 2-D Array Elements In C | Retrieving Elements from two Dimension Arrays

Reading 2-D Array Elements In C | Retrieving Elements from two Dimension Arrays 

 


#include
#include
void main()
{
int i,j,a[3][3];

/* i = For Counting Rows
j = For Counting Columns */

/* two loops will Accept 9 elements from user */

for(i=0;i<3;i++)
   for(j=0;j<3;j++)
   {
   printf("\n Enter the a[%d][%d] = ",i,j);
   scanf("%d",&a[i][j]);
   }

/* Print 9 elements */

for(i=0;i<3;i++)
{
   for(j=0;j<3;j++)
      printf("%d\t",a[i][j]);
printf("\n");
}

getch();
}


Related Links :

Simple sorting of numeric array in C | C Program to Sort Numbers by Simple Sorting Method

Simple sorting of numeric array in C | C Program to Sort Numbers by Simple Sorting Method | Simple Sorting method Example in C Language



#include < stdio.h >
#include < conio.h > 

void main()
{
	int a[20];
	int i,j,n;
	clrscr();
	printf("\n Enter value for n: ");
	scanf("%d",&n);
	printf("\n Enter %d values \n",n);
	for(i=0; i < n ; i++)
	{
		scanf("%d", &a[i]);
	}
	printf(" %d values before sorting are \n",n);
	for(i=0; i < n; i++)
	{
		printf("%4d",a[i]);
	}
	simple_sort(a,n);
	printf("\n %d values after sorting are \n",n);
	for(i=0;in;i++)
	{
		printf("%4d",a[i]);
	}
}
int simple_sort(int a[],int n)
{
	int i,j,t;
	for(i = 0;i < n-1;i++)
	{
		for(j= i + 1;j < n;j++)
		{
			if(a[i]>a[j])
			{
				t=a[i];
				a[i]=a[j];
				a[j]=t;
			}
		}
	}
 return;
}


Related Links :

C Program to Counting total number of nodes in a tree

C Program to Counting total number of nodes in a tree



#include
#include
struct tree_node
{
 int data;
 struct tree_node *left,*right,*parent;
};
typedef struct tree_node node;
node * new_node(int x);
node * insert_node(node *root, int x);
int count_nodes(node *root);

int main()
{
 node *root=NULL;
 int i,x,max;
 printf("how many numbers do you want to enter in tree?\n");
 scanf("%d",&max);
 printf("Enter %d numbers \n",max);
 for(i=0;i<max;i++)
 {
  scanf("%d",&x);
  root = insert_node(root, x);
 }
 printf("all numbers inserted into the tree\t press enter to count");
 fflush(stdin);
 getchar();
 printf("\nThere are total %d nodes in this tree\n",count_nodes(root));
    return 0;
}

node * insert_node(node *root, int x)
{
 if(!root)
 {
  root = new_node(x);
  return root;
 }
 if(root->data > x)
  root->left = insert_node(root->left,x);
 else
  root->right = insert_node(root->right,x);
 return root;
}

int count_nodes(node *root)
{
 if(!root)
 return 0;
 else
 return(count_nodes(root->left) + 1 + count_nodes(root->right));
}

node * new_node(int x)
{
 node *furnished;
 furnished = (node*)malloc(sizeof(node));
 furnished->data=x;
 furnished->left=NULL;
 furnished->right=NULL;
 furnished->parent=NULL;
 return furnished;
}


Related Links :

C Program to find the Largest Number using Ternary Operator

C Program to find the Largest Number using Ternary Operator | Ternary Operators in C | Ternary Operator Example in C | C Program to Find Biggest number out of given 3 numbers 





#include
void main()
{
       int a,b,c,big;
       printf("\n Enter 3 numbers:\n");
       scanf("%d %d %d",&a,&b,&c);
       big=(a> b&&a> c?a:b> c?b:c);
       printf("\n The biggest number is: %d",big);
       getch();
}




Related Links :

C program to find the factorial of the inputted number

C program to find the factorial of the inputted number



#include
void main()
{
     int n,i,fact=1;

     printf("Please Enter any number\n");
     scanf("%d",&n);

     for(i=n;i>=1;i--)
     {
                      fact=fact*i;
     }
     printf("\n factorial = \t %d",fact);
     getch();
     }


Related Links :

C Program to sort all even and odd numbers from array | Array Program to sort the Given numbers in C

C Program to sort all even and odd numbers from array | Array Program to sort the Given numbers in C



#include
#include
void main()
{
     int ar[100],i,n;

     printf("Enter the size of the array \n");
     scanf("%d",&n);

     printf("Enter the elements of the array \n");
     for(i=0; i<n; i++)
     {
           scanf("%d",&ar[i]);
     }

     printf("Even numbers in the array are - ");
     for(i=0;i<n;i++)
     {
           if(ar[i]%2==0)
           {
                 printf("%d \t",ar[i]);
           }
     }

     printf("\n Odd numbers in the array are - ");
     for(i=0;i<n;i++)
     {
           if(ar[i]%2!=0)
           {
                 printf("%d \t",ar[i]);
           }
     }
     getch();
}



Related Links :

C program to print palindrome series upto given number | palindrome series printing in C

C program to print palindrome series upto given number | palindrome series printing in C | palindrome series numbers printing in C



#include
void main()
{
     int limit,i2,n2,n3=0,i;
     
     printf("Enter limit to print all palindrome no. upto that limit \n");
     scanf("%d",&limit);
     
     for(i=1;i< =limit;i++)
     {
           i2=i;
           do
           {
                 n2=i2%10;
                 i2=i2/10;
                 n3=n3*10+n2;
           }
           while(i2>0);
           
           if(n3==i)
           {
               printf("%d \t",n3);
           }
           n3=0;
     }
     getch();
}



Related Links :

C program to find the factorial of given number | Factorial of number in C

C program to find the factorial of given number | Factorial of number in C | Assignment to find factorial in C Programming




#include
void main()
{
     int n,i,fact=1;

     printf("Enter any number\n");
     scanf("%d",&n);

     for(i=n;i>=1;i--)
     {
                      fact=fact*i;
     }
     printf("\n factorial = \t %d",fact);
     getch();
     }



Related Links :

C Program to find leap Year | Program to find given year leap year or not

C Program to find leap Year | Program to find given year leap year or not | Find Leap Year in C language



#include
#include
void main()
{
 int yr;
 clrscr();
 printf("enter the year");
 scanf("%d",&yr);
 if((yr%400==0)&&(yr/4==0))
  printf("leap year");
 else
  printf("not leap year");
 getch();
}




Related Links :

C Program to find absolute value of given number | C Language program to find absolute value of given number in C

C Program to find absolute value of given number | C Language program to find absolute value of given number in C



#include
#include
void main()
{
  int a,b;
  clrscr();
  printf("enter the value of a");
  scanf("%d",&a);
  if(a<0)
  {
   b=a*-1;
   printf("\nabsolute value of %d is %d",a,b);
  }
  else
   {
     printf("\nabsolute value=%d",a);
   }
   getch();
}




Related Links :

C Program to calculate Total, average in C | C assignment to calculate total and average C Program to calculate Total, average in C | C assignment to calculate total and average

C Program to calculate Total, average in C | C assignment to calculate total and average





#include
#include

int main()
{
  int a[5],i,tot=0,avg;
  clrscr();
  for(i=1;i< 6;i++)
  {
  printf("enter the marks of subjects");
  scanf("%d",&a[i]);

      tot=tot+a[i];
  }
  printf("total=%d\n",tot);
  avg=tot/5;
  printf("average=%d",avg);
  getch();
  return 0;
}



Related Links :

C Program to reverse the given Number | Reverse the given number in C

C Program to reverse the given Number | Reverse the given number in C | C Language assignment to reverse given number




/* wap yo find the reverse of the no.*/
#include
#include
int main()
{
   int a,b=0;
   clrscr();
   printf("enter the no.");
   scanf("%d",&a);
   while(a>0)
   {
       b=a%10;
       printf("%d",b);
       a=a/10;
   }
   getch();
   return 0;
}



Related Links :

C Program to find given number is Prime Number or Not | Program to find Prime Number

C Program to find given number is Prime Number or Not | Program to find Prime Number | Prime Number Assignment in C language




/* wap to find no. is prime or not */
#include
#include

int main()
{
  int num,x=2,p;
  clrscr();
  printf("enter the no");
  scanf("%d",&num);
  while(x<num)
  {
     if(num%x==0)
     {
	p=1;
	break;
     }
     else
     {
       p=0;
     }
     x++;
  }
  if(p==0)
  {
     printf("no.is prime");
  }
  if(p==1)
  {
     printf("no. is not prime");
  }
  getch();
  return 0;
}



Related Links :

Queue implementation on linked lists in C language

Queue implementation on linked lists in C language



/*
This is a program showing the queue implimentation on linked list
program writtem by RP Singh
compiled and tested on C-free4.0 standard
*/
#include
#include
#include
struct node1
{
    int item;
    struct node1 *next;
};
typedef struct node1 node;        //defining datatype node

struct q1
{
    node *front;
    node *rear;
    int length;
};
typedef struct q1 queue;        //defining queue datatype

void enqueue(node *, queue *);
node *dequeue(queue *);
int queue_length(queue *);
void view_queue(queue *);
node *create_node(void);
void fill_node(node *);


int main()
{
    int i,j;
    node *current_node;
    queue que;                    //local queue
    que.front=NULL;
    que.rear=NULL;
    que.length=0;
    printf("This is a demo program to show working of queues");
    anchor:                    //anchor is a label
    printf("\n\nyou have following options\n");
    printf("1. enqueue an item\n2. dequeue an item\n3. view queue\n");
    printf("4. count items in queue\n5. exit program\n\n");
    scanf("%d",&j);
    switch(j)
    {
        case 1:
            printf("\nEnter a number to be enqueued =>\t");
            current_node=create_node();
            fill_node(current_node);
            enqueue(current_node, &que);
            goto anchor;
        case 2:
            current_node = dequeue(&que);
            if(current_node)
            printf("The item %d dequeued successfully\n",current_node->item);
            goto anchor;
        case 3:
            view_queue(&que);
            goto anchor;
        case 4:
            printf("total items in the queue are %d\n",que.length);
            goto anchor;
        case 5:
            printf("Thank you\n");
            exit(0);
            goto anchor;
        default:
            printf("Invalid choice...!!!\n try choosing again\n\n");
            goto anchor;
    }
  
    return 0;
}

void enqueue(node *p,queue *q)                    //definition of enqueue function
{
    if(q->rear==NULL&&q->front==NULL)
    {
        q->rear=p;
        q->front=p;
    }
    else
    {
        q->rear->next=p;
        q->rear=p;
    }
    q->length++;
    printf("item %d enqueued successfully.\n",p->item);
}

node *dequeue(queue *q)                    //definition of dequeue function
{
    node *temp=NULL;
    if(q->rear==NULL&&q->front==NULL)        //this is the case when queue is empty
    {
        printf("queue is empty hence can not be dequeued\n");
        return temp;
    }
    else if(q->rear==q->front&&q->front!=NULL)    //this is the case when queue has only one node
    {
        temp=q->front;
        q->front=NULL;
        q->rear=NULL;
    }
    else
    {
        temp=q->front;
        q->front=q->front->next;
    }
    q->length--;
    return temp;
}

void view_queue(queue *q)
{
    node *temp_front;
    temp_front=q->front;
    if(q->length==0)
    {
        printf("\nThe queue is empty...!!!\n");
        return;
    }
    printf("The queue is\n");
    while(temp_front!=NULL)
    {
        printf("%d -> ",temp_front->item);
        temp_front=temp_front->next;
    }
    printf("\n");
    return;
}

node *create_node()                    //function to create a blank node
{
    node *temp;
    temp=(node*)malloc(sizeof(node));
    temp->next=NULL;
    return temp;
}

void fill_node(node *p)                    //function to fill a blank node with values taken from user
{
    int i;
    scanf("%d",&i);                    //this is the value taken from user
    p->item=i;
}



Related Links :

C Program to get nth term of Fibonacci series using recursion | Fibonacci series in C Language

C Program to get nth term of Fibonacci series using recursion | Fibonacci series in C Language | C Assignments to print Fibonacci using recursion function




#include
#include
int fibo(int x);

int main()
{
    int n,Tn;
    printf("Enter which term do you want?\n");
    scanf("%d",&n);
    if(n> 0)
    Tn=fibo(n);
    else
    exit(printf("term index entered should be natural number\n"));
    printf("T(%d) of fibonacci series = %d\n",n,Tn);
    return 0;
}

int fibo(int x)
{
    int term;
    if(x==1)
      term=1;
    else if(x==2)
      term=1;
    else
      term= fibo(x-1) + fibo(x-2);
    return term;
}


Related Links :

C Program to Find k'th root of a number in C | C assignments to find kth root of given number

C Program to Find  k'th root of a number in C | C assignments to find kth root of given number





#include
#include
main()
{
    printf ("*********a program to find k'th root of x ....\n\n");
    char c;
    float i,j,k,m;
    do
    {
    printf ("To get k'th root of x ....\n");
    printf ("type the values .....\n\n");
    printf ("x= "); 
    scanf("%f",&i);
    printf ("k=  ");
    scanf("%f",&k);
    j=pow(i,(1/k));
    printf("\n %dth root of%f = %f\n\n\n",(int)k,i,j);
    printf("do you want to try more? y/n \t");
    fflush(stdin);
    c=getchar();
    }while(c=='y');
}

Related Links :

C Program to find nth term of the series using recursion function

C Program to find nth term of the series using recursion function | nth term using recursion | recursion Function Example in C



#include
int get_term(int x);

int main()
{
    int n,Tn;
    printf("Enter which term do you want?\n");
    scanf("%d",&n);
    Tn=get_term(n);
    printf("T(%d) = %d\n",n,Tn);
    return 0;
}

int get_term(int x)
{
    int term;
    if(x==1)
      term=2;
    else
      term= get_term(x-1) + 1;
    return term;
}




Related Links :

C Program to transpose a 2D array | Transpose of Matrix in C

C Program to transpose a 2D array | Transpose of Matrix in C | C Assignments to Transpose of matrix



#include

void main()
{
     int a[10][10], b[10][10], m, n, i, j;
     
     printf("\nEnter number of rows & columns of aray : ");
     scanf("%d %d", &m, &n);
     
     printf("\nEnter elements of 2-D array:\n");
     for(i=0; i<m; i++)
     {
              for(j=0; j<n; j++)
              {
                       scanf("%d", &a[i][j]);
              }
     }
     
     printf("\n\n2-D array before transposing:\n\n");
     for(i=0; i<m; i++)
     {
              for(j=0; j<n; j++)
              {
                       printf("\t%d", a[i][j]);
              }
              printf("\n\n");
     }
     
     /* Transposing array */
     for(i=0; i<m; i++)
     {
              for(j=0; j<n; j++)
              {
                       b[j][i] = a[i][j];
              }
     }
     
     printf("\n\n2-D array after transposing:\n\n");
     for(i=0; i<n; i++)
     {
              for(j=0; j<m; j++)
              {
                       printf("\t%d", b[i][j]);
              }
              printf("\n\n");
     }
     getch();
}



Related Links :

Bisection Method Example In C Language

Bisection Method Example In C Language | C Program to show Bisection method

 


#include
#include
#include
//bisection method to solve the equation x^4-x-10=0//
float f(float x)
{
return(pow(x,4)-pow(x,1)-10);
}
void main()
{
float x,x1,a,b,err=0.00005; //err is the max error allowed
int i=1,n;
clrscr();
printf("enter the values of a,b and maximum iterations\n");
scanf("%f%f%d",&a,&b,&n);
x=(a+b)/2;
printf("iteration no. %d x=%10.5f\n",i,x);
i++;
while(i<n)
{
if(f(a)*f(x)<0) //checking for the signs
b=x;   //new interval (a,x)//
else
a=x;   //new interval (x,b)//
x=(a+b)/2;
printf("iteration no. %d x=%10.5f\n",i,x);
if(fabs(x1-x)<err)
{
printf("\nafter %d iterations, the value of root is %f\n",i,x);
break;
}
x1=x;
i++;
}
if(i>=n)
{
printf("\nsolution does not exist as iterations are not sufficient");
}
getch();
}



Related Links :

program to delete an element from the array in C | C Language Program to delete element from array

program to delete an element from the array in C | C Language Program to delete element from array | Deleting value from Array in C



#include< stdio.h> 

void main()
{
     int a[20], n, loc, item,i;
     
     printf("\n Enter size of an array : ");
     scanf("%d", &n);

     printf("\n Enter elements of an array:\n");
     for(i=0; i< n; i++)
     {
              scanf("%d", &a[i]);
     }
     
     printf("\n Enter location of deletion: ");
     scanf("%d", &loc);
     
     item = a[loc-1];
     for(i=loc-1; i< n; i++)
     {
              a[i] = a[i+1];
     }
     n--;
     printf("\nITEM deleted: %d", item);
     
     printf("\n\nAfter deletion:\n");
     for(i=0; i< n; i++)
     {
              printf("\n%d", a[i]);
     }
     getch();
}


Related Links :

C Program to draw SWASTIKA on center on the screen with blink effect | Blink Effect in C Language

C Program to draw SWASTIKA on center on the screen with blink effect | Blink Effect in C Language | C Program to Blink Swastik Symbol | creating swastika symbol in C



Draw a SWASTIK on center on the screen with blink effect.


#include<stdio.h> 
#include<conio.h> 
void main()
{
int i=0;
clrscr();
textcolor(RED+BLINK);
for(i=8;i<25;i++)
{
gotoxy(20,i); cprintf("*");
gotoxy(40,i); cprintf("*");
if(i<23)
{ 
gotoxy(20+2,i);
cprintf("*");
gotoxy(40+2,i+2); 
cprintf("*");
}
}

for(i=23;i<=40;i++)
{
if(i<39)
{
gotoxy(40,i);
cprintf("*");
gotoxy(60,i+2); 
cprintf("*");

}

gotoxy(40+2,i); 
cprintf("*");
gotoxy(60+2,i); c
printf("*");
}
for(i=20;i<42;i++)
{
gotoxy(i,25);
cprintf("*");
gotoxy(i,40); 
cprintf("*");
if(i<40)
{
gotoxy(i+2,25-2);
cprintf("*");
gotoxy(i,40-2); cprintf("*");
}
}
for(i=40;i<62;i++) 
{
if(i<60)
{
gotoxy(i+2,10);
cprintf("*");
gotoxy(i,25);
cprintf("*");
}
gotoxy(i,10-2); 
cprintf("*");
gotoxy(i,25-2); 
cprintfr*");
} 
getch();
}




OUTPUT IN C

Related Links :

C Program to print a linked list in reverse | Linked List in reverse order in C language

C Program to print a linked list in reverse | Linked List in reverse order in C language | C assignment to print linklist in reverse Order | Link list using malloc Function | creating runtime memory allocation in C | use of malloc() function in linklist





#include
#include
typedef struct linked_list
{
    int item;
    struct linked_list *next;
}node;

node *create_list();
void print_list(node *);
void print_list_in_reverse(node *);

int main()
{
    node *head=NULL;

    printf("you can create a list by entering elements press -999 to end\n");
    head=create_list();
    printf("\n The list is\n");
    print_list(head);
    printf("\n The list in reverse order is");
    print_list_in_reverse(head);
    printf("\n");
    return 0;
}

node *create_list()
{
    int x;
    node *temp=NULL;
    scanf("%d",&x);
    if(x!=-999)
    {
        temp=(node*)malloc(sizeof(node));
        temp->item=x;
        temp->next=NULL;
        temp->next=create_list();
    }
    return temp;
}
void print_list(node *p)
{
    if(p)
    {
        printf("%d ",p->item);
        print_list(p->next);
    }
    else
    printf("\n");
}

void print_list_in_reverse(node *p)
{
    if(p)
    {
        print_list_in_reverse(p->next);
        printf("%d ",p->item);
    }
    else
    printf("\n");
}

Related Links :

C Program to create Fibonacci series using recursion | Fibonacci series using recursion in C

C Program to create Fibonacci series using recursion | Fibonacci series using recursion in C | Fibonacci series with recursion function in C language



#include
#include
int fibo(int x);

int main()
{
    int n,Tn;
    printf("Enter which term do you want?\n");
    scanf("%d",&n);
    if(n>0)
    Tn=fibo(n);
    else
    exit(printf("term index entered should be natural number\n"));
    printf("T(%d) of Fibonacci series = %d\n",n,Tn);
    return 0;
}

int fibo(int x) // UD Function 
{
    int term;
    if(x==1)
      term=1;
    else if(x==2)
      term=1;
    else
      term= fibo(x-1) + fibo(x-2);
    return term;
}



Related Links :

binary Conversion of given Digit in C

binary Conversion of given Digit in C | C Program for binary Conversion | Digit to binary conversion in C



#include
main()
{
    int i=0,j,k,l,m;
    char str1[50],str2[50];
    printf("Enter a decimal integer => ");
    scanf("%d",&k);
    m=k;                   //it storing k for further use
    while(k>0){            //the while loop for storing remainder
       if(k%2==0)          //of each division by 2 in a string
       str1[i]='0';        //variable str1
       else
       str1[i]='1';
       k/=2;
       i++;
    }
    str1[i]='\0';
    l=i;
    j=i-1;
    for(i=0;j>=0;i++,j--)
      str2[j]= str1[i];
    printf("The binary equivalent of %d is ==> ",m);
    for(j=0;j<l;j++)
    putchar(str2[j]);
    getchar();
}



Related Links :

C Program to Print the Double Pyramid Pattern | creating double Pyramid in C

C Program to Print the Double Pyramid Pattern | creating double Pyramid in C | C assignment to create double Pyramid of Alphabets in C



#include
#include

void main()
{
int i,j,k;
int blank=0;
int lines = 6;
char symbol='A';
int temp ;
int diff[7]= {0,1,3,5,7,9,11};
clrscr();
k=0;
//Step 0

    for(i=lines;i>=0;i--)
    {
       printf("\n");
       symbol = 'A';

       for(j=i;j>=0;j--)    //step 1
            printf("%c ",symbol++);

       blank = diff[k++];   //step 2

       for(j=0;j<blank;j++)
            printf(" ");    //step 3


       symbol = 'F' - (blank/2);

       if (blank== 0)
           temp = i-1;
       else
           temp = i;

       for(j=0;j<=temp;j++)  //step 4
           printf("%c ",symbol--);

    }
getch();
}



Related Links :

C program to Pascal's triangle

Pascal's triangle in C language | C Program to create Pascal's triangle | Pascal's triangle in C 




#include
main()
{
    int i,j,k[50],l[50][50],m;
    printf("Enter the number of rows.");
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        for(j=0;j<(m-i);j++)
        printf("   ");
        for(j=0;j<i;j++)
        {
            k[0]=1;
            if(j==i-1)
            k[j]=1;
            l[i][j]=k[j];
            k[j+1]=l[i-1][j]+l[i-1][j+1];
            printf("%5d ",k[j]);
        }
        printf("\n");
    }
}

Related Links :

Triangle pyramid of Numbers in C

Triangle pyramid of Numbers in C | C Program to Print Inverse Triangle of Numbers in C




#include
#include
int main()
{
 int num,r,c,sp;
 printf("Enter loop repeat number : ");
 scanf("%d",&num);
 for(r=1; r<=num; r++)
 {
   for(sp=r; sp>1; sp--)
      printf(" ");
   for(c=r; c<=num; c++)
      printf("%d",c);
   for(c=num-1; c>=r; c--)
      printf("%d",c);
   printf("\n");
 }
 getch():
 return 0;
}





Related Links :

swapping two values using malloc function in C | Swapping Numbers using malloc in C

Swapping two values using malloc function in C | Swapping Numbers using malloc in C



#include
#include
struct stud
{
 char nam[30];
 struct stud *next;
};
struct stud *p,*q;
int main()
{
 p=(struct stud *)malloc(sizeof(struct stud));
 q=(struct stud *)malloc(sizeof(struct stud));
 printf("Enter name p : ");
 gets(p->nam);
 //fflush(stdin);
 printf("Enter name q : ");
 gets(q->nam);
 p->next=q;
 q->next=p;
 printf("\nName of p : %s",p->next->nam);
 printf("\nName of q : %s",q->next->nam);
 getch();
 return 0;
}


Output :
Enter name p : Ram
Enter name q : Shyam

Name of p : Shyam
Name of q : Ram

Related Links :

Program to shows Size of different data types in C | Printing Size of Data types in C

Program to shows Size of different data types in C | Printing Size of Data types in C


#include
#include
int main()
{
 int i;
 char ch;
 float f;
 long l;
 double d; 
 long double ld;
 printf("\n\tSize of different data types as following");
 printf("\n");
 printf("\nSize of character ch is %d",sizeof(ch));
 printf("\nSize of integer is %d",sizeof(i)); 
 printf("\nSize of float is %d",sizeof(f));
 printf("\nSize of long is %d",sizeof(l));
 printf("\nSize of double is %d",sizeof(d));
 printf("\nSize of long double is %d",sizeof(ld));
 getch();
 return 0;
}



Related Links :

swap numbers using call by reference in C program

swap numbers using call by reference in C program



#include
#include
void swaping(int *x, int *y);
int main()
{
 int n1,n2;
 printf("Enter first number (n1) : ");
 scanf("%d",&n1);
 printf("Enter second number (n2) : "); 
 scanf("%d",&n2);
 printf("\nBefore swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 swaping(&n1,&n2);
 printf("\nAfter swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 getch();
 return 0;
}
void swaping(int *x, int *y)
{
  int z;
  z=*x;
  *x=*y;
  *y=z;
} 



Related Links :

GCD or HCF of two numbers in C | C Program to find GCD and HCF

GCD or HCF of two numbers in C | C Program to find GCD and HCF



#include
 #include
 int main()
 {
  int n1,n2,rem;
  printf("Enter first number : ");
  scanf("%d",&n1);
  printf("Enter second number : ");
  scanf("%d",&n2);
  if(n2>n1)
  {
    n1=n1+n2;
    n2=n1-n2;
    n1=n1-n2;
  }
  do
{
      rem=n1%n2;
      n1=n2;
      n2=rem;
    }
while(rem!=0);
  printf("\nGCD of two number is %d",n1);
  getch();
  return 0;
 }


Related Links :

Addition of two 3x3 matrix in C | Addition on matrix in C

Addition of two 3x3 matrix in C | Addition on matrix in C

 


#include
#include
int main()
{
 int mata[3][3],matb[3][3],matc[3][3];
 int r,c,k;
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter first matrix : ");
    scanf("%d",&mata[r][c]);
  }
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    printf("Enter second matrix : ");
    scanf("%d",&matb[r][c]);
  } 
 }
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
  {
    matc[r][c]=0;
    for(k=0; k<3;k++)
       matc[r][c]=mata[r][c] + matb[r][c];
  }
 }
 printf("New addition matrix : \n"); 
 for(r=0; r<3; r++)
 {
  for(c=0; c<3; c++)
     printf(" %d",matc[r][c]);
  printf("\n");
 }
 getch();
 return 0;
}

Related Links :

C program for counting the number of characters and words in a given string

C program for counting the number of characters and words in a given string 


Program to find number Chars in given String, without strlen function, counting chars in given line


#include
#include
int main()
{
 int count_words=0,i;
 int count_char=0;
 char str[20];
 printf("Enter string : ");
 gets(str);
 for(i=0; str[i]!=NULL; i++)
 {
   count_char++;
   if(str[i]==' ')
      count_words++;
 }
 printf("\nNumber of characters in string : %d",count_char);
 printf("\nNumber of words in string : % d",count_words+1);
 getch();
 return 0;
}

Related Links :

C program to find whether a number is prime or not.

C program to find whether a number is prime or not.


Prime Number in C, C Program to find Prime Number, Prime Number in C Language, c Assignment for Prime Number


#include
#include
int main()
{
 int x,num;
 printf("Enter number : ");
 scanf("%d",&num);
 x=2;
 while(x<=num-1)
 {
   if(num%x==0)
   {
      printf("Number is not prime!!");
      break;
   }
   x++;
 }
 if(x==num)
    printf("Number is prime!!");
 getch();
 return 0;
}

Related Links :

How to Declare Union in C ? | Using Union in C | Union Example in C | C Program to Explain use of union

How to Declare Union in C ? | Using Union in C | Union Example in C | C Program to Explain use of union



union tag
{
union_member1;
union_member2;
union_member3;
..
..
..
union_memberN;
}instance;





    Union Members that compose a union , all share the same storage area within the computers memory
    Each member within a structure is assigned its own unique storage area
    Thus unions are used to observe memory.
    Unions are useful for application involving multiple members , where values need not be assigned to all the members at any one time.

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!