Pages

E-mail us at prgms4starters@gmail.com for any queries or to share your programs. Please mention the program definition in the subject line.

In case you need help in finishing a program, do mention "incomplete pgm" in the subject line. Our team of experts will do their best to help you.

Click the link to follow this page for updates and reminders: FOLLOW THIS SITE

You can also visit our Facebook page by clicking HERE

**don't forget to participate in the polls at the bottom of the page

Tuesday, November 9, 2010

Implementation of stack using operator overloading

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

using namespace std;
int s[100];

class stackk
{
public:
int top;
stackk():top(-1)
{}

void operator +(int d)
{
top++;
s[top]=d;
cout<<"\n data added is"<<s[top];


}
void operator --()
{
top--;
}
void operator ++()
{
int a=top;
while(a>=0)
{
cout<<s[a];
a--;
}
}

};
int main()
{
system("cls");
stackk s1;
int data,ch,f=1;
while(f==1)
{
cout<<"\n 1.push \n2.pop \n 3.display";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n enter the data to push";
cin>>data;
s1+data;
break;
case 2:
--s1;
break;
case 3:
++s1;
break;
case 4:return 0;
break;
}
cout<<"\n press 1 to conitnue";
cin>>f;
}
return 1;
}

C program to check if a date is valid

 include<stdio.h>
void main()
{
int dd,mm,yy,temp=1;
clrscr();
printf("\nenter date in format dd mm yy");
scanf("%d%d%d",&dd,&mm,&yy);
if(dd<=0||dd>31||mm<=0||mm>12||yy<=0)temp=0;
else
if((mm==4||mm==6||mm==9||mm==1)&&(dd>30))temp=0;
else
if(mm==2)
{
       if(yy%4==0)
         {
          if(dd>29)temp=0;
          }
else
        {
          if(dd>28)temp=0;
         }
}
if(temp) printf("\nDate is valid");
else printf("\nDate is invalid");
getch();
}

C program to find Julian Date (day of the year)

This program does not check for validity of date ie. user has to enter a vaild date in the form dd mm yy
eg.31 12 04 ->julian date: 366 since its a leap year

#include<stdio.h>
void main()
{
int dd,mm,yy,julian;
clrscr();
printf("\nenter a valid date (dd mm yy)");
scanf("%d%d%d",&dd,&mm,&yy);
julian=dd;
mm=mm-1;
switch(mm)
{
case 11 : julian=julian+30;
case 10 : julian=julian+31;
case 9 : julian=julian+30;
case 8 : julian=julian+31;
case 7 : julian=julian+31;
case 6: julian=julian+30;
case 5: julian=julian+31;
case 4 : julian=julian+30;
case 3 : julian=julian+31;
case 2 : if(yy%4==0)julian=julian+29;
             else julian=julian+28;
case 1 : julian=julian+31;
}
printf("\nJulian date is %d",julian);
getch();
}

Monday, November 8, 2010

C++ program to convert celsius to fahrenheit

#include<iostream>
#include<conio.h>
using namespace std:
void main ()
{
float c,f;
system("cls");
cout<<"Enter the value of celcius: ";
cin>>c;
f=(float)9/5*c+32;
cout<<"\nFahrenheit is:"<<f;
getch ();
}

C++ program to print stars upside down like a pyramid

#include<iostream>
#include<conio.h>
using namespace std:
void main()
{
system("cls");
int j=1;
for (int i=5;i>=1;i--)
{
for (int k=1;k<=j;k++)
{
cout << " ";
}
for (int x=1;x<=i;x++)
{
cout << " *";
}
cout <<endl;
j=j+1;
}
getch();
}

C++ program for printing the table of any given no:

#include<iostream>
#include<conio.h>
using namespace std:
void main()
{
system("cls");
int num1,num2;
cout <<"enter first number";
cin >> num1;
cout <<"enter second number";
cin >> num2;
for (int i=num1;i<=num2;i++)
{
for(int k=1;k<=10;k++)
{
cout << i*k<<endl;
}
getch();
}
getch();
}
}

c program to find the transpose of a matrix and find the product of the transpose and the original matrix

#include<stdio.h>

int main()
{

int A[10][10],B[10][10],C[10][10],i,j,k,n,ch=1;

while(ch==1)
{
printf("Matrix\n");
printf("To find transpose and multiply with the original matrix:\n");

printf("Enter the limit of the matrix:\n");
scanf("%d",&n);
printf("\n");
printf("Enter the elements of matrix A:\n");

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

{
for(j=0;j<n;j++)

{
scanf("%d",&A[i][j]);

}
}

printf("\nThe original matrix A:\n");

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

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

}

printf("\nThe transpose matrix :\n");

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

{
for(j=0;j<n;j++)

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

{
for(j=0;j<n;j++)

{
C[i][j]=0;
for(k=0;k<n;k++)
{
C[i][j]+=(A[i][k]*B[k][j]);
}
}
}

printf("\nThe multiplied matrix:\n");

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

{
for(j=0;j<n;j++)

{
printf("%d\t",C[i][j]);
}

printf("\n");
}
printf("\nEnter 1 to continue and 0 to exit:");
scanf("%d",&ch);

}
}

c program to display armstrong numbers from 1-500

#include<stdio.h>
int main()
{
int i,a,j,sum;
printf("\n to display armstrong nos. from 1 to 500\n");
for(i=1;i<=500;i++)
{
j=i;
sum=0;
while(j>0)
{
a=j%10;
sum=sum+(a*a*a);
j=j/10;
}
if(i==sum)
printf("\n%d\n",sum);
}}

