Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

13 June, 2013

Mini Project :~ C Program To Perform Various Operations On Strings. [Such As : Find Length, Copy, Reverse, Palindrome, Concatenation, Sub-string]

This Post Contains A Mini Project :~ C Program To Perform Various Operations On Strings. [Such As : Find Length, Copy, Reverse, Palindrome, Concatenation, Sub-string] With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'String Operations', 'Character Arrays', 'Functions', 'Menu Driven Concept' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Mini Project :~ C Program To Perform Various Operations On Strings. [Such As : Find Length, Copy, Reverse, Palindrome, Concatenation, Sub-string]
/* Declaration Of Header Files */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

/* Function Declarations */
void Scan1(char *);
void Scan2(char * , char *);
void Leng (char *);
void Copy (char * , char *);
void Reve (char * , char *);
void Palindrome(char * , char *);
void Concat    (char * , char *);
void Subs (char Str1[] , char Str2[]);

/* Start Of Main Program */
void main()

{
 /* Declaration Of Local Variables Of 'main()' Function */

 int  Num = 0;
 char Str1[50] , Str2[50];

 clrscr();

 /* Printing 'Menu' On Screen/Console */
 printf(" \n :: Welcome To 'String Operation' Program :: \n ");

     do
     {
         printf(" \n Please Select One Of The Options From Following Menu : ");

         printf(" \n 1. Length Of Given String. \n 2. Copy   Of Given String.");
         printf(" \n 3. Reverse The String. \n 4. Check For Palindrome.");
         printf(" \n 5.Concatenate The Strings. \n 6. To Find The Sub-string.");
         printf(" \n 7. EXIT  From  PROGRAM.");

         printf(" \n Please Enter Your Option Here ::\t\a");

         scanf("%d" , &Num);

         switch(Num)
         {

              case 1: textcolor(RED);
                      clrscr();
                      printf(" \n :: Welcome To Length Function :: \n ");
                      Scan1( Str1 );  // Call To Function.
                      Leng ( Str1 );   // Call To Function.
                      break;

              case 2: textcolor(GREEN);
                      clrscr();
                      printf(" \n :: Welcome To Copy Function :: \n ");
                      Scan1( Str1 );
                      Copy ( Str1 , Str2 );  // Call To Function.
                      break;

              case 3: textcolor(YELLOW);
                      clrscr();
                      printf(" \n :: Welcome To Reverse Function :: \n ");
                      Scan1( Str1 );
                      Reve ( Str1 , Str2 );   // Call To Function.
                      break;

              case 4: textcolor(10);
                      clrscr();
                      printf(" \n :: Welcome To Palindrome Function :: \n ");
                      Scan1( Str1 );
                      Palindrome( Str1 , Str2 );  // Call To Function.
                      break;

              case 5: textcolor(12);
                      clrscr();
                      printf(" \n :: Welcome To Concatenation Function :: \n ");
                      Scan2( Str1 , Str2 );
                      Concat(Str1 , Str2);   // Call To Function.
                      break;

              case 6: textcolor(5);
                      clrscr();
                      printf(" \n :: Welcome To Substring Function :: \n ");
                      Scan2( Str1 , Str2 );
                      Subs ( Str1 , Str2 );   // Call To Function.
                      break;

              case 7: exit (0);  // Exit From Program.
                      break;

              default: printf(" \n Sorry!!!. Please Enter A Valid Choice. ");
        }
 }while( Num <= 7 );
 /* End Of 'Menu' */
 getch();
}
/* End Of Main Program */

void Scan1(char *Str1)   // Function Definition.

{

    printf(" \n Please Enter The String ::\t ");

    fflush( stdin );

    gets  ( Str1 );

}

void Scan2(char *Str1 , char *Str2)   // Function Definition.

{

    printf(" \n Please Enter String 1 ::\t ");

    fflush( stdin );

    gets  ( Str1 );

    printf(" \n Please Enter String 2 ::\t ");

    fflush( stdin );

    gets  ( Str2 );

}

void Leng( char *Str1 )   // Function Definition.

{

    int i = 0;

    while( Str1[i] != '\0' )

    {

        i++;

    }

    printf(" \n The Length Of String Is ::\t%d ", i);

    getch();

}

void Copy( char *Str1 , char *Str2 )  // Function Definition.

{

    int i = 0 , j = 0;

    while( Str1[i] != '\0' )

    {

        Str2[j] = Str1[i];

        j++;

        i++;

    }

    Str2[j] = '\0';

    printf("The Entered String Is ::\t");

    puts(Str1);

    printf("Copy Of Given String Is ::\t");

    puts(Str2);

    getch();

}

void Reve( char *Str1 , char *Str2 )    // Function Definition.

{

    int i = 0 , j = 0;

    while( Str1[i] != '\0' )

    {

        i++;

    }

    for(i--; i>=0; i--)

    {

        Str2[j] = Str1[i];

        j++;

    }

    Str2[j] = '\0';

    printf("The Entered String Is ::\t");

    puts(Str1);

    printf("Reversed Of Given String Is ::\t");

    puts(Str2);

    getch();

}

void Palindrome( char *Str1 , char *Str2 )   // Function Definition.

{

    int i = 0, j = 0, flag = 0;

    while( Str1[i] != '\0' )

    {

        i++;

    }

    for(i--; i>=0; i--)

    {

        Str2[j] = Str1[i];

        j++;

    }

    Str2[j] = '\0';

    i=0;

    j--;

    while( Str1[i] == Str1[j] )

    {

        flag = 1;

        i++;

        j--;

    }

    if( flag == 1 )

    {

        printf("\nThe Given String Is A Palindrome");

    }

    else

    {

        printf("\nThe Given String Is Not A Palindrome");

    }

    getch();

}

