0% found this document useful (0 votes)
16 views2 pages

C Programming Array Operations Guide

The document contains six C programming code snippets demonstrating various array operations and functions. These include array traversal and linear search, finding maximum and minimum values, reversing an array in-place, counting even and odd numbers, checking for prime numbers, and a simple addition function with multiple calls. Each code snippet is self-contained and illustrates a specific programming concept.
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)
16 views2 pages

C Programming Array Operations Guide

The document contains six C programming code snippets demonstrating various array operations and functions. These include array traversal and linear search, finding maximum and minimum values, reversing an array in-place, counting even and odd numbers, checking for prime numbers, and a simple addition function with multiple calls. Each code snippet is self-contained and illustrates a specific programming concept.
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

Q1.

Array Traversal & Linear Search


-----------------------------------
#include <stdio.h>
int main() {
int N, i, key, found = 0;
printf("Enter N: ");
scanf("%d", &N);
int arr[N];
printf("Enter %d integers:\n", N);
for (i = 0; i < N; i++) scanf("%d", &arr[i]);
printf("Array elements:\n");
for (i = 0; i < N; i++) printf("%d ", arr[i]);
printf("\nEnter value to search: ");
scanf("%d", &key);
for (i = 0; i < N; i++) {
if (arr[i] == key) { printf("Found at index %d\n", i); found = 1; break; }
}
if (!found) printf("Not Found\n");
return 0;
}

Q2. Maximum & Minimum


----------------------
#include <stdio.h>
int main() {
int arr[10], i, max, min, maxPos, minPos;
for (i = 0; i < 10; i++) scanf("%d", &arr[i]);
max = min = arr[0];
maxPos = minPos = 0;
for (i = 1; i < 10; i++) {
if (arr[i] > max) { max = arr[i]; maxPos = i; }
if (arr[i] < min) { min = arr[i]; minPos = i; }
}
printf("Largest = %d at %d\n", max, maxPos);
printf("Smallest = %d at %d\n", min, minPos);
return 0;
}

Q3. Reverse Array (In-place)


-----------------------------
#include <stdio.h>
int main() {
int n, i, temp;
scanf("%d", &n);
int arr[n];
for (i = 0; i < n; i++) scanf("%d", &arr[i]);
for (i = 0; i < n/2; i++) {
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
for (i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

Q4. Count Even & Odd


----------------------
#include <stdio.h>
int main() {
int N, i, even=0, odd=0;
scanf("%d", &N);
int arr[N];
for (i = 0; i < N; i++) {
scanf("%d", &arr[i]);
if (arr[i] % 2 == 0) even++; else odd++;
}
printf("Even = %d, Odd = %d", even, odd);
return 0;
}

Q5. Prime Check Function


-------------------------
#include <stdio.h>
int isPrime(int n){
if(n<=1) return 0;
for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
return 1;
}
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d is %s\n",a,isPrime(a)?"Prime":"Not Prime");
printf("%d is %s\n",b,isPrime(b)?"Prime":"Not Prime");
printf("%d is %s\n",c,isPrime(c)?"Prime":"Not Prime");
return 0;
}

Q6. Add Function (5 calls)


---------------------------
#include <stdio.h>
int add(int a,int b){ return a+b; }
int main(){
printf("%d\n", add(5,10));
printf("%d\n", add(20,7));
printf("%d\n", add(12,18));
printf("%d\n", add(100,50));
printf("%d\n", add(9,3));
return 0;
}

You might also like