Dsa Group-11
Dsa Group-11
AN
DATA STRUCTURE AND ALGORITHM PROJECT
AT
(AKGEC)
Submitted by
Name: HIMANSHU CHAUDHARY Submitted to
University Roll No.:2000270210040 Mrs. Arti Chaudhary
Year: 3rd Year
Semester: 5 Semester
Section:1
Branch: EN
Date
Write a program in C to store elements in
an array and print it.
void main()
int arr[10];
int i;
printf("-----------------------------------------\n");
printf("element - %d : ",i);
scanf("%d", &arr[i]);
printf("\n");
}
OUTPUT
Write a program in C to read n number of values
in an array and display it in reverse order.
void main()
{ int i,n,a[100];
scanf("%d",&n);
for(i=0;i<n;i++)a
{ printf("element - %d : “,i); a
scanf(“%d",&a[i]); }
for(i=0;i<n;i++)
{ printf("% 5d”,a[i]); }
for(i=n-1;i>=0;i--)
{ printf("% 5d",a[i]); }
printf(“\n\n"); }
OUTPUT
Write a program in C to find the sum of all
elements of the array.
void main()
{ int a[100];
int i, n, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("element - %d : ",i);
scanf("%d",&a[i]);}
{sum += a[i];}
}
OUTPUT
Write a program in C to copy the elements
of one array into another array.
void main()
int i, n;
scanf("%d",&n);
for(i=0;i<n;i++)
{printf("element - %d : ",i);
scanf("%d",&arr1[i])}
{arr2[i] = arr1[i];}
printf(“\n\n");}
{
OUTPUT
Write a program in C for a 2D array of
size 3x3 and print the matrix
void main()
{ int arr1[3][3],i,j;
printf("------------------------------------------------------\n");
{ for(j=0;j<3;j++)
scanf(“%d",&arr1[i][j]);}}
{ printf("\n");
for(j=0;j<3;j++)
printf("%d\t",arr1[i][j]); } printf("\n\n");}
OUTPUT
Write a program in C for addition of two
Matrices of same size.
Void main()
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;
printf("------------------------------\n");
printf("Input the size of the square matrix (less than 5): ");
scanf("%d", &n);
for(i=0;i<n;i++) { for(j=0;j<n;j++){
scanf(“%d",&arr1[i][j]); }}
printf("Input elements in the second matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&brr1[i][j]);} }
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",arr1[i][j]); }
printf("\nThe Second matrix is :\n");
{ printf("\n");
for(j=0;j<n;j++)
printf("%d\t",brr1[i][j]);}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
crr1[i][j]=arr1[i][j]+brr1[i][j];
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",crr1[i][j]);}printf("\n\n");}
OUTPUT
Write a program in C to show the simple
structure of a function.
#include <stdio.h>
#include <stdio.h>
double square(double )
{
return ( * );
}
int main()
{
int ;
double ;
printf("\n\n Function : find square of any number :\n");
printf("------------------------------------------------\n");
int checkOddEven(int )
{
return ( );
}
int main()
{
int ;
printf("\n\n Function : check the number is even or odd:\n");
printf("------------------------------------------------\n");
printf("Input any number : ");
scanf("%d", & );
if(checkOddEven( ))
{
printf("The entered number is odd.\n\n");
}
else
{
printf("The entered number is even.\n\n");
}
return 0;
}
OUTPUT
Write a program in C to print first 50
natural numbers using recursion.
int numPrint(int);
int main()
{
int = 1;
printf("\n\n Recursion : print first 50 natural numbers :\n”);
printf(" The natural numbers are :");
numPrint( );
printf("\n\n");
return 0;
}
int numPrint(int )
{
if( <=50)
{
printf(" %d ", );
numPrint( +1);
}
OUTPUT
Write a program in C to calculate the sum
of numbers from 1 to n using recursion
int sumOfRange(int);
int main()
{
int ;
int ;
printf("\n\n Recursion : calculate the sum of numbers from 1 to n :\n");
printf("-----------------------------------------------------------\n");
printf(" Input the last number of the range starting from 1 : ");
scanf("%d", & );
= sumOfRange( );
printf("\n The sum of numbers from 1 to %d : %d\n\n", , );
return (0);
}
int sumOfRange(int )
{
int ;
if ( == 1)
{
return (1);
} else
{
= + sumOfRange( - 1); //calling the function sumOfRange itself
}
return ( );
}
OUTPUT
Write a program in C to print the array
elements using recursion.
#include <stdio.h>
#define MAX 100
int main()
{
int [ ];
int , ;
printf("\n\n Recursion : Print the array elements :\n");
printf("-------------------------------------------\n");
int ;
int fibonacci(int , int );
void main()
{
static int = 0, = 1;
printf("\n\n Recursion : Print Fibonacci Series :\n");
printf("-----------------------------------------\n");
printf(" Input number of terms for the Series (< 20) : ");
scanf("%d", & );
printf(" The Series are :\n");
printf(" 1 ");
fibonacci( , );
printf("\n\n");
}
if ( == )
return (0);
else
{
= + ;
= ;
= ;
printf("%d ", );
++;
fibonacci( , ); //recursion, calling the function fibonacci itself
}
return (0);
}
OUTPUT
STRINGS IN C
#include <stdio.h>
#include <string.h>
int main()
{
printf("%s\n", str);
int length = 0;
length = strlen(str);
return 0;
}
OUTPUT
READ FROM USER
#include<stdio.h>
int main()
{
char str[50];
scanf("%s",str);
printf("%s",str);
return 0;
}
OUTPUT
ACESS,MODIFY,DIFFERENCE
char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
char greetings2[] = "Hello World!";
printf("%lu\n", sizeof(greetings));
printf("%lu\n", sizeof(greetings2));
OUTPUT
COPY STRING
#include<stdio.h>
#include<string.h>
int main ()
{
char str1[]="Hello Geeks!";
char str2[] = "GeeksforGeeks";
char str3[40];
char str4[40];
char str5[] = "GfG";
strcpy(str2, str1);
strcpy(str3, "Copy successful");
strcpy(str4, str5);
printf ("str1: %s\nstr2: %s\nstr3: %s\nstr4:
%s\n", str1, str2, str3, str4);
return 0;
}
OUTPUT
COMPARE STRING
#include<stdio.h>
#include<string.h>
int main()
{
if (res==0)
printf("Strings are equal");
else
printf("Strings are unequal");
int main()
{
p = strstr(s1, s2);
if (p) {
printf("String found\n");
printf("First occurrence of string '%s' in '%s' is '%s'", s2, s1, p);
} else
printf("String not found\n");
return 0;
}
OUTPUT
FIRST CHARACTER MATCHING IN
STRINGS
#include <stdio.h>
#include <string.h>
// Driver function
int main()
{
r = strpbrk(s1, s2);
if (r != 0)
printf("First matching character: %c\n", *r);
else
printf("Character not found");
t = strpbrk(s1, s3);
if (t != 0)
printf("\nFirst matching character: %c\n", *t);
else
printf("Character not found");
return (0);
}
OUTPUT
C IMPLEMENTATION OF STACK USING ARRAY
#include <stdio.h>
#include<conio.h>
int push (int stack, int *top, int val, int max_index);
int pop (int stack, int *top);
scanf(“%d”,&val) ;
result = push ( stack , &top, val, max_index);
if (result == 0)
printf(“\n The stack is full”);
else
printf(“\n Item inserted successfully”);
break;
case 2: result = pop (stack, &top)
if (result == 9999)
printf(“\n The stack is empty”);
else
printf(“\The popped value = %d”, result) ;
break;
case 3: display(stack, top);
break;
}
printf(“\n\n Press any key to continue”);
getch( ); //to hold the current screen
} while ( choice != 4);
}
/* push ( ) function would return 1 if the insertion is successful and 0 if stack is full */
int push (int stack, int *top, int val, int max_index)
{
if ( *top == max_index)
return 0 ; /* the stack is full */
else
{
*top = *top + 1 ;
stack [*top] = val ;
return 1;
}
}
/* pop ( ) function would return the popped value if the operation is successful and 9999 if stack is empty
*/
int pop (int stack, int *top)
{
int val ;
if ( *top == -1 )
return 9999 ; /* stack is empty assume 9999 is not the stack entry*/
else
{
val = stack [*top] ;
*top = *top + 1 ;
return val;
}
}
/* display ( ) function shows the items in the stack */
int display (int stack, int top)
{
int i ;
printf( “\n the contents of stack are: \n”) ;
for( i = top ; i >= 0 ; i = i - 1)
printf(“ %d \t ”, stack [i] );
}
OUTPUT