Thursday 27 February 2014

Others programs

//C program for ATM transactions
#include<stdio.h>
int totalThousand =1000;
int totalFiveFundred =1000;
int totalOneHundred =1000;
int main(){
    unsigned long withdrawAmount;
    unsigned long totalMoney;
    int thousand=0,fiveHundred=0,oneHundred=0;
    printf("Enter the amount in multiple of 100: ");
    scanf("%lu",&withdrawAmount);
    if(withdrawAmount %100 != 0){
         printf("Invalid amount;");
         return 0;
    }
    totalMoney = totalThousand * 1000 + totalFiveFundred* 500 +  totalOneHundred*100;
    if(withdrawAmount > totalMoney){
         printf("Sorry,Insufficient money");
         return 0;
    }
    thousand = withdrawAmount / 1000;
    if(thousand > totalThousand)
         thousand = totalThousand;
    withdrawAmount = withdrawAmount - thousand * 1000;
    if (withdrawAmount > 0){
         fiveHundred = withdrawAmount / 500;
         if(fiveHundred > totalFiveFundred)
             fiveHundred = totalFiveFundred;
         withdrawAmount = withdrawAmount - fiveHundred * 500;
    }
    if (withdrawAmount > 0)
         oneHundred = withdrawAmount / 100;
    printf("Total 1000 note: %d\n",thousand);
    printf("Total  500 note: %d\n",fiveHundred);
    printf("Total  100 note: %d\n",oneHundred);
    return 0;
}
Sample output:
Enter the amount in multiple of 100: 7800
Total 1000 note: 7
Total  500 note: 1
Total  100 note: 3
//How to pass one dimensional array to function in c
#include <stdio.h>
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main(){
int a[N];
printf("Input data into the matrix:\n");
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}
void fstore1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
scanf("%d", &a[i]);
}
void fretrieve1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
printf("%6d ", a[i]);
printf("\n");
}
void fedit1D(int a[], int n){
int i, q;
for ( i = 0; i < n; ++i ){
printf("Prev. data: %d\nEnter 1 to edit 0 to skip.", a[i]);
scanf("%d", &q);
if ( q == 1 ){
printf("Enter new value: ");
scanf("%d", &a[i]);
}
}
}
Write a c program which passes two dimension array to function
#include <stdio.h>
#define M 3
#define N 5
void fstore2D(int a[][N]);
void fretrieve2D(int a[][N]);
int main(){
  int a[M][N];
  printf("Input data in matrix (%d X %d)\n", M, N);
  fstore2D(a);
  fretrieve2D(a);
  return 0;
}
void fstore2D(int a[][N]){
    int i, j;
    for (i = 0; i < M; ++i){
    for (j = 0; j < N; ++j)
         scanf("%d", &a[i][j]);
    }
}
void fretrieve2D(int a[][N]){
   int i, j;
   for ( i = 0; i < M; ++i ){
       for ( j = 0; j < N; ++j)
            printf("%6d ", a[i][j]);
       printf("\n");
   }
}
//Write a c program which takes password from user
#include<stdio.h>
#define MAX 500
int main(){
    char password[MAX];
    char p;
    int i=0;
    printf("Enter the password:");

    while((p=getch())!= 13){
         password[i++] = p;
         printf("*");
    }
    password[i] = '\0';
    if(i<6)
         printf("\nWeak password");
    printf("\nYou have entered: %s",password);
    return 0;
}
Sample output:
Enter the password:*******
You have entered: fgt67m,
//Write a scanf function in c which accept sentence from user
#include<stdio.h>
#define MAX 500
int main(){
    char arr[MAX];
    printf("Enter any sentence which can include spaces.\n");
    printf("To exit press enter key.\n");
    scanf("%[^\n]s",arr);
    printf("You had entered: \n");
    printf("%s",arr);
    return 0;
}
Sample output:
Enter any sentence which can include spaces.
To exit press enter key.
May I help you?
You had entered:
May I help you?
//Write a scanf function in c which accept paragraph from user
#include<stdio.h>
#define MAX 500
int main(){
    char arr[MAX];
    printf("Enter any paragraph which can include spaces or new line.\n");
    printf("To exit press the tab key.\n");
    scanf("%[^\t]s",arr);
    printf("You had entered: \n");
    printf("%s",arr);
    return 0;
}
//Print prime numbers between 1-300 using break and continue in c
Definition of prime number:
A natural number greater than one has not any other divisors except 1 and itself. In other word we can say which has only two divisors 1 and number itself. For example: 5
Their divisors are 1 and 5.
Note: 2 is only even prime number.
Example of prime numbers : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199 etc.
#include <math.h>
#include <stdio.h>
main(){
  int i, j;
  i = 2;
  while ( i < 300 ){
     j = 2;
     while ( j < sqrt(i) ){
         if ( i % j == 0 )
            break;
         else{
            ++j;
            continue;
         }
      }
      if ( j > sqrt(i) )
            printf("%d\t", i);
      ++i;
  }
  return 0;
}
//Palindrome in c without using string function
#include<stdio.h>
int main(){
  char str[100];
  int i=0,j=-1,flag=0;
  printf("Enter a string: ");
  scanf("%s",str);
  while(str[++j]!='\0');
  j--;
  while(i<j)
      if(str[i++] != str[j--]){
           flag=1;
           break;
      }
    
  if(flag == 0)
      printf("The string is a palindrome");
  else
      printf("The string is not a palindrome");
  return 0;
}
//How to get the ASCII value of a character in c
#include<stdio.h>
int main(){

    char c;
    printf("Enter any character: ");
    scanf("%c",&c);
    printf("ASCII value of given character: %d",c);
    
    return 0;
}
Sample output:
Enter any character: a
ASCII value of given character: 97
}

