0% found this document useful (0 votes)
3 views16 pages

Ds Programs Part A

The document contains multiple C programming examples, including programs to find the GCD of two numbers, create a Pascal triangle, generate a Fibonacci series, implement the Tower of Hanoi, manage dynamic arrays, and sort numbers using selection and bubble sort. Each program includes code snippets, user prompts, and example outputs. The programs cover various algorithms and data structures, demonstrating basic 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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Ds Programs Part A

The document contains multiple C programming examples, including programs to find the GCD of two numbers, create a Pascal triangle, generate a Fibonacci series, implement the Tower of Hanoi, manage dynamic arrays, and sort numbers using selection and bubble sort. Each program includes code snippets, user prompts, and example outputs. The programs cover various algorithms and data structures, demonstrating basic 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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

program:- 1

program to find gcd of two numbers

#include<stdio.h>

int gcd(int m,int n)

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;

printf("enter two numbers:\n");

scanf("%d%d",&num1,&num2);

result=gcd(num1,num2);

printf("gcd of %d and %d is:%d\n",num1,num2,result);

output:-

enter two numbers:

gcd of 10 and 20 is:10

program :-2

program to create a pascal triangle

#include<stdio.h>

void main()
{

int c[15][15],n,r,rows,num=15,k;

printf("enter the number of rows:");

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:-

enter number of rows:- 3


program:-3

program to genrate fibonacci series

#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;

printf("enter the number to generate fiboncci series:\n");

scanf("%d",&n);

while(i<n)

++i;

printf("%d",fibon(i));

printf("\n%dth element of the series is:%d\n",i,fibon(i));

output:-

enter the number to genrate fibonacci series :5

01123

5th element of series is: 3

program:-4

program to implement tower of hanoi

#include <stdio.h>

void toh(int, char, char, char);

int count=0;

void main()

char source = ‘s’, temp = ‘t’, dest = ‘d’;

int n;

printf ("enter the number of disks: ");

scanf ("%d", & n);

printf ("\n sequence is: ");

toh (n, source, temp, dest);


printf ("\n the number of moves: %d", count);

void toh (int n, char source, char temp, char dest)

if (n > 0)

toh (n-1, source, dest, temp);

printf ("\n move disk %d %c->%c \n", n, source, dest);

count++;

toh (n-1, temp, source, dest);

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,

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy