0% found this document useful (0 votes)
172 views7 pages

C Program To Put Even & Odd Elements of An Array in 2 Separate Arrays

The document contains 9 code snippets of C programs that perform various tasks on arrays including: 1) Separating even and odd elements of an array into two separate arrays 2) Inserting an element into a specified position in an array 3) Deleting a specified integer from an array
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
172 views7 pages

C Program To Put Even & Odd Elements of An Array in 2 Separate Arrays

The document contains 9 code snippets of C programs that perform various tasks on arrays including: 1) Separating even and odd elements of an array into two separate arrays 2) Inserting an element into a specified position in an array 3) Deleting a specified integer from an array
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

1.

C Program to Put Even & Odd Elements of an Array in 2 Separate Arrays


#include <stdio.h>

Void main()

int a[10], b[10], c[10];

int i, j = 0, k = 0, n;

printf("Enter the size of array array");

scanf("%d", &n);

printf("Enter the elements of the array n");

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

scanf("%ld", &a[i]);

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

if (a[i] % 2 == 0)

b[j] = a[i];

j++;

else

c[k] = a[i];

k++;

printf("The elements of even array are:\n");

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

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


}

printf("The elements of odd array are:\n");

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

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

2. C program to insert an element in a specified position in the given array.


#include <stdio.h>

void main()

int a[10], b[10], c[10];

int i, pos,ele, n;

printf("Enter the size of array array");

scanf("%d", &n);

printf("Enter the elements of the array n");

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

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

printf("Enter the element and position to insert");

scanf("%d%d",&ele,&pos);

printf("The elements of array before insertion are:\n");

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

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

a[pos-1]=ele;

printf("The elements of array after insertion are:\n");

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


{

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

3.C program to delete specified integer from an array.


#include <stdio.h>
void main()
{
   int array[100], position, c, n;

   printf("Enter number of elements in array\n");


   scanf("%d", &n);

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

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to delete element\n");


   scanf("%d", &position);

   if (position >= n+1)
      printf("Deletion not possible.\n");
   else
   {
      for (c = position - 1; c < n - 1; c++)
         array[c] = array[c+1];

      printf("Resultant array:\n");

      for (c = 0; c < n - 1; c++)
         printf("%d\n", array[c]);
}
}

4. C Program to Find the Roots of a Quadratic Equation

#include <math.h>

#include <stdio.h>

void Quadratic_eq(float ,float ,float ,float);

void main() {

float a, b, c,dis;

printf("Enter coefficients a, b and c: ");

scanf("%f %f %f", &a, &b, &c);


dis = b * b - 4 * a * c;

Quadratic_eq(a,b,c,dis);

void Quadratic_eq(float a,float b,float c,float dis)

float root1,root2,realPart,imagPart;

if (dis > 0) {

root1 = (-b + sqrt(dis)) / (2 * a);

root2 = (-b - sqrt(dis)) / (2 * a);

printf("root1 = %f and root2 = %f", root1, root2);

else if (dis == 0) {

root1 = root2 = -b / (2 * a);

printf("root1 = root2 = %f;", root1);

else {

realPart = -b / (2 * a);

imagPart = sqrt(-dis) / (2 * a);

printf("root1 = %f+%fi and root2 = %.f-%.fi", realPart, imagPart,


realPart, imagPart);

6.C program to find first n fibonacci series using function.


#include <stdio.h>

void fibo(int ,int ,int );

void main()

int n, t1 = 0, t2 = 1;
printf("Enter the number of terms:\n");

scanf("%d", &n);

fibo(n,t1,t2);

void fibo(int n,int t1,int t2)

int i,nextTerm;

printf("Fibonacci Series:\n ");

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

printf("%d ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

7.C Program to Find the GCD and LCM of Two Integers using
function.

#include <stdio.h>

void Calculate(int ,int );

void main()

int num1, num2;

printf("Enter two numbers\n");

scanf("%d %d", &num1, &num2);

Calculate(num1,num2);

}
void Calculate(int num1,int num2)

int gcd, lcm, remainder, numerator, denominator;

if (num1 > num2)

numerator = num1;

denominator = num2;

else

numerator = num2;

denominator = num1;

remainder = numerator % denominator;

while (remainder != 0)

numerator = denominator;

denominator = remainder;

remainder = numerator % denominator;

gcd = denominator;

lcm = num1 * num2 / gcd;

printf("GCD of %d and %d = %d\n", num1, num2, gcd);

printf("LCM of %d and %d = %d\n", num1, num2, lcm);

9.C program to accept sorted array and perform binary search.


#include <stdio.h>

void main()
{

int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

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

scanf("%d", &array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search) {

printf("%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)

printf("Not found! %d isn't present in the list.\n", search);

You might also like