Friday, June 5, 2020

C - All Array programs- print, add, userdefined size, reverse and sort.




 Array is a group of elements having same datatype. they share the same name in the memory and can be differentiated with index.


Programs:-

1. Simple C Program to print array
2. C Program to make User defined array size and print the elements
3. C program to add array elements

5.Program to sort Array - (a) Ascending (b)Descending




1. Simple C Program to print array

#include<stdio.h>
void main()
{
//size of array is 7  i.e. index range is from 0 to 6
int num[7];

for(int i=0;i<7;i++) // i less than 7 because index range is from 0 to 6
{
printf("\nenter numbers to add in array = \t");
scanf("%d",&num[i]);
}

for(int i=0;i<7;i++)
{
printf("\nArray element [i]= ");
scanf("%d",&num[i]);
}
}




2.Make User defined array size and print the elements

#include<stdio.h>
void main()
{
int n=0;
int num[n];

printf("Enter total times to input number:");
scanf("%d",&n);
int b=n;
printf("Total times to input number is %d",b);

for(int i=0;i<b;i++)
{
printf("\nenter numbers to print");
scanf("%d",&num[i]);
}

printf("\n \n the array contains these numbers\n");

for(int i=0;i<b;i++)
{
printf("\n%d",num[i]);
}
}



 3.Addition of array elements:- 

#include<stdio.h>
void main()
{
int n=0 , total =0 ;
int num[n];

printf("Enter total times to input number:");
scanf("%d",&n);
int b=n;
printf("\n input numbers for %d times\n",b);

for(int i=0;i<b;i++)
{
printf("\nenter numbers to add = \t");
scanf("%d",&num[i]);
}

printf("\n \n the addition of these numbers is:-   ");

for(int i=0;i<b;i++)
{
total=total+num[i];
}
printf("%d",total);   
}

4. Program to reverse array

5.Program to sort Array - (a) Ascending (b)Descending

 (a) Ascending


    #include <stdio.h>
    int main()
   {
    int n,i,j;
    int arr[n];
    int temp;
    
    printf("\nEnter total number of elements to be input: ");
    scanf("%d",&n);
    
   
    printf("\nEnter array elements:\n");
    for(i=0;i<n;i++)
    {
        printf("Enter element %d: ",i+1);
        scanf("%d",&arr[i]);
    }
    
  
    for(i=0;i< n;i++)
    {
        for(j=i+1;j< n;j++)
        {
            if(arr[i]>arr[j])
            {
                temp    =arr[i];
                arr[i]  =arr[j];
                arr[j]  =temp;
            }
        }
    }
    
    printf("\nArray elements after sorting:\n");
    for(i=0;i< n;i++)
    {
        printf("%d\n",arr[i]);
    }
    return 0;
 }



(b)Progam to sort array in decending order

#include <stdio.h>
int main()
{
    int n,i,j;
    int arr[n];
    int temp;
    
    printf("\nEnter total number of elements to be input: ");
    scanf("%d",&n);
    
   
    printf("\nEnter array elements:\n");
    for(i=0;i<n;i++)
    {
        printf("Enter element %d: ",i+1);
        scanf("%d",&arr[i]);
    }
     for(i=0;i< n;i++)
    {
        for(j=i+1;j< n;j++)
        {
            if(arr[i]<arr[j])
            {
                temp    =arr[i];
                arr[i]  =arr[j];
                arr[j]  =temp;
            }
        }
    }
    
    printf("\nArray elements after sorting:\n");
    for(i=0;i< n;i++)
    {
        printf("%d\n",arr[i]);
    }
    return 0;

}


Do Visit this blog for many other Language Programs of microprocessor 8086, C++ , C , Data Structures visit our  BLOG .
There are programs of , Operators (logical, arithmetic ,relational,etc) ,exception handling , inheritance ,string operations ,etc


No comments:

Post a Comment