void Concat( char *Str1 , char *Str2 )   // Function Definition.

{

    int i = 0 , j = 0;

    while( Str1[i] != '\0' )

    {

        i++;

    }

    while( Str2[j] != '\0' )

    {

        Str1[i] = Str2[j];

        i++;

        j++;

    }

    Str1[i] = '\0';

    printf("\n The Concatenated String Is :");

    puts( Str1 );

    getch();

}

void Subs( char Str1[] , char Str2[] )   // Function Definition.

{

    int i , j , flag = 0;

    for(j=0; Str2[j] != '\0'; j++)

    {

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

        {

            if( Str1[i] == Str2[j] )

            {

                flag = 1;

                break;

            }

        }

    }

    if( flag == 1 )

        {

            printf("\nThe Given String 2 Is A Substring Of String 1");

        }

        else

        {

            printf("\nThe Given String 2 Is Not A Substring Of String 1");

        }

    getch();

}




09 June, 2013

C Program To Find LCM & GCD Of Integers/Numbers.

LCM: Least Common Multiple.

In Arithmetic, The LCM: Least Common Multiple[Also Called As 'Lowest Common Multiple'] Of Two Integers a & b, Usually Denoted By LCM[a,b], Is The Smallest Positive Integer That Is Divisible By Both a & b. If Either a Or b Is '0' [Zero], LCM[a,b] Is '0' [Zero].

Overview

A Multiple Of A Number Is The Product Of That Number & An Integer. For Ex, 10 Is A Multiple Of 5 Because 5 x 2 = 10, So 10 Is Divisible By 5 & 2. Because 10 Is The Smallest Positive Integer That Is Divisible By Both 5 & 2, It Is The Least Common Multiple Of 5 & 2. By The Same Principle, 10 Is The Least Common Multiple Of -5 & 2 As Well.

Example

What Is The LCM Of 4 & 6?

Multiples Of 4 Are:
  
   4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, ...

and the multiples of 6 are:
  
   6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, ...

Common multiples of 4 and 6 are simply the numbers that are in both lists:

   12, 24, 36, 48, 60, 72, ....

So, from this list of the first few common multiples of the numbers 4 and 6, their least common multiple is 12.

Application

When adding, subtracting, or comparing vulgar fractions, it is useful to find the least common multiple of the
denominators, often called the lowest common denominator, because each of the fractions can be expressed as a
fraction with this denominator. For instance,

[2\21] + [1\6] = [4\42] + [7\42] = [11\42]

where the denominator 42 was used because it is the least common multiple of 21 and 6.

This Post Contains A C Program To Find LCM & GCD Of Integers/Numbers With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Cascading Of Operators' & 'Functions' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C Program To Find LCM & GCD Of Integers/Numbers.

/* Declaration Of Header Files */
# include <stdio.h>
# include <conio.h>
int gcd ( int , int );      // Function Declaration.
void lcm ( int , int );   // Function Declaration.

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
   int a, b;
   clrscr();

/* Asking For The Input From User */
   printf(" \n Enter Two Nos : ");
   scanf("%d %d", &a, &b);
   if (  a  <  b  )
   {
      printf(" \n GCD Of %d & %d Is : %d",a, b, gcd (a , b));    // Function Call.
   }
   else
   {
      printf(" \n GCD Of %d & %d Is : %d",a, b, gcd (a , b));   // Function Call.
   }
   lcm (a , b);    // Function Call.
   getch();
   }
/* End Of Main Program */

   int gcd ( int c , int d )     // Function Definition.
   {
      int r;
      r = d % c;
      while ( r  !=  0  )
      {
         d = c;
         c = r;
         r = d % c;
      }
      return (c);
   }
void lcm ( int x , int y )     // Function Definition.
{
   int l;
   if ( x < y )
   {
     l = ( x * y ) / gcd ( x , y );
   }
   else
   {
     l = ( x * y ) / gcd ( y , x );
   }
   printf(" \n LCM Of %d & %d Is : %d", a, b, l);
}

GCD: Greatest Common Divisor

In mathematics, the GCD: Greatest Common Divisor[also known as the 'Greatest Common Factor (GCF)', or 'Highest Common Factor (HCF)'], of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.

Notation

In this article we will denote the greatest common divisor of two integers a and b as gcd(a,b). Some older textbooks use (a,b).

Example

The number 54 can be expressed as a product of two other integers in several different ways:

   54 x 1 = 27 x 2 = 18 x 3 = 9 x 6.

Thus the divisors of 54 are:

    1, 2, 3, 6, 9, 18, 27, 54.

Similarly the divisors of 24 are:

    1, 2, 3, 4, 6, 8, 12, 24.

The numbers that these two lists share in common are the common divisors of 54 and 24:

    1, 2, 3, 6.

The greatest of these is 6. That is the greatest common divisor of 54 and 24. One writes:

    gcd(54,24) = 6.

Reducing fractions

The greatest common divisor is useful for reducing fractions to be in lowest terms. For example, gcd(42, 56) = 14, therefore,

   {42\56} = {(3)(14) \ (4)(14)} = {3\4}.

Co-prime numbers

Two numbers are called relatively prime, or co-prime if their greatest common divisor equals 1. For example, 9 and 28 are relatively prime.




07 June, 2013

C++ Program To Perform Merge Sort.

