Sunday, September 30, 2012

program for queue operation

Program for queue operation ,using   class object concept.

#include<iostream.h>
#include<conio.h>
class queue
{
public:
int q[50],n,limit,front, rear;
queue()
{
cout<<"\nEnter the limit\n";
cin>>limit;
front=0;
rear=0;
}
void push();
void pop();
void peep();
};
void queue::push()
{
if(rear<limit)
{
cout<<"Enter the data\n";
cin>>q[rear];
rear++;
cout<<"Element inserted\n";
}
else
{
cout<<"\nqueue is full\n";
}

}
void queue::pop()
{
if(front<rear)
{
front++;
model for queue insertion operation
push operation in a queue



cout<<"\nElement is deleted\n";
}
else
{
cout<<"Queue is empty\n";
}
}


 void queue::peep()
 {
 int i;
 if(front<rear)
 {
 cout<<"The queue elements are:";
 for(i=front;i<rear;i++)
 {
 cout<<"\n"<<q[i];
 }
 }
 else
 {
 cout<<"Queue is empty\n";
 }
 }
 void main()
 {
 clrscr();
 queue ob;
 int n,limit;
 do
 {
 cout<<"\nMENU\n\n1.PUSH\n2.POP\n3.PEEP\n4.EXIT\n";
 cout<<"\nENTER YOUR CHOICE:";
 cin>>n;
 switch(n)
 {
 case 1:ob.push();
       break;
case 2:ob.pop();
break;
case 3: ob.peep();
break;
}

}while(n!=4);
getch();
}

what is a queue i?

A queue is a singly linked list that models a first in first out model (just as a queue of diners are dealt with on a first come first served basis). Insertions always occur at the tail of the queue and extractions always at the head.
A variation of the queue is a priority queue where nodes are weighted such that those with higher priority are moved in front of those with lower priority. This is akin to diners who have a reservation being moved to the head of the queue. In this case, insertions always occur at the head but the head node will pass the data onto the next node if the data has lower priority. The process continues until the data has higher priority than the current node at which point is it is placed in front of the current node. If the current node has no next node to pass the data onto, the data becomes the tail node. This is known as an insertion sort.

Difference between Stack and Queue  

Stack is a collection of objects that works in LIFO (Last in First out) mechanism while Queue is FIFO (First in First out). This means that the object that is inserted first is removed last in a stack while an object that is inserted first is removed first in a queue

program for all stack operation

c++ program for stack operations..(operation include pop,peep,push)
#include<iostream.h>
#include<conio.h>
class stack
{
public:
int s[50],n,item,top;
void read();
void push();                // for stck insertion
void pop();                //for stack deletion
void display();          // displaying stack element
};
void stack:: read()
{
cout<<"Enter the limit of the stack\n";
cin>>n;
}
void stack::push()
{
top++;
if(top==n)
{
cout<<"Stack full\n";
getch();
}
else
{
cout<<"Enter the item\n";
cin>>item;
s[top]=item;
cout<<"Element inseted into the stack\n";
}
}
void stack::pop()
{
if(top==-1)
{
cout<<"Stack empty\n";
}
else
{
top--;
cout<<"Element deleted\n";
}
}
 void stack::display()
 {
 int i;
 if(top==-1)
 {
 cout<<"Stack is empty\n";
 getch();
 }
 else
 {
 cout<<"The stack elements are:";
 for(i=top;i>=0;i--)
 {
 cout<<"\n"<<s[i];
 getch();
 }
 }
 }
 void main()
 {
 clrscr();
 stack a;
 a.top=-1;
 int n,c,k;
 a.read();
 do
 {
 cout<<"\nMENU\n\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n";
 cout<<"\nENTER YOUR CHOICE:";
 cin>>n;
 switch(n)
 {
 case 1:
 cout<<"\nenter number of insertion:";
 cin>>c;
 for(k=0;k<c;k++)
 {
 a.push();
 }
 break;
case 2:
 cout<<"\nenter number of deletion:";
 cin>>c;
 for(k=0;k<c;k++)
 {
 a.pop();
 }
 break;
case 3:
 a.display();
 break;

}
}while(n!=4);
getch();
}
what is stack operation?
Stack is an ordered group of homogeneous items. —Items are added to and removed from the top of the stack (the most recently added items are at the top of the stack). —The last item to be added is the first to be removed (LIFO: Last In, First Out). A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end called the TOP of the stack.
—The implementation of stack provides for the insertion and deletion of items (at run time), so that a stack is a dynamic, constantly changing object.—

Difference between Stack and Queue  

Stack is a collection of objects that works in LIFO (Last in First out) mechanism while Queue is FIFO (First in First out). This means that the object that is inserted first is removed last in a stack while an object that is inserted first is removed first in a queue

Monday, September 24, 2012

Recursive program for finding tha greatest common divisor.

greatest common divisor(GCD) or HCF highest common factor....

#include<iostream.h>
#include<conio.h>
#include<process.h>
void hcf(int,int);
void main()
{
int a,b,c;
clrscr();
cout<<"Enter two numbers\n";
cin>>a>>b;
hcf(a,b);
getch();
}
void hcf(int a,int b)
{
int rem;
if(b>0)
{
rem=a%b;
a=b;
b=rem;
hcf(a,b);  //recursive function call for greatest common factor
}
cout<<" The GCD is\t"<<a;
getch();
exit(0);
}

method 2
#include<iostream.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int a,b,c;
clrscr();
cout<<"Enter two numbers\n";
cin>>a>>b;
c=gcd(a,b);
cout<<"The Greatest Common Divisor is \t"<<c;
getch();
}
int gcd(int n,int m)
{
if(n%m== 0)
{
 return m;
 }
 else
 return gcd(m,n%m);
 }

what is recursion in c++?

Recursion, in any programming language, refers to, most commonly, a call to a function inside that function, (i.e a function calling itself) .


Saturday, September 22, 2012

program for binary search

// program for binary search.
 .#include