c program to generate a menu driven application to ship stocks to customers checking their credits and stock availability

#include<stdio.h>
main()
{
int credit,qty,stock=10000,bal,s,d,m,y,q,n=1;

printf("\nWELCOME\n");
printf("\n checking system date, please enter the date\n");
printf("\nday:");
scanf("%d",&d);
printf("\nmonth:");
scanf("%d",&m);
printf("\nyear:");
scanf("%d",&y);
printf("\n Enter your credit\n");
scanf("%d",&credit);
while(n==1)
{
printf("\n Enter you order qty\n");
scanf("%d",&qty);
s=qty*2000;
bal=credit-s;
if(credit<s)
printf("\n sorry your credit is low, your requirement can't be supplied\n");
else
if(credit>=s&&qty<stock)
{
printf("\n Thank you, your requirement of %d items has been supplied\n",qty);
printf("\n the total cost is Rs. %d",s);
printf("\n your current credit is Rs%d\t",bal);
}
else
if(credit>=s&&qty>stock)
{
printf("\n your credit is ok but your requirement is more than the stock present\n");
q=qty-stock;
printf("\n Thank you, your requirement of %d items has been supplied\n",qty);
printf("\n your current credit is Rs%d\t",bal);
printf("\n the remaining qty: %d items will be shipped after 1 month,on %d.%d.%d\n",q,d,m+1,y);
printf("\n sorry for the inconvenience\n");
}
printf("\n enter 1 to order more items and 0 to exit\n");
scanf("%d",&n);
}
}

Saturday, November 6, 2010

COMPILERS

For those of you who are still wondering which compilers to use, heres a list of some of the widely used compilers with links to download sites. For more compilers visit the site www.brothersoft.com.

(click on the name to go to the download page)

1)Turbo C 3.0

2) Visual C++ Express Edition 2008

3) Dev-C++ 4.9.9.2

4) C++Builder 2010

Friday, November 5, 2010

C Program to check if a number is prime or not

#include<stdio.h>
int main()
{int num,i,count=1;
printf("\n Enter the number:");
scanf("%d",&num);
if(num==0||num==1)
printf("\nThe number %d is neither prime nor composite",num);
else
{
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count=0;
break;
}
}
if(count==1)
{
printf("\n The number %d is a prime number",num);
}
else
{
printf("\n The number %d is not a prime number",num);
}
}
}

C Program to generate fibonacci series

#include<stdio.h>
int main()
{
int num,a=0,b=1,c=0,i;
printf("\nEnter the number:");
scanf("%d",&num);
printf("%d\t%d",a,b);
for(i=2;i<num;i++)
{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
}
}

C program to count the occurrence of 2 consecutive vowels

#include<stdio.h>
#include<ctype.h>

int main()
{

int i,flag=0,count=0;
char a[100];

printf("Enter the string:");

scanf("%[^\n]",a);

for(i=0;a[i]!='\0';i++)

{
a[i]=(toupper(a[i]));

if((a[i]=='A') || (a[i]=='E') || (a[i]=='I') || (a[i]=='O') || (a[i]=='U'))

{a[i+1]=(toupper(a[i+1]));
if((a[i+1]=='A') || (a[i+1]=='E') || (a[i+1]=='I') || (a[i+1]=='O') || (a[i+1]=='U'))

{

count++;
flag=0;
printf("\n the occurrences are\n");
printf("%c",a[i]);
printf("%c\n",a[i+1]);
}

}
}

printf("The no of such occurrences %d\n",count);

}

C Program to generate tribonacci series

#include<stdio.h>
int main()
{int num,a=0,b=1,c=2,d=0,i;
printf("\nEnter the number:");
scanf("%d",&num);
printf("%d\t%d\t%d",a,b,c);
for(i=3;i<num;i++)
{
d=a+b+c;
printf("\t%d ",d);
a=b;
b=c;
c=d;
}
}

C program to sort names in alphabetical order

#include<stdio.h>
struct names
{
char a[100];
}s[100];
int i,j,n;
int main()
{struct names temp;
printf("\n enter the no of names to be entered");
scanf("%d",&n);
printf("\n enter the names");
for(i=0;i<n;i++)
{
scanf("%s",s[i].a);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(s[i].a,s[j].a)>=0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}}
printf("\n sorted names is ");
for(i=0;i<n;i++)
printf("%s\t",s[i].a);
}

C program to reverse a given string

#include<stdio.h>
main()
{
int ret=1;
while(ret==1)
{
char *s[]={ "To err is human...",
"But to really mess things up...",
"One must really know C"
};
char a[3][300]; int i=0,j=0,count=0;

while(i<3)
{
count=0;
for(j=0;(*(s[i]+j))!='\0';j++)
count++;
a[2-i][count]='\0';
for(j=0;j<count;j++)
a[2-i][count-j-1]=*(s[i]+j);
i++;
}
printf("\n\nThe given string is:\n");
for(i=0;i<3;i++)
{
count=0;
for(j=0;(*(s[i]+j))!='\0';j++)
count++;
for(j=0;j<count;j++)
{
printf("%c",*(s[i]+j));
}
printf("\n");
}

printf("\n\nThe reverse of the given string is:\n");
for(i=0;i<3;i++)
{
count=0;
for(j=0;a[i][j]!='\0';j++)
count++;
for(j=0;j<count;j++)
{
printf("%c",a[i][j]);
}
printf("\n");
}
printf("Enter 1 to to continue and 0 to exit:");
scanf("%d",&ret);
}
}

