Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Subject- Data Structure and Algorithms Assignment

Name- Ankita Bangabash

(1) Write a program in C to print Fibonacci series upto a given range.

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

void main()
{
int n,a=0,b=1,c,i;
printf("Enter the limit of the fibonacci series:");

scanf("%d",&n);

for(i=1;i<=n;i++)

{
printf("%d\n",a);
c=a+b;
a=b;
b=c;
}
}

(2)Bubble Sort

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

int main()
{
int a[100], number, i, j, temp;

printf("\n Please Enter the total Number of Elements : ");


scanf("%d", &number);

printf("\n Please Enter the Array Elements : ");


for(i = 0; i < number; i++)
scanf("%d", &a[i]);
for(i = 0; i < number - 1; i++)
{
for(j = 0; j < number - i - 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("\n Sorted in ascending order : ");
for(i = 0; i < number; i++)
{
printf(" %d \t", a[i]);
}
return 0;
}

(3)Define a Structure named Student which will have three members name,
roll , marks. Enter three students record and print the details.

int main()

{
int i,n;
struct student s[3];
printf("Enter np. of students: ");
scanf("%d",&n);
printf("Enter Students Record\n\n");

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

{
printf("Enter sRoll: ");
scanf("%d",&s[i].Roll);
printf("Enter sName: ");
scanf("%s",&s[i].Name);
printf("Enter sMarks: ");
scanf("%f",&s[i].Marks);
}
printf("\nStudents Record\n\n");

for(i=0;i<3;i++)
{
printf("sRoll: %d\n",s[i].Roll);

printf("sName: %s\n",s[i].Name);

printf("sMarks: %f\n\n",s[i].Marks);
}
return 0;
}

(4)Enter 10 values in ascending order and search a given element using


binary search method.

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

int main()
{
int i, arr[10], search, first, last, middle;
printf("Enter 10 elements (in ascending order): ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
printf("\nEnter element to be search: ");
scanf("%d", &search);
first = 0;
last = 9;
middle = (first+last)/2;
while(first <= last)
{
if(arr[middle]<search)
first = middle+1;
else if(arr[middle]==search)
{
printf("\nThe number, %d found at Position %d", search, middle+1);
break;
}
else
last = middle-1;
middle = (first+last)/2;
}
if(first>last)
printf("\nThe number, %d is not found in given Array", search);
getch();
return 0;
}

You might also like