#include
int search(int[],int,int);                                                                    //binary search declaration
void main()
{
int a[20],n,item,p,i;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
printf("Enter the array\n");
for(i=0;iscanf("%d",&a[i]);
printf("Enter the array to be searched\n");
scanf("%d",&item);
p=search(a,n,item);                                                                         //   binary search call
if(p==-1)
{
printf("sorry\n");
}
else{
printf("Item found at %d position",p+1);
}

getch();
}
int search(int a[],int n, int item)                                                                //   binary search definition
{
int beg=0,end=n-1,mid;
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
return mid;
 }
else if(item>a[mid])
{
beg=mid+1;
}
else
{
end=mid-1;
}
}
return -1;
}


 A fast way to search a sorted array is to use a binary search. The idea is to look at the element in the middle. If the key is equal to that, the search is finished. If the key is less than the middle element, do a binary search on the first half. If it's greater, do a binary search of the second half.

Algorithm

Algorithm is quite simple. It can be done either recursively or iteratively:
  1. get the middle element;
  2. if the middle element equals to the searched value, the algorithm stops;
  3. otherwise, two cases are possible:
    • searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.
    • searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.
Now we should define, when iterations should stop. First case is when searched element is found. Second one is when sub array has no elements. In this case, we can conclude, that searched value doesn't present in the array.

 // program for binary search.
 // program for binary search.

PROGRAM FOR LINEAR SEARCH

program for linear search
#include<iostream.h>
#include<conio.h>
int a[15],i,n,item;
void read();
int lsearch(int [],int,int);
void main()
{
int p;
clrscr();
read();
cout<<"Enter the search element\n";
cin>>item;
p=lsearch(a,n,item);
cout<<"\nElement found at "<<p+1<<" position";
getch();
}
int lsearch(int a[],int n,int item)
{
for(i=0;i<n;i++)
{
if(a[i]==item)
{
break;
}
}
return(i);
}
void read()
{
cout<<"Enter the limit\n";
cin>>n;
cout<<"Enter the numbers\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}

 In Linear Search the list is searched sequentially and the position is returned if the key element to be searched is available in the list, otherwise -1 is returned.. The search in Linear Search starts at the beginning of an array and move to the end, testing for a match at each item. All the elements preceding the search element are traversed before the search element is traversed. i.e. if the element to be searched is in position 10, all elements form 1-9 are checked before 10.



Friday, September 21, 2012

merge sorting

program for merge sort...
#include<conio.h>
#include<iostream.h>

#include<stdio.h>
void main()
{
int i,j,n,p,m,a[20],b[20],c[40],k;
clrscr();
cout<<"Enter the limit 1\n";
cin>>m;
cout<<"Enter the elements\n"; /* 2 arrays must be in sorted order otherwise you make them sorted..*/
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"Enter the limit 2\n";
cin>>n;
cout<<"Enter the elements\n";
for(j=0;j<n;j++)
{
cin>>b[j];
}
i=0;
j=0;
k=0;
while((i<m)&&(j<n))
{
if(a[i]<=b[j])
{
c[k]=a[i];
i++;
k++;
}
else
{
c[k]=b[j];
k++;
j++;
}
}
while(i<m)
{
c[k]=a[i];
k++;
i++;
}
while(j<n)
{
c[k]=b[j];
k++;
j++;
}
cout<<"sorted elements\n";
for(i=0;i<n+m;i++)
{
cout<<c[i]<<" ";
}
getch();
}


MERGE SORT

The mergesort algorithm is based on the classical divide-and-conquer paradigm. It operates as follows:

DIVIDE: Partition the n-element sequence to be sorted into two subsequences of n/2 elements each.

CONQUER: Sort the two subsequences recursively using the mergesort.

COMBINE: Merge the two sorted sorted subsequences of size n/2 each to produce the sorted sequence consisting of n elements.

Note that recursion "bottoms out" when the sequence to be sorted is of unit length. Since every sequence of length 1 is in sorted order, no further recursive call is necessary. The key operation of the mergesort algorithm is the merging of the two sorted sequencesin the "combine step". To perform the merging, we use an auxilliary procedure Merge(A,p,q,r), where A is an array and p,q and r are indices numbering elements of the array such that dure assumes that the subarrays A[p..q] and A[q+1...r] are in sorted order.It merges them to form a single sorted subarray that replaces the current subarray A[p..r]. Thus finally,we obtain the sorted array A[1..n], which is the solution.

program for quick sort

c++ program for quick sort..
#include<iostream.h>
#include<conio.h>
int a[50],l,u,i,j,n;
void quick(int a[] ,int,int); function declaration for quiick sort
void main()
{
clrscr();
cout<<"Enter the limit\n";
cin>>n;
cout<<"Enter "<<n<<"  elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
l=0;u=n-1;
quick(a,l,u);                // function call for quick sort
cout<<"\n sorted elements are\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}
void quick(int a[],int l,int u)              //function definition for quick sort
{
int p,temp;
if(l<u)
{
p=a[l];
i=l;
j=u;
while(i<j)
{
while((a[i]<=p)&&( i<j))
  {
  i++;
  }
  while((a[j]>p)&&(i<j))
  {
  j--;
  }
  if(i<=j)
  {
  temp=a[i];
  a[i]=a[j];
  a[j]=temp;
  }
  }
  temp=a[j];
  a[j]=a[l];
  a[l]=temp;
  if(l!=j-1)
  quick(a,l,j-1);

   if((j+1)!=u)
  quick(a,j+1,u);          //recursive function call

  }
  }


Quick Sort

In some ways the quick sort uses a similar idea to the bubble sort in that it compares items and swaps them if they are out of sequence. However, the idea of the quick sort is to divide the list into smaller lists which can then also be sorted using the quick sort algorithm. This is usually done through recursion. Lists of length 0 are ignored and those of length 1 are considered to be sorted.
Quick sort, like Merge Sort, is a divide-and-conquer sorting algorithm. The premise of quicksort is to separate the "big" elements from the "small" elements repeatedly. The first step of the algorithm requires choosing a "pivot" value that will be used to divide big and small numbers. Each implementation of quicksort has its own method of choosing the pivot value--some methods are much better than others. The implementation below simply uses the first element of the list as the pivot value. Once the pivot value has been selected, all values smaller than the pivot are placed toward the beginning of the set and all the ones larger than the pivot are placed to the right. This process essentially sets the pivot value in the correct place each time. Each side of the pivot is then quicksorted.
Ideally, the pivot would be selected such that it were smaller than about half the elements and larger than about half the elements. Consider the extreme case where either the smallest or the largest value is chosen as the pivot: when quicksort is called recursively on the values on either side of it, one set of data will be empty while the other would be almost as large as the original data set. To improve the efficiency of the sort, there are clever ways to choose the pivot value such that it is extremely unlikely to end up with an extreme value. One such method is to randomly select three numbers from the set of data and set the middle one as the pivot. Though the comparisons make the sort slightly slower, a "good" pivot value can drastically improve the efficiency of quicksort.
  1. 1. Pick an element from the table you're sorting. We call this the 'pivot'.
  2. 2. Exchange the pivot with the right-most element in the table.
  3. 3. Go through the table from both left and right ends; from the left end, search for elements GREATER than the pivot; from the right end, search for elements SMALLER than the pivot.
  4. 4. When you find these two elements, exchange them and go on.
  5. 5. When the two go-throughs cross, exchange the pivot and the element where the left go-through is pointing.
  6. 6. The pivot is on its final place in the table, and to the left there's only elements smaller than it, to the right there's only elements greater than it. Now perform the same process for both sides of the table recursively.
Consider the data set 5 9 3 8 6 4 2 1 7 0. For simplicity, take the first element as the pivot value, in this case the 5. After iterative comparisons, the array has the following arrangement: [0 3 4 2 1 5 8 6 7 9]. Note that all the values to the left of the 5 are smaller and all those to the right are larger. When quicksort is called on the smaller half, the problem of a "bad" pivot value is highlighted. Note that the array of smaller numbers, [ 0 3 4 2 1 ] does not change and the next array that gets quicksorted is practically the same: [ 3 4 2 1 ]. A complete trace of the algorithm follows.
5 9 3 8 6 4 2 1 7 0
Quicksorting subarray: [ 5 9 3 8 6 4 2 1 7 0 ]
Quicksorting subarray: [ 0 3 4 2 1 ]
Quicksorting subarray: [ ]
Quicksorting subarray: [ 3 4 2 1 ]
Quicksorting subarray: [ 1 2 ]
Quicksorting subarray: [ ]
Quicksorting subarray: [ 2 ]
Quicksorting subarray: [ 4 ]
Quicksorting subarray: [ 8 6 7 9 ]
Quicksorting subarray: [ 7 6 ]
Quicksorting subarray: [ 6 ]
Quicksorting subarray: [ ]
Quicksorting subarray: [ 9 ]
0 1 2 3 4 5 6 7 8 9

program for selection sort

c++program for selection sort..
#include<conio.h>
#include<iostream.h>

#include<stdio.h>
void main()
{
int i,j,n,p,a[20],s,t;
clrscr();
cout<<"Enter the limit\n";
cin>>n;
cout<<"Enter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=0;i<n;i++)
 {
 s=a[i];
 p=i;
 for(j=i+1;j<n;j++)
 {
 if(a[j]<s)
  {
  s=a[j];
  p=j;
  }
 }

  t=a[p];
  a[p]=a[i];
  a[i]=t;

  }

for(i=0;i<n;i++)
  {
  cout<<a[i]<<"  ";
  }


getch();
}


The Selection Sort Algorithm

Like bubble sort, selection sort is implemented with one loop nested inside another. This suggests that the efficiency of selection sort, like bubble sort, is n 2 . To understand why this is indeed correct, consider how many comparisons must take place. The first iteration through the data require n - 1 comparisons to find the minimum value to swap into the first position. Because the first position can then be ignored when finding the next smallest value, the second iteration requires n - 2 comparisons and third requires n - 3 . This progression continues as follows:
(n - 1) + (n - 2) + ... +2 + 1 = n(n - 1)/2 = O(n 2)
Unlike other quadratic tests, the efficiency of selection sort is independent of the data. Bubble sort, for example, can sort sorted and some nearly-sorted lists in linear time because it is able to identify when it has a sorted list. Selection sort does not do anything like that because it is only seeking the minimum value on each iteration. Therefore, it cannot recognize (on the first iteration) the difference between the following two sets of data: 1 2 3 4 5 6 7 8 9 and 1 9 8 7 6 5 4 3 2. In each case, it will identify the 1 as the smallest element and then go on to sorting the rest of the list. Because it treats all data sets the same and has no ability to short-circuit the rest of the sort if it ever comes across a sorted list before the algorithm is complete, insertion sort has no best or worst cases. Selection sort always takes O(n 2) operations, regardless of the characteristics of the data being sorted.

program for insertion sort

c++ program for insertion sort..
#include<conio.h>
#include<iostream.h>

#include<stdio.h>
void main()
{
int i,j,t,n=4,a[15],k;
clrscr();
cout<<"Enter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=1;i<n;i++)
 {
 j=i;
 for(j=0;j<i;j++)
  {
     if(a[i]<a[j])
     {
     t=a[i];

     for(k=i;k>j;k--)
       {
       a[k]=a[k-1];
       }
       a[k]=t;
     }
  }
  }
  for(i=0;i<n;i++)
  {
  cout<<a[i]<<"  ";
  }


getch();
}


Insertion Sort

The insertion sort algorithm is the sort unknowingly used by most card players when sorting the cards in their hands. When holding a hand of cards, players will often scan their cards from left to right, looking for the first card that is out of place. For example, if the first three cards of a player's hand are 4, 5, 2, he will often be satisfied that the 4 and the 5 are in order relative to each other, but, upon getting to the 2, desires to place it before the 4 and the 5. In that case, the player typically removes the 2 from the list, shifts the 4 and the 5 one spot to the right, and then places the 2 into the first slot on the left. This is insertion sort. Unlike other simple sorts like selection sort and bubble sort which rely primarily on comparing and swapping, the insertion sort achieves a sorted data set by identifying an element that out of order relative to the elements around it, removing it from the list, shifting elements up one place and then placing the removed element in its correct location. Follow the step by step process of sorting the following small list.
  • (4) 3 1 2 --> The four is in the correct place relative to the elements that have been
  • considered to this point.
  • (4 3) 1 2 --> The four and the three are incorrectly placed relative to one another, so remove and shift
  • (4 _) 1 2 --> Remove the 3 from the list
  • (_ 4) 1 2 --> shift the four into the relative correct place
  • (3 4) 1 2 --> Now the sublist that was being considered is in sorted order
  • (3) 4 1 2 --> The three is in sorted order relative to the data before it
  • (3 4) 1 2 --> The three and the four are in sorted order relative to the data before it
  • (3 4 1) 2 --> The 3, 4, and 1 are not in sorted order, so remove and shift
  • (3 4 _) 2 --> Remove the 1
  • (3 _ 4) 2 --> Shift the 4 up one place
  • (_ 3 4) 2 --> Shift the 3 into its relatively correct place
  • (1 3 4) 2 --> Place the one such that the sublist being considered is in sorted order
  • (1) 3 4 2 --> (1) is a sorted list
  • (1 3) 4 2 --> (1 3) is a sorted list
  • (1 3 4) 2 --> (1 3 4) is a sorted list
  • (1 3 4 2) --> The two is out of order, so remove and shift
  • (1 3 4 _) --> Remove the 2
  • (1 3 _ 4) --> Shift the 4
  • (1 _ 3 4) --> Shift the 3
  • (1 2 3 4) --> Place the 2 into its correct place
  • (1) 2 3 4 --> (1) is a sorted list
  • (1 2) 3 4 --> (1 2) is a sorted list
  • (1 2 3) 4 --> (1 2 3) is a sorted list
  • (1 2 3 4) --> (1 2 3 4) is a sorted list, sort complete.
With a larger data set, it is even easier to see the sorted sublist growing in size with each successive iteration. Note that after each iteration, the size of the sorted data at the beginning of the list grows by one.

8 9 3 5 6 4 2 1 7 0
3 8 9 5 6 4 2 1 7 0
3 5 8 9 6 4 2 1 7 0
3 5 6 8 9 4 2 1 7 0
3 4 5 6 8 9 2 1 7 0
2 3 4 5 6 8 9 1 7 0
1 2 3 4 5 6 8 9 7 0
1 2 3 4 5 6 7 8 9 0
0 1 2 3 4 5 6 7 8 9

program for bubble sort

c++ program for bubble sort...
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int i,t,a[20],b[20],m,j,n;
clrscr();
cout<<"Enter the limit 1\n";
cin>>m;
cout<<"Enter the elements\n";
for(i=0;i<m;i++)
{
cin>>a[i];
}
for(i=0;i<m;i++)
{
  for(j=i+1;j<m;j++)
  {
   if(a[i]>=a[j])
    {
    t=a[i];
    a[i]=a[j];
    a[j]=t;
    }

  }
}
for(i=0;i<m;i++)
{
cout<<a[i]<<" ";
}

getch();
}


Bubble Sort

Because of bubble sort's simplicity, it is one of the oldest sorts known to man. It based on the property of a sorted list that any two adjacent elements are in sorted order. In a typical iteration of bubble sort each adjacent pair of elements is compared, starting with the first two elements, then the second and the third elements, and all the way to the final two elements. Each time two elements are compared, if they are already in sorted order, nothing is done to them and the next pair of elements is compared. In the case where the two elements are not in sorted order, the two elements are swapped, putting them in order.
Consider a set of data: 5 9 2 8 4 6 3. Bubble sort first compares the first two elements, the 5 and the 6. Because they are already in sorted order, nothing happens and the next pair of numbers, the 6 and the 2 are compared. Because they are not in sorted order, they are swapped and the data becomes: 5 2 9 8 4 6 3. To better understand the "bubbling" nature of the sort, watch how the largest number, 9, "bubbles" to the top in the first iteration of the sort.
(5 9) 2 8 4 6 3 --> compare 5 and 9, no swap 5 (9 2) 8 4 6 3 --> compare 9 and 2, swap 5 2 (9 8) 4 6 3 --> compare 9 and 8, swap 5 2 8 (9 4) 6 3 --> compare 9 and 4, swap 5 2 8 4 (9 6) 3 --> compare 9 and 6, swap 5 2 8 4 6 (9 3) --> compare 9 and 3, swap 5 2 8 4 6 3 9 --> first iteration complete
The bubble sort got its name because of the way the biggest elements "bubble" to the top. Notice that in the example above, the largest element, the 9, got swapped all the way into its correct position at the end of the list. As demonstrated, this happens because in each comparison, the larger element is always pushed towards its place at the end of the list.
In the second iteration, the second-largest element will be bubbled up to its correct place in the same manner:
  • (5 2) 8 4 6 3 9 --> compare 5 and 2, swap
  • 5 (2 8) 4 6 3 9 --> compare 2 and 8, no swap
  • 5 2 (8 4) 6 3 9 --> compare 8 and 4, swap
  • 5 2 4 (8 6) 3 9 --> compare 8 and 6, swap
  • 5 2 4 6 (8 3) 9 --> compare 8 and 3, swap
  • 5 2 4 6 3 8 9 --> no need to compare last two In the next pass through the list, the 6 would bubble up to its position, then the 5, the 4, the 3, and finally the 2. Here is a complete trace of the bubble sort algorithm on a ten element data set:
    8 9 3 5 6 4 2 1 7 0
    8 9 3 5 6 4 2 1 7 0
    8 3 9 5 6 4 2 1 7 0
    8 3 5 9 6 4 2 1 7 0
    8 3 5 6 9 4 2 1 7 0
    8 3 5 6 4 9 2 1 7 0
    8 3 5 6 4 2 9 1 7 0
    8 3 5 6 4 2 1 9 7 0
    8 3 5 6 4 2 1 7 9 0
    8 3 5 6 4 2 1 7 0 9
    3 8 5 6 4 2 1 7 0 9
    3 5 8 6 4 2 1 7 0 9
    3 5 6 8 4 2 1 7 0 9
    3 5 6 4 8 2 1 7 0 9
    3 5 6 4 2 8 1 7 0 9
    3 5 6 4 2 1 8 7 0 9
    3 5 6 4 2 1 7 8 0 9
    3 5 6 4 2 1 7 0 8 9
    3 5 6 4 2 1 7 0 8 9
    3 5 6 4 2 1 7 0 8 9
    3 5 4 6 2 1 7 0 8 9
    3 5 4 2 6 1 7 0 8 9
    3 5 4 2 1 6 7 0 8 9
    3 5 4 2 1 6 7 0 8 9
    3 5 4 2 1 6 0 7 8 9
    3 5 4 2 1 6 0 7 8 9
    3 4 5 2 1 6 0 7 8 9
    3 4 2 5 1 6 0 7 8 9
    3 4 2 1 5 6 0 7 8 9
    3 4 2 1 5 6 0 7 8 9
    3 4 2 1 5 0 6 7 8 9
    3 4 2 1 5 0 6 7 8 9
    3 2 4 1 5 0 6 7 8 9
    3 2 1 4 5 0 6 7 8 9
    3 2 1 4 5 0 6 7 8 9
    3 2 1 4 0 5 6 7 8 9
    2 3 1 4 0 5 6 7 8 9
    2 1 3 4 0 5 6 7 8 9
    2 1 3 4 0 5 6 7 8 9
    2 1 3 0 4 5 6 7 8 9
    1 2 3 0 4 5 6 7 8 9
    1 2 3 0 4 5 6 7 8 9
    1 2 0 3 4 5 6 7 8 9
    1 2 0 3 4 5 6 7 8 9
    1 0 2 3 4 5 6 7 8 9
    0 1 2 3 4 5 6 7 8 9

program for strings sorting(word by word)

c program for strings sorting.........
#include<conio.h>
#include<stdio.h>
#include<string.h>                          //c program for strings sorting
void main()
{
char a[50][50],cpy[50],t[50];
int l,i,j,n;
clrscr();
printf("enter the limiit\n");
scanf("%d",&n);
printf("String\n");
for(i=0;i<n;i++)
scanf("%s",a[i]);
for(i=0;i<n;i++)
for(i=0;i<n;i++)
{  if(strcmp(a[i],a[i+1])>0)     //compare each strings
      {
      strcpy(t,a[i])    ;
      strcpy(a[i],a[i+1]);
      strcpy(a[i+1],t);
      }
}
     for(i=0;i<n;i++)
     {
     printf("%s\n",a[i]);     //prints sorted string
     }
 getch();
 }
// c program for strings sorting

program to perform string replace

c program to perform string replace..
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char cpy[30][30],st[30],t[30],a[20],b[20];
int l,i,j,c=0,r=0;
clrscr();
printf("Enter the string\n");
gets(st);
printf("Enter the search string\n");
scanf("%s",a);
printf("Enter the replace string\n");
scanf("%s",b);
l=strlen(st);
st[l]=' ';
for(i=0;i<=l;i++)
{
  if(st[i]!=' ')
  {
  t[r]=st[i];
  r++;
  }
  else
  {
  t[r]='\0';
  r=0;
  strcpy(cpy[c],t);
   c++;
  }
}
strcpy(st," ");
for(i=0;i<c;i++)
 {
if(strcmp(cpy[i],a)==0)
  {
  strcpy(cpy[i],b);
  }

 }
 for(i=0;i<c;i++)
 {
 strcat(st,cpy[i]);
 strcat(st," ");
 }
 printf("\n");
 puts(st);  //prints replaced string

getch();
}

