Ds Programs Part A
Ds Programs Part A
#include<stdio.h>
if(n==0)
return(m);
else if(n>m)
return gcd(n,m);
else
return gcd(n,m%n);
void main()
{int num1,num2,result;
scanf("%d%d",&num1,&num2);
result=gcd(num1,num2);
output:-
program :-2
#include<stdio.h>
void main()
{
int c[15][15],n,r,rows,num=15,k;
scanf("%d",&rows);
for(n=0;n<rows;n++)
for(k=num-2*n;k>=0;k--)
printf(" ");
for(r=0;r<=n;r++)
if(r==0||n==r)
c[n][r]=1;
else
c[n][r]=c[n-1][r-1]+c[n-1][r];
printf("%4d",c[n][r]);
printf("\n");
output:-
#include<stdio.h>
fibon(int n)
if((n==0)||(n==1))
return(0);
else if(n==2)
return(1);
else
return(fibon(n-1)+fibon(n-2));
void main()
int n;
int i=0;
scanf("%d",&n);
while(i<n)
++i;
printf("%d",fibon(i));
output:-
01123
program:-4
#include <stdio.h>
int count=0;
void main()
int n;
if (n > 0)
count++;
output:-
program-5
program to implement dynamic array,find smallest and
largest element of an array
# include<stdio.h>
#include <stdlib.h>
int main() {
int n, i, largest, smallest;
int *arr;
printf("enter the total number of elements: ");
scanf("%d", &n);
// allocating memory for n elements
arr = (int*)calloc(n, sizeof(int));
if (arr == NULL)
{
printf("error!!! memory not allocated.");
return 1;
}
// read numbers from the user
for (i = 0; i < n; ++i) {
printf("enter number %d: ", i + 1);
scanf("%d", arr + i);
}
largest = smallest = arr[0];
for (i = 0; i < n; i++)
{
if (largest <*(arr+i))
largest = *(arr+i);
if (smallest >*(arr+i))
smallest = *(arr+i);
}
printf("\nlargest number in the array is: %d\n", largest);
printf("smallest number in the array is: %d\n", smallest);
free(arr);
return 0;
}
output:-
program:-6
program to create a file to store even and odd numbers
#include<stdio.h>
#include <stdlib.h>
int main()
{
file *fp1, *fp2;
int i,n,num;
fp1=fopen("even.txt","w");
fp2=fopen("odd.txt","w");
printf("enter number of elements:");
scanf("%d",&n);
printf("\n enter %d numbers:",n);
for(i=0;i<n;i++)
{
scanf("%d",&num);
if(num%2==0)
putw(num,fp1);
else
putw(num,fp2);
}
fclose(fp1);
fclose(fp2);
fp1=fopen("even.txt","r");
fp2=fopen("odd.txt","r");
printf("\n contents of even number file :");
while((num=getw(fp1)) !=eof)
{
printf("%d\t",num);
}
printf("\n contents of odd number file:");
while((num=getw(fp2)) !=eof)
{
printf("%d\t",num);
}
fclose(fp1);
fclose(fp2);
return(0);
}
output:-
program:-7
program to create a file to store student details
#include <stdio.h>
struct std
{
char student_name[20];
int student_id;
float student_score;
};
int main()
{
int n, i;
FILE *fptr = fopen("student", "w");
if (fptr == NULL)
{
printf("!!!failed to create a file");
return -1;
}
printf("\n enter number of students information to store in a
file:");
scanf("%d", &n);
struct std s[n];
for (i = 0; i < n; i++)
{
printf("enter student %d name :", i + 1);
scanf("%s", s[i].student_name);
printf("enter student %d ID:", i + 1);
scanf("%d", &s[i].student_id);
printf("enter student %d score: ", i + 1);
scanf("%f", &s[i].student_score);
fprintf(fptr, "%s %d %f\n", s[i].student_name, s[i].student_id,
s[i].student_score);
}
fclose(fptr);
fptr = fopen("student", "r");
printf("\n student details : \n");
printf("-----------\n");
printf("student name\t\t student id\t student score\n");
for (i = 0; i < n; i++)
{
fscanf(fptr, "%s %d %f", s[i].student_name, &s[i].student_id,
&s[i].student_score);
printf("%s\t\t %d\t\t %f\n", s[i].student_name, s[i].student_id,
s[i].student_score);
}
fclose(fptr);
return 0;
}
OUTPUT:-
PROGRAM 8:-
PROGRAM TO READ THE NAMES OF CITIES AND ARRANGE
ALPHABETICALLY
#include<stdio.h>
#include<string.h>
Void main()
{
Int I,j,n;
Char str[20] [15], temp[100];
Printf("Enter Number of City Names to be Sorted:\n");
Scanf("%d",&n);
Printf("Enter City Names in Any Order\n");
For(i=0;i<n;i++)
Scanf("%s", str[i]);
For(i=0;i<n;i++)
{
For(j=i+1;j<n;j++)
{
If(strcmp(str[i],str[j])>0)
{
Strcpy(temp, str[i]);
Strcpy(str[i], str[j]);
Strcpy(str[j], temp);
}
}
}
Printf("\nThe Sorted Order of City Names are: \n");
For(i=0;i<n;i++)
Printf("%s\n", str[i]);
}
OUTPUT:-
Enter Number of City Names to be Sorted:
4
Enter City Names in Any Order
Banglore
Mysore
Malur
Kolar
The Sorted Order of City Names are:
Banglore
Kolar
Malur
Mysore
PROGRAM:-09
PROGRAM TO SORT NUMBERS USING SELECTION SORT
#include<stdio.h>
Int min (int a[], int k, int n)
{
Int loc, j, min;
Min = a[k];
Loc = k;
For (j = k + 1; j <= n – 1; j++)
If (min > a[j])
{
Min = a[j];
Loc = j;
}
Return (loc);
}
Void main()
{
Int I,a[100],k,n, loc=0,temp;
Printf("Enter the number of elements:\n");
Scanf("%d",&n);
Printf("\n Enter the array elements:\n");
For(i=0;i<n;i++)
Scanf("%d",&a[i]);
For(k=0; k<n; k++)
{
Loc=min(a,k,n); /* find the smallest element location */
Temp=a[k];
A[k]=a[loc];
A[loc]=temp;
}
Printf("\n The Sorted Array is:\n");
For(i=0;i<n;i++)
Printf("%d ",a[i]);
}
OUTPUT:-
Enter the number of elements:
5
Enter the array elements:
40,30,20,50,10
The Sorted Array is:
10,20,30,40,50
PROGRAM:-10
PROGRAM TO SORT N NUMBERS USING BUBBLE SORT
#include<stdio.h>
Void bubble_sort(int a[], int n)
{
Int pass, temp, j;
For (pass = 1; pass < n; pass ++)
{
For (j = 0; j <= n-pass-1; j++)
{
If (a [j] > a[j + 1])
{
Temp = a [j];
A [j] = a [j + 1];
A [j + 1] = temp;
}
}
}
}
Int main()
{
Int I,j,a[20],n, temp;
Printf("\n Enter the number of elements: ");
Scanf("%d",&n);
Printf("\n Enter the array elements: \n");
For( I = 0; I < n ;i++)
Scanf("%d",&a[i]);
Bubble_sort (a, n);
Printf("\n The sorted elements are: \n");
For( I =0; I < n ;i++)
Printf("%d ",a[i]);
}
OUTPUT:-
Enter the number of elements: 7
Enter the array elements:
5, 6,1,3,0,2,4
The sorted elements are:
0,1,2,3,4,5,6,