08 March, 2013

C++ Program To Find Number Of Times The Given Number Occurred In An Array

This Post Contains A C++ Program To Find Number Of Times The Given Number Occurred In An Array 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 'If...else' & 'For-Loop' 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 Number Of Times The Given Number Occurred In An Array.

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


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

   /* Declaration Of Variables */
   int a[10], num, i, counter=0;

   clrscr();

   /* Asking For The Input From User */
   cout<<"\n Enter 10 Values :";
   for(i=0; i<10; i++)
   {
      cin>>a[i];
   }
   cout<<"\n Enter Any No. From Array For Counting :";
   cin>>num;
   for(i=0; i<10; i++)
   {
      if(num == a[i])
      {
         counter++;
      }
   }

   /* Printing The Output Onto The Screen/Console */
   if(counter == 0)
   {
      cout<<"\n"<< num <<"Does Not Exist In The Array";
   }
   else
   {
      cout<<"\n"<< num <<"Occurred"<< counter <<"Times";
   }
   getch();
}
/* End Of Main Program */

Output :

Enter 10 Values :
1  6  3  4  5  6  7  8  6  10

Enter Any No. From Array For Counting : 6

6 Occurred 3 Times




C++ Program For Calculating Cricket Team Average.

This Post Contains A C++ Program For Calculating Cricket Team Average 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 'For-Loop' 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 For Calculating Cricket Team Average.

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

/* Start Of Main Program */
void main()
{
  
   /* Declaration Of Variables */
   int p, no_ply, ing[10], run[10], avg[10];
   char name[10][10];

   clrscr();

  
   /* Asking For The Input From User */
   cout<<"\n Enter No. Of Players :";
   cin>>no_ply;
   for(p=0; p<no_ply; p++)
   {
      avg[p]=0;
      cout<<"\n Enter Player's Name :";
      cin>>name[p];
      cout<<"\n Enter Runs Scored :";
      cin>>run[p];
      cout<<"\n Enter No. Of Innings :";
      cin>>ing[p];
      avg[p]=run[p]/ing[p];
   }


   
   /* Printing The Output Onto The Screen/Console */
   for(p=0; p<no_ply; p++)
   {
      cout<<"\n Player's Name : "<<name[p];
      cout<<"\n Runs Scored : "<<run[p];
      cout<<"\n No. Of Innings : "<<ing[p];
      cout<<"\n Avg Runs : "<<avg[p];
   }

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


Output :

Enter No. Of Players : 2

Enter Player's Name : Saurav Ganguly
Enter Runs Scored : 234
No. Of Innings : 2

Enter Player's Name : Rahul Dravid
Enter Runs Scored : 345
No. Of Innings : 3

Enter Player's Name : Saurav Ganguly
Enter Runs Scored : 234
No. Of Innings : 2
Avg Runs : 117

Enter Player's Name : Rahul Dravid
Enter Runs Scored : 345
No. Of Innings : 3
Avg Runs : 115




C Program For Printing ASCII Values On Screen/Console.

This Post Contains A C Program For Printing ASCII Values On Screen/Console With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'For-loop' 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 For Printing ASCII Values On Console.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i = 1;
    char Ans = 'y';
    clrscr();
   
    printf("\t\t\t :: Welcome To 'ASCII' Program ::");
    
    printf("\t\t\t\tNo    ASCII Value");
    
    do
    {
        if(i % 10 == 0)
        {
            printf("\t\t\t\t%d     =     %c", i, i);
            
            printf("Do You Want To Continue...? (Y/N) :\a\t");
            fflush(stdin);
            scanf("%c", &Ans);
        }
        else
        {
            printf("\t\t\t\t%d     =     %c\n", i, i);
            printf("\r\n");
        }
        i++;
    }while((Ans == 'y') || (Ans == 'Y'));
    getch();
}
Output :

The Output Will Be The ASCII Values Of All The Respective Characters, Which Are Valid.







C Program For Printing Triangle Of Numbers.

This Post Contains A C Program For Printing Triangle Of Numbers With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'For-Loop' & '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 For Printing Triangle Of Numbers.

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

/* Start Of Main Program */
void main()
{
          /* Declaration Of Variables */
          int   i,   j,   num;
          clrscr();

          /* Asking For The Input From User */
          printf ( " \n Enter No. : " );
          scanf( " %d ", &num );

          /* Source Code For Printing Triangle Of Numbers */
          for ( i  =  1;   i  <=   num;   i++  )
         {
                    for (  j  =  1;  j   <=   i;  j++  )
                   {
                                if (  i  <  10  )
                                      printf ( " %d ",  i );
                                else
                                       printf ( " %d ",  i );
                   }
                   printf ( " \n" );
          }
          getch();
}
/* End Of Main Program */



Output  :





C Program For Conversion Of Celsius To Fahrenheit.

This Post Contains A C Program For Conversion Of Celsius To Fahrenheit With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arithematic Manipulations' 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 For Conversion Of Celsius To Fahrenheit.

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