program to check the string is palindrum or not

 check the string is palindrum or not.

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,l,c=0,n;
char a[20];
 printf("\nenter thestring\n");
 scanf("%s",a);
 n=strlen(a);
 j=n-1;
for(i=0;i<n/2;i++,j--)
{ if((a[i]!=a[j]))
  {
  c++;
  printf("\nnot palindrum\n");
  break;
  }
}
if(c==0)
{
 printf("\npalindrum\n");
}

getch();
}

program for matrix multiplication

c program code  for matrix multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,m,n,m2,n2,k;
clrscr();
printf("enter the rows and columns of the first matrics\n");
scanf("%d%d",&m,&n);
printf("Enter the first matrics\n");
for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
      {
      scanf("%d",&a[i][j]);    //reads first matrics
      }
 }
 printf("enter the rows and columns of the SECOND  matrics\n");
scanf("%d%d",&m2,&n2);
printf("Enter the second matrics\n");
for(i=0;i<m2;i++)
 {
  for(j=0;j<n2;j++)
      {
      scanf("%d",&b[i][j]);     reads second matrics
      }
  }
 printf("The matrics product \n");
for(i=0;i<m;i++)
 {
  for(j=0;j<n2;j++)
   {   c[i][j]=0;

      for(k=0;k<n;k++)
      {
      c[i][j]+=a[i][k]*b[k][j];
      }
     printf("%d   ",c[i][j]);
   }
    printf("\n");
  }

  getch();
  }

