0% found this document useful (0 votes)
7 views37 pages

Array Operations in C Programming

The document contains various C programming codes written by Priya Bisht, including programs to find the largest element in an array, calculate the sum and average of array elements, reverse an array, count even and odd numbers, and implement linear and binary search. It also includes codes for printing patterns such as squares, diamonds, and Floyd's triangle, as well as functions for calculating factorial, checking for prime numbers, and swapping values. Each program is accompanied by sample input and output for clarity.
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)
7 views37 pages

Array Operations in C Programming

The document contains various C programming codes written by Priya Bisht, including programs to find the largest element in an array, calculate the sum and average of array elements, reverse an array, count even and odd numbers, and implement linear and binary search. It also includes codes for printing patterns such as squares, diamonds, and Floyd's triangle, as well as functions for calculating factorial, checking for prime numbers, and swapping values. Each program is accompanied by sample input and output for clarity.
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

/* PROGRAM CODE -To find the largest element in an array.

Name – Priya Bisht


Section – A

Roll No – 2594073
Branch – CSE */
#include <stdio.h>
void main() {
int n, i;
int arr[100], largest;
printf("Enter the number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
for(i = 1; i < n; i++)
{ if(arr[i] > largest) {
largest = arr[i];
}
}
printf("The largest element in the array is: %d\n", largest);
}
return 0;

/*Output
Enter the number pf elements : 8
Enter 8 elements :

14 5 2 6 7 8 56 67 88 99 22
The largest element in a array is : 99 */
/* PROGRAM CODE - calculate sum and average of array element.
Name – Priya Bisht
Section – A

Roll No – 2594073
Branch – CSE */

#include <stdio.h>

void main() {

int n, i;

int arr[100];

float sum= 0, avg;

printf("Enter the number of elements: ");

scanf("%d", &n);

printf("Enter %d elements:\n", n);

for(i = 0; i < n; i++)

{ scanf("%d", &arr[i]);

sum += arr[i];

avg = sum / n;

printf("Sum of array elements = %f\n", sum);

printf("Average of array elements = %f\n", avg);

return 0;

/*Output
Enter 5 element :
7 12 1 3 15
Sum of array elements = 38.000000

Average of array elements = 7.60000000 */


/* PROGRAM CODE - Reverse an array and also find whether number is palindrome or not.
Name – Priya Bisht
Section – A
Roll No – 2594073

Branch – CSE */
#include <stdio.h>
void main()
{ int n, i, num, original, reversed = 0;
int arr[100], revArr[100];
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Reversing array
for(i = 0; i < n; i++)
{ revArr[i] = arr[n - i - 1];
}

printf("Reversed array:\n"); for(i = 0; i


< n; i++) {
printf("%d ", revArr[i]);
}
printf("\n");

// --- Palindrome Check Part ---


printf("Enter a number to check palindrome: ");
scanf("%d", &num);

original = num; while(num > 0) { reversed =


reversed * 10 + (num % 10); num = num / 10;
}

if(original == reversed)
printf("%d is a palindrome number.\n", original);
else
printf("%d is not a palindrome number.\n", original);
}
return 0;
}
/*Output

Enter the number of elements in the array : 6


Enter 6 elements :
7 12 7 1 3 13
Reversed array :
13 3 1 7 12 7

Enter a number to check palindrome : 3


3 is a palindrome number. */
/* PROGRAM CODE – count even and odd numbers in an array.
Name – Priya Bisht
Section – A
Roll No – 2594073

Branch – CSE */

#include <stdio.h>

void main() {
int n, i; int arr[100];
int evenCount = 0, oddCount = 0;

printf("Enter the number of elements: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n); for(i = 0; i <


n; i++) { scanf("%d", &arr[i]);
}

for(i = 0; i < n; i++)


{ if(arr[i] % 2 == 0)
evenCount++;
else
oddCount++;
}

printf("Total even numbers: %d\n", evenCount);


printf("Total odd numbers: %d\n", oddCount);
}

return 0;
}
/*Output
Enter the number of elements : 7
Enter 7 elements :
8 12 1 11 5 3 2
Total even numbers : 3
Total odd numbers : 4 */
/* PROGRAM CODE – Search an element in an array (Linear Search) .
Name – Priya Bisht

Section – A

Roll No – 2594073

Branch – CSE */

#include <stdio.h>

// Function to perform linear search

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

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

if (arr[i] == target) {

return i; // Return index if element is found

return -1; // Return -1 if element is not found

int main() {

int arr[] = {10, 23, 45, 70, 11, 15, 36, 48};

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

int target;

printf("Array elements: ");

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

printf("%d ", arr[i]);

printf("\n");

printf("Enter element to search: ");

scanf("%d", &target);

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

if (result != -1) {

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

} else {

printf("Element %d not found in the array\n", target);

return 0;

}
/*Output
Array elements: 10 23 45 70 11 15 36 48
Enter element to search: 11
Element 11 found at index 4 * /
/* PROGRAM CODE –Search an element in an array (Binary Search )- Iterative Search.
Name – Priya Bisht

Section – A

Roll No – 2594073

Branch – CSE */

#include <stdio.h>

// Function to perform iterative binary search

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

int left = 0;

int right = n - 1;

while (left <= right) {

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

// Check if target is at mid

if (arr[mid] == target) {

return mid;

// If target is greater, ignore left half

if (arr[mid] < target) {

left = mid + 1;

// If target is smaller, ignore right half

else {

right = mid - 1;

return -1; // Element not found

int main() {

// Binary search requires a sorted array


int arr[] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 67, 78};

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

int target;

printf("Sorted array elements: ");

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

printf("%d ", arr[i]);

printf("\n");

printf("Enter element to search: ");

scanf("%d", &target);

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

if (result != -1) {

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

} else {

printf("Element %d not found in the array\n", target);

return 0;

/*Output

Sorted array elements: 2 5 8 12 16 23 38 45 56 67 78

Enter element to search: 38

Element 38 found at index 6 */


/* PROGRAM CODE - Print a Hallow square pattern.

Name – Priya Bisht


Section – A
Roll No – 2594073
Branch – CSE */
#include <stdio.h>

void main()

int n, i, j;

printf("Enter the size of the square: ");

scanf("%d", &n);

for(i = 1; i <= n; i++)

{ for(j = 1; j <= n; j++)

// Print * for boundary positions, space otherwise

if(i == 1 || i == n || j == 1 || j == n)

printf("* ");

else

printf(" ");

printf("\n");

return 0;
}
/*Output
Enter the size of square : 5
* * * * *
* *
* *
* *
* * * * * */
/* PROGRAM CODE – Print a diamond pattern.
Name – Priya Bisht
Section – A
Roll No – 2594073
Branch – CSE */

#include <stdio.h>
void main()
{
int n, i, j, space;

printf("Enter the number of rows: ");


scanf("%d", &n);

for(i = 1; i <= n; i++)


{ for(space = i; space < n; space++)
printf(" ");
for(j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}
}
return 0;
}
/*Output

Enter the number of rows : 5


*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
* */
/* PROGRAM CODE – Print a pyramid pattern.
Name – Priya Bisht
Section – A
Roll No – 2594073

Branch – CSE */

#include <stdio.h>

void main()
{
int n, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &n);

for(i = 1; i <= 1; i++)


{ for(space = i; space <n; space++)
printf(" ");
for(j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}
}
return 0;
}
/*Output
Enter the number of the rows : 5
*
* * *

* * * * *
* * * * * * *
* * * * * * ** * */
/* PROGRAM CODE - Print a inverted pyramid pattern.
Name – Priya Bisht
Section – A

Roll No – 2594073
Branch – CSE */

#include <stdio.h>

void main()
{
int n, i, j, space;
printf("Enter the number of rows: "); scanf("%d",
&n);

for(i = n; i >= 1; i--)


{ for(space = 0; space < n - i; space++) printf("
");
for(j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}
}
return 0;
}

/*Output
Enter the number of rows : 5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
* */
/* PROGRAM CODE – Print Floyd’s Triangle.
Name – Priya Bisht
Section – A
Roll No – 2594073
Branch – CSE */
//Floyd's triangle

#include<stdio.h>

int main()

int n,r=1,val=1,j;

printf("Enter the numbers of rows in the triangle\t");

scanf("%d",&n);

while(r<=n)

for(j=1;j<=r;j++)

printf("%d\t",val++);

printf("\n");

r++;

return 0;

/*Output
1
2 3
4 5 6
7 8 9 10 */
/* PROGRAM CODE - Print Square Star Pattern.
Name – Priya Bisht
Section – A
Roll No – 2594073
Branch – CSE */

#include <stdio.h>

void main()
{
int n, i, j;
printf("Enter the size of the square: ");
scanf("%d", &n);

for(i = 0; i < n; i++)


{ for(j = 0; j < n; j++)
{ printf("* ");
}
printf("\n");
}
}
return 0;
}
/*Output
Enter the size of the square : 5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * * */
/* PROGRAM CODE - Write a function to find the sum of two
numbers.
Name – Priya Bisht
Section – A
Roll No – 2594073
Branch – CSE */

#include <stdio.h>

int sum(int a, int b)


{ return a + b;
}

void main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d%d", &x, &y);
printf("Sum = %d\n", sum(x, y));
}

/*Output
Enter two numbers : 45 45
Sum = 90 */
/* PROGRAM CODE – Write a function to calculate the factorial of a number.

Name – Priya Bisht


Section – A
Roll No – 2594073

Branch – CSE */

#include<stdio.h>
int fact(int);
void main()
{
int no,factorial;
printf("Enter the number\t");
scanf("%d",&no);
factorial = fact(no);
printf("factorial of %d is %d",no,factorial);
}
int fact(int no)
{
if(no==1)
return 1;
else
return no*fact(no-1);
}

/*output
Enter the number 6
factorial of 6 is 720 */
/* PROGRAM CODE – Write a function to check whether a number is prime or not.

Name – Priya Bisht


Section – A
Roll No – 2594073

Branch – CSE */

#include <stdio.h>

int isPrime(int n)
{ if(n <= 1) return 0;
for(int i = 2; i <= n/2; i++)
if(n % i == 0)
return 0;
return 1;
}

void main()
{ int num;
printf("Enter a number: ");
scanf("%d", &num);
if(isPrime(num))
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
}

/*Output
Enter the number : 12
12 is not a prime number */
/* PROGRAM CODE – Write a function to find the maximum of
three number.
Name – Priya Bisht

Section – A
Roll No – 2594073
Branch – CSE */
#include <stdio.h>

int maxOfThree(int a, int b, int c)


{ if(a >= b&& a >= c
return a;
else if(b >= a && b >= c)
return b;
else
return c;
}

void main() {
int x, y, z;
printf("\nEnter three numbers: ");
scanf("%d%d%d", &x, &y, &z);
printf("Maximum = %d\n", maxOfThree(x, y, z));
}

/*Output
Enter three numbers : 12 2 8
Maximum = 12 */
/* PROGRAM CODE – Write a function to swap two numbers using call by reference.

Name – Priya Bisht


Section – A
Roll No – 2594073

Branch – CSE */

#include <stdio.h>

void swap(int *a, int *b)


{ int temp = *a;
*a = *b;
*b = temp;
}

void main() {
int x, y;
printf("\nEnter two numbers: ");
scanf("%d%d", &x, &y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
}

/*Output
Enter two numbers : 12 8
After swapping : x=8 , y =12 */
/* PROGRAM CODE – Write a function to calculate the sum and average of array element.

Name – Priya Bisht


Section – A
Roll No – 2594073

Branch – CSE */
#include <stdio.h>

void sumAvg(int arr[], int n)

{ int sum = 0;

float avg;

for(int i = 0; i < n; i++)

sum += arr[i];

avg = (float)sum / n;

printf("\nSum = %d, Average = %.2f\n", sum, avg);

void main()

{ int n, arr[100];

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter %d elements:\n", n);

for(int i = 0; i < n; i++)

scanf("%d", &arr[i]);

sumAvg(arr, n);

/*Output
Enter the number of elements : 4

Entre 4 elements :
12 3 7 2
Sum = 24, Average = 6.0000 */
/* PROGRAM CODE – Write a function to find the GCD(Greatest Common Divisor) of two
numbers.

Name – Priya Bisht


Section – A

Roll No – 2594073
Branch – CSE */
#include <stdio.h>

int gcd(int a, int b)

{ while(b != 0)

{ int temp = b;

b = a % b;

a = temp;

} return a;

void main() {

int x, y;

printf("\nEnter two numbers: ");

scanf("%d%d", &x, &y);

printf("GCD = %d\n", gcd(x, y));

return 0;

}
/*Output
Enter two numbers :12 6
GCD = 6 */
/* PROGRAM CODE – Write a function to check whether a number is a palindrome.

Name – Priya Bisht


Section – A
Roll No – 2594073

Branch – CSE */

#include <stdio.h>

// Function to check if a number is palindrome


int isPalindrome(int num) {
int original = num;
int reversed = 0;
int remainder;

// Handle negative numbers (not palindromes)


if (num < 0) {
return 0;
}

// Reverse the number


while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num = num / 10;
}

// Check if original equals reversed


if (original == reversed) {
return 1; // Palindrome
} else {
return 0; // Not a palindrome
}
}

int main()
{
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (isPalindrome(num)) {
printf("%d is a palindrome number.\n", num);
} else {
printf("%d is not a palindrome number.\n", num);
}
}

/*Output
Enter a number: 121
121 is a palindrome number.*/
/* PROGRAM CODE – Write a function to display Fibonacci series.
Name – Priya Bisht
Section – A
Roll No – 2594073
Branch – CSE */
#include <stdio.h>
void displayFibonacci(int n)
{ if (n <= 0)
{ printf("Please enter a positive integer.\n”)
return;
} int a = 0, b = 1, next;
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++)
{ printf("%d ", a);
next = a + b; a = b; b = next;
} printf("\n");
} int main()
{ int count;
printf("\nEnter the number of terms: ");
if (scanf("%d", &count) != 1)
{ printf("Invalid input.\n
return 1;
}

displayFibonaci(count);

return 0;

}
/*Output
Enter the number of terms :7
Fibonacci series : 0 1 1 2 3 5 8 */
/* PROGRAM CODE – Write a function to find the power (x^n) of a number using a loop.

Name – Priya Bisht


Section – A
Roll No – 2594073

Branch – CSE */

#include <stdio.h>

// Function to calculate x^n using a loop


long power(int x, int n) {
long result = 1;

// Handle negative exponent


if (n < 0) {
printf("Negative exponents not supported in this function\n");
return 0;
}

// Multiply x, n times
for (int i = 0; i < n; i++) {
result = result * x;
}

return result;
}
int main() {
int base, exponent;

printf("Enter base (x): ");


scanf("%d", &base);

printf("Enter exponent (n): ");


scanf("%d", &exponent);

long result = power(base, exponent);

if (exponent >= 0) {
printf("%d^%d = %lld\n", base, exponent, result);
}

/*Output
Enter base (x): 2
Enter exponent (n): 2
2^2 = 4
/* PROGRAM CODE – Recursive number operations (sum,reverse,factorial).

Name – Priya Bisht


Section – A
Roll No – 2594073

Branch – CSE */

#include <stdio.h>

int sumDigits(int n)
{ if (n == 0) return 0;
return n % 10 + sumDigits(n / 10);
}
int reverse(int n, int rev)
{ if (n == 0) return rev
return reverse(n / 10, rev * 10 + n % 10);
}
long factorial(int n
{ if (n == 0 || n == 1) return 1
return (long long)n * factorial(n - 1);
}
int main()

{ int num;
printf("\nEnter a number: ");
scanf("%d", &num);
printf("Sum of digits = %d\n", sumDigits(num));

printf("Reversed number = %d\n", reverse(num, 0));


printf("Factorial = %lld\n", factorial(num));
}
/*Output
Enter a number : 13
Sum of digits = 4
Reverse of digit = 31
Factorial = 6227020800 */
/* PROGRAM CODE – Pascal’s Triangle using recursion.

Name – Priya Bisht

Section – A
Roll No – 2594073
Branch – CSE */

#include <stdio.h>

int factorial(int n)

{ if(n == 0 || n == 1) return 1;

return n * factorial(n - 1);

int combination(int n, int r)

{ return factorial(n) / (factorial(r) * factorial(n - r));

void pascalTriangle(int rows)

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

{ for(int space = 0; space < rows - i - 1; space++)

printf(" ");

for(int j = 0; j <= i; j++)

printf("%d ", combination(i, j));

printf("\n");

void main()

{ int rows;

printf("\nEnter number of rows: ");

scanf("%d", &rows);

pascalTriangle(rows)

}
/*Output
Enter number of rows : 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 */
/* PROGRAM CODE – Tower of Hanoi.
Name – Priya Bisht
Section – A
Roll No – 2594073
Branch – CSE */

#include <stdio.h>

void towersOfHanoi(int n, char from, char to, char


aux)
{ if(n == 1)
{ printf("Move disk 1 from %c to %c\n", from,
to);
return;
}
towersOfHanoi(n - 1, from, aux, to);
printf("Move disk %d from %c to %c\n", n, from,
to);
towersOfHanoi(n - 1, aux, to, from);
}

void main() {
int n;
printf("\nEnter number of disks: ");
scanf("%d", &n);
towersOfHanoi(n, 'A', 'C', 'B');
}
/*Output
Entre the number of disks :
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to B
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C */

You might also like