Showing posts with label Array Programs. Show all posts
Showing posts with label Array Programs. Show all posts

Wednesday, 12 August 2015

C Program to read array of elements and print only odd numbers


Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
         int i, a[10];
         for( i = 0; i < 5; i++ )
         {
                    printf("Enter the value for a[ %d ]: ", i);
                    scanf("%d", &a[ i ]);
          }
          printf("\nThe array of Even elements are:\n");
          for( i = 0; i < 5; i++ )
          {
                    if( a[ i ] % 2 != 0 )
                    {
                               printf(" %d ", a[ i ] );
                     }
           }
}


Output:

Enter the value for a[ 0 ]: 1

Enter the value for a[ 1 ]: 2

Enter the value for a[ 2 ]: 3

Enter the value for a[ 3 ]: 4

Enter the value for a[ 4 ]: 5

The array of Even elements are:

1 3 5

C Program to read array of elements and print only even numbers


Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
         int i, a[10];
         for( i = 0; i < 5; i++ )
         {
                    printf("Enter the value for a[ %d ]: ", i);
                    scanf("%d", &a[ i ]);
          }
          printf("\nThe array of Even elements are:\n");
          for( i = 0; i < 5; i++ )
          {
                    if( a[ i ] % 2 == 0 )
                    {
                               printf(" %d ", a[ i ] );
                     }
           }
}


Output:

Enter the value for a[ 0 ]: 1

Enter the value for a[ 1 ]: 2

Enter the value for a[ 2 ]: 3

Enter the value for a[ 3 ]: 4

Enter the value for a[ 4 ]: 5

The array of Even elements are:

2 4








C program to read array of elements separated by comma and print array of elements



Input   : 2, 6, 7, 8, 1, 0
Output: 2 6 7 8 1 0

Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
          int i = 0;
          char a[ ] = "2, 6, 7, 8, 1, 0";
          char *p, *b[ 10 ];
          p = strtok(a, ",");
          while( p != NULL )
          {
                 b[ i++ ] = p;
                 p = strtok( NULL, ",");
          }
          for( i = 0; i < 6; i++ )
          {
                 printf("%s", b[ i ]);
          }
}

Output:

2 6 7 8 1 0



C program to read and print array elements in reverse order



Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
         int a[ 5 ], i;
         for( i = 1; i < 5; i++)
        {
                printf("\nEnter the value for [ %d ]: ",i);
                scanf("%d", &a[ i ]);
         }
         printf("\nThe array of elements in reverse order:\n");
         for(i=5;i>0;i--)
         {
               printf("%d\t", a[ i ]);
         }
}


Output:

Enter the value for [ 0 ]: 0

Enter the value for [ 1 ]: 2

Enter the value for [ 2 ]: 1

Enter the value for [ 3 ]: 9

Enter the value for [ 4 ]: 7

The array of elements in reverse order:

7 9 1 2 0


C Program to read and print array of elements


Program:

#include< stdio.h >
#include< conio.h >
void main( )
{
          int a[5], i;
          for( i = 0; i < 5; i++ )
         {
                  printf("\nEnter the value for a[%d]: ", i);
                  scanf("%d", &a[i]);
         }
         printf("\nThe array elements are: \n");
         for( i = 0; i < 5; i++ )
         {
                 printf("%d\t", a[i]);
         }
}

Output:

Enter the value for a[ 1 ]: 3

Enter the value for a[ 2 ]: 2

Enter the value for a[ 3 ]: 1

Enter the value for a[ 4 ]: 9

Enter the value for a[ 5 ]: 7

The array elements are:

3 2 1 9 7