program for eulers theoram

c program for eulers theoram..
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
float i,f,j,n;
float s=1;
clrscr();
printf("Enter the num\n");
scanf("\n%f",&n);
for(i=1;i<=n;i++)
{
f=1;
 for(j=1;j<=i;j++)
 {
 f=f*j;
 }
 s=s+1/f;
 }
 printf("\n%f\n",s);
 getch();
 }

program for decimal to binary conversion

1)program for decimal to binary conversion
#include<conio.h>
#include<stdio.h>
void gbit(int num);
void main()
{
int num;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
printf("\n\n");
gbit(num);
getch();
}
void gbit(int num)
{
int temp;
if(num)
{
temp=num%2;
gbit(num/=2);
printf("%d",temp);

}
}

2)
 #include<stdio.h>
#include<conio.h>
void main()
{
int n,a[30],r,i=0,j;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);  //reads the decimal number
printf("the binary number requred is\n");
while(n)
{
a[i]=n%2;
n/=2;
i++;
}
j=i-1;
for(i=j;i>0;i--)
{
printf("%d",a[i]);  //prints the binary number
}


getch();
}


Thursday, September 20, 2012

program for diagonal elements of a matrix

c program for diagonal elements of a matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],i,j,m,n;
clrscr();
printf("enter the rows and columns of the matrixs\n");
scanf("%d%d",&m,&n);
printf("Enter the first matrics\n");
for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
      {
      scanf("%d",&a[i][j]);   //reads the matrics
      }
 }
printf("The diagonal element of the matrics are\n");
for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
      {
      if(i==j)
      printf("%d ,",a[i][j]);   //prints the diagonal element of the matrics
      }

 }
 getch();
 }

program for sum of digits of a number

program for sum of digits  of a number
#include<stdio.h>
#include<conio.h>
void main()                // program for sum of digits  of a number
{
int n,r,s=0;
printf("Enter the value\n");
scanf("%d",&n);  //reads the digit
while(n!=0)
{
r=n%10;   
s=s+r;
n=n/10;
}
printf("%d",s);  //prints the sum of the digit
getch();
}


// program for sum of digits  of a number

