C Programming

C Programs on Arrays and sortings

C Program to demonstrate single dimension array
#include<stdio.h>
#include<conio.h>
main()
{
int a[10], i;
clrscr();
printf("Enter 5 elements for an array\n");
for(i=0; i<5; i++)
{
scanf("%d", &a[i]);
}
printf("\t\t INPUT of  an ARRAY\n");
for(i=0; i<5; i++)
{
printf("\t\t\t %d\n", a[i]);
}
getch();
}

C Program to demonstrate multi dimension array [Sum of two Matrix Program]

#include<stdio.h>
main()
{
int a[2][2],b[2][2], c[2][2],i,j;
clrscr();

printf("Enter 2x2 Elements for Matrix A:");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d", &a[i][j]);
}
printf("Enter 2x2 Elements for Matrix B:");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
scanf("%d", &b[i][j]);
}
printf("Matrix A \n");
for(i=0;i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%2d", a[i][j]);
}
printf("\n");
}
printf("Matrix B \n");
for(i=0;i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%2d", b[i][j]);
}
printf("\n");
}
printf("Sum of 2 matrix  \n");
for(i=0;i<2; i++)
{
for(j=0; j<2; j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%2d", c[i][j]);
}
printf("\n");
}
getch();
}

C Program to demonstrate Bubble sorting. 

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

void main()
{
    int a[20],t;
     int i,j,n;
     clrscr();
     printf("\nEnter n :");
     scanf("%d",&n);
     printf("\nEnter %d values \n",n);
     for(i=0;i < n;i++)
    {
    scanf("%d",&a[i]);
    }
    printf("\n%d values before sorting are \n",n);
    for(i=0;i < n;i++)
    {
    printf("%4d",a[i]);
    }
    bubble_sort(a,n);
    printf("\n%d values after sorting are \n",n);
    for(i=0;i < n;i++)
    {
    printf("%4d",a[i]);
    }
    getch();
}

int bubble_sort(int a[],int n)
{
    int i,j,k;
    int t;

    for(k=n;k>=1;k--)
    {
    for(i=0,j=1;j<=k;i++,j++)
    {
        if (a[j] < a[i])
        {
            t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    }
    return;
}

C Program to demonstrate Employee Salary information using Arrays.
#include<stdio.h>
#include<conio.h>
main()
{
int i, empno[10], bs[10], net[10], bn[10], gr_sal=0;
clrscr();
printf("Enter EMPNO, BASIC salary, BONUS of 3 employees:");

for(i=0; i<3; i++)
{
scanf("%d %d %d", &empno[i], &bs[i], &bn[i]);
net[i]= bs[i]+ bn[i];
gr_sal=gr_sal+net[i];
}

printf("\n EMPNo \tBASIC SALARY \t BONUS \t NETSALARY 

\n");
printf("=============================\n");
for(i=0; i<3; i++)
{
printf(" \n %d \t%d \t\t %d \t %d \n ", empno[i], bs[i], bn[i], 

net[i]);
}
printf("====================================\n");
printf("Total salary of all employees    : %d", gr_sal);
getch();
}
C Program to demonstrate Factorial of given number
#include <stdio.h>
#include <conio.h>
int main()
{
int c, n, fact = 1;
clrscr();
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
getch();
}
SKB

Leave a Reply

Your email address will not be published. Required fields are marked *