0% found this document useful (0 votes)
18 views8 pages

PROGRAM 4a

c programs

Uploaded by

nayiid556
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)
18 views8 pages

PROGRAM 4a

c programs

Uploaded by

nayiid556
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/ 8

PROGRAM 2b

Program Name-Binary Search Name-Rishav Kumar Gupta

Domain -Array Roll No-2300320130196

Code

#include <stdio.h>

int binarySearch(int arr[], int size, int target) {

int left = 0;

int right = size - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid;

else if (arr[mid] < target) {

left = mid + 1;

else {

right = mid - 1;

} }

return -1;

int main() {

int arr[] = {2, 3, 4, 5, 8};

int size = sizeof(arr) / sizeof(arr[0]);

int target;

printf("Enter the number to search: ");

scanf("%d", &target);

int result = binarySearch(arr, size, target);

if (result != -1) {

printf("Element found at index: %d\n", result);

} else {

printf("Element not found in the array.\n");

return 0; }
PROGRAM 2b
Program Name-Binary Search Name-Rishav Kumar Gupta

Domain -Array Roll No-2300320130196

OUTPUT-

Enter the number to search: 8

Element found at index: 4


PROGRAM 2a
Program Name-Linier Search Name-Rishav Kumar Gupta

Domain -Array Roll No-2300320130196

Output –

Enter the number to search: 8

Element found at index: 2


PROGRAM 2a
Program Name-Linier Search Name-Rishav Kumar Gupta

Domain -Array Roll No-2300320130196

Code

#include <stdio.h>

int linearSearch(int arr[], int size, int target) {

for (int i = 0; i < size; i++) {

if (arr[i] == target) {

return i;

return -1;

int main() {

int arr[] = {5, 3, 8, 4, 2};

int size = sizeof(arr) / sizeof(arr[0]);

int target;

printf("Enter the number to search: ");

scanf("%d", &target);

int result = linearSearch(arr, size, target);

if (result != -1) {

printf("Element found at index: %d\n", result);

} else {

printf("Element not found in the array.\n");

return 0;

}
PROGRAM 4a
Program Name-Array implementation of Stack Name-Rishav Kumar
Gupta

Domain-Stack Roll No-2300320130196

Code:

#include<stdio.h>

#define MAXSIZE 5
int s[MAXSIZE],top=-1,item;
int isempty(){

if(top==-1)

return 1;

else

return 0;

int isfull(){

if(top==MAXSIZE-1)

return 1;

else

return 0;

void push(){

printf("Enter element");

scanf("%d",&item);

if(!isfull())

top=top+1;

s[top]=item;

else

printf("stack overflow");

void pop(){

if(!isempty()){

printf("Element deleated:%d",s[top]);

top=top-1;
PROGRAM 4a
Program Name-Array implementation of Stack Name-Rishav Kumar
Gupta

Domain-Stack Roll No-2300320130196

Code:

else{

printf("Stack is empty");

} }

void display(){

for(int i=top;i>=0;i--){

printf("\n%d",s[i]);

} }

int main()

int ch;

while(1){

printf("\n1.push");

printf("\n2.pop");

printf("\n3.display");

printf("\n4.exit");

printf("Enter your choice");

scanf("%d",&ch);

switch(ch){

case 1:push();

break;

case 2:pop();

break;

case 3:display();

break;

case 4:return 0;

} }
return 0;

}
PROGRAM 4a
Program Name-Array implementation of Stack Name-Rishav Kumar Gupta

Domain-Stack Roll No-2300320130196

OUTPUT:-
1.push

2.pop

3.display

4.exitEnter your choice1

Enter element12

1.push

2.pop

3.display

4.exitEnter your choice1

Enter element23

1.push

2.pop

3.display

4.exitEnter your choice1

Enter element44

1.push

2.pop

3.display

4.exitEnter your choice3

44

23

12

1.push

2.pop

3.display

4.exitEnter your choice4

PS C:\Users\RISHABH\c programs>

You might also like