program to display even natural numbers upto a limit

 program to display even natural numbers upto a limit

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0;
clrscr();   //it will clear the screen
printf("Enter the value\n");
scanf("%d",&n); //reads the limit value
while(i<=n)
{

printf("%d\n",i); //prints the number
i=i+2;   //incrementation of i by2
}
getch();
}

program for fibanosis series up to a limit

"program for fibanosis series up to a limit "

#include<conio.h>
#include<stdio.h>
void main()
{
int n,a=0,b=1,i=1,c;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
if(n==1)
printf("%d",a) ;
if(n==2)
printf("%d,%d",a,b) ;
if(n>2)
{
printf("%d,%d,",a,b) ;
i=i+2;
while(i<=n)
{
c=a+b;
printf("%d,",c);
a=b;
b=c;
i++;

}
}
getch();
}

program for float to integer conversion

// program for float to integer  conversion
#include<stdio.h>
#include<conio.h>
void main()
{
float n,r;
int t=0;
clrscr();
printf("Enter the number \n");          // program for float to integer  conversion
scanf("%f",&n);
r=n;
t=n;
t=t%10;
printf("the expected number\t%d",t);      // program for float to integer  conversion
printf("\nthe floating point number\t%f",r);
getch();
}            // program for float to integer  conversion

program for sum of elements up to a limit

 program for sum of elements up to a limit
#include<conio.h>
#include<stdio.h>
void main()
{
int n,s=0;
clrscr();
for(i=1;;i++)                
{
printf("Enter the number\n");
scanf("%d",&n);
if(n>0)
s=s+n;
if(n==0)
break;
}                                        
printf("the sum is\n");
scanf("%d",s);
getch();
}

program for largest among 3 numbers

// program for largest among 3 numbers
#include<conio.h>
#include<stdio.h>                                    // program for largest among 3 numbers
void main()
{

int a,b,c,large;
clrscr();
printf("Enter the numbers\n");
scanf("%d%d%d",&a,&b,&c);
large= ( a>b?(a>c?a:c):(b>c?b:c) );   //program for largest among 3 numbers
printf("the large is \t%d",large);
getch();
}
 program for largest among 3 numbers

program to find the largest among an array

c program code for finding  the largest among an array...................
The program travels to all the elements in an array and find which is larger...
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], n,i,t;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
t=a[0];
for(i=0;i<n;i++)
{
if(t<=a[i])
{
t=a[i];
}

}
printf("\n largest is\t%d",t);  //prints the largest


getch();
}

program for matrix addition(exact look)

c program code for finding  the sum of 2 matrics in exact look......
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,m,n;
clrscr();
printf("enter the rows and columns of the first matrics\n");
scanf("%d%d",&m,&n);
printf("Enter the first matrics\n");
for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
      {
      scanf("%d",&a[i][j]);
      }
 }
printf("Enter the second matrics\n");
for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
      {
      scanf("%d",&b[i][j]);
      }
  }
 printf("The matrics sum\n");
for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
      {
      printf("%d    ",a[i][j]);
      }
      if(i==n/2 )
      {
      printf("  +  ");
      }
      else
      {
      printf("     ");
      }
      for(j=0;j<n;j++)
      {
      printf("%d     ",b[i][j]);
      }
      if(i==n/2)
      {
      printf("  =  ");
      }
      else
      {
      printf("     ");
      }
      for(j=0;j<n;j++)
      {
      c[i][j] = a[i][j]+b[i][j];
      printf("%d    ",c[i][j]);
      }
      printf("\n");
  }
  getch();
  }

program for multiplication table of a number.

c program code for multiplication table of a number using for loop...

#include<conio.h>
#include<stdio.h>
void main()
{
int n,j,i;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
for(i=1;i<=10;i++);                                      
{
j=n*i;
printf("%d*%d=%d\n",i,n,j);  //it prints the multiplication
                                                   table line by line..
}
getch();
}

program for natural numbers upto a limit

c program code for printing n natural numbers using while loop.....
First we need to input the value of n,that is the limit.Then the program automatically produce the natural numbers up to that limit.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0;
clrscr();
printf("Enter the value\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i++;
}
getch();
}

program to check the number is positive or negative

" program to check the number is positive or negative"
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
if(n>0)
{
printf("Yes the number is positive\n");
}
else
{
printf("sorry the number is negative\n");
}
getch();
}

program to find the occurrence of a string

" program to find the occurrence of a string"
#include<conio.h>
#include<stdio.h>
void main()
{
char st[20],t[10],a[20];
int i,j,c=0,r=0,l;
clrscr();
printf("Enter the string\n");
gets(st);   //reads the string
printf("Enter the search string\n");
gets(t);  reads the string
l=strlen(st);    //reads the length of the string
st[l]=' ';
for(i=0;i<=l;i++)
{
 if(st[i]!=' ')
 {
 a[r]=st[i];
 r++;
 }
  else
  {
  a[r]='\0';
  if(strcmp(t,a)==0) // compares the string
  {
  c++;
  }
  r=0;
  }

}
printf("\n%d",c);
getch();
}

program to add the total time(limit n,using structure)

 program to add the total time(limit n,using structure)
#include<stdio.h>
#include<conio.h>

 struct time
 {
 int h,m,z;
 };
  void main()
  {
  int i,p,q,n,j;
  struct time s[10];
  clrscr();
  printf("Enter the limit\n");
  scanf("%d",&n);
 for(i=0;i<n;i++)
 {printf("Enter the %d time in hh/mm/ss form\n",(i+1));
 scanf("%d%d%d",&s[i].h, &s[i].m ,&s[i].z);
 }
  s[n].h=0;
  s[n].m=0;
  s[n].z=0;
  printf("Time in hh/mm/ss form\n");
 for(i=0;i<n;i++)
 {
 s[n].h=s[n].h+s[i].h;
 s[n].m=s[n].m+s[i].m;
 s[n].z=s[n].z+s[i].z;
 }
  p=s[n].z/60;
  s[n].z=s[n].z%60;
  q= (s[n].m+p)/60;
  s[n].m=(s[n].m+p)%60;
  s[n].h=s[n].h+q;

 printf("%d : %d : %d",s[n].h,s[n].m ,s[n].z);

 getch();
 }