This Post Contains A C++ Program To Perform Merge Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Merge Sort', 'Array', 'Function', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Perform Merge Sort.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Function Declaration */
void Merge(int c[], int r[]);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int a[10], b[10], c[20];
int i=0, j=0, x, y;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension For Array A : ";
cin>>x;
cout<<" \n Enter "<<x<<" Values For Array A : ";
for(i=0; i<x; i++)
{
   cin>>a[i];
}

cout<<" \n Enter Dimension For Array B : ";
cin>>y;
cout<<" \n Enter "<<y<<" Values For Array B : ";
for(i=0; i<y; i++)
{
   cin>>b[i];
}

/* Source Code For Computing Merge Sort */
for(i=0; i<x; i++)
c[i]=a[i];
while(j<y)
c[i++]=b[j++];
Merge(c,x+y);           // Function Call.
getch();
}
/* End Of Main Program */

/* Function Definition */
void Merge(int c[], int r[])
{

/* Declaration Of Local Variables Of Function */
int k, p, q, fl, fu, sl, su, size, d[20];
size=1;
while(size<r)
{
   fl=0;k=0;
   while(fl+size<r)
   {
      sl=fl+size;
      fu=sl-1;
      su=(sl+size-1<r)?sl+size-1:r-1;
      for(p=fl, q=sl; p<=fu && q<=su; k++)
      {
         if(c[p]<=c[q])
         {
           d[k]=c[p++];
         }
         else
         {
           d[k]=c[q++];
         }
      }
      for(; p<=fu; k++)
      {
         d[k]=c[p++];
      }
      for(; q<=su; k++)
      {
         d[k]=c[q++];
      }
      fl=su+1;
   }
   for(p=fl; k<r; p++)
   {
      d[k++]=c[p];
   }
   for(p=0; p<r; p++)
   {
      c[p]=d[p];
   }
   size*=2;
}
cout<<"\n";
for(q=0; q<r; q++)
   cout<<c[q]<<"\t";
}
/* End Of Function */


Output :

Enter Dimension For Array A : 4
Enter 4 Values For A :
01  2  83  45

Enter Dimension For Array B : 8
Enter 8 Values For B :
65  64  37  82  19  10  25  63

After Merge Sort :
01  10  19  25  29  37  45  63  64  65  82  83




C++ Program To Perform Shell Sort.

This Post Contains A C++ Program To Perform Shell Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Shell Sort', 'Array', 'Nested Loops', 'Functions' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Perform Shell Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
/* Function Declaration */
void ShellSort(int a[], int b[], int d, intx);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, dim, x, a[20], b[5];
clrscr();

/* Asking For The Input From User */
cout<<" \n Enter Dimension For Array : ";
cin>>dim;
cout<<" \n Enter "<<dim<<" Values For Array : ";
for(i=0; i<dim; i++)
{
   cin>>a[i];
}
/* Source Code For Computing Shell Sort */
cout<<" \n Enter Number Of Increments : ";
cin>>x;
cout<<" \n Enter Increments Last 1 : ";
for(i=0; i<x; i++)
{
   cin>>b[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0; i<dim; i++)
{
   cout<<a[i]<<"\t";
}
cout<<" \n Increments Are : ";
for(i=0; i<x; i++)
{
   cout<<b[i]<<"\t";
}

ShellSort(a, b, dim, x);         // Function Call.

getch();
}
/* End Of Main Program */

/* Function Definition */
void ShellSort(int a[], int b[], int d, intx)
{

/* Declaration Of Local Variables Of Function */
int j, k , l, y, inc, span;
for(j=0; j<x; j++)
{
   span=b[j];
   for(k=span; k<d; k++)
   {
      y=a[k];
      for(l=k-span; l>=0&&y<a[l]; l-=span)
       a[l+span]=a[l];
      a[l+span]=y;
   }
}

/* Printing The Output Onto The Screen/Console */
cout<<" \n After Sorting Array Is : ";
for(j=0; j<d; j++)
{
   cout<<a[j]<<"\t";
}
}
/* End Of Function */
Output :

Enter Dimension : 10

Enter 10 Values For Array :
91  28  73  46  50  19  82  37  64  05

Enter Number Of Increments : 3

Enter Increments Last 1 : 4  2  1

Values Of Array Are :
91  28  73  46  50  19  82  37  64  05

Increments : 4  2  1

After Sorting Array Is :
05  19  28  37  46  50  64  73  82  91




04 June, 2013

C++ Program To Perform Heap Sort.

This Post Contains A C++ Program To Perform Heap Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Heap Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Heap Sort.
 
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
void hsort(int x[], int n);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, dim;
int a[20];
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;

cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
   cin>>a[i];
}

cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

hsort(a, dim);        // Call To The Function.

cout<<" \n After Heap Sort : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

getch();
}
/* End Of Main Program */
/* Source Code For Computing Heap Sort */
/* Start Of Heap Sort Function */
void hsort(int x[], int n)
{
int i, s, f, elt, ivalue;
for(i=1;i<n;i++)
{
   elt=x[i];
   s=i;
   f=(s-1)/2;
   while(s>0  && x[f]<elt)
   {
      x[s]=x[f];
      s=f;
      f=(s-1)/2;
   }
   x[s]=elt;
}
for(i=n-1;i>0;i--)
{
   ivalue=x[i];
   x[i]=x[0];
   f=0;
   if(i==1)
   {
      s=-1;
   }
   else
   {
      s=1;
   }
   if( (i>2) && (x[2]>x[1]) )
   {
      s=2;
   }
   while( (s>=0) && (ivalue<x[s]) )
   {
      x[f]=x[s];
      f=s;
      s=2*f+1;
      if( (s+1<=i-1) && (x[s]<x[s+1]) )
      s=s+1;
      if(s>i-1)
      s=-1;
   }
   x[f]=ivalue;
}
}
/* End Of Heap Sort Function */

 
Output :

