21BCE5450 Navyansh DSA Lab 1
21BCE5450 Navyansh DSA Lab 1
21BCE5450 Navyansh DSA Lab 1
4. Write a C program, which outputs all local maximums of a given data of elements. A
number xi is a local maximum if it is more than both xi-1 and xi+1. If the elements are 25,
19, 22, 23, 21, 12, 10, 17, 11, 13, 10 then 23, 17 and 13 are local maximums.
5. Write a C program that outputs the smallest ‘i’ such that xi is even. For example, 22 is
the output for the input 25, 19, 22, 23, 21, 12, 10, 17, 11, 13, 10
6. Write a C program that outputs the smallest ‘i’ such that xi and xi+1 are both even. In
above case 6. (Because 12 and 10 are even).
1.
#include <stdio.h>
int main()
{
int x[20],sum,i,n;
printf("how many numbers ");
scanf("%d",&n);
for (i=0;i<n;i++){
printf("give %d th number: ",i+1);
scanf("%d",&x[i]);
}
sum=0;
for(i=0;i<n-2;i++){
sum+=(x[i]+x[i+1])*x[i+2];
}
printf("%d",sum);
2.
#include <stdio.h>
int main()
{
int x[20],sum,i,n;
printf("how many numbers ");
scanf("%d",&n);
for (i=0;i<n;i++){
printf("give %d th number: ",i+1);
scanf("%d",&x[i]);
}
sum=1;
for(i=0;i<n-2;i++){
sum*=(x[i]+x[i+2]);
}
printf("%d",sum);
}
3.
#include <stdio.h>
int main()
{
int x[20],sum,i,n;
printf("how many numbers ");
scanf("%d",&n);
for (i=0;i<n;i++){
printf("give %d th number: ",i+1);
scanf("%d",&x[i]);
}
sum=0;
for(i=0;i<n-2;i++){
sum+=(x[i]-x[i+1])*(x[i+1]+x[i+2]);
}
printf("%d",sum);
}
4.
#include <stdio.h>
int main(){
int n;
printf("Enter the total number of elements of the array:");
scanf("%d",&n);
int arr[n];
printf("Enter the elements:");
for (int i=0;i<=n;i++){
scanf("%d",&arr[i]);
}
// int i=1;
for ( int i=0; i < (n) ; i++) {
if ( (arr[i] > arr[i-1]) && (arr[i] > arr[i+1]) ) {
printf("The local maximums are: ");
printf("%d\n",arr[i]);
}
}
}
5.
// Write a C program that outputs the smallest ‘i’ such that xi is even.
For example, 22 is the
// output for the input 25, 19, 22, 23, 21, 12, 10, 17, 11, 13, 10
#include <stdio.h>
int main(){
int i,n,arr[20];
printf("Enter the size of array: ");
scanf("%d",&n);
for (i=0;i<n;i++){
printf("Enter the %d element of array: ",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
if(arr[i]%2==0){
printf("%d",arr[i]);
break;
}
}
return 0;
}
//here i is index
//Xi is the value at index
6.
// Write a C program that outputs the smallest ‘i’ such that xi and xi+1
are both even. In above
// case 6. (Because 12 and 10 are even).
#include <stdio.h>
int main(){
int i,n,arr[20];
printf("Enter the size of array: ");
scanf("%d",&n);
for (i=0;i<n;i++){
printf("Enter the %d element of array: ",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
if(arr[i]%2==0&&arr[i+1]%2==0){
printf("%d",arr[i]);
break;
}
return 0;
}
//here i is index
//Xi is the value at index