program for string sorting

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[20],b[20],t;
int n,j,i,r;
clrscr();
printf("Enter the string\n");
gets(a);
n=strlen(a);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
 }
}
printf("After sorting\n");
puts(a);



getch();
}

program to perform string functions

 it perform string functions
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[20];
int n,t;
clrscr();
printf("1.CONCATINATION\n2.COPYING\n3.COMPARING\n4.STRING LENGTH\n\n");

printf("CHOOSE THE NUMBER \n");
m:
scanf("%d",&n);
switch (n)
{
case 1: printf("\nenter the string 1\n");
       scanf("%s",a);
      printf("\nenter the string 2\n");
       scanf("%s",b);
    strcat( a,b);
    puts(a);
    break;
case 2: printf("\nenter the string \n");
       scanf("%s",b);
    strcpy( a,b);
    printf("The source string is\t%s\n",b);

    printf("The copied string is\t%s",a);

    break;
case 3: printf("\nenter the first string \n");
       scanf("%s",a);
    printf("Enter the second string \n");
       scanf("%s",b);
   t=    strcmp( a,b);
      if(t==0)
      {
      printf("The strings are same\n");
      }
      else
      {
    printf("The strings are NOT same\n");
    }
    break;
case 4: printf("\nenter the string \n");
       scanf("%s",a);
   t=    strlen( a);
    printf("The string length is\t%d",t);

    break;
default: printf("Sorry\t chose any valid option\n");
goto m;
}
getch();
}


 C string functions
#include <string.h>


char *strcpy( char *s1, const char *s2)
  • copies the string s2 into the character array s1.  The value of s1 is returned.
char *strncpy( char *s1, const char *s2, size_t n)
  • copies at most n characters of the string s2 into the character array s1.  The value of s1 is returned.
char *strcat( char *s1, const char *s2)
  • appends the string s2 to the end of character array s1.  The first character from s2 overwrites the '\0' of s1. The value of s1 is returned.
char *strncat( char *s1, const char *s2, size_t n)
  • appends at most n characters of the string s2 to the end of character array s1.   The first character from s2 overwrites the '\0' of s1. The value of s1 is returned.
char *strchr( const char *s,  int c)
  • returns a pointer to the first instance of c in s.  Returns a NULL pointer if c is not encountered in the string.
char *strrchr( const char *s,  int c)
  • returns a pointer to the last instance of c in s.  Returns a NULL pointer if c is not encountered in the string.
int strcmp( const char *s1, const char *s2)
  • compares the string s1 to the string s2.  The function returns 0 if they are the same, a number < 0 if s1 < s2, a number > 0 if s1 > s2.
int strncmp( const char *s1, const char *s2, size_t n)
  • compares up to n characters of the string s1 to the string s2.  The function returns 0 if they are the same, a number < 0 ifs1 < s2, a number > 0 if s1 > s2.
size_t strspn( char *s1, const char *s2)
  • returns the length of the longest substring of s1 that begins at the start of s1and consists only of  the characters found in s2.
size_t strcspn( char *s1, const char *s2)
  • returns the length of the longest substring of s1 that begins at the start of s1and contains none of the characters found in s2.
size_t strlen( const char *s)
  • determines the length of the string s.  Returns the number of characters in the string before the '\0'.
char *strpbrk( const char *s1,  const char *s2)
  • returns a pointer to the first instance in s1 of any character found in s2.  Returns a NULL pointer if no characters from s2 are encountered in s1.
char *strstr( const char *s1,  const char *s2)
  • returns a pointer to the first instance of string s2 in s1.  Returns a NULL pointer if s2 is not encountered in s1.
char *strtok(char *s1, const char *s2)
  • repeated calls to this function modifies string s1 by breaking it into "tokens"--that is the string is broken into substrings, each terminating with a '\0', where the '\0' replaces any characters contained in string s2. The first call uses the string to be tokenized as s1; subsequent calls use NULL as the first argument. A pointer to the beginning of the current token is returned; NULL is returned if there are no more tokens.
    Warning: this changes the original string!

program to convert uppercase to lower case

to convert uppercase to lower case

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch,c;
printf("Enter the char\n");
scanf("%c",&c);
ch=tolower(c);
printf("\n%c",ch);
getch();
}

program to find the power of any number

it provides the Nth power for any number
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,i=0,s=1;
clrscr();
printf("Enter the x-value & nvalue\n");
scanf("%d%d",&x,&n);
while(i<n)
{
s=s*x;
i++;
}
printf("the the value is\n %d",s);
getch();
}

program to find the odd numbers up to a limit

count of odd numbers

#include<conio.h>
#include<stdio.h>
void main()
{
int n,count=0,i;
printf("Enter the limit\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i%2!=0)
{printf("\n%d",i);
 count++;
}
}
getch();
}

program for the right most digit of a number


"right most digit of a number"


#include<conio.h>
#include<stdio.h>
void main()
{
int n,r;
printf("Enter the number\n");
scanf("%d",&n);
r=n%10;
printf("The right most digit is\t%d",r);
getch();
}

program to print the triangle

*just atriangle */

#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

program for DECIMAL TO BINARY CONVERSION


/*DECIMAL TO BINARY CONVERSION*/
WITHOUT USING FUNCTION

 C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2)

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[30],r,i=0,j;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
printf("the binary number requred is\n");
while(n)
{
a[i]=n%2;
n/=2;
i++;
}
j=i-1;
for(i=j;i>0;i--)
{
printf("%d",a[i]);
}
getch();
}




WITH FUNCTION(recursive)
recursive function is a function which call itself


#include<conio.h>
#include<stdio.h>
void gbit(int num);
void main()
{
int num;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
printf("\n\n");
gbit(num);
getch();
}
void gbit(int num)
{
int temp;
if(num)
{
temp=num%2;
gbit(num/=2);
printf("%d",temp);

}
}

program for amstrong numbers upto a limit


