Monday, 3 September 2012

Array020


Write a program for Binary search.

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

void main ( )
{
             int        n[] = {12, 45, 50, 55, 60, 65, 70, 75, 80, 90, 99, 100};
             int        i,no, b = 0, l =11, m, flag = 1;
             clrscr ( );

             printf ("\n\n\t List of Numbers =");
             for ( i = 0; i <= 11; i++ )
             {                                      
                             printf (" %d", n[i]);
             }                                                                
                                                                                         
             printf ("\n\n\n\t\t\t Enter a number...........:"); scanf ("%d", &no);
             while ( b <= l )
             {                                                        
                           m = ( b + l ) / 2;
                           if ( n[m] == no )
                           {                                      
                                          flag = 0;
                                          break;
                           }                  
                           if ( n[m] < no )
                           {                                      
                                          b = m + 1;
                           }                                      
                           else                                
                           {                                          
                                          l = m - 1;
                           }                                    
             }
             if ( flag == 0 )
                          printf ("\n\n\t\t\t SUCCESFUL");
             else                                          
                          printf ("\n\n\t\t\t UNSUCCESFUL");
             getch ( );
}
Click here to contact us

Sunday, 2 September 2012

Array017

Input five numbers in an array and write a program to sort them in reverse order (Using Bubble sort method).
#include <stdio.h>
#include <conio.h>

void main ( )
{
          int        nos[5], i, j, tem;
          clrscr ( );
                                          
          for ( i = 0; i < 5; i++ )
          {                      
                          printf ("\n\t\t\t Enter a number...........:"); scanf ("%d", &nos[i]);
          }                             
                                                   
          for ( i = 0; i < 5; i++ )
          {                                     
                          for ( j = 0; j < 5 - ( i + 1 ); j++ )
                          {                                                 
                                          if ( nos[ j] <= nos[ j +1] )
                                          {                                           
                                                          tem = nos [ j];
                                                          nos[ j] = nos[ j + 1];
                                                          nos[ j + 1] = tem;
                                          }
                          }
          }
          printf ("\n\n");
          for ( i = 0; i < 5; i++ )
          {                 
                          printf ("\n\t\t    Position : %d \t Output Number : %d", i, nos[i] );
          }
          getch ( );
}

Click here to contact us

Array019

Input five numbers in an array and write a program to sort them in reverse order (Using Selection sort method).

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

void main ( )
{
            int    nos[5], i, j, tem;
            clrscr ( );
              
            for ( i = 0; i < 5; i++ )
            {
                           printf ("\n\t\t\t Enter a number...........:"); scanf ("%d", &nos[i]);
            }

            for ( i = 0; i < 5; i++ )
            {
                           for ( j = i + 1; j < 5; j++ )
                           {
                                            if ( nos[i] <= nos[ j] )
                                            {
                                                            tem = nos [i];
                                                            nos[i] = nos[ j];
                                                            nos[ j] = tem;
                                            }
                           }
            }
            printf ("\n\n");
            for ( i = 0; i < 5; i++ )
            {
                            printf ("\n\t\t    Position : %d \t Output Number : %d", i, nos[i] );
            }
            getch ( );
}

Click here to contact us

Array018

Input five numbers in an array and write a program to sort them (Using Selection sort method).

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

void main ( )
{              
           int    nos[5], i, j, tem;
           clrscr ( );
                                         
           for ( i = 0; i < 5; i++ )
           {                                  
                           printf ("\n\t\t\t Enter a number...........:"); scanf ("%d", &nos[i]);
           }
                                              
           for ( i = 0; i < 5; i++ )
           {                                    
                           for ( j = i + 1; j < 5; j++ )
                           {                                       
                                           if ( nos[i] >= nos[ j] )
                                           {                                     
                                                           tem = nos [i];
                                                           nos[i] = nos[ j];
                                                           nos[ j] = tem;
                                           }
                           }
           }
           printf ("\n\n");
           for ( i = 0; i < 5; i++ )
           {                                     
                           printf ("\n\t\t    Position : %d \t Output Number : %d", i, nos[i] );
           }
           getch ( );
}
Click here to contact us

Array016

Input five numbers in an array and write a program to sort them (Using Bubble sort method).

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

void main ( )
{
          int        nos[5], i, j, tem;
          clrscr ( );
                                           
          for ( i = 0; i < 5; i++ )
          {                       
                          printf ("\n\t\t\t Enter a number...........:"); scanf ("%d", &nos[i]);
          }                              
                                                    
          for ( i = 0; i < 5; i++ )
          {                                      
                          for ( j = 0; j < 5 - ( i + 1 ); j++ )
                          {                                                  
                                          if ( nos[ j] >= nos[ j +1] )
                                          {                                            
                                                          tem = nos [ j];
                                                          nos[ j] = nos[ j + 1];
                                                          nos[ j + 1] = tem;
                                          }
                          }
          }
          printf ("\n\n");
          for ( i = 0; i < 5; i++ )
          {                  
                          printf ("\n\t\t    Position : %d \t Output Number : %d", i, nos[i] );
          }
          getch ( );
}

Click here to contact us

Saturday, 1 September 2012

Array015

Input a five digit number and store all the odd and even digits of that number in two different array.

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

void main ( )
{
             int           no, md, co = 0, so = 0, i[5], j[5];
             clrscr ( );
                               
             printf ("\n\t Enter a number (less than 32767)...........:"); scanf ("%d", &no);
                                   
             while ( no > 0 )
             {                                 
                             md = no % 10;
                             no = no / 10;
                             if ( md % 2 != 0 )
                             {                             
                                               i[co++] = md;
                             }                                     
                             else                                    
                             {                                       
                                               j[so++] = md;
                             }
             }
                                                
             printf ("\n\n\t Odd Digits are :-");
             for ( md = co - 1; md >= 0; md-- )
             {                                                  
                                        printf (" %d", i[md] );
             }                            
             printf ("\n\n\t Even Digits are :-");
             for ( md = so - 1; md >= 0; md-- )
             {                                                      
                                        printf (" %d", j[md] );
             }
             getch ( );
}

Click here to contact us

Array014

Input a five digit number and store all the odd digits of that number in an array.

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

void main ( )
{
            int           no, md, co = 0, i[5];
            clrscr ( );
                        
            printf ("\n\t Enter a number (less than 32767)...........:"); scanf ("%d", &no);
                            
            while ( no > 0 )
            {                         
                            md = no % 10;
                            no = no / 10;
                            if ( md % 2 != 0 )
                            {                             
                                              i[co++] = md;
                            }
            }                                     
            printf ("\n\n\t Odd Digits are :-");
            for ( md = co-1; md >= 0; md-- )
            {                                                    
                            printf (" %d", i[md] );
            }
            getch ( );
}


Click here to contact us

Related Posts Plugin for WordPress, Blogger...