/* Start Of Main Program */
void main()
{
  
   /* Declaration Of Variables */ 
   float cel, fah;

   clrscr();

   /* Asking For The Input From User */
   printf ( " \n Enter Temp. In Celsius : " );
   scanf ( " %f ", &cel );
  
   fah  =  (  cel  +  32  )  *  9.0  /  5.0;

   /* Printing The Output Onto The Screen/Console */
   printf ( " \n Temp. In Fahrenheit :  %f  ",  f ah);
   getch();
}

Output :

Enter Temp. In Celsius : 37 C
Temp. In Fahrenheit : 98.6 F




C Program For Conversion Of Fahrenheit To Celsius.

This Post Contains A C Program For Conversion Of Fahrenheit To Celsius With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Conversion' 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 For Conversion Of Fahrenheit To Celsius.

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

/* Start Of Main Program */
void main()
{
 
   /* Declaration Of Variables */
   float cel, fah;
   clrscr();

   /* Asking For The Input From User */
   printf ( " \n Enter Temp. In Fahrenheit : " );
   scanf ( " %f ", &fah );

   cel  =  (  fah  -  32  )  *  5.0  /  9.0;

   printf ( " \n Temp. In Celsuis :  %f  ",  cel );
   getch();
}
/* End Of Main Program */ 

Output :

Enter Temp. In Fahrenheit : 110 F
Temp. In Celsius : 43.333332 C



C Program To Find Number Of Odd & Even Numbers.

This Post Contains A C Program To Find Number Of Odd & Even Numbers From An Array 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 'If...else' & 'For-Loop' 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 Number Of Odd & Even Numbers From An Array.

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

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

/* Declaration Of Variables */
int a[20], Val, Odd_No, Even_No, i;
clrscr();

/* Asking For The Input From User */
printf ( " \n Enter Number Of Values : " );
scanf ( " %d ", &Val );
printf ( " \n Enter  %d  Values : ", Val );
for( i=0; i<Val;i ++ )
{
 scanf ( " %d ", &a[i] );
}

/* Printing The User Entered Values Onto The Screen/Console */
printf ( " \n Entered Values Are : " );
for( i=0; i<Val; i++ )
{
 printf ( " %d  \t ",  a[i] );
}

/* Source Code For Computing No. Of Odd & Even Numbers */
Odd_No = Even_No = 0;
for(i=0; i<Val; i++)
{
 if( a[i] % 2 == 0 )
 {
  Even_No++;
 }
 else
 {
  Odd_No++;
 }
}

/* Printing The Output Onto The Screen/Console */
printf ( " \n Total Number Of Odd Numbers :  %d ",  Odd_No );
printf ( " \n Total Number Of Even Numbers :  %d ",  Even_No );

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

Output :

Enter Number Of Values : 5
Enter 5 Values : 1 2 3 4 5
Entered Values Are : 1 2 3 4 5
Number Of Odd Numbers : 3
Number Of Even Numbers : 2

Algorithm :

1. Start.
2. Initialize necessary variables & one integer array.
3. Accept the dimension [Number Of Values] of array, i.e. 'Val' from user.
4. Accept the values for array.
 4.1 for(i=0;i<Val;i++)
 4.2 Accept the value & store it in array, i.e. a[i].
 4.3 End of for loop.
5. Print the values of array.
 5.1 for(i=0;i<Val;i++)
 5.2 Print the value of array, i.e. a[i].
 5.3 End of for loop.
6. Make 'Odd_No' & 'Even_No' variables zero.
7. Find odd or even number from array.
 7.1 for(i=0;i<Val;i++)
 7.2 If value, a[i] is completely divisible by 2, then
 7.2.1 Increment Even_No variable by 1.
 7.3 Else
 7.3.1 Increment Odd_No variable by 1.
 7.4 End of if...else.
 7.5 End of for loop.
8. Print Number of odd numbers, Odd_No.
9. Print Number of even numbers, Even_No.
10. Exit.



C Program To Find Prime Factors Of A Number.

This Post Contains A C Program To Find Prime Factors Of A Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'If...else' & 'While-Loop' 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.


# Algorithm :

1. Accept any number from user.
2. Initialize i. i=1.
3. Run a loop from 1 to that number to divide the accepted number.
    while( i <= num )
4. if 'i' is a Factor or perfectly divides 'num'.
    if( num % i == 0 )
5. Check whether it is a prime number or not.
    Initialize two variables.
    int j = 1, k = 0;
6. If it is a prime number or k==2.
    Print the Number is a Prime Factor.
7. End if
8. Increment i. i++.
9. End 'while' loop.
10. Stop.


C Program To Find Prime Factors Of A Number.

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

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

    /* Declaration Of Variables */
    int num,  i;
    clrscr();

    /* Asking For The Input From User */
    cout  <<  " Enter Any Number : ";
    cin >>  num;

    /* Source Code For Computing Prime Factors Of A Number */
    i  =  1;
    while(  i  <=  num  )
    {
        if( num % i == 0 )
        {
            int j = 1, k = 0;
            while( j  <=  i)
            {
                if( i % j == 0 )
                k++;
                j++;
            }
            if( k == 2 )
            printf ( "\n  %d  Is A Prime Factor Of   %d  ",  i,  num );
            printf ( " \n " );
        }
        i++;
    }
    getch();
}
/* End Of Main Program */

Output :

Enter Any Number :  60

2 Is A Prime Factor Of 60

3 Is A Prime Factor Of 60

5 Is A Prime Factor Of 60



C Program To Find A Factorial Of A Number.

This Post Contains A C Program To Find A Factorial Of A Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'For-Loop' 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 A Factorial Of A Number.

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

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

{

   /* Declaration Of Variables */
   int i, Num = 0, f;

   clrscr();

   /* Asking For The Input From User */
   printf ( " \n Enter Any Number : " );

   scanf ( " %d ", &Num );

   /* Source Code For Computing Factorial Of A Number */
   f  =  1;

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

   {

       f  =  f  *  i;

   }

   printf( " \n Factorial Of  No.  %d  :  %d  ", Num, f );

   getch();

}
/* End Of Main Program */

Output :

Enter Any Number  :   6!

Factorial Of  No. 6  :   720



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(" }");
}




C++ Program For Converting A User Entered Number Into A Single Digit.

This Post Contains A C++ Program For Converting A User Entered Number Into A Single Digit With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Do...While-Loop', 'While-Loop' & '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 For Converting A User Entered Number Into A Single Digit.

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

/* Start Of Main Program */
void main()
{
 /* Declaration Of Variables */
 int s, r, a[10], i;
 long Num, Temp;
 clrscr();

 /* Asking For The Input From User */
 cout << " \n Enter Any Number : ";
 cin >> Num;
 Temp=Num;

 /* Source Code For Computing Converting User Entered Number, i.e 'Num' Into A Single Digit */
 do
 {
  i=s=0;
  while(Num>0)
  {
   r=Num%10;
   a[i]=r;
   s=s+r;
   i++;
   Num=Num/10;
  }
  --i;
  if(i!=0)
  {
   Num=s;
  }
  if(i==0 || i==1)
  {
   cout << " \n Answer After Converting " << Temp << " Into A Single Digit Is : " << s;
   break;
  }
 }while(i>0);

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

Output :

Enter Any Number  :  12345
[1+2+3+4+5] = 15 = [1+5] = 6
Answer After Converting 12345 Into A Single Digit Is  :  6




C++ Program To Print A Number In An Ordered Form.

This Post Contains A C++ Program To Print A Number In An Ordered Form 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 'For-Loop', 'While-Loop' & '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 Print A Number In An Ordered Form.

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

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

/* Declaration Of Variables */
long Num, s;
int a[10], i, j, k, temp;
clrscr();

/* Asking For The Input From User */
cout << " \n Enter Any Number : ";
cin >> Num;
cout << " \n Entered Number Is : " << Num;

/* Storing Number In Array */
j=0;
while( Num > 0 )
{
 i=Num%10;
 a[j]=i;
 j++;
 Num=Num/10;
}

/* Sorting An Array */
for(i=0;i<j;i++)
{
 for(k=i+1;k<j;k++)
 {
  if(a[i]>a[k])
  {
   temp=a[i];
   a[i]=a[k];
   a[k]=temp;
  }
 }
}

/* Converting Array Of Numbers Into A Single Number */
temp=j-2;
Num=s=0;
for(i=0;i<j;i++)
{
 s=a[i];
 for(k=0;k<=temp;k++)
 {
  s=s*10;
 }
 temp--;
 Num=Num+s;
}

/* Printing The Output Onto Screen/Console */
cout << " \n Ordered Number Is : " << Num;
getch();
}
/* End Of Main Program */

Output :

Enter Any Number : 25314
Entered Number Is : 25314
Ordered Number Is : 12345

Algorithm :

1. Initialize necessary variables & one integer array.
2. Accept one 'long int' or 'int' from user.
3. Print accepted number.
4. Store each digit of number in array.
 4.1 While(Num>0)
 4.2 Divide Num by 10, assign remainder to 'i';
 4.3 Store it in array.
 4.4 Increment the array count.
 4.5 Take remaining part of number.
     Num=Num/10
 4.6 End of while loop.
5. Sort the array in ascending order by using 'temp' variable.
6. Convert array into asingle number.
 6.1 temp=j-2. Units position.
 6.2 Initialize necessary variables.
 6.3 for(i=0;i<j;i++)
 6.3.1 Assign 1st array element to 's'.
 6.3.2 for(k=0;k<=temp;k++)
 6.3.3 Multiply s by 10 & assign same to s.
 6.3.4 End of inner for loop.
 6.3.5 Decrement temp.
 6.3.6 Add s to Num.
 6.3.7 End of outer for loop.
7. Print the ordered number.
8. Exit.




Subscribe To:

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