Professional Documents
Culture Documents
/*
Basic salary of an employee is input through the keyboard. The DA
is 25% of the basic salary while the HRA is 15% of the basic
salary. Provident Fund is deducted at the rate of 10% of the gross
salary(BS+DA+HRA).
Program to Calculate the Net Salary.
*/
#include <stdio.h>
main()
{
float basic_sal, da, hra, pf, gross_sal, net_sal;
printf("\nEnter basic salary of the employee: Rs. ");
scanf("%f", &basic_sal);
da = (basic_sal * 25)/100;
hra = (basic_sal * 15)/100;
gross_sal = basic_sal + da + hra;
pf = (gross_sal * 10)/100;
net_sal = gross_sal - pf;
printf("\n\nNet Salary: Rs. %.2f", net_sal);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
/*
Program to Convert Temperature from Degree Centigrade to Fahrenheit
f = (1.8*c) + 32
*/
#include <stdio.h>
main()
{
float c, f;
printf("\nEnter temperature in degree Centigrade: ");
scanf("%f", &c);
f = (1.8*c) + 32;
printf("\n\nTemperature in degree Fahrenheit: %.2f", f);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
/*
Program to Convert Time in Seconds to Hours, Minutes and Seconds
*/
#include <stdio.h>
main()
{
long sec, hr, min, t;
printf("\nEnter time in seconds: ");
scanf("%ld", &sec);
hr = sec/3600;
t = sec%3600;
min = t/60;
sec = t%60;
printf("\n\nTime is %ld hrs %ld mins %ld secs", hr, min, sec);
getch();
}
#include <stdio.h>
#define PI 3.142
main()
{
float len, r, area, circum;
printf("\nEnter length of a square: ");
scanf("%f", &len);
area = len * len;
printf("\nEnter radius of a circle: ");
scanf("%f", &r);
circum = 2 * PI * r;
printf("\nArea of square = %.2f", area);
printf("\nCircumference of circle = %.2f", circum);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
/*
Program to find Simple Interest and Compound Interest
SI = (p * r * t) / 100
CI = p * pow((1 + r/100), t) - p
*/
#include <stdio.h>
#include <math.h>
main()
{
float p, r, t, si, ci;
printf("\nEnter priciple, rate and time: ");
scanf("%f %f %f", &p, &r, &t);
si = (p * r * t) / 100;
ci = p * pow((1 + r/100), t) - p;
printf("\n\nSimple Interest: %.2f", si);
printf("\n\nCompound Interest: %.2f", ci);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
/*
Program to find Sphere Surface Area and Volume of a Sphere
Sphere Surface Area = 4 * PI * r * r
Volume of Sphere = (4/3) * PI * r * r * r
*/
#include <stdio.h>
#define PI 3.142
main()
{
float r, area, vol;
printf("\nEnter radius of Sphere: ");
scanf("%f", &r);
area = 4 * PI * r * r;
vol = (4/3) * PI * r * r * r;
printf("\nSphere Surface Area = %.2f", area);
printf("\nVolume of Sphere = %.2f", vol);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
/**** Program to Find Sum and Average of Three Real Numbers ****/
#include <stdio.h>
main()
{
float a, b, c, sum, avg;
printf("\nEnter value of three numbers: ");
scanf("%f %f %f", &a, &b, &c);
sum = a + b + c;
avg = sum / 3;
printf("\nSum = %.2f", sum);
printf("\nAverage = %.2f", avg);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
/*
Program to Swap Values of Two Variables using Third Variable
*/
#include <stdio.h>
main()
{
int a, b, temp;
printf("\nEnter any two numbers: ");
scanf("%d %d", &a, &b);
printf("\n\nBefore Swapping:\n");
printf("\na = %d\n", a);
printf("\nb = %d\n", b);
temp = a;
a = b;
b = temp;
printf("\n\nAfter Swapping:\n");
printf("\na = %d\n", a);
printf("\nb = %d\n", b);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
/*
Program to Swap Values of Two Variables Without using 3rd Variable
*/
#include <stdio.h>
main()
{
int a, b, temp;
printf("\nEnter any two numbers: ");
scanf("%d %d", &a, &b);
printf("\n\nBefore Swapping:\n");
printf("\na = %d\n", a);
printf("\nb = %d\n", b);
a = a + b;
b = a - b;
a = a - b;
printf("\n\nAfter Swapping:\n");
printf("\na = %d\n", a);
printf("\nb = %d\n", b);
getch();
}
--------------------------------------------
Control statement
Program to Check Whether a Character is a Vowel or not by using
switch Statement
*/
#include <stdio.h>
main()
{
char ch;
printf("\nEnter any character: ");
scanf("%c", &ch);
switch (ch)
{
case 'a':
case 'A':
printf("\n\n%c is a vowel", ch);
break;
case 'e':
case 'E':
printf("\n\n%c is a vowel", ch);
break;
case 'i':
case 'I':
printf("\n\n%c is a vowel", ch);
break;
www.w3professors.com Gursharan Singh Tatla Page No. 2
case 'o':
case 'O':
printf("\n\n%c is a vowel", ch);
break;
case 'u':
case 'U':
printf("\n\n%c is a vowel", ch);
break;
default:
printf("\n\n%c is not a vowel", ch);
}
getch();
}
-------------------------------------------Program to Check Whether the Given Number is an Armstrong Number
*/
#include <stdio.h>
main()
{
int n, temp, d, arm=0;
printf("\nEnter any number: ");
scanf("%d", &n);
temp = n;
while (temp > 0)
{
d = temp%10;
temp = temp/10;
arm = arm + (d*d*d);
}
if (arm == n)
printf("\n\n%d is an Armstrong number\n", n);
else
printf("\n\n%d is not an Armstrong number\n", n);
getch();
}
#include <stdio.h>
#include <string.h>
main()
{
char str[20];
int i=0, word=0, chr=0;
printf("\nEnter any string: ");
gets(str);
while (str[i] != '\0')
{
if (str[i] == ' ')
{
word++;
chr++;
}
else
chr++;
i++;
}
printf("\nNumber of characters: %d", chr);
printf("\nNumber of words: %d", word+1);
getch();
}
-------------------------------------------Program to Find Factorial of a Number without using Recursion */
#include <stdio.h>
main()
{
int n, i;
long fact=1;
printf("\nEnter any number: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
fact = fact*i;
printf("\nFactorial = %ld", fact);
getch();
}
-------------------------------------------Program to Find HCF of Two Numbers using Recursion */
#include <stdio.h>
int hcf(int, int);
main()
{
int h, i, a, b;
printf("\nEnter values of two numbers: ");
q = 1;
while (q != 0)
{
q = m % n;
if (q != 0)
{
m = n;
n = q;
}
}
printf("\nHCF of number is: %d", n);
end:
getch();
}
-------------------------------------------Program to Find Largest of Three Numbers ***/
#include <stdio.h>
main()
{
int a, b, c;
printf("\nEnter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a>b && a>c)
printf("\n\n%d is greater", a);
else if (b>a && b>c)
printf("\n\n%d is greater", b);
else
printf("\n\n%d is greater", c);
getch();
}
-------------------------------------------Program to Find the Sum of Digits of a Positive Integer */
#include <stdio.h>
main()
{
long n;
int digit, sum = 0;
printf("\nEnter any number: ");
scanf("%d", &n);
while (n > 0)
{
digit = n%10;
n = n/10;
sum = sum + digit;
}
printf("\n\nSum of digits: %d", sum);
getch();
}
-------------------------------------------Program to Find the Sum of Even and Odd Numbers from First 100
Positive Integers
*/
#include <stdio.h>
main()
{
int i, sumEven=0, sumOdd=0;
for (i=0; i<=100; i++)
if (i%2 == 0)
sumEven = sumEven + i;
else
sumOdd = sumOdd + i;
printf("\nSum of first even 100 numbers: %d\n", sumEven);
printf("\nSum of first odd 100 numbers: %d\n", sumOdd);
getch();
}
-------------------------------------------Program to Find the Sum of First 100 Positive Integers */
#include <stdio.h>
main()
{
int i, sum=0;
printf("\n\tSum of first 100 positive numbers\n");
for(i=0; i<=100; i++)
sum = sum + i;
printf("\nSum = %d", sum);
getch();
}
m = m * (i - 1);
www.w3professors.com Gursharan Singh Tatla Page No. 2
if (i <= pwr)
goto ab;
printf("\n\nSum = %f", sum);
getch();
}
}
----------------------------------------------------------
if (j <= k)
printf("%d", j);
else
printf("%d", k);
}
putchar('\n');
}
getch();
}
----------------------------------------------------------
String
www.w3professors.com Gursharan Singh Tatla Page No. 1
-----------------------------------------
#include <string.h>
main()
{
char string1[30], string2[20];
int i, length=0, temp;
printf("Enter the Value of String1: \n");
gets(string1);
printf("\nEnter the Value of String2: \n");
gets(string2);
for(i=0; string1[i]!='\0'; i++)
length++;
temp = length;
for(i=0; string2[i]!='\0'; i++)
{
string1[temp] = string2[i];
temp++;
}
string1[temp] = '\0';
printf("\nThe concatenated string is:\n");
puts(string1);
getch();
}
---------------------------------------------------------www.w3professors.com Neeru Babber Page No. 1
#include <stdio.h>
#include <string.h>
main()
{
char s1[20], s2[20];
printf("\nEnter string into s1: ");
gets(s1);
strcpy(s2, s1);
printf("\ns2: %s", s2);
getch();
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
#include <string.h>
main()
{
char s1[20], s2[20];
int result;
printf("\nEnter any string: ");
gets(s1);
strcpy(s2, s1);
strrev(s2);
result = strcmp(s1, s2);
if(result == 0)
printf("\nIt is a palindrome string");
else
printf("\nIt is not a palindrome string");
getch();
}
else
printf("\nString is not palindrome");
getch();
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
scanf("%s", name);
printf("\nName: %s", name);
getch();
}
---------------------------------------------------------------------------------
Function
Program to Show Call by Reference ****/
#include <stdio.h>
swap (int *, int *);
main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
getch();
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
----------------------------------------------------------
b = temp;
}
-----------------------------------------------------
Way-1
Formal parameters as a pointer as follows. You will study what is pointer in next chapter.
void myFunction(int *param)
{...}
Way-2
Way-3
Formal parameters as an unsized array as follows:
void myFunction(int param[])
{...}
Example
Now, consider the following function, which will take an array as an arg ument along
with another arg ument and based on the passed arg uments, it will return averag e of
the numbers passed throug h the array as follows:
double getAverage(int arr[], int size)
{
int i;
double avg;
double sum;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
avg = sum / size;
return avg;
}
When the above code is compiled tog ether and executed, it produces the following
result:
Average value is: 214.400000
As you can see, the leng th of the array doesn't matter as far as the function is concerned
because C performs no bounds checking for the formal parameters.
----------------------------------------------------------
Recursion
/** Program to Find Factorial of a Number using Recursion **/
#include <stdio.h>
long fact(int);
main()
{
int n;
long f;
printf("\nEnter number to find factorial: ");
scanf("%d", &n);
f = fact(n);
printf("\nFactorial: %ld", f);
getch();
}
long fact(int n)
{
int m;
if (n == 1)
return n;
else
{
m = n * fact(n-1);
return m;
}
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
}
fibbo(int x, int y, int n, int i)
{
int z;
if (i <= n)
{
z = x + y;
printf("\t%d", z);
x = y;
y = z;
i++;
fibbo(x,y,n,i);
}
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1
Array
www.w3professors.com Gursharan Singh Tatla Page No. 1
Sort
INSERTION SORT
/**** Program to Sort an Array using Insertion Sort ****/
#include <stdio.h>
void insertion_sort();
int a[50],n;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
insertion_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void insertion_sort()
{
int j, k, temp;
for(j=1; j<n; j++)
{
temp = a[j];
k = j-1;
while (k>=0 && a[k]>temp)
{
a[k+1] = a[k];
k--;
}
a[k+1] = temp;
}
}
----------------------------------------------------------
BUBBLE SORT
/***** Program to Sort an Array using Bubble Sort *****/
#include <stdio.h>
void bubble_sort();
int a[50], n;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
bubble_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void bubble_sort()
{
int j, k, temp;
for(j=0; j<n; j++)
for(k=0; k<(n-1)-j; k++)
if(a[k] > a[k+1])
{
temp = a[k];
a[k] = a[k+1];
a[k+1] = temp;
}
}
----------------------------------------------------------
MERGE SORT
}
else
{
a[k] = b[j];
j++; k++;
}
}
if (i <= mid)
{
while (i <= mid)
{
a[k] = b[i];
i++; k++;
}
}
else
{
while (j <= end)
{
a[k] = b[j];
j++; k++;
}
}
}
----------------------------------------------------------
QUICK SORT
SELECTION SORT
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
------------------------------------------------------------------------------------------------------------------Program to Add Two Matrices
#include <stdio.h>
main()
{
int a[10][10], b[10][10], c[10][10], i, j, row, col;
printf("\nEnter number of rows and columns: ");
scanf("%d %d", &row, &col);
printf("\t%d", a[i][j]);
printf("\n\n");
}
printf("\nElements of Matrix B:\n\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
printf("\t%d", b[i][j]);
printf("\n\n");
}
for (i=0; i<row; i++)
for (j=0; j<col; j++)
c[i][j] = a[i][j] + b[i][j];
printf("\nMatrix Addition is:\n\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
printf("\t%d", c[i][j]);
printf("\n");
}
getch();
}
-------------------------------------------Program to Find Smallest Among N Numbers ***/
#include <stdio.h>
main()
{
int a[10], i, small;
printf("\nEnter elements of an array:\n");
for (i=0; i<=9; i++)
scanf("%d", &a[i]);
small = a[0];
for (i=0; i<=9; i++)
if (a[i] < small)
small = a[i];
printf("\nSmallest number is %d", small);
getch();
}
-------------------------------------------Program to Illustrate the Concept of Passing 1-D Array to Function
Program to Find Largest from an Array
*/
#include <stdio.h>
#define SIZE 50
int big(int [], int);
main()
{
int a[SIZE], n, i, b;
printf("\nEnter size of array: ");
scanf("%d", &n);
printf("\nEnter elements:\n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
b = big(a, n);
printf("\nLargest number: %d", b);
getch();
}
int big(int a[], int n)
{
int b, i;
b = a[0];
www.w3professors.com Gursharan Singh Tatla Page No. 2
}
printf("\n\nArray B:\n\n");
for(i=0; i<x; i++)
{
for(j=0; j<y; j++)
printf("\t%d", b[i][j]);
printf("\n\n");
}
multiply();
printf("\n\nArray after multiplication:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<y; j++)
printf("\t%d", c[i][j]);
printf("\n\n");
}
getch();
}
void multiply()
{
int i, j, k;
if(m != y || n != x)
{
printf("\n\nMultiplication is not possible.");
exit();
}
else
for(i=0; i<n; i++)
for(j=0; j<x; j++)
{
c[i][j] = 0;
for(k=0; k<y; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
---------------------------------------------Program to multiply two matrices. The order and the elements of the
two matrices will be entered by the user as input to the program
and if multiplication is not possible then it should be reported to
the user
#include <stdio.h>
main()
{
int a[10][10], b[10][10], c[10][10], i, j, k, r1, r2, c1, c2;
back:
}
getch();
}
-------------------------------------------Program to Print Transpose of a Matrix ***/
#include <stdio.h>
main()
{
int a[10][10], i, j, row, col;
printf("\nEnter no. of rows & columns: ");
scanf("%d %d", &row, &col);
printf("\nEnter elements of Matrix:\n");
for (i=0; i<row; i++)
for (j=0; j<col; j++)
scanf("%d", &a[i][j]);
printf("\n\nElements of Matrix:\n\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
printf("\n\nTranspose of Matrix:\n\n");
for (i=0; i<col; i++)
{
for (j=0; j<row; j++)
printf("\t%d", a[j][i]);
printf("\n\n");
}
getch();
}
-------------------------------------------Program to Search an Element in the Array using Linear Search */
#include <stdio.h>
main()
{
int a[10], i, item;
printf("\nEnter elements of an array:\n");
for (i=0; i<=9; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
for (i=0; i<=9; i++)
if (item == a[i])
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
--------------------------------------------
TRAVERSE
/**** Program to Traverse an Array ****/
#include <stdio.h>
void traverse();
int a[50], n;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
traverse();
getch();
}
void traverse()
{
int i;
printf("\n\nElements of an array are:\n\n");
for(i=0; i<n; i++)
printf("%d\n", a[i]);
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
TRAVERSE
/**** Program to Traverse a 2-D Array ****/
#include <stdio.h>
void traverse();
int a[50][50], m, n;
main()
{
int i, j;
printf("\nEnter number of rows & cols: ");
scanf("%d%d", &m, &n);
printf("\nEnter elements of 2-D array:\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
printf("\n\n2-D array before traversing:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
traverse();
printf("\n\n2-D array after traversing:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
getch();
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2
void traverse()
{
int i, j;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
a[i][j] = a[i][j] * 2;
}
--------------------------------------------
TRANSPOSE
/**** Program to Transpose a 2-D Array ****/
#include <stdio.h>
void transpose();
int a[10][10], b[10][10], m, n;
main()
{
int i, j;
printf("\nEnter number of rows & cols: ");
scanf("%d%d", &m, &n);
printf("\nEnter elements of 2-D array:\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
printf("\n\n2-D array before transposing:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
transpose();
printf("\n\n2-D array after transposing:\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
printf("\t%d", b[i][j]);
printf("\n\n");
}
getch();
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2
void transpose()
{
int i, j;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
b[j][i] = a[i][j];
}
--------------------------------------------
MERGE SORTED
i++;
}
else
{
c[k] = b[j];
j++;
}
k++;
}
if(i >= m)
while(j<n)
{
c[k] = b[j];
j++;
k++;
}
if(j >= n)
while(i<m)
{
c[k] = a[i];
i++;
k++;
}
}
--------------------------------------------
MERGE UNSORTED
/**** Program to Merge Two Unsorted Arrays ****/
#include <stdio.h>
void merge_unsorted();
int a[50], b[50], c[100], m, n;
main()
{
int i;
printf("\nEnter size of array A: ");
scanf("%d", &m);
printf("\nEnter elements of array A:\n");
for(i=0; i<m; i++)
scanf("%d", &a[i]);
printf("\nEnter size of array B: ");
scanf("%d", &n);
printf("\nEnter elements of array B:\n");
for(i=0; i<n; i++)
scanf("%d", &b[i]);
merge_unsorted();
printf("\nAfter merging:\n");
for(i=0; i<m+n; i++)
printf("\n%d", c[i]);
getch();
}
void merge_unsorted()
{
int i, j, k=0;
for(i=0; i<m; i++)
c[i] = a[i];
for(j=m; j<m+n; j++)
{
c[j] = b[k];
k = k+1;
}
}
--------------------------------------------
INSERT SORTED
/**** Program to Insert an Element into Sorted Array ****/
#include <stdio.h>
void insert_sorted();
int a[50], n, item;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter sorted elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter ITEM to insert: ");
scanf("%d", &item);
insert_sorted();
printf("\n\nAfter insertion:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void insert_sorted()
{
int i = n-1;
while(item<a[i] && i>=0)
{
a[i+1] = a[i];
i--;
}
a[i+1] = item;
n++;
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
INSERT UNSORTED
DELETE
/**** Program to Delete an Element from an Array ****/
#include <stdio.h>
void delet();
int a[50], n, loc, item;
main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter location of deletion: ");
scanf("%d", &loc);
delet();
printf("\n\nAfter deletion:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void delet()
{
int i;
item = a[loc-1];
for(i=loc-1; i<n; i++)
a[i] = a[i+1];
n--;
printf("\nITEM deleted: %d", item);
}
---------------------------------------------------------------------------------------
Structure
www.w3professors.com Neeru Babber Page No. 1
{
struct student s1 = {'x', 888, 450};
std_func(s1.name, s1.rollno, s1.marks);
getch();
}
struct student std_func(char name, int rollno, float marks)
{
printf("Name\tRoll No.\tMarks\n");
printf("%c\t%d\t\t%f", name, rollno, marks);
}
-------------------------------------------www.w3professors.com Neeru Babber Page No. 1
Link List
INSERT FIRST
/**** Program to Insert First Node in a Linked List ****/
#include <stdio.h>
void insert_first();
void display();
struct node
{
int info;
void insert_first()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = ptr;
}
printf("\nItem inserted: %d\n", item);
}
void display()
{
struct node *ptr = start;
int i=1;
if (ptr == NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
INSERT LAST
/**** Program to Insert Last Node in a Linked List ****/
#include <stdio.h>
void insert_last();
void display();
struct node
{
int info;
struct node *link;
} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Display\n3. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_last();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("\n\nInvalid choice. Please try again.\n");
}
} while (1);
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = (struct node *)malloc(sizeof(struct node));
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
}
printf("\nItem inserted: %d\n", item);
}
void display()
{
struct node *ptr = start;
int i=1;
if (ptr == NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
--------------------------------------------
INSERT SPECIFIC
/**** Program to Insert at Specific Node in a Linked List
****/
#include <stdio.h>
void insert_last();
void insert_specific();
void display();
struct node
{
int info;
struct node *link;
} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Insert Specific\n3.
Display\n4.
Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_last();
break;
case 2:
insert_specific();
break;
case 3:
display();
break;
www.eazynotes.com Gursharan Singh Tatla Page No. 2
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Please try again.\n");
}
} while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = (struct node *)malloc(sizeof(struct node));
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
}
printf("\nItem inserted: %d\n", item);
}
www.eazynotes.com Gursharan Singh Tatla Page No. 3
void insert_specific()
{
int n;
struct node *nw, *ptr;
if (start == NULL)
printf("\n\nLinked list is empty. It must have at least one
node.\n");
else
{
printf("\n\nEnter INFO after which new node is to be
inserted: ");
scanf("%d", &n);
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
ptr = start;
nw = start;
while (ptr != NULL)
{
if (ptr->info == n)
{
nw = (struct node *)malloc(sizeof(struct node));
nw->info = item;
nw->link = ptr->link;
ptr->link = nw;
else
{
Printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
INSERT SORTED
/**** Program to Insert Node in a Sorted Linked List ****/
#include <stdio.h>
void insert_sorted();
void display();
struct node
{
int info;
struct node *link;
} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Sorted\n2. Display\n3. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_sorted();
break;
case 2:
display();
break;
case 3:
exit(0);
default:
printf("\n\nInvalid choice. Please try again.\n");
}
} while (1);
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2
void insert_sorted()
{
struct node *ptr, *prev;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else if (item < start->info)
{
ptr = start;
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = ptr;
}
else
{
ptr = start;
prev = start;
while (ptr != NULL)
{
if (item < ptr->info)
{
prev->link = (struct node *)malloc(sizeof(struct
node));
prev = prev->link;
prev->info = item;
prev->link = ptr;
return;
}
else if (ptr->link == NULL)
{
ptr->link = (struct node *)malloc(sizeof(struct
node));
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
return;
}
www.eazynotes.com Gursharan Singh Tatla Page No. 3
else
{
prev = ptr;
ptr = ptr->link;
}
}
}
}
void display()
{
struct node *ptr = start;
int i=1;
if (ptr == NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
DELETE FIRST
/**** Program to Delete First Node in a Linked List ****/
#include <stdio.h>
void insert_last();
void delete_first();
void display();
struct node
{
int info;
struct node *link;
} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Delete First\n3.
Display\n4.
Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_last();
break;
case 2:
delete_first();
break;
case 3:
display();
break;
case 4:
exit(0);
www.eazynotes.com Gursharan Singh Tatla Page No. 2
default:
printf("\n\nInvalid choice: Please try again.\n");
}
} while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = (struct node *)malloc(sizeof(struct node));
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
}
printf("\nItem inserted: %d\n", item);
}
void delete_first()
{
struct node *ptr;
if (start == NULL)
printf("\n\nLinked list is empty.\n");
else
{
ptr = start;
item = start->info;
start = start->link;
free(ptr);
www.eazynotes.com Gursharan Singh Tatla Page No. 3
DELETE LAST
default:
printf("\n\nInvalid choice: Please try again.\n");
}
} while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = (struct node *)malloc(sizeof(struct node));
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
}
printf("\nItem inserted: %d\n", item);
}
void delete_last()
{
struct node *ptr, *prev;
if (start == NULL)
printf("\n\nLinked list is empty.\n");
else
{
ptr = start;
prev = start;
www.eazynotes.com Gursharan Singh Tatla Page No. 3
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
DELETE SPECIFIC
/**** Program to Delete any Specific Node in a Linked List
****/
#include <stdio.h>
void insert_last();
void delete_specific();
void display();
struct node
{
int info;
struct node *link;
} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Delete Specific\n3.
Display\n
4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_last();
break;
case 2:
delete_specific();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Please try again.\n");
}
} while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = (struct node *)malloc(sizeof(struct node));
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
}
printf("\nItem inserted: %d\n", item);
}
void delete_specific()
{
struct node *ptr, *prev;
printf("\n\nEnter ITEM which is to be deleted: ");
scanf("%d", &item);
if (start == NULL)
printf("\n\nLinked list is empty.\n");
www.eazynotes.com Gursharan Singh Tatla Page No. 3
ptr = start;
prev = start;
while (ptr != NULL)
{
if (ptr->info == item)
{
prev->link = ptr->link;
free(ptr);
}
else
{
prev = ptr;
ptr = ptr->link;
}
}
printf("\n\nItem deleted: %d", item);
}
}
void display()
{
struct node *ptr = start;
int i=1;
if (ptr == NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
REVERSE
/**** Program to Reverse a Linked List ****/
#include <stdio.h>
void insert_last();
void reverse();
void display();
struct node
{
int info;
default:
printf("\n\nInvalid choice: Please try again.\n");
}
} while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if (start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
start = prev;
printf("\n\nReversed Linked is:\n\n");
display();
}
void display()
{
struct node *ptr = start;
int i=1;
if (ptr == NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
SEARCH SORTED
/**** Program to Search in a Sorted Linked List ****/
#include <stdio.h>
void insert_sorted();
void search_sorted();
void display();
struct node
{
int info;
struct node *link;
} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Sorted\n2. Search Sorted\n3.
Display\n
4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_sorted();
break;
case 2:
search_sorted();
break;
case 3:
display();
break;
case 4:
exit(0);
www.eazynotes.com Gursharan Singh Tatla Page No. 2
default:
printf("\n\nInvalid choice: Please try again.\n");
}
} while (1);
}
void insert_sorted()
{
struct node *ptr, *prev;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
return;
}
else
{
prev = ptr;
ptr = ptr->link;
}
}
}
}
void search_sorted()
{
struct node *ptr = start;
int loc = 1;
printf("\n\nEnter ITEM to be searched: ");
scanf("%d", &item);
while (ptr != NULL)
{
if (item > ptr->info)
{
ptr = ptr->link;
loc++;
}
else if (item == ptr->info)
{
printf("\n\nItem %d is present at location %d\n",
item,loc);
return;
}
else
{
printf("\n\nItem is not present in the list\n");
return;
}
}
}
void display()
{
struct node *ptr = start;
int i=1;
www.eazynotes.com Gursharan Singh Tatla Page No. 4
if (ptr == NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
SEARCH UNSORTED
/**** Program to Search in an Unsorted Linked List ****/
#include <stdio.h>
void insert_last();
void search_unsorted();
void display();
struct node
{
int info;
struct node *link;
} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Search Unsorted\n3.
Display\n
4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_last();
break;
case 2:
search_unsorted();
break;
case 3:
display();
break;
case 4:
exit(0);
www.eazynotes.com Gursharan Singh Tatla Page No. 2
default:
printf("\n\nInvalid choice: Please try again.\n");
}
} while (1);
}
void insert_last()
{
struct node *ptr;
printf("\n\nEnter item: ");
scanf("%d", &item);
if(start == NULL)
{
start = (struct node *)malloc(sizeof(struct node));
start->info = item;
start->link = NULL;
}
else
{
ptr = start;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = (struct node *)malloc(sizeof(struct node));
ptr = ptr->link;
ptr->info = item;
ptr->link = NULL;
}
printf("\nItem inserted: %d\n", item);
}
void search_unsorted()
{
struct node *ptr = start;
int loc = 1;
printf("\n\nEnter ITEM to be searched: ");
scanf("%d", &item);
while (ptr != NULL)
{
www.eazynotes.com Gursharan Singh Tatla Page No. 3
if (item == ptr->info)
{
printf("\n\nItem %d is present at location %d\n", item,
loc);
return;
}
else
{
ptr = ptr->link;
loc++;
}
}
printf("\n\nItem is not present in the list\n");
}
void display()
{
struct node *ptr = start;
int i=1;
if (ptr == NULL)
printf("\nLinklist is empty.\n");
else
{
printf("\nSr. No.\t\tAddress\t\tInfo\t\tLink\n");
while(ptr != NULL)
{
printf("\n%d.\t\t%d\t\t%d\t\t%d\n", i, ptr, ptr->info,
ptr->link);
ptr = ptr->link;
i++;
}
}
}
---------------------------------------------------------------------------------------
void push()
{
if(top == MAX-1)
printf("\n\nStack is full.");
else
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
top++;
stack[top] = item;
printf("\n\nITEM inserted = %d", item);
}
}
void pop()
{
if(top == -1)
printf("\n\nStack is empty.");
else
{
item = stack[top];
top--;
printf("\n\nITEM deleted = %d", item);
}
}
void display()
{
int i;
if(top == -1)
printf("\n\nStack is empty.");
else
{
for(i=top; i>=0; i--)
printf("\n%d", stack[i]);
}
}
--------------------------------------------
{
int ch;
do
{
printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
www.eazynotes.com Gursharan Singh Tatla Page No. 2
default:
printf("Invalid choice. Please try again.\n");
}
} while(1);
getch();
}
void push()
{
struct node *ptr;
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if (top == NULL)
{
top = (struct node *)malloc(sizeof(struct node));
top->info = item;
top->link = NULL;
}
else
{
ptr = top;
top = (struct node *)malloc(sizeof(struct node));
top->info = item;
top->link = ptr;
}
printf("\nItem inserted: %d\n", item);
}
void pop()
{
struct node *ptr;
if (top == NULL)
printf("\n\nStack is empty\n");
else
{
ptr = top;
item = top->info;
top = top->link;
free(ptr);
printf("\n\nItem deleted: %d", item);
}
}
void display()
{
struct node *ptr;
if (top == NULL)
printf("\n\nStack is empty\n");
else
{
ptr = top;
while(ptr != NULL)
{
printf("\n\n%d", ptr->info);
ptr = ptr->link;
}
}
}
--------------------------------------------------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1
void insert()
{
if(rear == MAX-1)
printf("\n\nQueue is full.");
else
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if (rear == -1 && front == -1)
{
rear = 0;
front = 0;
}
else
rear++;
queue[rear] = item;
printf("\n\nItem inserted: %d", item);
}
}
void delet()
{
if(front == -1)
printf("\n\nQueue is empty.");
else
{
item = queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
front++;
printf("\n\nItem deleted: %d", item);
}
}
void display()
{
int i;
if(front == -1)
printf("\n\nQueue is empty.");
else
{
printf("\n\n");
for(i=front; i<=rear; i++)
printf(" %d", queue[i]);
}
}
--------------------------------------------
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Please try again...\n");
}
} while(1);
getch();
}
void insert()
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node *)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}
else
{
rear->link = (struct node *)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}
}
void delet()
{
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
printf("\nItem deleted: %d\n", item);
if(front == NULL)
rear = NULL;
}
}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
while(ptr != NULL)
{
printf("%d\t",ptr->info);
ptr = ptr->link;
}
}
}
--------------------------------------------
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Pleasr try again...\n");
}
} while(1);
getch();
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2
void insert()
{
if((front==0 && rear==SIZE-1) || (front==rear+1))
printf("\n\nQueue is full.");
else
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == -1)
{
rear = 0;
front = 0;
}
else if(rear == SIZE-1)
rear = 0;
else
rear++;
queue[rear] = item;
printf("\n\nItem inserted: %d\n", item);
}
}
void delet()
{
if(front == -1)
printf("\n\nQueue is empty.\n");
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1;
}
else if(front == SIZE-1)
front = 0;
else
front++;
printf("\n\nITEM deleted: %d", item);
}
}
void display()
{
int i;
if((front == -1) || (front==rear+1))
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
for(i=front; i<=rear; i++)
printf("\t%d",queue[i]);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
File Handling
Program to Copy Contents of One File to Another ***/
#include <stdio.h>
main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("abc.txt", "r");
fp2 = fopen("xyz.txt", "w");
while((ch = getc(fp1)) != EOF)
putc(ch, fp2);
fclose(fp1);
fclose(fp2);
getch();
}
--------------------------------------------
fclose(fp);
printf("\n\n\nNo. of characters = %d",noc);
printf("\n\nNo. of lines = %d",nol);
printf("\n\nNo. of white spaces = %d",nob);
getch();
}
-------------------------------------------Program to Read Data from File using fscanf() ***/
#include <stdio.h>
main()
{
FILE *fptr;
int rollno;
char name[20];
float marks;
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------