/* AMSTRONG NUMBERS UP TO A LIMIT*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,count=0;
clrscr();
printf("Enter the  limit\n");
scanf("%d",&n);
while(i<=n)
{
int s=0,r=0,k=i;
 while(k!=0)
 {
 r=k%10;
 k=k/10;
 s=s+(r*r*r);
 }
 if(i==s)
 {
 count++;
 printf("\n%d",i);
 }
 i++;
 }
 printf("\nTotal count\t%d",count);
 getch();
 }

An Armstrong number is an n digit number, which is equal to the sum of the nth powers of its digits.
All the 1 digit numbers (1-9) are Armstrong number because
  • 1*1=1 which is equals to number (1) itself,
  • 2*1=2 which is equals to number(2) itself so on for all the 1 digit numbers (1-9).
There are no 2 digit Armstrong numbers.
An Armstrong number of three digit is a number such that that sum of the cubes of it's digits is equal to the number itself. so to check if a 3 digit number is an armstrong number or not we have to multiply all the three digits with itself 3 times.
For example 153 is an Armstrong number because cube of 1 is 1(1x1x1=1) + cube of 5 is 125(5*5*5=125) + cube of 3 is 27(3*3*3=27). Now add all the cubes 1+125+27=153 which is equals to number itself

 

Example 1: Check if 153 is an armstrong number or not

There are three digits in 153. First Digit is 1, Second digit is 5, and Third digit is 3.
Now,
We will multiply each of these three digits i.e 1,5,3 with itself 3 times because there are 3 digits in 153
So,
1 * 1 * 1 =   1

5 * 5 * 5 = 125

3 * 3 * 3 =  27
___________________
   Sum of=  153
Because the sum is equals to the digit itself, so we can say that 153 is an armstrong number.

second largest among 3 numbers

#include<stdio.h>
#include<conio.h>
void main();
{
int lar2,a,b,c;
clrscr();
printf("Enter the values\n");
scanf("%d%d%d",&a,&b,&c);
lar2= ( a>b?(a>c?c:a):(b>c?c:b));
printf("%d",lar2);
getch();
}

The program may seems to be difficult.But this program is so simple that we only need to know about the conditional operators.
a small explanation about conditional operators

The Conditional Operator in C Language

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.

Syntax:
condition ? result1 : result2

If the condition is true, result1 is returned else result2 is returned.

Examples:

10==5 ? 11: 12 // returns 12, since 10 not equal to 5.
10!=5 ? 4 : 3 // returns 4, since 10 not equal to 5.
12>8 ? a : b // returns the value of a, since 12 is greater than 8.

Sunday, September 16, 2012

program to print the diamond


/*program for diamond*/
#include<conio.h>
#include<stdio.h>
void main()
{
int n,i,j,p=1,k,z;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
z=n;
for(i=0;i<z;i++)
{
 for(j=0;j<n;j++)
  {
  printf(" ");
  }
  for(k=1;k<=p;k++)
     {
     printf("*");
     }
     printf("\n");
     p=p+2;

  n--;
}
if(i==z)
{
int m=1;
n=z;
for(i=0;i<z;i++)
{
 for(j=0;j<m+1;j++)
  {
  printf(" ");
  }
  for(k=1;k<=(p-4);k++)
     {
     printf("*");
     }
     printf("\n");
     p=p-2;

  m++;
}
}
getch();
}
This program prints '*' in shape of diamond. First we have to input ,how many '*' the diagonal of the diamond have. The program then produce the required diagonal.If we want to make the diagonal of some other character ,it is so simple ie we need to replace the '*' by the required value.

program to perform basic arithmetic operations

/*program to perform basic arithmetic operations*/

#include<stdio.h>
#include<conio.h>           
void main()
{
float a,b,s;
char ch;
clrscr();
printf("+addition\n-subtraction\n*multiplication\n/.divition\n");
do
{
printf("\nchoose the right option\n");
scanf("%c",&ch);
switch(ch)
{
case '+':printf("Enter the values\n") ;
      scanf("%f%f",&a,&b);
      s=a+b;
      printf("the value is \t%f",s);
      break;

case '-':printf("Enter the values\n");
      scanf("%f%f",&a,&b);
      s=a-b;
      printf("the value is \t%f",s);
      break;

case '*':printf("Enter the values\n");
      scanf("%f%f",&a,&b);
      s=a*b;
      printf("the value is \t%f",s);
      break;

case '/':printf("Enter the values\n");
      scanf("%f%f",&a,&b);
      s=a/b;
      printf("the value is \t%f",s);
      break;

default : printf("sorry choose the right one\n");
}
printf("\n\nDo you want to continue(y/n)\n");
scanf("%s",&ch);
}while((ch=='y')||(ch=='Y'));

 getch();

 }
This program takes operators as input at first time.Then it reads operand .According to the corresponding operators and operands it produces the output

program to display the number b/w 100&200

/* NUMBER BETWEEN 100 & 200

#include<conio.h>
#include<stdio.h>                           
void main()
{
int i=101,count=0;
while((i<200)&&(i>100) )                       
{
 if(i%7==0)
  {
  printf("\n%d",i);
  count++;
  }
  i++;
}
printf("\nTotal count is\t%d",count);            
getch();
}
 program to display the number b/w 100&200

some basic for c++ beginers..

If you are a beginner for c++ ,just walk through it  ....
1. // this double slash not include in the program,and they used to make a simple -line comment within the program(/*.......*/  is used  for multiple line.
2.<iostream.h>- input output stream header file
Provides functionality to use an abstraction called streams specially designed to perform input and output operations on sequences of character, like files or strings.
Objects cin, cout are from iostream header.
<conio.h> console input output header file
conio.h is a C header file used in old MS-DOS compilers to create text user interfaces

Both are header files which contains libery of serveral functions (use for input & output ).
eg. clrscr() ,getch() are from conio.h.
<< extraction operator used to output something.
>>insertion operator used to read something.
This is type of opening files before we use these function.
If we use these function them without importing header files , sometimes compiler throws error.
 now look the example
//program for printing the sentence "Hellow world"one time

#include<iostream.h>
#include<conio.h>
void main()
{
cout<<"Hellow world";
getch();
}


check out a simple program which display 10 times the same sentence..

 #include<iostream.h>
#include<conio.h>
void main()
{
int i=0,n=10; // here we declare the values at the time of initialisation
while(i<n)  // if condition fails,the the loop will not execute
{
cout<<"Hellow world\n"; // \n is used to jump to next stence
i++; // incrementing the value of the i by one.
}
getch();
}

diisplays the  sentence hellow world  using for loop

#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,n;
clrscr();   // this is a function used to clear the screen
cout<<"Enter the limit\n";
cin>>n;// reading the value at run time
for(i=0;i<n;i++)
{
cout<<"Hellow world\n";
}
getch();
}