Enter Dimension :  5

Enter Values For Array :
3  1  5  2  4

After Heap Sort :
1  2  3  4  5




C++ Program To Perform Quick Sort.

This Post Contains A C++ Program To Perform Quick Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Quick Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Perform Quick Sort.


/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
int dim, a[10];
void qsort(int, int);
/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, l, r;
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;

cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
   cin>>a[i];
}

cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

l=0;
r=dim-1;
qsort(l, r);
cout<<" \n After Quick Sort : ";
for(i=0;i<dim;i++)
{
   cout<<a[i]<<"\t";
}

getch();
}
/* Source Code For Computing Quick Sort */
void qsort(int left, int right)
{
int i, j, p, temp;
if(right>left)
{
   i=left;
   j=right;
   p=a[left];
   while(right>left)
   {
      do
      {
          i++;
      }
      while( (a[i]<=p) && (i<=right) );
      while( (a[j]>=p) && (j>left) )
      {
          --j;
      }
      if(j>i)
      {
         temp=a[i];
         a[i]=a[j];
         a[j]=temp;
      }
   }
   temp=a[left];
   a[left]=a[j];
   a[j]=temp;
   qsort(left, j-1);
   qsort(i, right);
}
}
/* End Of Main Program */

 
Output :
Enter Dimension :  5

Enter Values For Array :
3  1  5  2  4

After Quick Sort :
1  2  3  4  5




Mini Project :~ Sorting Techniques Using Functions #02.

This Post Contains A Mini Project :~ Sorting Techniques Using Functions #02 With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Quick Sort' & 'Merge Sort' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

In The Following Program, We Would Perform Following Sorting Techniques Such As :
1. Quick Sort.
2. Merge Sort.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Program To Perform 'Quick' And 'Merge' Types Of Sorting Techniques Using Functions.

/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  a[100],b[100],l,k,No2=0,j,t,m,i;
int  Scan(int a[100]);
int  QuickS(int a[100],int ,int);
void Disp1();
void Split(int,int);
void MergeS(int ,int ,int);
void Disp2();

