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

Dsa Group-11

This document contains summaries of 14 programs written in C language to perform various operations on arrays and strings. The programs include: 1) Storing and printing elements of an array 2) Reading array elements and printing in reverse order 3) Finding the sum of all elements of an array 4) Copying elements of one array into another 5) Defining a 2D array and printing the matrix 6) Adding two matrices of the same size 7) Demonstrating a simple function structure 8) Finding the square of a number using a function 9) Checking if a number is even or odd using a function 10) Printing the first 50 natural numbers using recursion 11) Calculating the

Uploaded by

sudhesh
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)
39 views

Dsa Group-11

This document contains summaries of 14 programs written in C language to perform various operations on arrays and strings. The programs include: 1) Storing and printing elements of an array 2) Reading array elements and printing in reverse order 3) Finding the sum of all elements of an array 4) Copying elements of one array into another 5) Defining a 2D array and printing the matrix 6) Adding two matrices of the same size 7) Demonstrating a simple function structure 8) Finding the square of a number using a function 9) Checking if a number is even or odd using a function 10) Printing the first 50 natural numbers using recursion 11) Calculating the

Uploaded by

sudhesh
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/ 51

AJAY KUMAR GARG ENGINEERING COLLEGE

27 KM STONE DELHI-HAPUR BYPASS ROAD, P.O. ADHYATMIK NAGAR GHAZIABAD-201009

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\nRead and Print elements of an array:\n");

printf("-----------------------------------------\n");

printf("Input 10 elements in the array :\n");

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

printf("element - %d : ",i);

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

printf("\nElements in array are: ");

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

printf("%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];

printf("\n\nRead n number of values in an array and display it in reverse order:\n");

printf("Input the number of elements to store in the array :");

scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);

for(i=0;i<n;i++)a

{ printf("element - %d : “,i); a

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

printf("\nThe values store into the array are : \n");

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

{ printf("% 5d”,a[i]); }

printf("\n\nThe values store into the array in reverse are :\n");

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;

printf("\n\nFind sum of all elements of array:\n");

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

{ printf("element - %d : ",i);

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

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

{sum += a[i];}

printf("Sum of all elements stored in the array is : %d\n\n", sum);

}
OUTPUT
Write a program in C to copy the elements
of one array into another array.
void main()

{ int arr1[100], arr2[100];

int i, n;

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

{printf("element - %d : ",i);

scanf("%d",&arr1[i])}

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

{arr2[i] = arr1[i];}

printf("\n\nThe elements copied into the second array are :\n");

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

{printf("% 5d", arr2[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\nRead a 2D array of size 3x3 and print the matrix :\n");

printf("------------------------------------------------------\n");

printf("Input elements in the matrix :\n");

for( =0; <3; ++)

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

{ printf("element - [%d],[%d] : ", , );

scanf(“%d",&arr1[i][j]);}}

printf("\nThe matrix is : \n");

for( =0; <3; ++)

{ 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\nAddition of two Matrices :\n");

printf("------------------------------\n");

printf("Input the size of the square matrix (less than 5): ");

scanf("%d", &n);

printf("Input elements in the first matrix :\n");

for(i=0;i<n;i++) { for(j=0;j<n;j++){

printf("element - [%d],[%d] : ", , );

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++)

{ printf("element - [%d],[%d] : ", , );

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

printf("\nThe First matrix is :\n");

for( =0; < ; ++)

printf("\n");

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

printf("%d\t",arr1[i][j]); }
printf("\nThe Second matrix is :\n");

for( =0; < ; ++)

{ 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];

printf("\nThe Addition of two matrix is : \n");

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>

int sum (int, int);


int main (void)
{
int ;
printf("\n\n Function : a simple structure of function :\n");
printf("------------------------------------------------\n");
= sum (5, 6);//function call
printf ("The total is : %d\n", );
return 0;
}

int sum (int , int )


{
int ;
= + ;
return ;
}
OUTPUT
Write a program in C to find the square of
any number using the function.

#include <stdio.h>

double square(double )
{
return ( * );
}
int main()
{
int ;
double ;
printf("\n\n Function : find square of any number :\n");
printf("------------------------------------------------\n");

printf("Input any number for square : ");


scanf("%d", & );
= square( );
printf("The square of %d is : %.2f\n", , );
return 0;
}
OUTPUT
Write a program in C to check a given
number is even or odd using the function.
#include <stdio.h>

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

void ArrayElement(int [], int , int );

int main()
{
int [ ];
int , ;
printf("\n\n Recursion : Print the array elements :\n");
printf("-------------------------------------------\n");

printf(" Input the number of elements to be stored in the array :");


scanf("%d",& );

printf(" Input %d elements in the array :\n", );


for( =0; < ; ++)
{
printf(" element - %d : ", );
scanf("%d",& [ ]);
}

printf(" The elements in the array are : ");


ArrayElement( , 0, );//call the function ArrayElement
printf("\n\n");
return 0;
}

void ArrayElement(int [], int , int )


{
if( >= )
return;

//Prints the current array element


printf("%d ", [ ]);

/* Recursively call ArrayElement to print next element in the array */


ArrayElement( , +1, );//calling the function ArrayElement itself
}
OUTPUT
Write a program in C to Print Fibonacci
Series using recursion
#include<stdio.h>

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");
}

int fibonacci(int , int )


{
static int = 1;
int ;

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()
{

char str[] = "Geeks";

printf("%s\n", str);

int length = 0;
length = strlen(str);

printf("Length of string str is %d", length);

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[] = "Hello World!";


printf("%c", greetings[0]);

char greetings[] = "Hello World!";


greetings[0] = 'J';
printf("%s", greetings);

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()
{

char leftStr[] = "g f g";


char rightStr[] = "g f g";

int res = strcmp(leftStr, rightStr);

if (res==0)
printf("Strings are equal");
else
printf("Strings are unequal");

printf("\nValue returned by strcmp() is: %d" , res);


return 0;
}
OUTPUT
OCCURRENCE OF SECOND STRING
WITHIN FIRST STRING
#include <string.h>
#include <stdio.h>

int main()
{

char s1[] = "GeeksforGeeks";


char s2[] = "for";
char* p;

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()
{

char s1[] = "geeksforgeeks";


char s2[] = "app";
char s3[] = "kite";
char* r, *t;

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

int display (int stack, int top);


void main( )
{

int stack [20] ; /* the stack declaration */


int top = -1; /* initially the stack is empty */
int val, max_index, choice, result ;
max_index = 20 - 1; /* maximum index of the stack *
/* create menu */
do
{
clrscr ( );
printf (“\n Menu – Stack Operations”);
printf (“\n Push 1”);
printf (“\n Pop 2”);
printf (“\n Display Stack 3”);
printf (“\n Quit 4”);
/* collect the choice */
printf (“\n Enter your choice : \t ”);
scanf( “%d” , &choice ) ;
/* Perform action according to choice */
switch(choice)
{
case 1: printf( “\n Enter the value to be pushed”) ;

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

You might also like