Binary and Linear Search

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
int binarysearch(int[],int,int);
void sort(int[],int);
int count;

int linear(int [],int,int);
int iar[20],isize,ioptn,item,cou=0,ivalu;
void main()
{

clrscr();
int ar[50],item,n,index,b=1,ic,w=1;
while(w==1)
{
 cout<<"\nSearching techinques";
 cout<<"\n______________________";
 cout<<"\n1.Binary seaching:";
 cout<<"\n2.Linear searching:";
 cout<<"\n3.Exit:";
 cout<<"\n Enter the choice:";
 cin>>ic;
 switch(ic)
 {
 case 1:
 cout<<"\n_____________________";
 cout<<"\nEnter the no of elements: ";
 cin>>n;
 cout<<"\nEnter the elements:\n ";
 for(int i=0;i<n; i++)
  {
 cin>>ar[i];
  }
 while(b==1)
 {
  sort(ar,n);
  cout<<"\nThe sorted array is: \n";
  for( i=0;i<n;i++)
  {
 cout<<ar[i]<<" ";

  }
 cout<<"\nEnter the item to be searched: ";
 cin>>item;
 index=binarysearch(ar,n,item);
 if(index==-1)
 cout<<"\ninvalid item: ";
 else
 cout<<"\nElement found at position: "<<index+1;
 cout<<"\nTotal no of comparisons= "<<count;

 cout<<"\nPress 1 to continue or any other digits to exit : ";
 cin>>b;
 }
 break;
case 2:
clrscr();
cout<<"\n  SEARCHING TECHIQUES";
cout<<"\n ______________________";
cout<<"\n Linear searching:";
cout<<"\nEnter the array size :";
cin>>isize;
cout<<"Enter the Elements of the array :";
for(i=0;i<isize;i++)
{
cin>>iar[i];
cout<<"\t";
}
while(n==1)
{
cout<<"\nEnter the item to search :";
cin>>item;
ivalu=linear(iar,isize,item);
cout<<"\n The array : ";
for(int j=0;j<isize;j++)
{
 cout<<iar[j];
 cout<<"\t";
}
if(ivalu==-1)
cout<<"\nThe element was not found:";
else
{
cout<<"\n The element found ";
cout<<"\n The element :"<<item;
cout<<"\n The postion of the item is:"<<ivalu+1;
cout<<"\n Total number of searching:"<<cou;
}
cout<<"\nPress 1 to continue or any other no to exit :";
cin>>n;
 }
break;
case 3:exit(0);
       break;
}
}
cout<<"\nPress 1 to continue or any other no to exit :";
cin>>w;
getch();
}

void sort(int ar[],int size)
{
int s,p,temp;
for(int i=0;i<size;i++)
{
s=ar[i];
p=i;
for(int j=i+1;j<size;j++)
{
if( ar[j]<s)
{
s=ar[j];
p=j;
}
}
temp=ar[i];
ar[i]=ar[p];
ar[p]=temp;

}

}

int binarysearch(int ar[],int size,int item)
{


int low,high,mid;
low=0;
high=size-1;
while(low<=high)
{
count++;
mid=(low+high)/2;
if(item==ar[mid])
return mid;
else if(item>ar[mid])
low=mid+1;
else
high=mid-1;
}
return -1;
}

int linear(int iar[],int isize,int item)
{
  for(int i=0;i<isize-1;i++)
  {
  cou++;
  if(iar[i]==item)
   return i;

  }
  return -1;

}

Sorting using insertion and selection

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int a[15],n;
void select()
{
int small,k,j,pos;
for(k=0;k<n;k++)
{
small=a[k];
pos=k;
for(j=k+1;j<n;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;
}
}
int temp=a[k];
a[k]=a[pos];
a[pos]=temp;
cout<<a[k]<<"\t";
}
}
void insert()
{
int t,l;
for(int i=0;i<n;i++)
{

t=a[i];
l=i-1;
while(t<a[l]&&l>=0)
{
a[l+1]=a[l];
l--;
}
a[l+1]=t;

}
}
void entry()
{
cout<<"\n enter the number of elements to be sorted";
cin>>n;
cout<<"\n enter the array";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void main()
{ clrscr();
int c=1,h;

while(c==1)
{
cout<<"\n welcome to the menu";
cout<<"\n 1. selection sort \n 2.insertion sort";
cout<<"\n enter your choice";
cin>>h;
switch(h)
{
case 1: entry();
cout<<"\n sorted array is ";
select();
break;
case 2: entry();
cout<<"\n sorted array is";
insert();
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
break;
default:cout<<"\n invalid entry";
}
cout<<"\n press 1 to continue";
cin>>c;
}
}

Program to display the calendar of a particular month and find out which is the next Monday's date

#include<iostream>
#include<conio.h>
using namespace std;

class date
{
char ch;
public:
int day,month,year,i,j;
date():day(0),month(0),year(0)
{}
void get()
{
cout<<"\n enter the date";
cin>>day>>ch>>month>>ch>>year;
}
void operator ++()
{
cout<<"\n"<<"mon"<<"\t"<<"tue"<<"\t"<<"wed"
<<"\t"<<"thu"<<"\t"<<"fri"<<"\tsat\tsun";
cout<<"\n";
for(i=1,j=0;i<=31;i++,j++)
{ cout<<i<<"\t";
if(j==6)
{ j=-1;cout<<endl;}
}
cout<<"\n";
}
void operator -()
{
int week=0;
int dd=0,mm=0,yy=0;
dd+=day;
mm=(dd/31)+month;
yy+=year+(mm/12);
// cout<<dd;
int k;
for(i=1,j=7,k=1;k<=5;j=j+7,i=i+7,k++)
{
if(dd>=i&&dd<=(j))
week=k;
}
if(week==5)
cout<<(week)<<ch<<mm+1<<ch<<yy;
else
cout<<((week*7)+1)<<ch<<mm<<ch<<yy;

}
};

void main()
{
date d1,d2,d3;
int ch,f=1;
system("cls");

while(f==1)
{
cout<<"\n main menu";
cout<<"\n1.display the calendar\n2.to find the next mondays date";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1: ++d1;
break;
case 2:
d2.get();
-d2;
break;
default:cout<<"\n invalid entry";
}
cout<<"\n press 1 to continue";
cin>>ch;
}
getch();
}

C++ program to SWAP without using temp variable/call by reference

#include<iostream.h>
#include<conio.h>
class swap
{ public:
int a,b;
void display()
{
cout<<"\n enter the value of the first number to be swapped";
cin>>a;
cout<<"\n enter the value of the second number to be swapped";
cin>>b;
cout<<"\n value before swapping is"<<a<<" "<<b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\n value after swapping is "<<a<<" "<<b;
}
};

void main()
{
clrscr();
swap s;
s.display();
getch();
}

C program to convert a decimal integer to binary form

#include<stdio.h>
int array[100],a,i,count=0,temp;
void binary(int n)
{

if(n>0)
{
array[count]=n%2;
count++;
binary(n/2);
}
else
{
count--;
for(i=0;i<=(count+1)/2;i++)
{
temp=array[i];
array[i]=array[count-i];
array[count-i]=temp;
}

}
}
main()
{
int x=0;
while(x==0)
{
int n;
count=0;
printf("\nEnter a decimal integer: ");
scanf("%d",&n);
binary(n);
printf("The binary form of %d is:\n",n);
for(i=0;i<=count;i++)
printf("%d",array[i]);
printf("\nPress 0 to repeat conversion or any other digit to exit: ");
scanf("%d",&x);
}

}

C++ program for the application of QUEUE

#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
int f,r,q[50],n;
class queue
{
public:

void insert();
void deletion();
void front();
void rear();
void display();
void clearq();
void qsize();
};

void queue::insert()
{ int item;
if(r==(n-1))
{
cout<<"\n queue overflow";
}
else
{
if(r==-1 &&f==-1)
{
f=0;
r=0;
}
else
{
r++;
}
cout<<"\n enter the item to enter ";
cin>>item;
q[r]=item;
}

}
void queue::deletion()
{
int item;

if(f>r||(f==-1&&r==-1))
{
cout<<"\n underflow";
f=-1;
r=-1;
}
else
{
item=q[f];
f=f+1;
cout<<"\n deleted item ="<<item;
}
}
void queue::front()
{
if(f>r||(f==-1&&r==-1))
cout<<"\n underflow";
else
cout<<"\n queue front="<<q[f];
}
void queue::rear()
{
if(r>=n)
cout<<"\n overflow";
else
cout<<"\n queue rear="<<q[r] ;
}

void queue::display()
{
int i;
if(f==-1)
cout<<"\n no element element has been inserted";
else
{
for(i=f;i<=r;i++)

cout<<"|"<<q[i]<<"|";

}
}
void queue::clearq()
{
if(f>r)
cout<<"\n no element inserted";
else
{
q[r]='\0';
}
f=-1;
r=-1;
}
void queue::qsize()
{ int size;
if(f==-1&&r==-1)
cout<<"\n the queue is empty";
else
{
size=(r-f)+1;
cout<<"\n size of queue is="<<size;
}
}
void main()
{ f=-1;
r=-1;
int w=1,ch;
queue q1;
system("cls");
cout<<"\n ENTER THE MAXIMUM SIZE OF THE QUEUE";
cin>>n;
while(w==1)
{
system("cls");
cout<<"\n WELCOME TO THE MENU ";
cout<<"\n ====================================================";
cout<<"\n 1.To insert an element into the queue \n 2.To delete an element from the queue \n 3.To display front of the queue \n 4.To dispaly the rear of the queue \n 5.To display the elements in the queue \n 6.TO clear the queue \n 7.To show the size of the queue \n 8.exit";
cout<<"\n ===================================================";
cout<<"\n PLEASE ENTER ANY OF THE ABOVE CHOICE ";
cin>>ch;
switch(ch)
{
case 1:q1.insert();
break;
case 2:q1.deletion();
break;
case 3:q1.front();
break;
case 4:q1.rear();
break;
case 5:q1.display();
break;
case 6:q1.clearq();
cout<<"\n the queue has been cleared";
break;
case 7:q1.qsize();
break;
case 8:exit(0);
break;
default: cout<<"\n invalid entry";
}
cout<<"\n press (1) to continue or any other digit to exit";
cin>>w;
}
}

C++ program to manipulate complex no using class

#include<iostream.h>
#include<conio.h>

class complex
{
public:
float real;
float imag;

complex():real(0),imag(0)
{
}

}a,b,ans;
void add()
{
ans.real=a.real+b.real;
ans.imag=a.imag+b.imag;
cout<<"\n\nThe sum is "<<ans.real<<"+i"<<ans.imag;
}

void subtract()
{
ans.real=a.real-b.real;
ans.imag=a.imag-b.imag;
cout<<"\n\nThe difference is"<<ans.real<<"+i"<<ans.imag;
}

void multiply()
{
ans.real=(a.real*b.real)-(a.imag*b.imag);
ans.imag=(a.real*b.imag)+(a.imag*b.real);
cout<<"\n\nThe product is "<<ans.real<<"+i"<<ans.imag;
}

void divide()
{
float r,i,n;
r=(a.real*b.real)+(a.imag*b.imag);
i=(a.imag*b.real)-(b.imag*a.real);
n=(b.real)*(b.real)+(b.imag)*(b.imag);
ans.real=(r/n);
ans.imag=(i/n);
if(ans.imag<0)
{
ans.imag=-(ans.imag);
cout<<"\n\nThe quotient is "<<ans.real<<"-i"<<ans.imag;
}
else
{
cout<<"\n\nThe quotient is "<<ans.real<<"+i"<<ans.imag;
}
}

void main()
{
int c=1,ch;
clrscr();
cout<<"\n\nCOMPLEX NUMBER MANIPULATION";
while(c==1)
{

cout<<"\n welcome to the menu \n";
cout<<"\n1. Addition";
cout<<"\n2. Subtraction";
cout<<"\n3. Multiplication";
cout<<"\n4. Division";
cout<<"\nEnter choice: ";
cin>>ch;
if(ch>0||ch<=4)
{
cout<<"\nReal part of 1st number: ";
cin>>a.real;
cout<<"\nImaginary part of 1st number: ";
cin>>a.imag;
cout<<"\nReal part of 2nd number: ";
cin>>b.real;
cout<<"\nImaginary part of 2nd number: ";
cin>>b.imag;
}
switch(ch)
{
case 1:
add();
break;

case 2:
subtract();
break;

case 3:
multiply();
break;

case 4:
divide();
break;
default:cout<<"\n invalid entry";

}

cout<<"\n\nPress 1 to continue or any other digit to exit: ";
cin>>c;
clrscr();
}
}

stack implementation program in c++

#include<iostream.h>
#include<conio.h>
#include<process.h>

int stack[50],top=-1,item,n,i,size;

class stack1
{
public:
void push()
{

if(top==size-1)
{
cout<<"\n no more items can be pushed\n";
}
else
{
cout<<"enter the item to be pushed\n";
top++;
cin>>item;
stack[top]=item;
}
}


void pop()
{
if(top==-1)

cout<<"\nstack underflow\n";
else
{
cout<<"the item deleted is \t"<<stack[top];
top--;
}}

void stacktop()
{
if(top==-1)
cout<<"\nstack underflow\n";
else
cout<<"the top of the stack is "<<stack[top];
}

void clearstack()
{

if(top==-1)
cout<<"\n0 items in stack\n";
else
{
for(i=top;i>=0;i--)
stack[i]='\0';
top=-1;
}
}



void display()
{
if(top==-1)
cout<<"\n 0 items in stack\n";
else
{
for(i=top;i>=0;i--)
cout<<stack[i]<<"\n";
}
}
};

void main()
{
clrscr();
int a,b=1;
stack1 s;
cout<<"enter the size of the stack\t";
cin>>size;
while(b==1)
{
clrscr();
cout<<"\nMAIN MENU\n";
cout<<"\n1.push\n"<<"2.pop\n"<<"3.stack top\n"<<"4.clearstack\n"<<"5.display\n"<<"6.exit\n";
cout<<"\nenter your choice\t";
cin>>a;
switch(a)
{
case 1:s.push();
break;

case 2:s.pop();
break;

case 3:s.stacktop();
break;

case 4: s.clearstack();
cout<<"\nthe stack has been cleared\n";
break;

case 5: s.display();
break;
default:
cout<<"\nwrong choice\n";

case 6: exit(0);
break;


}
cout<<"\npress 1 to go to main menu or 0 to exit\n";
cin>>b;
}
}

Circular Queue