/* Start Of Main Program */
void main()
{
    int No1 = 0;
    clrscr();
    printf("\n\t\t :: Welcome To 'Quick' And 'Merge' Types Of Sorting Program ::");
    do                                                                               // Displaying Menu Onto Screen.
    {
        printf("\n\nPlease Select One Of The Following Options ::");
        printf("\n1. Quick Sorting");
        printf("\n2. Merge Sorting");
        printf("\n3. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                l = 0;
                QuickS(a , l , No2);                 // Call To The Function.
                Disp1();
                break;
            case 2: No2 = (Scan(a));
                Split(0,No2-1);
                Disp2();
                break;
            case 3: exit (0);
                break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 3);
    getch();
}
int Scan(int a[100])
{
    int i;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    return (No2);
}
QuickS(int a[],int l,int No2)
{
    if(l >= No2)
        return(0);
    k=l;
    i=l;
    j=No2;
    m=a[i];
    do
    {
        do
        {
            i++;
        }
        while(a[i]<=m && i<No2);
        do
        {
            j--;
        }
        while(a[j]>=m && j>l);
        if(i<j)
        {
            t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    while(i<j);

    t=a[j];
    a[j]=m;
    a[k]=t;
    QuickS(a,l,j);
    QuickS(a,j+1,No2);
    return(0);
}
void Disp1()
{
    printf("\nQuick Sorted Array  A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void MergeS(int low,int mid,int high)
{
    int i,h,j,k;
    i=low;
    h=low;
    j=mid+1;
    while(h <= mid && j <= high)
    {
        if(a[h] < a[j])
        {
            b[i] = a[h];
            h++;
        }
        else
        {
            b[i] =  a[j];
            j++;
        }
        i++;
    }
    if( h > mid )
    {
        for(k=j ; k <= high ; k++)
        {
            b[i] = a[k];
            i++;
        }
    }
    else
    {
        for(k=h ; k<= high ; k++)
        {
            b[i] =a[k];
            i++;
        }
    }
    for(k=low ; k <= high ; k++)
        a[k] = b[k];
}

void Split(int low,int high)
{
    int mid;
    if(low<high)
    {
        mid = (low+high)/2;
        Split(low,mid);
        Split(mid+1,high);
        MergeS(low,mid,high);                // Call To The Function.
    }
}
void Disp2()
{
    printf("\nMerge Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
/* End Of Main Program */

 
Output :~
           
Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 8
   
Please Enter The Elements For Array A ::

7  4  1  0  6  2  8  5

Quick Sorted Array Is As Follows ::

0  1  2  4  5  6  7  8

Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 10
   
Please Enter The Elements For Array A ::

7  4  1  0  6  2  8  5  9  3

Merge Sorted Array Is As Follows ::

0  1  2  3  4  5  6  7  8  9

Please Select One Of The Following Options ::
1. Quick Sorting
2. Merge Sorting
3. Exit From Menu
Please Enter Your Choice Here :: 3




26 May, 2013

Mini Project :~ Sorting Techniques Using Functions #01.

This Post Contains A Mini Project :~ Sorting Techniques Using Functions. With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Sorting Techniques' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

In The Following Program, We Would Perform Following Sorting Techniques Such As :
1. Bubble Sort.
2. Insertion Sort.
3. Selection Sort.
4. Shell Sort.



# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.




Program To Perform Different Types Of Sorting Techniques Using Functions.


/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  No2 = 0;
int  Scan(int a[100]);
void BubbleS(int a[100],int);
void InsertionS(int a[100],int);
void SelectionS(int a[100],int);
void ShellS(int a[100],int);

/* Start Of Main Program */
void main()
{
    int No1 = 0;
    int a[100];
    clrscr();
    printf("\n\t\t :: Welcome To 'Sorting' Program ::");    
    do                                                                        // Displaying 'Menu' On Screen.
    {
        printf("\nPlease Select One Of The Following Options ::");
        printf("\n1. Bubble Sorting");
        printf("\n2. Insertion Sorting");
        printf("\n3. Selection Sorting");
        printf("\n4. Shell Sorting");
        printf("\n5. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");     // Accepting User's Choice For Sorting.
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                        // Call To Function 'Bubble' Sort.
                        BubbleS(a , No2);
                        break;
            case 2: No2 = (Scan(a));
                        // Call To Function 'Insertion' Sort.
                        InsertionS(a , No2);
                        break;
            case 3: No2 = (Scan(a));
                        // Call To Function 'Slection' Sort.
                        SelectionS(a , No2);
                        break;
            case 4: No2 = (Scan(a));
                        // Call To Function 'Shell' Sort.
                       ShellS(a , No2);
                       break;
            case 5: exit (0);
                break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 5);     // End Of 'do...while' Loop.
    getch();
}
int Scan(int a[100])
{
    int i;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    return (No2);
}
void BubbleS(int a[100], int No2)
{
    int i , Ptr , Temp = 0;
    for(i=1;i<No2;i++)
    {
        Ptr = 0;
        while(Ptr < (No2 - i))
        {
            if(a[Ptr] > a[Ptr+1])
            {
                Temp = a[Ptr];
                a[Ptr] = a[Ptr+1];
                a[Ptr+1] = Temp;
            }
            Ptr++;
        }
    }
    printf("\nBubble Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void InsertionS(int a[100],int No2)
{
    int i , j , Temp = 0;
    for(i=1;i<No2;i++)
    {
        Temp = a[i];
        for(j=(i-1); j>=0 && Temp<a[j] ; j--)
        {
            a[j+1] = a[j];
        }
        a[j+1] = Temp;
    }
    printf("\nInsertion Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void SelectionS(int a[100],int No2)
{
    int i , j , k , Temp = 0;

    for(i=0;i<(No2-1);i++)
    {
        k = i;
        for(j=(i+1);j<No2;j++)
        {
            if(a[j] < a[k])
            {
                k = j;
            }
        }
        if(k != i)
        {
            Temp = a[i];
            a[i] = a[k];
            a[k] = Temp;
        }
    }
    printf("\nSelection Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}
void ShellS(int a[100],int No2)
{
    int i, k, Stop, Swap, Limit, Temp;
    int x = (int)(No2/2)-1;
    while(x > 0)
    {
        Stop  = 0;
        Limit = (No2 - x);
        while(Stop == 0)
        {
            Swap = 0;
            for(k=0;k<Limit;k++)
            {
                if(a[k] > a[k+x])
                {
                    Temp   = a[k];
                    a[k]   = a[k+x];
                    a[k+x] = Temp;
                    Swap   = k;
                }
            }
        Limit = (Swap - x);
        if(Swap == 0)
            Stop = 1;
        }
        x=(int)(x/2);
    }
    printf("\nShell Sorted Array A Is As Follows ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
}

Output :~

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Bubble Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Insertion Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 3

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Selection Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 4

Please Enter The Limit Of Array A :: 5

Please enter The Elements Of Array A ::

3  5  4  2  1

Shell Sorted Array A Is As Follows ::

1  2  3  4  5 

Please Select One Of The Following Options ::
1. Bubble Sorting
2. Insertion Sorting
3. Selection Sorting
4. Shell Sorting
5. Exit From Menu
Please Enter Your Choice Here :: 5




02 May, 2013

Mini Project :~ Searching Techniques Using Functions.

This Post Contains A Mini Project :~ Searching Techniques Using Functions. With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Searching Techniques' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



/* Program To Perform Different Types Of Searching Techniques Using Functions */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int  No2 = 0;
int  Scan(int a[100]);
void Lsearch(int a[100],int);
void Bsearch(int a[100],int);
void Fsearch();
int  FibSearch(int a[100],int ,int ,int ,int );                         
int  fib(int );
void main()
{
    int No1 = 0;
    int a[100];
    clrscr();
    printf("\n\t\t :: Welcome To 'Searching & Sorting' Program ::");
    do
    {
        printf("\n\nPlease Select One Of The Following Options ::");
        printf("\n1. Linear Searching");
        printf("\n2. Binary Searching");
        printf("\n3. Fibbonanci Searching");
        printf("\n4. Exit From Menu");
        printf("\nPlease Enter Your Choice Here ::\t");
        scanf("%d",&No1);
        switch(No1)
        {
            case 1: No2 = (Scan(a));
                    Lsearch(a , No2);
                    break;
            case 2: No2 = (Scan(a));
                    Bsearch(a , No2);
                    break;
            case 3: Fsearch();
                    break;
            case 4: exit (0);
                    break;
            default:printf("\nSorry!!! Invalid Choice");
        }
    }
    while(No1 <= 4);
    getch();
}
int Scan(int a[100])
{
    int i;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    return (No2);
}
void Lsearch(int a[100],int No2)
{
    int i , No3 = 0;
    int flag = 0;
    printf("\nPlease Enter The Element Which You Want Search Into The Array A ::\t");
    scanf("%d",&No3);
    for(i=0;i<No2;i++)
    {
        if(No3 == a[i])
        {
            flag = 1;
            break;
        }
        else
        {
            flag = 0;
        }
    }
    if(flag == 1)
    {
        printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location With Memory Location :- (%u)",a[i],(i+1),&a[i]);
    }
    else
    {
        printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
    }
}
void Bsearch(int a[100],int No2)
{
    int i , Ptr , Temp = 0 , No4 = 0;
    int Lb = 0 , Mid = 0 , Ub = No2;
    int flag = 0;
    for(i=1;i<No2;i++)
    {
        Ptr = 0;
        while(Ptr <= (No2 - i))
        {
            if(a[Ptr] > a[Ptr+1])
            {
                Temp = a[Ptr];
                a[Ptr] = a[Ptr+1];
                a[Ptr+1] = Temp;
            }
            Ptr++;
        }
    }
    printf("\nYour Entered Elements Have Been Sorted Into The Array A ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
    Mid = ((Lb + Ub) / 2);
    printf("\nPlease Enter The Element Which You Want Search Into The Array A ::\t");
    scanf("%d",&No4);
    if(No4 == a[Mid])
    {
        flag = 1;
    }
    else
    {
        if(No4 < a[Mid])
        {
            for(i=Lb;i<Mid;i++)
            {
                if(No4 == a[i])
                {
                    flag = 1;
                    break;
                }
            }
        }
        else
        {
            for(i=(Mid+1);i<Ub;i++)
            {
                if(No4 == a[i])
                {
                    flag = 1;
                    break;
                }
            }
        }
    }
    if(flag == 1)
    {
        printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location With Memory Location :- (%u)",a[i],(i+1),&a[i]);
    }
    else
    {
        printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
    }
}
void Fsearch()
{
    int No2 = 0 , a[100] , i , k , Ptr , Temp = 0 , key = 0 , p , q , Mid = 0 , Result = 0;
    printf("\nPlease Enter The Limit Of Array A ::\t");
    scanf("%d",&No2);
    printf("\nPlease Enter The Elements For Array A ::\n");
    for(i=0;i<No2;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<No2;i++)
    {
        Ptr = 0;
        while(Ptr < (No2 - i))
        {
            if(a[Ptr] > a[Ptr+1])
            {
                Temp     = a[Ptr];
                a[Ptr]   = a[Ptr+1];
                a[Ptr+1] = Temp;
            }
            Ptr++;
        }
    }
    printf("\nYour Entered Elements Have Been Sorted Into The Array A ::\n");
    for(i=0;i<No2;i++)
    {
        printf("%d \n",a[i]);
    }
    printf("\nEnter The Element To Be Searched ::\t ");
    scanf("%d",&key);
    for(k=1 ; fib(k) <= No2 ; k++);
    p   = fib(k-2);
    q   = fib(k-3);
    Mid = No2-p+1;
    Result = FibSearch(a,key,Mid,p,q);
    if(Result == -1)
           printf("\nSorry!!! The Entered Element Was Not Found Into Array A. Please Try Again!");
    else
           printf("\nThe Entered Element (%d) Is Found Into Array A At (%d) Location",key,Result);
}
int FibSearch(int a[],int key,int Mid ,int p ,int q)
   {
       int Temp;
       if(key==a[Mid])
            return(Mid);
       if(key> a[Mid])
       {
            if(p == 1)
                return(-1);

            Mid = (Mid + q);  // new mid
            p   = (p - q);      //p=fib(k-4)
            q   = (q - p);      //q=fib(k-5)
            return(FibSearch(a,key,Mid,p,q));
      }
      else    //key < a[mid]
      {
            if(q==0)
                return(-1);

            Mid  = (Mid - q);    // new mid
            Temp = (p - q);
            p    = q;         //p=fib(k-3)
            q    = Temp;      //q=fib(k-4)
            return(FibSearch(a,key,Mid,p,q));
      }
}
int fib(int n)
{
    if(n==0)
    {
        return(0);
    }
    if(n==1)
    {
        return(1);
    }
    return(fib(n-1)+fib(n-2));
}

Output :~

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 1

Please Enter The Limit Of Array A :: 5

Please Enter The Elements For Array A ::
5
1
3
2
4

Please Enter The Element Which You Want To Search Into Array A :: 3

The Entered Element (3) Is Found Into Array A At (3) Location With Memory Location :- 62230

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 2

Please Enter The Limit Of Array A :: 5

Please Enter The Elements For Array A ::
5
1
3
2
4

Your Entered Elements Have Been Sorted Into The Array A ::
1
2
3
4
5

Please Enter The Element Which You Want To Search Into Array A :: 5

The Entered Element (5) Is Found Into Array A At (5) Location With Memory Location :- 62234

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 3

Please Enter The Limit Of Array A :: 5

Please Enter The Elements For Array A ::
5
1
3
2
4

Your Entered Elements Have Been Sorted Into The Array A ::
1
2
3
4
5

Please Enter The Element Which You Want To Search Into Array A :: 3

The Entered Element (3) Is Found Into Array A At (2) Location

Please Select One Of The Following Options ::
1. Linear Searching
2. Binary Searching
3. Fibbonanci Searching
4. Exit From Menu
Please Enter Your Choice Here :: 4




01 March, 2013

Mini Project :~ C Program For Various Operations On Set. [Such As : Scanning, Union, Intersection, Difference, Symmetric Difference]

This Post Contains A Mini Project :~ C Program For Various Operations On Set [Such As : Scanning, Union, Intersection, Difference, Symmetric Difference] With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Do-While Loop', 'Switch Cases', 'Function Call' & 'Menu Driven Format' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Operations covered :

  • Scanning()  :  For creating a new set with initial members of the set.
  • Union()  :  Finds union of two sets, set1[] and set2[]  and stores the result in set3[] 
  • Intersection()  :  Finds intersection of two sets, set1[] and set2[] and stores the result in set3[]
  • Difference()  :  Finds difference of two sets, set1[] and set2[] and stores the result in set3[]
  • Symmetric Difference()  :  Finds Symmetric difference of two sets

Mini Project :~ C Program For Various Operations On Set.

/* Program To Carry Out Different Operations On Sets By Using 'Menu Driven' Concept */

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define Max 100
void Scaning();
void Union();
void Intersection();
void Difference(int *,int *,int ,int);
void SymetricDifference();
void Display();
void Display1();
void Display2(char ,char ,int );
void Display3();

int m,n,i,j,k,p,q,r,s,a[Max],b[Max],c[Max];
int flag=1;
char char1,char2,char3;

void main()
{
    int Num=0;
    clrscr();
    printf("\n\t\t::Welcome To The 'Set Operation' Program::\n");
    printf("\nPlease Select One Of The Following Operations To Proceed Further :: ");
    do
    {
        printf("\n\t1. Union Of Sets");
        printf("\n\t2. Intersection Of Sets");
        printf("\n\t3. Difference Of Sets");
        printf("\n\t4. Symmetric Difference Of Sets");
        printf("\n\t5. Exit From The Program\n");
        printf("\nPlease Select The Operation No:: ");
        scanf("%2d",&Num);
        switch(Num)
        {
            case 1:Scaning();
                   Union();
                   Display();                    // Function For Displaying Resultant Union Of 2 Sets.
                   break;

            case 2:Scaning();
                   Intersection();
                   Display1();                 // Function For Displaying Resultant Intersection Of 2 Sets.
                   break;

            case 3:Scaning();
                Difference(a,b,m,n);     // Function For Displaying Resultant Difference Of 2 Sets. i.e A-B.
                Display2('A','B',m);
                Difference(b,a,n,m);
                Display2('B','A',n);     // Function For Displaying Resultant Difference Of 2 Sets. i.e B-A.
                   break;

            case 4:Scaning();
                   SymetricDifference();
                   Display3();             // Function For Displaying Resultant Symmetric Difference Of 2 Sets.
                   break;

            default:abort();
        }
    }
    while(Num<=4);
    getch();
}

void Scaning()
{
    printf("\nPlease Enter The No Of Elements In A :\t");
    scanf("%d",&m);
    printf("\nPlease Enter The Elements For Set A :\n");
    printf(" { ");
    for(i=0;i<m;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("}");

    printf("\nPlease Enter The No Of Elements In B:\t");
    scanf("%d",&n);
    printf("\nPlease Enter The Elements For Set B :\n");
    printf(" { ");
    for(j=0;j<n;j++)
    {
        scanf("%d",&b[j]);
    }
    printf("}");
}
void Union()
{
    k=0;
    for(i=0;i<m;i++)
    {
        c[k]=a[i];
        k++;
    }

    p=m;
    for(j=0;j<n;j++)
    {
        flag=1;
        for(k=0;k<m;k++)
        {
            if(c[k]==b[j])
            {
                flag=1;
                break;
            }
            else
            {
                flag=0;
            }
        }
        if(flag==0)
        {
            c[p]=b[j];
            p++;
        }
    }
}
void Display()
{
    printf("\nThe Union Of Two Sets Is : { ");
    for(k=0;k<p;k++)
    {
        printf("%2d",c[k]);
    }
    printf(" }");
}
void Intersection()
{
    q=0;
    p=0;
    for(j=0;j<n;j++)
    {
        flag=1;
        for(k=0;k<m;k++)
        {
            if(a[k]==b[j])
            {
                flag=1;
                q++;
                break;
            }
            else
            {
                flag=0;
            }
        }
        if(flag==1)
        {
            c[p]=b[j];
            p++;
        }
    }
}
void Display1()
{
    printf("\nThe Intersection Of Two Sets Is : { ");
    for(p=0;p<q;p++)
    {
        printf("%2d",c[p]);
    }
    printf(" }");
}
void Difference(int *a1,int *b1,int m1,int n1)
{
    q=0;
    p=0;
    i=0;
    for(k=0;k<m1;k++)
    {
        flag=1;
        for(j=0;j<n1;j++)
        {
            if(b1[j]==a1[k])
            {
                flag=1;
                q++;
                break;
            }
            else
            {
                flag=0;
            }
        }
        if(flag==0)
        {
            c[p]=a1[k];
            p++;
        }
    }
}
void Display2(char ac,char bc,int m1)
{
    printf("\nThe Difference Of Two Sets i.e '%c - %c' Is : { ",ac,bc);
    r = m1 - q;
    for(p=0;p<r;p++)
    {
        printf("%2d",c[p]);
    }
    printf(" }");
}
void SymetricDifference()
{
    q=0;
    p=0;
    i=0;
    for(k=0;k<m;k++)
    {
        flag=1;
        for(j=0;j<n;j++)
        {
            if(b[j]==a[k])
            {
                flag=1;
                q++;
                break;
            }
            else
            {
                flag=0;
            }
        }
        if(flag==0)
        {
            c[p]=a[k];
            p++;
        }
    }
    for(j=0;j<n;j++)
    {
        flag=1;
        for(k=0;k<m;k++)
        {
            if(a[k]==b[j])
            {
                flag=1;
                break;
            }
            else
            {
                flag=0;
            }
        }
        if(flag==0)
        {
            c[p]=b[j];
            p++;
        }
    }

}
void Display3()
{
    printf("\nThe Symetric Difference Of Two Sets Is : { ");
    for(k=0;k<p;k++)
    {
        printf("%2d",c[k]);
    }
    printf(" }");
}




29 December, 2012

C++ Program To Find NCR Factor.

This Post Contains A C++ Program To Find NCR Factor With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'Functions' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Find NCR Factor.

/* Declaration Of Header Files */
# include < iostream.h >
# inlcude < conio.h >
int fact(int);  // Declaring fact() Function

/* Start Of Main Program */
void main()
{
       int n, c, r;
       clrscr();
       cout << " Enter Values For 'N' & 'R' :- ";
       cin >> n >> r;
       c = fact(n) / ( fact(r) * (fact( n - r ) ) );    // Calling fact() Function
       cout << endl << " NCR Factor Is :- " << c;
       getch();
}
/* End Of Main Program */

/* Defining fact() Function */
int fact( int x )
{
      int i, f = 1;
      for( i = x; i > 0; i-- )
      {
            f = f * i;
       }
       return(f);
}

Output :

Enter Values For 'N' & 'R' :-  6   4

NCR Factor Is :-  15





20 March, 2012

C++ Program To Find LCM & GCD Of Integers/Numbers.

LCM: Least Common Multiple.

In Arithmetic, The LCM: Least Common Multiple[Also Called As 'Lowest Common Multiple'] Of Two Integers a & b, Usually Denoted By LCM[a,b], Is The Smallest Positive Integer That Is Divisible By Both a & b. If Either a Or b Is '0' [Zero], LCM[a,b] Is '0' [Zero].

Overview

A Multiple Of A Number Is The Product Of That Number & An Integer. For Ex, 10 Is A Multiple Of 5 Because 5 x 2 = 10, So 10 Is Divisible By 5 & 2. Because 10 Is The Smallest Positive Integer That Is Divisible By Both 5 & 2, It Is The Least Common Multiple Of 5 & 2. By The Same Principle, 10 Is The Least Common Multiple Of -5 & 2 As Well.

Example

What Is The LCM Of 4 & 6?

Multiples Of 4 Are:
  
   4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, ...

and the multiples of 6 are:
  
   6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, ...

Common multiples of 4 and 6 are simply the numbers that are in both lists:

   12, 24, 36, 48, 60, 72, ....

So, from this list of the first few common multiples of the numbers 4 and 6, their least common multiple is 12.

Application

When adding, subtracting, or comparing vulgar fractions, it is useful to find the least common multiple of the
denominators, often called the lowest common denominator, because each of the fractions can be expressed as a
fraction with this denominator. For instance,

[2\21] + [1\6] = [4\42] + [7\42] = [11\42]

where the denominator 42 was used because it is the least common multiple of 21 and 6.

This Post Contains A C++ Program To Find LCM & GCD Of Integers/Numbers With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Cascading Of Operators' & 'Functions' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Find LCM & GCD Of Integers/Numbers.

/* Declaration Of Header Files */
# include <iostream.h>
# include <conio.h>
int gcd ( int , int );      // Function Declaration.
void lcm ( int , int );   // Function Declaration.

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
   int a, b;
   clrscr();

/* Asking For The Input From User */
   cout << " Enter Two Nos : ";
   cin >> a >> b;
   if (  a  <  b  )
   {
      cout << " \n GCD Of " << a << "&" << b << " : " << gcd (a , b);    // Function Call.
   }
   else
   {
      cout << " \n GCD Of " << b << "&" << a << " : " << gcd (b , a);   // Function Call.
   }
   lcm (a , b);    // Function Call.
   getch();
   }
/* End Of Main Program */

   int gcd ( int c , int d )     // Function Definition.
   {
      int r;
      r = d % c;
      while ( r  !=  0  )
      {
         d = c;
         c = r;
         r = d % c;
      }
      return (c);
   }
void lcm ( int x , int y )     // Function Definition.
{
   int l;
   if ( x < y )
   {
     l = ( x * y ) / gcd ( x , y );
   }
   else
   {
     l = ( x * y ) / gcd ( y , x );
   }
   cout << " \n LCM Of " << x << "&" << b << ":" << l;
}

GCD: Greatest Common Divisor

In mathematics, the GCD: Greatest Common Divisor[also known as the 'Greatest Common Factor (GCF)', or 'Highest Common Factor (HCF)'], of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.

Notation

In this article we will denote the greatest common divisor of two integers a and b as gcd(a,b). Some older textbooks use (a,b).

Example

The number 54 can be expressed as a product of two other integers in several different ways:

   54 x 1 = 27 x 2 = 18 x 3 = 9 x 6.

Thus the divisors of 54 are:

    1, 2, 3, 6, 9, 18, 27, 54.

Similarly the divisors of 24 are:

    1, 2, 3, 4, 6, 8, 12, 24.

The numbers that these two lists share in common are the common divisors of 54 and 24:

    1, 2, 3, 6.

The greatest of these is 6. That is the greatest common divisor of 54 and 24. One writes:

    gcd(54,24) = 6.

Reducing fractions

The greatest common divisor is useful for reducing fractions to be in lowest terms. For example, gcd(42, 56) = 14, therefore,

   {42\56} = {(3)(14) \ (4)(14)} = {3\4}.

Co-prime numbers

Two numbers are called relatively prime, or co-prime if their greatest common divisor equals 1. For example, 9 and 28 are relatively prime.




Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive