Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

28 Sept 2012

C PROGRAM TO FIND THE HCF OF GIVEN SET OF NUMBERS USING FUNCTION


/**program for finding the H.C.F (HIGHEST COMMON FACTOR) of given set of Numbers**/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50      // defining the maximum size of an array

int HCF(int, int);   // HCF function prototype

main()
{
     int arr[MAX];
     int num1,num2,size,result;
     int i,j;
     printf("\n>>> PROGRAM TO FIND THE H.C.F OF GIVEN NUMBERS 
             USING FUNCTIONS <<<\n\n");

     printf("\nEnter the size of an array : ");
     scanf("%d",&size); // reading size of an array from user

     printf("\nEnter the elements : \n");
     for(i=0;i<size;i++) //reading elements of array from user
     {
      printf("\n Element[%d]= ",i);
      scanf("%d", &arr[i]);
     }

     num1=arr[0];   // initiatilizing the variable
     for(j=0;j<size-1;j++)
     {
       num2=arr[j+1];
       result=HCF(num1,num2);    // calling function
       num1=result;

     }
     // printing the result
     printf("\n\n H.C.F Of The Given Numbers is : %d",result);      

     getch();
     return 0;
}

int HCF(int x, int y)      // function body starts here
{
     int result1;

     while((x%y)!=0)
     {
       result1=x%y;
       x=y;
       y=result1;
     }
     return y;    // returns value
}

Output: ( using GNU GCC Compiler with code::blocks IDE)



C PROGRAM TO SORT MARKS OF STUDENTS WITH THEIR NAMES USING FUNCTION

/** c program to sort the marks of the students with their 
     names using function **/


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 100 //maximum size of array
int sortArray(int); //FUNCTION DECLARATION
char Names[MAX][25]; //maximum
int array[MAX],new[MAX];
int main()
{
  int i,size,temp1;
  printf("\n>>>> PROGRAM TO SORT ARRAY WITH NAMES USING
             FUNCTION <<<<\n\n");
  printf("\n Enter the Number of Students: ");
  scanf("%d",&size);    // reading number of students from user
  if(size>100) //checks if size of array greater than 100
  { 
    printf("\n size sholud not be more than 100 \n");
     return; //terminates the program execution
  }
  printf("\n Enter the Names of %d Students: \n",size);
  for(i=0;i<size;i++)  //loop for reading names of students
  {
    printf("\n Names[%d]: ",i+1);
    scanf("%s",Names[i]);
  }

  printf("\n Enter the Marks Of Each Student: \n");

  for(i=0;i<size;i++) //loop for reading marks of each student
  {
   printf("\n %s: ",Names[i]);
   scanf("%d", &array[i]);
  }
  sortArray(size);  //calling function
   printf("\n The Sorted elements of array are:\n");
   for(i=0;i<size;i++)
   {
     temp1=new[i+1];
     printf("\n %s : %d\n-------------",Names[i],array[i]);
   }
   getch();
   return 0;
}

sortArray(n)     //function for sorting
{
  int temp=0,i,j;   //temp var is temporary variable for sorting
  char str[MAX];   //temporary string for sorting..
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {

      if(array[i]>array[j])
      {
       //swapping names and numbers for sorting
        temp=array[i];        strcpy(str,Names[i]);
        array[i]= array[j];   strcpy(Names[i],Names[j]);
        array[j]=temp;        strcpy(Names[j],str);
      }

    }
  }
}


Output: ( using GNU GCC Compiler with code::blocks IDE)

output 1: (here the output of the program after entering all   
            vlues)



  output 2: (output to show if the size > 100)


C PROGRAM TO SORT AN ARRAY USING FUNCTION


/** C PROGRAM TO SORT AN ARRAY USING FUNCTION **/
#include<stdio.h>
#include<conio.h>
#define MAX 100     // maximum no of elements of array
int sortArray(int);
int array[MAX];
int main()
{
  int i,size;
  printf("\n>>>> PROGRAM TO SORT ARRAY USING FUNCTION <<<<\n\n");
  printf("\n Enter the size of array: ");
  scanf("%d",&size);
  printf("\n Enter the %d elements of array: \n",size);
  for(i=0;i<size;i++)
  {
   printf("\n array[%d]=",i);
   scanf("%d", &array[i]);
  }
  sortArray(size);    //calling sortArray() function
   printf("\n The Sorted elements of array are:");
   for(i=0;i<size;i++)
   {
     printf(" %d",array[i]);
   }
   getch();
   return 0;
}

sortArray(n)   // function for sorting array elements
{
  int temp=0,i,j;   // temp var is temporary variable used for swapping
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
      if(array[i]>array[j])
      {
        temp=array[i];   //swapping for the array to be sorted
        array[i]= array[j];
        array[j]=temp;
      }
    }
  }
}
Output: ( using GNU GCC Compiler with code::blocks IDE)





C PROGRAM TO FIND THE AVERAGE OF MARKS USING FUNCTION


/** C PROGRAM TO FIND THE AVERAGE OF MARKS USING FUNCTION **/
#include<stdio.h>
#include<conio.h>
#define MAX 100 //maximum size of array is 100
int marks[MAX]; //function declaration
int CalculateAverage(int);
main()
{
  int i,n;
  int average;
  printf(">>> program to find average of marks using 
          function <<<");
  printf("\n\n Enter the Number of Subjects: ");
  scanf("%d",&n); //taking input from user
  printf("\n Enter the %d Subjects Marks: ",n);
  for(i=0;i<n;i++)
  {
   printf("\n subject[%d]:",i+1);
   scanf("%d",&marks[i]);   
   //taking input all the marks using for loop
  }
  average=CalculateAverage(n); //calling function
  printf("\n The Average Of Marks is: %d ",average);
  getch();
}


int CalculateAverage(int no) //function body
{
 int avg;
 int sum=0;
 int i;
  for(i=0;i<no;i++)
  {
   sum = sum + marks[i]; //Adding all marks to get sum
  }

  avg=sum/no;  //calculating average
  return avg;
}

Output: ( using GNU GCC Compiler with code::blocks IDE )



23 Sept 2012

C PROGRAM TO COUNT AND PRINT VOWELS IN A GIVEN STRING


/*** THIS C PROGRAM TAKES A LINE OF STRING AS INPUT AND PRINTS THE VOWELS AND THE COUNT OF VOWELS OCCURED IN IT ***/

#include<stdio.h>
#include<conio.h>
#define MAX 100  //limiting the maximum size of string
#define SIZE 5  //we have 5 vowels a,e,i,o,u
main()
{
  int i,j;
  char str[MAX],vowelcount[SIZE];
  char vowels[SIZE]={'a','e','i','o','u'};
  //clrscr();
  printf("\nPROGRAM TO COUNT VOWELS\n");
  printf("\nEnter the String:");
  //scanf("%s",&str); //this allows only a string(single word)
  gets(str); //this allows a line as string

  for(i=0;i<5;i++)
  {
   vowelcount[i]=0;
  }
  for(i=0;str[i]!='\0';i++)
  {
    for(j=0;j<SIZE;j++)
    {
      if((str[i]==vowels[j])||(str[i]==vowels[j]-32))
      {
        vowelcount[j]=vowelcount[j]++;


      }
   }

  }
   for(i=0;i<SIZE;i++) //printing vowels and its count
   {
     printf("\n %c occured:%d times",vowels[i],vowelcount[i]);
   }
    getch();

}

OUTPUT:



C PROGRAM TO SORT AN ARRAY USING FUNCTION


/*** c program to sort and print array using function ***/

#include<stdio.h>   
#include<conio.h>
#define MAX 100   
  
void SortArray(int[],int);  
  
main()   
{   
  
  int arr[MAX],i,size;  
  printf("\n >>>PROGRAM TO SORT ARRAY ELEMENTS USING
   FUNCTION<<<\n \n"); 
  
     
   printf("\n Enter the size : ");   
   scanf("%d",&size);       // READING SIZE OF ARRAY FROM USER
   printf("\n Enter the elements :\n");
        
   for( i=0;i<size;i++)   
   {
     printf("\n arr[%d]=",i);       
     scanf("%d",&arr[i]);  // reading elements of array from user
   }   
  
   SortArray(arr,size);   
  
   printf("\n The Sorted Elements of Array is: ");  
        
   for( i=0;i<size;i++)
   {   
    printf("  %d",arr[i]); //printing sorted array 
   }
       
   getch(); return 0; 
  
 }                                  
  
void SortArray(int arr1[],int size)   
{   
    int i,j,t;   
    for(i=size-2;i>=0;i--)   
      {   
         for(j=0;j<=i;j++)   
         {   
            if(arr1[j]>arr1[j+1])   
            {   
             t=arr1[j];   
             arr1[j]=arr1[j+1];   
             arr1[j+1]=t;   
            }   
         }   
     }                                   
}    

OUTPUT:



         

PRINT A MULTI-DIMENSIONAL ARRAY IN SNAIL SHELL WAY



/**C PROGRAM TO PRINT A MULTI-DIMENSIONAL ARRAY IN SNAIL SHELL WAY**/


//for printing matrix as in the below given format
//   1  2  3 4
//  12 13 14 5
//  11 16 15 6
//  10 9  8  7

#include<stdio.h>
//#include<conio.h>
#define rows 15
#define cols 15
main()
{
      int arr[rows][cols],i,j,k,size,count=0,a=0,b=1;
      // clrscr();
      printf("\n Enter the size of Square Matrix(for ex:5) :");
      scanf("%d",&size);
      if(size>15)  //limiting tye size of matrix
      {
          printf("\n Size of matrix should not nore than 15");
          getch();
          return; 
      }
      for(i=0;i<size;i++)
      {
       for(j=0;j<size;j++)
       {
          arr[i][j]=0;   
          // initializing all the elements to zero
       }
      }
       
       //loop starts here for performing actions
       //to get the required format
                           
       for(k=1;k<size;k++,a++) 
       {                                 
       for(i=a,j=a;j<size-k;j++)
       {
         arr[i][j]=++count;  
       } 
       for(i=a,j=size-k;i<size-k;i++)
       {
         arr[i][j]=++count;
       }
       for(i=size-k,j=size-k;j>=a;j--)
       {
        arr[i][j]=++count;
       }
       for(i=size-(k+1),j=a;i>=k;i--)
       {
          arr[i][j]=++count;
       } 
     } //loop ends here 
       
       
       printf("\n\n\t****** %d X %d MATRIX ******",size,size);
       printf("\n\n\n");
       printf("\t");
      for(i=0;i<size;i++) { printf("-------"); } printf("\n");

      for(i=0;i<size;i++)
      {
       for(j=0;j<size;j++)
       {
           if(arr[i][j]<10)  
           {
             printf("\t0%d",arr[i][j]);  
// for numbers less than zero printing in 01,02 etc.. format
           }
           
           else
           {
             printf("\t%d",arr[i][j]);
           }
            if(j==size-1) 
             { 
              printf("\n\n"); 
             }  
       }
      } 
      printf("\t");
      for(i=0;i<size;i++) { printf("-------"); } printf("\n");  //printing outline for matrix  
      
                         
  // getch();
}
     

Output: ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); so we are commenting that )

Other Links :
C# PROGRAM PRINT A MULTI-DIMENSIONAL ARRAY IN SNAIL SHELL WAY

17 Sept 2012

C PROGRAM TO PRINT MATRIX IN SNAIL SHELL WAY


/**C PROGRAM TO PRINT MATRIX IN SNAIL SHELL FORMAT**/

//for printing matrix as in the below given format
//   1  2  3 4
//  12 13 14 5
//  11 16 15 6
//  10 9  8  7

#include<stdio.h>
//#include<conio.h>
#define rows 15
#define cols 15
main()
{
      int arr[rows][cols],i,j,k,size,count=0,a=0,b=1;
      // clrscr();
      printf("\n Enter the size of Square Matrix(for ex:5) :");
      scanf("%d",&size);
      if(size>15)  //limiting tye size of matrix
      {
          printf("\n Size of matrix should not nore than 15");
          getch();
          return; 
      }
      for(i=0;i<size;i++)
      {
       for(j=0;j<size;j++)
       {
          arr[i][j]=0;   
          // initializing all the elements to zero
       }
      }
       
       //loop starts here for performing actions
       //to get the required format
                           
       for(k=1;k<size;k++,a++) 
       {                                 
       for(i=a,j=a;j<size-k;j++)
       {
         arr[i][j]=++count;  
       } 
       for(i=a,j=size-k;i<size-k;i++)
       {
         arr[i][j]=++count;
       }
       for(i=size-k,j=size-k;j>=a;j--)
       {
        arr[i][j]=++count;
       }
       for(i=size-(k+1),j=a;i>=k;i--)
       {
          arr[i][j]=++count;
       } 
     } //loop ends here 
       
       
       printf("\n\n\t****** %d X %d MATRIX ******",size,size);
       printf("\n\n\n");
       printf("\t");
      for(i=0;i<size;i++) { printf("-------"); } printf("\n");

      for(i=0;i<size;i++)
      {
       for(j=0;j<size;j++)
       {
           if(arr[i][j]<10)  
           {
             printf("\t0%d",arr[i][j]);  
// for numbers less than zero printing in 01,02 etc.. format
           }
           
           else
           {
             printf("\t%d",arr[i][j]);
           }
            if(j==size-1) 
             { 
              printf("\n\n"); 
             }  
       }
      } 
      printf("\t");
      for(i=0;i<size;i++) { printf("-------"); } printf("\n");  //printing outline for matrix  
      
                         
  // getch();
}
     

Output: ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); so we are commenting that )
  


For a c# program of this 

C# PROGRAM PRINT A MULTI-DIMENSIONAL ARRAY IN SNAIL SHELL WAY