0% found this document useful (0 votes)
2 views3 pages

Array Programming C Solutions

The document contains C programming exercises focused on array manipulation, including reading, printing, summing, and finding elements in arrays. Key exercises include printing negative elements, finding the maximum and minimum values, counting even and odd numbers, and merging two arrays. Each exercise is accompanied by a code snippet demonstrating the solution.

Uploaded by

devilsdome666
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)
2 views3 pages

Array Programming C Solutions

The document contains C programming exercises focused on array manipulation, including reading, printing, summing, and finding elements in arrays. Key exercises include printing negative elements, finding the maximum and minimum values, counting even and odd numbers, and merging two arrays. Each exercise is accompanied by a code snippet demonstrating the solution.

Uploaded by

devilsdome666
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

Array Programming Exercises – C Solutions

1. Read and print elements of array


#include <stdio.h>
int main() {
int a[100], n, i;
printf("Enter size: ");
scanf("%d", &n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}

2. Print all negative elements


#include <stdio.h>
int main() {
int a[100], n, i;
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&a[i]);
for(i=0;i<n;i++)
if(a[i]<0) printf("%d ",a[i]);
return 0;
}

3. Sum of all array elements


#include <stdio.h>
int main() {
int a[100], n, i, sum=0;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
sum+=a[i];
}
printf("Sum = %d",sum);
return 0;
}

4. Maximum and minimum element


#include <stdio.h>
int main() {
int a[100], n, i, max, min;
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&a[i]);
max=min=a[0];
for(i=1;i<n;i++) {
if(a[i]>max) max=a[i];
if(a[i]<min) min=a[i];
}
printf("Max=%d Min=%d",max,min);
return 0;
}

5. Second largest element


#include <stdio.h>
int main() {
int a[100], n, i, largest, second;
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&a[i]);
largest=second=a[0];
for(i=0;i<n;i++) {
if(a[i]>largest) {
second=largest;
largest=a[i];
} else if(a[i]>second && a[i]!=largest)
second=a[i];
}
printf("Second Largest = %d",second);
return 0;
}

6. Count even and odd


#include <stdio.h>
int main() {
int a[100], n, i, e=0, o=0;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
if(a[i]%2==0) e++;
else o++;
}
printf("Even=%d Odd=%d",e,o);
return 0;
}

7. Count negative elements


#include <stdio.h>
int main() {
int a[100], n, i, c=0;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
if(a[i]<0) c++;
}
printf("Negative = %d",c);
return 0;
}

8. Copy array elements


#include <stdio.h>
int main() {
int a[100], b[100], n, i;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
for(i=0;i<n;i++) printf("%d ",b[i]);
return 0;
}

9. Merge two arrays


#include <stdio.h>
int main() {
int a[50], b[50], c[100], n1, n2, i;
scanf("%d",&n1);
for(i=0;i<n1;i++) scanf("%d",&a[i]);
scanf("%d",&n2);
for(i=0;i<n2;i++) scanf("%d",&b[i]);
for(i=0;i<n1;i++) c[i]=a[i];
for(i=0;i<n2;i++) c[n1+i]=b[i];
for(i=0;i<n1+n2;i++) printf("%d ",c[i]);
return 0;
}
10. Reverse of an array
#include <stdio.h>
int main() {
int a[100], n, i;
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&a[i]);
for(i=n-1;i>=0;i--) printf("%d ",a[i]);
return 0;
}

11. Even and odd into separate arrays


#include <stdio.h>
int main() {
int a[100], e[100], o[100], n, i, ec=0, oc=0;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
if(a[i]%2==0) e[ec++]=a[i];
else o[oc++]=a[i];
}
printf("Even: ");
for(i=0;i<ec;i++) printf("%d ",e[i]);
printf("\nOdd: ");
for(i=0;i<oc;i++) printf("%d ",o[i]);
return 0;
}

12. Search an element


#include <stdio.h>
int main() {
int a[100], n, i, key, found=0;
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&a[i]);
scanf("%d",&key);
for(i=0;i<n;i++)
if(a[i]==key) { found=1; break; }
if(found) printf("Found");
else printf("Not Found");
return 0;
}

You might also like