Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acm_dsa programs by akshat mittal #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions acm_dsa_pr1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include<stdio.h>
int main(){char a;
do{
printf("number of elements to be added in an array-->");
int num;
int num1;
scanf("%d",&num);
int arr1[num],inp,elem;
//printf("%d",num);

for (int i=0;i<num;i++)
{
printf("enter the element %d--->",i+1);
scanf("%d",&num1);
arr1[i]=num1;
//printf("%d",arr1[i]);
}
printf("1:find index of element using binary search:\n2:find the index of element using linear search\n3:exit\nenter your choice-->");
scanf("%d",&inp);


if (inp==1)
{
printf("enter the element to find its index-->");
scanf("%d",&elem);
for (int j=0;j<num;j++)
{
for (int k=j+1;k<num;k++)
{
if(arr1[j]>arr1[k])
{
int temp=arr1[j];
arr1[j]=arr1[k];
arr1[k]=temp;
}
}
}
int low=0,high=num-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(arr1[mid]==elem)
{
printf("the binary index of %d is %d",elem,mid);
break;
}
else if(arr1[mid]>elem)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
/*for(int m=0;m<num;m++)
{
printf("%d",arr1[m]);
}*/
}
else if(inp==2)
{
printf("enter the element to find its index-->");
scanf("%d",&elem);
for (int i=0;i<num;i++)
{
if (arr1[i]==elem)
{
printf("the index of given element %d is %d\n",elem,i);
break;
}
}
}
else if (inp==3){break;}
printf("\nenter y to run the code again-->");
scanf(" %c",&a);
}while(a=='y');
return 0;
}
25 changes: 25 additions & 0 deletions acm_dsa_pr2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<stdio.h>
int fib(int);
void main()
{
int a;
printf("enter the number to find the nth fibonacci number-->");
scanf("%d",&a);
int result=fib(a);
printf("the element of fibonacci series is %d",result);

}
int fib(int y)
{
static int p=1;
static int sum=0;
if(y==1)
{return sum;}
else{
y--;

sum=sum+p;
p=sum-p;
fib(y);
}
}
32 changes: 32 additions & 0 deletions acm_dsa_pr3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>

void pla_t(int,int);
void pali(int);
void main()

{
int d;
printf("Enter a number to check whether the number is palidrome or not--->");
scanf("%d",&d);
pali(d);}

void pali(int d){
int xx,xy=0;
xx=d;
while(d>0)
{
xy=(xy*10)+(d%10);
d=d/10;}
pla_t(xx,xy);


}
void pla_t(int x,int y)
{
if (x==y){
printf("the given number %d is a palidrome number.\n",x);
}
else{
printf("the given number %d is not a palidrome number.\n",x);
}
}
20 changes: 20 additions & 0 deletions acm_dsa_pr4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<stdio.h>
int main()
{
char ent[20];
printf("enter the string-->");
scanf("%s",&ent);
int y=0;
for(int i=0;ent[i]!='\0';i++)
{
y++;
}
//printf("%d",y);
int con1=0,vov1=0;
for(int j=0;j<y;j++){
(ent[j]=='a' || ent[j]=='e' || ent[j]=='i' || ent[j]=='o' || ent[j]=='u' || ent[j]=='A' || ent[j]=='E' || ent[j]=='I' || ent[j]=='O' || ent[j]=='U' ? vov1++ : con1++);

}
printf("total number of vovel in the string is %d\n",vov1);
printf("total number of consonant in the string is %d",con1);
}
49 changes: 49 additions & 0 deletions acm_dsa_pr5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<stdio.h>
int main()
{
printf("number of elements to be added in an array-->");
int num,num1;
scanf("%d",&num);
int arr1[num],inp,elem,small,j;
//printf("%d",num);

for (int i=0;i<num;i++)
{
printf("enter the element %d--->",i+1);
scanf("%d",&num1);
arr1[i]=num1;
//printf("%d",arr1[i]);
}
/*for(int m=0;m<num;m++)
{
printf("%d\t",arr1[m]);
}*/
for(int i=0;i<num-1;i++)
{
small=arr1[i];
inp=arr1[i];
for (j=i+1 ;j< num;j++ ){
if (arr1[i]>arr1[j])
{
small=arr1[j];
elem=j;
arr1[i]=small;

}
else{
continue;
}
}
//printf("%d",small);
if (inp!=small){
arr1[elem]=inp;}



}
for(int m=0;m<num;m++)
{
printf("%d\t",arr1[m]);
}printf("\n");

}
32 changes: 32 additions & 0 deletions acm_dsa_pr6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>
int main()
{
int y,z;
printf("number of elements to be added in an array-->");
int num,num1;
scanf("%d",&num);
int arr1[num],inp,elem,j;
//printf("%d",num);
inp=num;
for (int i=0;i<num;i++)
{
printf("enter the element %d--->",i+1);
scanf("%d",&num1);
arr1[i]=num1;
//printf("%d",arr1[i]);
}

for(int i=0;i<num;i++){inp--;
for(y=0;y<inp;y++)
{
if(arr1[y]>arr1[y+1])
{
elem=arr1[y];
arr1[y]=arr1[y+1];
arr1[y+1]=elem;
}
}}
for(int m=0;m<num;m++)
{
printf("%d\t",arr1[m]);
}printf("\n");}
23 changes: 23 additions & 0 deletions acm_dsa_pr7.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

int power(int base, int exponent) {
if (exponent == 0)
return 1;
else if (exponent > 0)
return base * power(base, exponent - 1);

}

int main() {
int base,exponent,result;
printf("enter the base value-->");
scanf("%d",&base);
printf("enter the value of exponential power-->");
scanf("%d",&exponent);
result = power(base, exponent);


printf("%d^%d = %d\n", base, exponent, result);

return 0;
}