Very large numbers

//Multiplication of large numbers in c
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
char * multiply(char [],char[]);
int main(){
    char a[MAX];
    char b[MAX];
    char *c;
    int la,lb;
    int i;
    printf("Enter the first number : ");
    scanf("%s",a);
    printf("Enter the second number : ");
    scanf("%s",b);
    printf("Multiplication of two numbers : ");
    c = multiply(a,b);
    printf("%s",c);
    return 0;
}
char * multiply(char a[],char b[]){
    static char mul[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i,j,k=0,x=0,y;
    long int r=0;
    long sum = 0;
    la=strlen(a)-1;
        lb=strlen(b)-1;
  
        for(i=0;i<=la;i++){
                a[i] = a[i] - 48;
        }
        for(i=0;i<=lb;i++){
                b[i] = b[i] - 48;
        }
    for(i=lb;i>=0;i--){
         r=0;
         for(j=la;j>=0;j--){
             temp[k++] = (b[i]*a[j] + r)%10;
             r = (b[i]*a[j]+r)/10;
         }
         temp[k++] = r;
         x++;
         for(y = 0;y<x;y++){
             temp[k++] = 0;
         }
    }
  
    k=0;
    r=0;
    for(i=0;i<la+lb+2;i++){
         sum =0;
         y=0;
         for(j=1;j<=lb+1;j++){
             if(i <= la+j){
                 sum = sum + temp[y+i];
             }
             y += j + la + 1;
         }
         c[k++] = (sum+r) %10;
         r = (sum+r)/10;
    }
    c[k] = r;
    j=0;
    for(i=k-1;i>=0;i--){
         mul[j++]=c[i] + 48;
    }
    mul[j]='\0';
    return mul;
}
Sample output of above code:
Enter the first number: 55555555
Enter the second number: 3333333333
Multiplication of two numbers:
185185183314814815
//Division of large numbers in c
#include<stdio.h>
#include<string.h>
#define MAX 10000
int validate(char []);
char * division(char[],long);
long int remainder;
int main(){
    char dividend[MAX],*quotient;
    long int divisor;
    printf("Enter dividend: ");
    scanf("%s",dividend);
    if(validate(dividend))
         return 0;
    printf("Enter divisor: ");
    scanf("%ld",&divisor);
    quotient = division(dividend,divisor);
    while(*quotient)
         if(*quotient ==48)
             quotient++;
         else
             break;
    printf("Quotient: %s / %ld  =  %s",dividend,divisor,quotient);
    printf ("\nRemainder: %ld",remainder);
    return 0;
}
int validate(char num[]){
    int i=0;
    while(num[i]){
         if(num[i] < 48 || num[i]> 57){
             printf("Invalid positive integer: %s",num);
             return 1;
         }
         i++;
    }
    return 0;
}
char * division(char dividend[],long divisor){
  
    static char quotient[MAX];
    long temp=0;
    int i=0,j=0;
    while(dividend[i]){
       
         temp = temp*10 + (dividend[i] -48);
         if(temp<divisor){
           
             quotient[j++] = 48;
           
         }
         else{
             quotient[j++] = (temp / divisor) + 48;;
             temp = temp % divisor;
         }
         i++;
    }
    quotient[j] = '\0';
    remainder = temp;
    return quotient;
}
Sample output:
Enter dividend: 543109237823560187
Enter divisor: 456
Quotient: 543109237823560187 / 456 = 1191029030314824
Remainder: 443
Other program (source code):
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
char * division(char [],unsigned long);
int main(){
    char a[MAX];
    unsigned long b;
    char *c;
    printf("Enter the divdend : ");
    scanf("%s",a);
    printf("Enter the divisor : ");
    scanf("%lu",&b);
    c = division(a,b);
    printf("\nQuotient of the division : ");
    printf("%s",c);
    return 0;
}
char * division(char a[],unsigned long b){
    static char c[MAX];
    int la;
    int i,k=0,flag=0;
    unsigned long temp=1,reminder;
    la=strlen(a);
    for(i=0;i<=la;i++){
         a[i] = a[i] - 48;
    }
    temp = a[0];
    reminder = a[0];
    for(i=1;i<=la;i++){
         if(b<=temp){
             c[k++] = temp/b;
             temp = temp % b;
             reminder = temp;
             temp =temp*10 + a[i];
             flag=1;
         }
         else{
             reminder = temp;
             temp =temp*10 + a[i];
             if(flag==1)
                 c[k++] = 0;
         }
    }
    for(i=0;i<k;i++){
         c[i]=c[i]+48;
    }
    c[i]= '\0';
    printf("Reminder of division:  %lu  ",reminder);
    return c;
}
Sample output:
Enter the divdend:
55555555555555555555555555555555555555555
Enter the divisor: 5
Reminder of division:  0
Quotient of the division:
11111111111111111111111111111111111111111
//C program for modular division of large number
#include<stdio.h>
#include<string.h>
#define MAX 10000
int validate(char []);
int modulerDivision(char[],unsigned long);
int main(){
    char dividend[MAX];
    unsigned long int divisor,remainder;
    printf("Enter dividend: ");
    scanf("%s",dividend);
    if(validate(dividend))
         return 0;
    printf("Enter divisor: ");
    scanf("%lu",&divisor);
    remainder = modulerDivision(dividend,divisor);
    printf("Modular division: %s %% %lu  =  %lu",dividend,divisor,remainder);
    return 0;
}
int validate(char num[]){
    int i=0;
    while(num[i]){
         if(num[i] < 48 || num[i]> 57){
             printf("Invalid positive integer: %s",num);
             return 1;
         }
         i++;
    }
    return 0;
}
int modulerDivision(char dividend[],unsigned long divisor){
  
    unsigned long temp=0;
    int i=0;
    while(dividend[i]){
       
         temp = temp*10 + (dividend[i] -48);
         if(temp>=divisor){
             temp = temp % divisor;
         }
  
         i++;
    }
    return temp;
}
Sample output:
Enter dividend: 123456789
Enter divisor: 56
Modular division: 123456789 % 56 = 29
//C program to find power of a large number
#include<stdio.h>
#include<string.h>
#define MAX 10000
char * multiply(char [],char[]);
int main(){
    char a[MAX];
    char *c;
    int i,n;
    printf("Enter the base number: ");
    scanf("%s",a);
    printf("Enter the power: ");
    scanf("%d",&n);
    printf("Power of the %s^%d: ",a,n);
    c = multiply(a,"1");
    for(i=0;i<n-1;i++)
         c = multiply(a,c);
    while(*c)
    if(*c =='0')
         c++;
    else
         break;
    printf("%s",c);
    return 0;
}
char * multiply(char num1[],char num2[]){
    static char mul[MAX];
    char a[MAX];
    char b[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i=0,j,k=0,x=0,y;
    long int r=0;
    long sum = 0;
  
    while(num1[i]){
         a[i] = num1[i];
         i++;
    }
    a[i]= '\0';
    i=0;
    while(num2[i]){
         b[i] = num2[i];
         i++;
    }
    b[i]= '\0';
    la=strlen(a)-1;
        lb=strlen(b)-1;

        for(i=0;i<=la;i++){
                a[i] = a[i] - 48;
        }
        for(i=0;i<=lb;i++){
                b[i] = b[i] - 48;
        }
    for(i=lb;i>=0;i--){
         r=0;
         for(j=la;j>=0;j--){
             temp[k++] = (b[i]*a[j] + r)%10;
             r = (b[i]*a[j]+r)/10;
         }
         temp[k++] = r;
         x++;
         for(y = 0;y<x;y++){
             temp[k++] = 0;
         }
    }

    k=0;
    r=0;
    for(i=0;i<la+lb+2;i++){
         sum =0;
         y=0;
         for(j=1;j<=lb+1;j++){
             if(i <= la+j){
                 sum = sum + temp[y+i];
             }
             y += j + la + 1;
         }
         c[k++] = (sum+r) %10;
         r = (sum+r)/10;
    }
    c[k] = r;
    j=0;
    for(i=k-1;i>=0;i--){
         mul[j++]=c[i] + 48;
    }
    mul[j]='\0';
    return mul;
}
Sample output:
Enter the base number: 5
Enter the power: 100
Power of the 5^100: 78886090522101180541172856528278622
96732064351090230047702789306640625

Area and volume

//C PROGRAM TO CALCULATE AREA OF A CIRCLE
#include <stdio.h>
#define PI 3.141
int main(){
  float r, a;
  printf("Radius: ");
  scanf("%f", &r);
  a = PI * r * r;
  printf("%f\n", a);
  return 0;
}
//Write a c program to find the area of a triangle
#include<stdio.h>
#include<math.h>
int main(){
 
    float a,b,c;
    float s,area;
 
    printf("Enter size of each sides of triangle");
    scanf("%f%f%f",&a,&b,&c);
 
    s = (a+b+c)/2;
    area = sqrt(s*(s-a)*(s-b)*(s-c));
 
    printf("Area of triangle is: %.3f",area);
 
    return 0;
}
Sample output:
Enter size of each sides of the triangle: 2 4 5
Area of triangle is: 3.800
//Write a c program to find the area of an equilateral triangle
#include<stdio.h>
#include<math.h>
int main(){
    float a;
    float area;
    printf("Enter size of side of the equilateral triangle : ");
    scanf("%f",&a);
    area = sqrt(3)/4*(a*a);
    printf("Area of equilateral triangle is: %.3f",area);
    return 0;
}
Sample output:
Enter size of side of the equilateral triangle: 5
Area of equilateral triangle is: 10.825
//Write a c program to find the area of a right angled triangle
#include<stdio.h>
int main(){
    float h,w;
    float area;
    printf("Enter height and width of the right angled triangle : ");
    scanf("%f%f",&h,&w);
    area = 0.5 * h * w;
    printf("Area of right angled triangle is: %.3f",area);
    return 0;
}
Sample output:
Enter height and width of the right angled triangle: 10 5
Area of right angled triangle is: 25.000
//Write a c program to find the area of a rectangle
#include<stdio.h>
int main(){
    float l,w;
    float area;
    printf("Enter size of each sides of the rectangle : ");
    scanf("%f%f",&l,&w);
    area = l * w;
    printf("Area of rectangle is: %.3f",area);
    return 0;
}
Sample output:
Enter size of each sides of the rectangle: 5.2 20
Area of rectangle is: 104.000
//Write a c program to find the area of a trapezium
#include<stdio.h>
int main(){
    float b1,b2,h;
    float area;
    printf("Enter the size of two bases and height of the trapezium : ");
    scanf("%f%f%f",&b1,&b2,&h);
    area = 0.5 * ( b1 + b2 ) * h ;
    printf("Area of trapezium is: %.3f",area);
    return 0;
}
Sample output:
Enter the size of two bases and height of the trapezium: 5 8 3
Area of trapezium is: 19.500
//Write a c program to find the volume and surface area of a cube
#include<stdio.h>
int main(){
    float a;
    float surface_area,volume;
    printf("Enter size of any side of a cube : ");
    scanf("%f",&a);
    surface_area = 6 * (a * a);
    volume = a * a * a;
    printf("Surface area of cube is: %.3f",surface_area);
    printf("\nVolume of cube is : %.3f",volume);
    return 0;
}
Sample output:
Enter size of any side of a cube: 3
Surface area of cube is: 54.000
Volume of cube is: 27.000
//Write a c program to find the volume and surface area of cuboids
#include<stdio.h>
int main(){
    float w,l,h;
    float surface_area,volume,space_diagonal;
    printf("Enter size of width, length and height of a cuboids : ");
    scanf("%f%f%f",&w,&l,&h);
    surface_area = 2*(w*l + l*h + h*w);
    volume = w * l * h;
    space_diagonal = sqrt(w*w + l*l + h*h);
    printf("Surface area of cuboids is: %.3f",surface_area);
    printf("\nVolume of cuboids is : %.3f",volume);
    printf("\nSpace diagonal of cuboids is : %.3f",space_diagonal);
    return 0;
}
Sample output:
Enter size of width, length and height of cuboids: 5 10 4
Surface area of cuboids is: 220.000
Volume of cuboids is: 200.000
Space diagonal of cuboids is: 11.874
//Write a c program to find the volume and surface area of cylinder
#include<stdio.h>
#include<math.h>
int main(){
    float r,h;
    float surface_area,volume;
    printf("Enter size of radius and height of a cylinder : ");
    scanf("%f%f",&r,&h);
    surface_area = 2 * M_PI * r * (r + h);
    volume = M_PI * r * r * h;
    printf("Surface area of cylinder is: %.3f",surface_area);
    printf("\nVolume of cylinder is : %.3f",volume);
    return 0;
}
Sample output:
Enter size of radius and height of a cylinder: 4 10
Surface area of cylinder is: 351.858
Volume of cylinder is: 502.655
//Write a c program to find the volume and surface area of cone
#include<stdio.h>
#include<math.h>
int main(){
    float r,h;
    float surface_area,volume;
    printf("Enter size of radius and height of a cone : ");
    scanf("%f%f",&r,&h);
    surface_area = M_PI * r * (r + sqrt(r*r + h*h));
    volume = (1.0/3) * M_PI * r * r * h;
    printf("Surface area of cone is: %.3f",surface_area);
    printf("\nVolume of cone is : %.3f",volume);
    return 0;
}
Sample output:
Enter size of radius and height of a cone: 3 10
Surface area of cone is: 126.672
Volume of cone is: 94.248
//Write a c program to find the volume and surface area of sphere
#include<stdio.h>
#include<math.h>
int main(){
    float r;
    float surface_area,volume;
    printf("Enter radius of the sphere : ");
    scanf("%f",&r);
    surface_area =  4* M_PI * r * r;
    volume = (4.0/3) * M_PI * r * r * r;
    printf("Surface area of sphere is: %.3f",surface_area);
    printf("\nVolume of sphere is : %.3f",volume);
    return 0;
}
Sample output:
Enter radius of the sphere: 5
Surface area of sphere is: 314.159
Volume of sphere is: 523.599

Wednesday 26 February 2014

using pointers

//CONCATENATION OF TWO STRINGS USING POINTER IN C PROGRAM
#include<stdio.h>
int main(){
  int i=0,j=0;
  char *str1,*str2,*str3;
  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);
  printf("Before concatenation the strings are\n");
  puts(str1);
  puts(str2);
  while(*str1){
      str3[i++]=*str1++;
  }
  while(*str2){
      str3[i++]=*str2++;
  }
  str3[i]='\0';
  printf("After concatenation the strings are\n");
  puts(str3);
  return 0;
}/
/LINEAR SEARCH USING C PROGRAM
#include<stdio.h>
int main(){
    int a[10],i,n,m,c=0;
    printf("Enter the size of an array: ");
    scanf("%d",&n);
    printf("Enter the elements of the array: ");
    for(i=0;i<=n-1;i++){
         scanf("%d",&a[i]);
    }
    printf("Enter the number to be search: ");
    scanf("%d",&m);
    for(i=0;i<=n-1;i++){
         if(a[i]==m){
             c=1;
             break;
         }
    }
    if(c==0)
         printf("The number is not in the list");
    else
         printf("The number is found");
    return 0;
}
Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found
//BINARY SEARCH USING C PROGRAM
#include<stdio.h>
int main(){
    int a[10],i,n,m,c=0,l,u,mid;
    printf("Enter the size of an array: ");
    scanf("%d",&n);
    printf("Enter the elements in ascending order: ");
    for(i=0;i<n;i++){
         scanf("%d",&a[i]);
    }
    printf("Enter the number to be search: ");
    scanf("%d",&m);
    l=0,u=n-1;
    while(l<=u){
         mid=(l+u)/2;
         if(m==a[mid]){
             c=1;
             break;
         }
         else if(m<a[mid]){
             u=mid-1;
         }
         else
             l=mid+1;
    }
    if(c==0)
         printf("The number is not found.");
    else
         printf("The number is found.");
    return 0;
}
Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.

size of data types

//Write a c program to find the size of int without using sizeof operator
#include<stdio.h>
int main(){
  int *ptr = 0;
  ptr++;
  printf("Size of int data type:  %d",ptr);
  return 0;
}
//Write a c program to find the size of double without using sizeof operator
#include<stdio.h>
int main(){
  double *ptr = 0;
  ptr++;
  printf("Size of double data type:  %d",ptr);
  return 0;
}
//Write a c program to find the size of structure without using sizeof operator
#include<stdio.h>
struct student{
    int roll;
    char name[100];
    float marks;
};
int main(){
  struct student *ptr = 0;
  ptr++;
  printf("Size of the structure student:  %d",ptr);
  return 0;
}
//Write a c program to find the size of union without using sizeof operator
#include<stdio.h>
union student{
    int roll;
    char name[100];
    float marks;
};
int main(){
  union student *ptr = 0;
  ptr++;
  printf("Size of the union student:  %d",ptr);
  return 0;

SORTING programms

//BUBBLE SORT USING C PROGRAM
#include<stdio.h>
int main(){
  int s,temp,i,j,a[20];
  printf("Enter total numbers of elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  //Bubble sorting algorithm
  for(i=s-2;i>=0;i--){
      for(j=0;j<=i;j++){
           if(a[j]>a[j+1]){
               temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
           }
      }
  }
  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}
Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting:  0 2 6 9 11
//SELECTION SORT USING C PROGRAM
#include<stdio.h>
int main(){
  int s,i,j,temp,a[20];
  printf("Enter total elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  for(i=0;i<s;i++){
      for(j=i+1;j<s;j++){
           if(a[i]>a[j]){
               temp=a[i];
              a[i]=a[j];
              a[j]=temp;
           }
      }
  }
  printf("After sorting is: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}
Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is:  0 4 5 7 21
//QUICK SORT USING C PROGRAM
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
  int x[20],size,i;
  printf("Enter size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements: ",size);
  for(i=0;i<size;i++)
    scanf("%d",&x[i]);
  quicksort(x,0,size-1);
  printf("Sorted elements: ");
  for(i=0;i<size;i++)
    printf(" %d",x[i]);
  return 0;
}
void quicksort(int x[10],int first,int last){
    int pivot,j,temp,i;
     if(first<last){
         pivot=first;
         i=first;
         j=last;
         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }
         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);
    }
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
//Merge sort program in c
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
  
    int merge[MAX],i,n;
    printf("Enter the total number of elements: ");
    scanf("%d",&n);
    printf("Enter the elements which to be sort: ");
    for(i=0;i<n;i++){
         scanf("%d",&merge[i]);
    }
    partition(merge,0,n-1);
    printf("After merge sorting elements are: ");
    for(i=0;i<n;i++){
         printf("%d ",merge[i]);
    }
   return 0;
}
void partition(int arr[],int low,int high){
    int mid;
    if(low<high){
         mid=(low+high)/2;
         partition(arr,low,mid);
         partition(arr,mid+1,high);
         mergeSort(arr,low,mid,high);
    }
}
void mergeSort(int arr[],int low,int mid,int high){
    int i,m,k,l,temp[MAX];
    l=low;
    i=low;
    m=mid+1;
    while((l<=mid)&&(m<=high)){
         if(arr[l]<=arr[m]){
             temp[i]=arr[l];
             l++;
         }
         else{
             temp[i]=arr[m];
             m++;
         }
         i++;
    }
    if(l>mid){
         for(k=m;k<=high;k++){
             temp[i]=arr[k];
             i++;
         }
    }
    else{
         for(k=l;k<=mid;k++){
             temp[i]=arr[k];
             i++;
         }
    }
  
    for(k=low;k<=high;k++){
         arr[k]=temp[k];
    }
}
Sample output:
Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9

10:ARRAY programms

//FIND OUT LARGEST NUMBER IN AN ARRAY USING C PROGRAM
#include<stdio.h>
int main(){
  int a[50],size,i,big;
  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);
  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
           big=a[i];
  }
  printf("\nBiggest: %d",big);
  return 0;
}
//FIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY USING C PROGRAM
#include<stdio.h>
int main(){
  int a[50],size,i,j=0,big,secondbig;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i]){
           big=a[i];
           j = i;
      }
  }
  secondbig=a[size-j-1];
  for(i=1;i<size;i++){
      if(secondbig <a[i] && j != i)
          secondbig =a[i];
  }

  printf("Second biggest: %d", secondbig);
  return 0;
}
Sample output:
Enter the size of the array: 5
Enter 5 elements in to the array: 5 3 2 1 0
Second biggest: 3
/Write a c program to find out second smallest element of an unsorted array
#include<stdio.h>
int main(){
  int a[50],size,i,j=0,small,secondsmall;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
         scanf("%d",&a[i]);
  small=a[0];
  for(i=1;i<size;i++){
         if(small>a[i]){
               small=a[i];
               j = i;
      }
  }
  secondsmall=a[size-j-1];
  for(i=1;i<size;i++){
         if(secondsmall > a[i] && j != i)
              secondsmall =a[i];
  }
  printf("Second smallest: %d", secondsmall);
  return 0;
}
Enter the size of the array: 5
Enter 5 elements in to the array: 5 7 3 2 6
Second smallest: 3
//REMOVE DUPLICATE ELEMENTS IN AN ARRAY USING C PROGRAM
#include<stdio.h>
int main(){
  int arr[50];
  int *p;
  int i,j,k,size,n;
  printf("\nEnter size of the array: ");
  scanf("%d",&n);
  printf("\nEnter %d elements into the array: ",n);
  for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
  size=n;
  p=arr;
  for(i=0;i<size;i++){
    for(j=0;j<size;j++){
         if(i==j){
             continue;
         }
         else if(*(p+i)==*(p+j)){
             k=j;
             size--;
             while(k < size){
                 *(p+k)=*(p+k+1);
                 k++;
              }
              j=0;
          }
      }
  }
  printf("\nThe array after removing duplicates is: ");
  for(i=0;i < size;i++){
    printf(" %d",arr[i]);
  }
  return 0;
}
//INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION USING C PROGRAM
#include<stdio.h>
int main(){
int a[50],size,num,i,pos,temp;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;iscanf("%d",&a[i]);
printf("\nEnter position and number to insert: ");
scanf("%d %d",&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;iprintf(" %d",a[i]);
return 0;
}
//C program to find largest and smallest number in an array
#include<stdio.h>
int main(){
  int a[50],size,i,big,small;
  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);
  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
           big=a[i];
  }
  printf("Largest element: %d",big);

  small=a[0];
  for(i=1;i<size;i++){
      if(small>a[i])
           small=a[i];
  }
  printf("Smallest element: %d",small);
  return 0;
}
Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1
//Write a program to convert the two-dimensional array into one-dimensional array
#include
#include
#define mrow 3
#define mcol 2
main()
{
int a[mrow][mcol],b[mrow*mcol];
int i,j,k=0;
clrscr();
printf("Enter the Matrix elements in row order\n");
for(i=0;i<mrow;i++)
for(j=0;j<mcol;j++)
{
scanf("%d",&a[i][j]);
b[k++]=a[i][j];
}
printf("Given Two_dimensinol arry: \n");
for(i=0;i<mrow;i++)
{
for(j=0;j<mcol;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("Equialent One_Dimenson array \n");
for(i=0;i<mrow*mcol;i++)
printf("%d\t",b[i]);
getch();
return;
}
Output:
Enter the Matrix elements in row order
10 15 20 25 30 35
Given Two_dimensinol arry:
10 15
20 25
30 35
Equialent One_Dimenson array
10 15 20 25 30 35




program input integer and store in character array and add it big numbers for example