#include<iostrem.h>
#include<conio.h>
#include<process.h>
int q[50],f,r,n;
class cqueue
{ public:
void enqueue();
void dequeue();
void front();
void rear();
void display();
void clearq();
void qsize();
};
void cqueue::enqueue()
{ int item;
if((f==0&&r==(n-1))||(f==r+1))
cout<<"\n overflow";
else if(r==(n-1))
{ r=0;
cout<<"\n enter the item to enter";
cin>>item;
q[r]=item;
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
else if(f==-1&&r==-1)
{ f=r=0;
cout<<"\n enter the item to enter";
cin>>item;
q[r]=item;
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
else
{
r=((r+1)%n);
cout<<"\n enter the item to enter";
cin>>item;
q[r]=item;
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
}

void cqueue::dequeue()
{
if(f==-1&&r==-1)
{
cout<<"\n underflow";
}
else
{
cout<<"\n deleted value is "<<q[f];
if(f==r)
f=r=-1;
else if(f==(n-1))
f=0;
else
f=((f+1)%n);
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}


}

void cqueue::front()
{
if(f>r||(f==-1&&r==-1))
cout<<"\n underflow";
else
cout<<"\n queue front="<<q[f];
cout<<"\n queue rear="<<r;
}
void cqueue::rear()
{
if(r>=n)
cout<<"\n overflow";
else
cout<<"\n queue rear="<<q[r];
cout<<"\n queue front="<<f;
}

void cqueue::display()
{
int i;
if(f==-1&&r==-1)
cout<<"\n no element element has been inserted";

else
{ if(f>r)
{ for(i=0;i<=r;i++)
cout<<"|"<<q[i]<<"|";
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
for(i=f;i<n;i++)
cout<<"|"<<q[i]<<"|";
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}


else
{
if(r>=f)
{ for(i=f;i<=r;i++)
cout<<"|"<<q[i]<<"|";
cout<<"\n queue rear="<<r> ; }
}
} }
void cqueue::clearq()
{
if(f==-1)
cout<<"\\n no element inserted";
else
{
q[r]='\0';
}
f=-1;
r=-1;
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}
void cqueue::qsize()
{if(f==-1&&r==-1)
cout<<"\n no element in the queue";
else if(f<=r)
{cout<<"\n queue size is"<<(r-f+1);
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ; }
else
{ cout<<"\n queue size is"<<(n-f+r+1);
cout<<"\n queue front="<<f;
cout<<"\n queue rear="<<r ;
}

}
void main()
{ f=-1;
r=-1;
int w=1,ch;
cqueue c;
clrscr();
cout<<"\n ENTER THE MAXIMUM SIZE OF THE QUEUE";
cin>>n;
while(w==1)
{
clrscr();
cout<<"\n WELCOME TO THE MENU ";
cout<<"\n ====================================================";
cout<<"\n 1.To insert an element into the queue \n 2.To delete an element from the queue \n 3.To display front of the queue \n 4.To dispaly the rear of the queue \n 5.To display the elements in the queue \n 6.TO clear the queue \n 7.size of queue \n 8.exit";
cout<<"\n ===================================================";
cout<<"\n PLEASE ENTER ANY OF THE ABOVE CHOICE ";
cin>>ch;
switch(ch)
{
case 1:c.enqueue();
break;
case 2:c.dequeue();
break;
case 3:c.front();
break;
case 4:c.rear();
break;
case 5:c.display();
break;
case 6:c.clearq();
cout<<"\n the queue has been cleared";
break;
case 7:c.qsize();
break;
case 8:exit(0);
default: cout<<"\n invalid entry";
}
cout<<"\n press (1) to continue or any other digit to exit";
cin>>w;
}
}


C program to find the no of vowels in a line of text

#include<stdio.h>
main()
{
int x=0;
char i=0,a[100];
int count=0,count2=0,choice;
printf("\n\nEnter a line of text:");
scanf("%[^\n]",a);

while(a[i]!='\0')
{
if(a[i]!=' ')
count++;
if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||a[i]=='U')
{
count2++;
}
i++;
}
printf("\nThe number of letters in this word is: %d",count);
printf("\nThe number of vowels in this word is: %d\n",count2);
}


Library Management System using Virtual Functions in C++

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;

int isbn2,index;
int search(int isbn);
int nstaff=0,nstud=0,bkid=0;
class library
{
public:
virtual int returnbook(){return 0;}
virtual int takebook(){return 0;}
virtual void addbook(){}
virtual void addstaff(){}
virtual void adds(){}
virtual int isb(){return 0;}
virtual string str(){return"";}
virtual int sto(){return 0;}
};

library *stud[100],*sta[100],*bks[100];

class library_store:public library
{
public:
string bookname;
int isbn;
int stock;
library_store():stock(0)
{ }

int isb(){return isbn;}
string str(){return bookname;}
int sto(){ return stock;}

int returnbook()
{
stock++;
return 1;
}
int takebook()
{
if(stock>0)
{
stock--;
return 1;
}
else
{
cout<<"\nThe books are not available!!!";
return -1;
}
}
void addbook()
{
cout<<"Enter name of the book: ";
cin>>bookname;
cout<<"Enter book number: ";
cin>>isbn;
cout<<"Enter number of books available: ";
cin>>stock;
}

};

class staff:public library
{
public:
string name;
int bookstaken;


int returnbook()
{
int days;
cout<<"\nEnter book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nEnter the number of days since the book was borrowed: ";
cin>>days;
cout<<"\nExtra fees to be paid: Rs."<<(days-10)*1;
return bks[index]->returnbook();
}
else
return -1;
}
int takebook()
{
cout<<"\nEnter Book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nPlease return the book within 10 days!!!";
return bks[index]->takebook();
}
else
return -1;
}
void addstaff()
{
cout<<"\nEnter the name of the staff:";
cin>>name;
}
};

class students:public library
{
public:
string name;

int returnbook()
{
int days;
cout<<"\nEnter book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nEnter the number of days since the book was borrowed: ";
cin>>days;
cout<<"\nExtra fees to be paid: Rs."<<(days-5)*1;
return bks[index]->returnbook();
}
else
return -1;
}
int takebook()
{
cout<<"\nEnter the book number: ";
cin>>isbn2;
index=search(isbn2);
if(index!=-1)
{
cout<<"\nPlease return the book within 10 days!!!";
return bks[index]->takebook();
}
else
return -1;
}
void adds()
{
cout<<"\nEnter the name of the student:";
cin>>name;
}
};

void display()
{
cout<<"\nThe available books are:\n\nISBN\t-\tName\t-\tStock\n";
for(int i=0;i<bkid;i++)
{
cout<<"\n"<<bks[i]->isb()<<"\t-\t"<<bks[i]->str()<<"\t-\t"<<bks[i]->sto();
}
}

