0% found this document useful (0 votes)
4 views

Data Structure Using C_Sample

The document provides several sample C programs demonstrating basic data structures and algorithms, including array declaration, bubble sort, prime number checking, palindrome checking, and matrix multiplication. Each program includes code snippets and sample outputs to illustrate their functionality. These examples serve as practical applications for understanding fundamental programming concepts in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structure Using C_Sample

The document provides several sample C programs demonstrating basic data structures and algorithms, including array declaration, bubble sort, prime number checking, palindrome checking, and matrix multiplication. Each program includes code snippets and sample outputs to illustrate their functionality. These examples serve as practical applications for understanding fundamental programming concepts in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data structure using C

Sample Program
1. Write a program for declaring and utilizing an array.
#include <stdio.h>
int main() {
int numbers[5]; // Declares an integer array with a size of 5 elements

// Assign values to the array elements


numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

// Display the values stored in the array


printf("Values in the array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");

return 0;
}
Output:
Values in the array: 10 20 30 40 50

2. Write a program to sort a given numbers using bubble sort.


#include <stdio.h>

void bubble_sort(int arr[], int n) {


int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Sorted array: 11 12 22 25 34 64 90
3. Write a program check whether the number is prime or not.
#include<stdio.h>
int main(){
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
printf("Number is prime");
return 0;
}

Output
Enter the number to check prime:56
Number is not prime

Enter the number to check prime:23


Number is prime

4. Write a program to check given number is Palindrome program in

#include<stdio.h>

int main()

int n,r,sum=0,temp;

printf("enter the number=");

scanf("%d",&n);
temp=n;

while(n>0)

r=n%10;

sum=(sum*10)+r;

n=n/10;

if(temp==sum)

printf("palindrome number ");

else

printf("not palindrome");

return 0;

Output

enter the number=151


palindrome number

enter the number=5621


not palindrome number

5. Write a program for matrix multiplication using C

#include<stdio.h>

#include<stdlib.h>

int main(){

int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;

system("cls");

printf("enter the number of row=");

scanf("%d",&r);

printf("enter the number of column=");

scanf("%d",&c);

printf("enter the first matrix element=\n");

for(i=0;i<r;i++)
{

for(j=0;j<c;j++)

scanf("%d",&a[i][j]);

printf("enter the second matrix element=\n");

for(i=0;i<r;i++)

for(j=0;j<c;j++)

scanf("%d",&b[i][j]);

printf("multiply of the matrix=\n");

for(i=0;i<r;i++)

for(j=0;j<c;j++)

mul[i][j]=0;

for(k=0;k<c;k++)

mul[i][j]+=a[i][k]*b[k][j];

//for printing result

for(i=0;i<r;i++)

for(j=0;j<c;j++)
{

printf("%d\t",mul[i][j]);

printf("\n");

return 0;

Output

enter the number of row=3


enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18

You might also like