void staf()
{
int ch,id,rep=1;
system("cls");

cout<<"\nEnter staff library number: ";
cin>>id;
id--;
while(rep==1)
{
system("cls");
cout<<"\nSTAFF\n-----\n";
cout<<"\n1.Display books\n2.Take book\n3.Return book\n4.Go to main menu\n";
cin>>ch;
switch(ch)
{
case 1:
display();
break;
case 2:
if(sta[id]->takebook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...";
break;
case 3:
if(sta[id]->returnbook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...\n";
break;
case 4:
rep=0;
break;
}
getch();
}
}

void student()
{
int ch,id,cont=1;
system("cls");
cout<<"\nSTUDENT\n--------\n";
cout<<"Enter library number: ";
cin>>id;
id--;
while(cont==1)
{
system("cls");
cout<<"\n1.Display books\n2.Take books\n3.Return book\n4.Go to main menu\n";
cin>>ch;
switch(ch)
{
case 1: display();
break;

case 2: if(stud[id]->takebook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...\n";
break;

case 3: if(stud[id]->returnbook()==-1)
cout<<"\nInvalid Number!!!";
else
cout<<"\nPlease wait...\n";
break;

case 4: cont=0;
break;
}
getch();
}
}

void admin()
{
int ch,rep=1;
system("cls");
while(rep==1)
{
system("cls");
cout<<"\nDATABASE\n---------\n\n1.Add staff\n2.Add student\n3.Add books\n4.Display\n5.Exit\n";
cin>>ch;
switch(ch)
{
case 1: sta[nstaff]=new staff;
sta[nstaff]->addstaff();
nstaff++;
cout<<"\nThe Library number is: "<<nstaff;
break;

case 2: stud[nstud]=new students;
stud[nstud]->adds();
nstud++;
cout<<"\nThe Library number is: "<<nstud;
break;

case 3: bks[bkid]=new library_store;
bks[bkid]->addbook();
bkid++;
break;

case 4: display();
break;

case 5: rep=0;
break;
}
cout<<"\nPress Enter to continue:";
getch();
}
}

int search(int isbn)
{
for(int i=0;i<bkid;i++)
{
if(bks[i]->isb()==isbn)
return i;
}
cout<<"No book found!!!";
return -1;
}

void main()
{
int choice,cont=1;
while(cont==1)
{
cout<<"\n1.Administrator\n2.Staff\n3.Student\n4.Exit\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: admin();
break;

case 2: staf();
break;

case 3: student();
break;

case 4: cont=0;
break;

default: cout<<"\n\nInvalid Entry!!";
}
}
}

Randomize the array of elements and print in all combinations

for eg: if array is 1 2 3 then all display all  2^n  combinations
                                         output: 1 2 3
                                                     2 3 1
                                                     ........


#include<iostream>
#include<conio.h>

using namespace std;

int siz;

class funstore
{

public:

int iSet1[100], iSet2[100], sum;

int ispresent(int n)
{
int r, flag, count=0, k=n;
flag=0;

while(n>0)
{
r=n%10;
for(int i=0;i<siz;i++)
{
if(r==iSet1[i])
{
count++;
break;
}

}

n=n/10;
}

if(count==siz) flag=1;

while(k!=0)
{
r=k%10;
k=k/10;
if((r==k%10)||(r==(k/10)%10)) flag=0;
}

return flag;

}

int summ(int n)
{
int r, sum;
sum=0;

while(n!=0)
{
r=n%10;
sum=sum+r;
n/=10;
}

return sum;
}

}fun;

int main()
{
int i, j, temp, num, rev;

cout<<"Enter number of values : ";
cin>>siz;

cout<<"\nEnter the values :\n";

for(i=0;i<siz;i++)
{
cout<<"\t-";
cin>>fun.iSet1[i];
}

for(i=0;i<siz;i++)
{

for(j=i+1;j<siz;j++)
{
if(fun.iSet1[j]<fun.iSet1[i])
{
temp=fun.iSet1[i];
fun.iSet1[i]=fun.iSet1[j];
fun.iSet1[j]=temp;
}
}
}

for(j=0;j<siz;j++)
{
fun.iSet2[siz-j-1]=fun.iSet1[j];
}

num=0;
fun.sum=0;
for(i=0;i<siz;i++)
{
num=(num*10)+(fun.iSet1[i]);
fun.sum=fun.sum+fun.iSet1[i];
}

rev=0;
for(i=0;i<siz;i++)
{
rev=(rev*10)+(fun.iSet2[i]);
}

cout<<"\nThe possible combinations are: \n";

for(i=num;i<=rev;i++)
{
if((fun.ispresent(i)==1)&&(fun.summ(i)==fun.sum))
{
cout<<"\n\t-"<<i;
}
}

cout<<"\n\n";

getch();
}

matrix manipulation using operator overloading




#include<iostream>
#include<conio.h>
#include<process.h>
int n;
class matrix
{
public:
int a[10][10];

void get()
{
cout<<"\nEnter the values in the matrix:\n\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void show()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout< }
}
}
matrix operator +(matrix m3)
{
matrix temp;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
temp.a[i][j]=m3.a[i][j]+a[i][j];
}
}
return temp;
}
matrix operator -(matrix m4)
{
matrix temp1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
temp1.a[i][j]=a[i][j]-m4.a[i][j];
}
}
return temp1;
}

};
void main()
{
system("cls")
cout<<"\n enter the size of the matrix";
cin>>n;
matrix m1,m2,m5;
m1.get();
m2.get();
m5=m1+m2;
cout<<"\n added matrix is";
m5.show();
m5=m1-m2;
cout<<"\n subtracted matrix is ";
m5.show();
getch();
}



student details using linked list in vc++

#include<iostream>
#include<conio.h>
#include<process.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

char reg[10];
struct student{
char regno[10];
char name[20];
char class1[10];
int marks[3];
student *link;
}*temp,*head,*t,*x;

int find()
{ int str=0;
t=head;
x=head;
cout<<"\n\nPlease enter the Regno to find : "; cin>>reg;
if(head!=NULL)
{
do
{
str=strcmpi(t->regno,reg) ;
if (str==0) //||t->regno==reg)
{
return 1;
}
x=t;
t=t->link;
}while(t->link!=NULL);

str=strcmpi(t->regno,reg) ;
if (str==0) // ||t->regno==reg)
{
return 1;
}

}
else
return 0;


}


void add()
{
int choice;
clrscr();
cout<<"\n-= Add Student =-"; cout<<"\n1. Add In Beginning"; cout<<"\n2. Add In Between"; cout<<"\n3. Add At End"; cout<<"\nSelection : "; cin>>choice;

temp=new student;


switch(choice)
{
case 1:

if(head==NULL)
{
head=temp;
temp->link=NULL;
}
else
{
temp->link=head;
head=temp;
}
break;
case 2:
if(find())
{
temp->link=t->link;
t->link=temp;
}
else
{cout<<"\nInvalid Regno!!"; free (temp); goto lab; } break; case 3: temp->link=NULL;
t=head;
while(t->link!=NULL)
t=t->link;
t->link=temp;
break;
default:
{cout<<"\nInvalid Selection"; free (temp); goto lab; } } cout<<"\nPlease enter the student Regno : "; gets(temp->regno);
cout<<"\nPlease enter the student Name : "; gets(temp->name);
cout<<"\nPlease enter the student Class : "; gets(temp->class1);
cout<<"\nPlease enter the Marks Of The Subject 1: "; cin>>temp->marks[0];
cout<<"\nPlease enter the Marks Of The Subject 2: "; cin>>temp->marks[1];
cout<<"\nPlease enter the Marks Of The Subject 3: "; cin>>temp->marks[2];
lab:
}

void display()
{
system("cls");
cout<<"\nProgress Report"; if(find()) { cout<<"\nRegno :"<regno;
cout<<"\nName :"<name;
cout<<"\nClass :"<class1;
cout<<"\n--------------------------"; cout<<"\n\nMarks"; cout<<"\nSubject 1 : "<marks[0];
cout<<"\nSubject 2 : "<marks[1];
cout<<"\nSubject 3 : "<marks[2];
}
else
cout<<"\n Invalid Reg No"; } void del() { if(find()) { if(head==t) { head=t->link;
free(t);

}
else if(t->link==NULL)
{
x->link=NULL;
free(t);
}
else
{
x->link=t->link;
free(t);
}
}
else
cout<<"\nInvalid Reg No"; } void main() { system("cls"); int choice; head=NULL; while(1) { system("cls"); cout<<"\n-={Student Details Using Singly Linked List}=-"; cout<<"\nPlease Select an Operation To Perform"; cout<<"\n1. Add a Student"; cout<<"\n2. Display Student Details"; cout<<"\n3. Delete a Student"; cout<<"\n4. Exit"; cout<<"\nSelection : "; cin>>choice;

switch(choice)
{
case 1:
add();
break;
case 2:
display();
break;
case 3:
del();
break;
case 4:
exit(0);
break;
default:
cout<<"\nInvalid Selection"; } getch(); } }

Quick Sort Using Recursion in C++

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

using namespace std;

void qsort(int data[],int low,int high);

void main()
{
system("cls");
int data[50],n,c=1;
while(c==1)
{
cout<<"\nEnter the size of the array: "; cin>>n;
cout<<"\n\nEnter the elements of the array"; for(int i=0;i<n;i++)
{
cin>>data[i];
cout<<"\n"; } getch(); qsort(data,0,n-1); cout<<"\nThe sorted array is: \n"; for(i=0;i<n;i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nPress 1 to continue: "; cin>>c;
}
}
void qsort(int data[],int low,int high)
{
int pivot,temp,i,j;
i=low;
j=high;
pivot=data[(low+high)/2];
while(i<=j) { while(data[i]<pivot)
i++;
while(pivot<data[j])
j--;
if(i<=j) { temp=data[i]; data[i]=data[j]; data[j]=temp; i++; j--; } } if(low<j)
(data,low,j);
if(i<high)
qsort(data,i,high);
}





- Benil Babu Mathew
 

Thursday, November 4, 2010

C program for factorial of a number without recursion

#include<stdio.h>
void main()
{
int i,fact=1,num;
clrscr();
printf("\nenter a number ");
scanf("%d",&num);
if(num==0)printf("\nfactorial is 1");
else
{
for(i=1;i<num;i++) fact=fact*i;
printf("\nthe factoral is %d",fact);
}
getch();
}

We have noticed that 20% of our visitors are still using internet explorer. For a better and faster surfing experience we recommend that you switch to either Google Chrome or Mozilla Firefox. We cannot provide you the downloads but here are the links to sites where you can download them from

Google Chrome - click here to go to site

Mozilla Firefox - click here to go to site

Where are you from

Do you think this site was helpful

Should we put up more C or C++ programs