You are on page 1of 85

Simple Program

www.w3professors.com Gursharan Singh Tatla Page No. 1

/*
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();
}

-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/** Program to Find Area of a Triangle using Hero s Formula **/


#include <stdio.h>
#include <math.h>
main()
{
float a, b, c, s, area;
back:
printf("\nEnter three sides of a triangle: ");
scanf("%f %f %f", &a, &b, &c);
if (a==0 || b==0 || c==0)
{
printf("\nValue of any side should not be equal to
zero\n");
goto back;
}
if (a+b<c || b+c<a || c+a<b)
{
printf("\nSum of two sides should not be less than
third\n");
goto back;
}
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("\n\nArea of triangle: %.2f", area);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/** Program to Find Area of Square & Circumference of a Circle **/

#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();
}

-------------------------------------------Program to Count Number of Words and Number of Characters in a


String
*/

#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: ");

scanf("%d %d", &a, &b);


h = hcf(a, b);
printf("\nHCF of numbers is: %d", h);
getch();
}
int hcf(int a, int b)
{
if (a%b == 0)
return b;
else
return hcf(b, a%b);
}

-------------------------------------------Program to Find HCF of Two Numbers Without Recursion ***/


#include <stdio.h>
main()
{
int q, m, n, temp;
printf("\nEnter values of two numbers: ");
scanf("%d %d", &m, &n);
if (m == 0)
{
printf("\nHCF of number is: %d", n);
goto end;
}
if (n == 0)
{
printf("\nHCF of numbers is: %d", m);
goto end;
}
if (n > m)
{
temp = m;
m = n;
n = temp;
}
www.w3professors.com Gursharan Singh Tatla Page No. 2

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();
}

-------------------------------------------Program to Find Value of sin(x) using Expansion Series Given Below:


sin(x) = x - x3/3! + x5/5! - x7/7!........
*/
#include <stdio.h>
#include <math.h>
main()
{
float base, pwr, sum, c=1, m=2, i=3, g, h;
printf("\nEnter the base value: ");
scanf("%f", &base);
printf("\nEnter the power value: ");
scanf("%f", &pwr);
sum = base;
ab:
m = m * i;
h = pow(-1, c);
g = pow(base, i);
sum = sum + (h * g) / m;
i = i + 2;
c++;

m = m * (i - 1);
www.w3professors.com Gursharan Singh Tatla Page No. 2

if (i <= pwr)
goto ab;
printf("\n\nSum = %f", sum);
getch();
}

-------------------------------------------Program to Find Vowels in a String ***/


#include <stdio.h>
#include <string.h>
main()
{
char str[20];
int count=0, i=0;
printf("\nEnter any string: ");
gets(str);
while (str[i] != '\0')
{
if (str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u')
count++;
i++;
}
printf("\nNo. of vowels: %d", count);
getch();
}
-------------------------------------------Program to Find Whether a Number is Palindrome or Not ***/
#include <stdio.h>
main()
{
int n, pal=0, temp, x;
printf("\nEnter any number: ");
scanf("%d", &n);
temp = n;
while (temp > 0)
{
x = temp%10;
temp = temp/10;
pal = pal*10 + x;
}
if (pal == n)
printf("\n%d is palindrome", n);
else

printf("\n%d is not palindrome", n);


getch();
}

-------------------------------------------Program to Implement break Statement ***/


#include <stdio.h>
main()
{
int i;
for (i=1; i<=10; i++)
{
printf("\n%d", i);
if (i == 7)
break;
}
getch();
}

-------------------------------------------Program to Implement continue Statement ***/


#include <stdio.h>
main()
{
int i, n, a, sq;
printf("\n\tProgram finds square of positive numbers only\n");
printf("\nHow many numbers you want to enter: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
printf("\nEnter number: ");
scanf("%d", &a);
if (a < 0)
continue;
sq = a * a;
printf("\nSquare = %d\n", sq);
}
getch();
}

-------------------------------------------Program to Print a Table of any Number ****/


#include <stdio.h>
main()
{
int n, mul, i;
printf("\nEnter any no.: ");
scanf("%d", &n);

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


{
mul = n*i;
printf("\n\n%d\tx\t%d\t=\t%d", n, i, mul);
}
getch();
}
-------------------------------------------Program to Print Fibonacci Series using Recursion ***/
#include <stdio.h>
int fibbo(int, int, int, int);
main()
{
int n, f, x=0, y=1, i=3;
printf("\nEnter value of n: ");
scanf("%d", &n);
printf("\n%d\t%d", x, y);
fibbo(x, y, n, i);
getch();
}
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);
}
}

-------------------------------------------Program to Print Fibonacci Series without Recursion **/


#include <stdio.h>
main()
{
int x, y, z, n, i;
x=0; y=1;
printf("\nEnter value of n: ");
scanf("%d", &n);
printf("\nFibonacci Series:\n\n");
printf("\n%d", x);
printf("\t%d", y);

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


{
z = x+y;
printf("\t%d", z);
x = y;
y = z;
}
getch();
}
-------------------------------------------Program to Print First N Prime Numbers ***/
#include <stdio.h>
main()
{
int i, j, n;
printf("\nEnter how many prime numbers you want to print: ");
scanf("%d", &n);
printf("\n2");
for (i=2; i<=n; i++)
for (j=2; j<=i; j++)
{
if(i%j == 0)
break;
else
{
printf("\n%d", i);
break;
}
}
getch();
}

-------------------------------------------Program to Print the Numbers, Which are Divisible by 3 and 5 from


First 100 Natural Numbers
*/
#include <stdio.h>
main()
{
int i;
printf("\nFirst 100 numbers which are divisible by 3 and
5\n\n");
for (i=1; i<=100; i++)
if (i%3==0 && i%5==0)
printf("\t%d", i);
getch();

}
----------------------------------------------------------

Program to Print the Numbers, Which are Divisible by 3 and 5 from


First 100 Natural Numbers
*/
#include <stdio.h>
main()
{
int i;
printf("\nFirst 100 numbers which are divisible by 3 and
5\n\n");
for (i=1; i<=100; i++)
if (i%3==0 && i%5==0)
printf("\t%d", i);
getch();
}
----------------------------------------------------------

Program to Print the Following Output:


1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
*/
#include <stdio.h>
main()
{
int i, j, k, space, n=9;
for (i=1; i<=n; i++)
{
for (j=1; j<=n-i; j++)
putchar(' ');
for (j=1,k=2*i-1; j<=2*i-1; j++,k--)
{
www.eazynotes.com Gursharan Singh Tatla Page No. 2

if (j <= k)
printf("%d", j);
else
printf("%d", k);
}
putchar('\n');

}
getch();
}
----------------------------------------------------------

Program to Reverse a Given Number ***/


#include <stdio.h>
main()
{
long n;
int rev;
printf("\nEnter any number: ");
scanf("%ld", &n);
printf("\nReverse no. is:\n\n");
while (n > 0)
{
rev = n % 10;
n = n / 10;
printf("%d", rev);
}
getch();
}
-------------------------------------------------------------------------------------------------------------------

String
www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Compare Two Strings using strcmp() ***/


#include <stdio.h>
#include <string.h>
main()
{
char s1[20], s2[20];
int result;
printf("\nEnter first string: ");
gets(s1);
printf("\nEnter second string: ");
gets(s2);
result = strcmp(s1, s2);
if (result == 0)
printf("\nBoth strings are equal");
else
printf("\nBoth strings are not equal");
getch();
}

-----------------------------------------

www.w3professors.com Neeru Babber Page No. 1

/** Program to Compare Two Strings Without using strcmp() **/


#include<stdio.h>
main()
{
char string1[5],string2[5];
int i,temp = 0;
printf("Enter the string1 value:\n");
gets(string1);
printf("\nEnter the String2 value:\n");
gets(string2);
for(i=0; string1[i]!='\0'; i++)
{
if(string1[i] == string2[i])
temp = 1;
else
temp = 0;
}
if(temp == 1)
printf("Both strings are same.");
else
printf("Both string not same.");
getch();
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/**** Program to Concatenate Two Strings using strcat() ****/


#include <stdio.h>
#include <string.h>
main()
{
char s1[20], s2[20];
printf("\nEnter first string: ");
gets(s1);
printf("\nEnter second string: ");
gets(s2);
strcat(s1, s2);
printf("\nThe concatenated string is: %s", s1);
getch();
}

----------------------------------------www.w3professors.com Neeru Babber Page No. 1

/* Program to Concatenate Two Strings without using strcat() */


#include <stdio.h>
#include <conio.h>

#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

/* Program to Copy one String to Another Without Using strcpy() */


#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char string1[20], string2[20];
int i;
printf("Enter the value of STRING1: \n");
gets(string1);
for(i=0; string1[i]!='\0'; i++)
string2[i]=string1[i];
string2[i]='\0';
printf("\nThe value of STRING2 is:\n");
puts(string2);
getch();
}

----------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Copy String using strcpy() ***/

#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

/*** Program to Find Length of a String using strlen() ***/


#include <stdio.h>
#include <string.h>
main()
{
char s1[20];
int len;
printf("\nEnter any string: ");
gets(s1);
len = strlen(s1);
printf("\nLength of string: %d", len);
getch();
}

----------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Find Length of a String Without using strlen() ***/


#include <stdio.h>
main()
{
char str[20];
int i = 0;
printf("\nEnter any string: ");
gets(str);
while (str[i] != '\0')
i++;
printf("\nLength of string: %d", i);
getch();
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Find Whether a String is Palindrome or Not ***/


#include <stdio.h>

#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();
}

----------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/* Program to Find Whether a String is Palindrome or Not without


using String Functions */
#include <stdio.h>
#include <string.h>
main()
{
char s1[20];
int i, j, len=0, flag=0;
printf("\nEnter any string: ");
gets(s1);
for (i=0; s1[i]!='\0'; i++)
len++;
i = 0;
j = len-1;
while (i < len)
{
if (s1[i] != s1[j])
{
flag = 1;
break;
}
i++;
j--;
}
if (flag == 0)
printf("\nString is palindrome");

else
printf("\nString is not palindrome");
getch();
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Input/Output Strings using Character Functions ***/


#include <stdio.h>
main()
{
char name[20], ch = '\0';
int i=0;
printf("\nEnter your name: ");
while (ch != '\r')
{
ch = getche();
name[i] = ch;
i++;
}
printf("\nName: ");
for (i=0; name[i]!='\0'; i++)
putchar(name[i]);
getch();
}

----------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/** Program to Input/Output Strings using gets() and puts() **/


#include <stdio.h>
main()
{
char name[20];
puts("\nEnter your name: ");
gets(name);
puts("\nName: ");
puts(name);
getch();
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/** Program to Input/Output Strings using printf() and scanf() **/


#include <stdio.h>
main()
{
char name[20];
printf("\nEnter your name: ");

scanf("%s", name);
printf("\nName: %s", name);
getch();
}

----------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Reverse a String using strrev() ***/


#include <stdio.h>
#include <string.h>
main()
{
char s1[20];
printf("\nEnter any string: ");
gets(s1);
strrev(s1);
printf("\nReverse of string: %s", s1);
getch();
}

----------------------------------------www.w3professors.com Neeru Babber Page No. 1

/* Program to Reverse a String without using strrev() */


#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char string1[10], string2[10];
int i, length;
printf("Enter any string:\n");
gets(string1);
length = strlen(string1)-1;
for(i=0; string1[i]!='\0'; i++)
{
string2[length]=string1[i];
length--;
}
string2[length]='\0';
printf("\nThe Reverse of string is:\n");
puts(string2);
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;
}
----------------------------------------------------------

Program to Show Call by Value ****/


#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 a, int b)
{
int temp;
temp = a;
a = b;

b = temp;
}
-----------------------------------------------------

PASSING ARRAYS AS FUNCTION ARGUMENTS IN


C
If you want to pass a sing le-dimension array as an arg ument in a function, you would
have to declare function formal parameter in one of following three ways and all three
declaration methods produce similar results because each tells the compiler that an integ
er pointer is g oing to be received. Similar way you can pass multidimensional array as
formal parameters.

Way-1
Formal parameters as a pointer as follows. You will study what is pointer in next chapter.
void myFunction(int *param)
{...}

Way-2

Formal parameters as a sized array as follows:


void myFunction(int param[10])
{...}

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;
}

Now, let us call the above function as follows:


#include <stdio.h>
/* function declaration */
double getAverage(int arr[], int size);
int main ()
{
/* an int array with 5 elements */
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
/* pass pointer to the array as an argument */
avg = getAverage( balance, 5 ) ;
/* output the returned value */
printf( "Average value is: %f ", avg );
return 0;

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

/*** Program to Print Fibonacci Series using Recursion ***/


#include <stdio.h>
int fibbo(int, int, int, int);
main()
{
int n, f, x=0, y=1, i=3;
printf("\nEnter value of n: ");
scanf("%d", &n);
printf("\n%d\t%d", x, y);
fibbo(x, y, n, i);
getch();

}
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

/* 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: ");
scanf("%d %d", &a, &b);
h = hcf(a, b);
printf("\nHCF of numbers is: %d", h);
getch();
}
int hcf(int a, int b)
{
if (a%b == 0)
return b;
else
return hcf(b, a%b);
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/**** Program to Illustrate the Concept of Pointers ****/


#include <stdio.h>
main()
{
int a = 10;
int *p;
p = &a;
printf("\nAddress of a: %u", &a);

printf("\n\nAddress of a: %u", p);


printf("\n\nAddress of p: %u", &p);
printf("\n\nValue of p: %d", p);
printf("\n\nValue of a: %d", a);
printf("\n\nValue of a: %d", *(&a));
printf("\n\nValue of a: %d", *p);
getch();
}
-------------------------------------------------------------------------------------------------------------------

Array
www.w3professors.com Gursharan Singh Tatla Page No. 1

/* 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])
{
printf("\nItem found at location %d", i+1);
break;
}
if (i > 9)
printf("\nItem doesnot exist.");
getch();
}
---------------------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/***** Program to Search an Array using Binary Search *****/


#include <stdio.h>
void binary_search();
int a[50], n, item, loc, beg, mid, end, i;
main()
{
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array in sorted form:\n");

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


scanf("%d", &a[i]);
printf("\nEnter ITEM to be searched: ");
scanf("%d", &item);
binary_search();
getch();
}
void binary_search()
{
beg = 0; end = n-1;
mid = (beg + end) / 2;
while ((beg<=end) && (a[mid]!=item))
{
if (item < a[mid])
end = mid - 1;
else
beg = mid + 1;
mid = (beg + end) / 2;
}
if (a[mid] == item)
printf("\n\nITEM found at location %d", mid+1);
else
printf("\n\nITEM doesn't exist");
}
----------------------------------------------------------

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

/**** Program to Sort an Array using Merge Sort ****/


#include <stdio.h>
void merge_sort(int [], int, int);
void merge_array(int [], int, int, int);
main()
{
int a[50], n, 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]);
merge_sort(a, 0, n-1);
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void merge_sort(int a[], int beg, int end)
{
int mid;
if (beg < end)
{
mid = (beg+end)/2;
merge_sort(a, beg, mid);
merge_sort(a, mid+1, end);
merge_array(a, beg, mid, end);
}
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2

void merge_array(int a[], int beg, int mid, int end)


{
int i, left_end, num, temp, j, k, b[50];
for(i=beg; i<=end; i++)
b[i] = a[i];
i = beg;
j = mid+1;
k = beg;
while ((i<=mid) && (j<=end))
{
if (b[i] <= b[j])
{
a[k] = b[i];
i++; k++;

}
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

/**** Program to Sort an Array using Quick Sort ****/


#include <stdio.h>
void quick_sort(int [], int, int);
int partition(int [], int, int);
main()
{
int a[50], n, 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]);
quick_sort(a, 0, n-1);
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void quick_sort(int a[], int beg, int end)
{
int x;

if (beg < end)


{
x = partition(a, beg, end);
quick_sort(a, beg, x-1);
quick_sort(a, x+1, end);
}
}

www.eazynotes.com Gursharan Singh Tatla Page No. 2

int partition(int a[], int beg, int end)


{
int loc = beg, temp;
while (1)
{
while (a[loc]<=a[end] && loc!=end) /* Scan from right to
left */
end--;
if (loc == end)
return loc;
temp = a[loc];
a[loc] = a[end];
a[end] = temp;
loc = end;
while (a[loc]>=a[beg] && loc!=beg) /* Scan from left to
right */
beg++;
if (loc == beg)
return loc;
temp = a[loc];
a[loc] = a[beg];
a[beg] = temp;
loc = beg;
}
}
----------------------------------------------------------

SELECTION SORT

/***** Program to Sort an Array using Selection Sort *****/


#include <stdio.h>
void selection_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]);

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("\nEnter elements of Array A:\n");


for (i=0; i<row; i++)
for (j=0; j<col; j++)
scanf("%d", &a[i][j]);
printf("\nEnter elements of Array B:\n");
for (i=0; i<row; i++)
for (j=0; j<col; j++)
scanf("%d", &b[i][j]);
printf("\nElements of Matrix A:\n\n");
for (i=0; i<row; i++)
{
for (j=0; j<col; j++)

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

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


if (a[i] > b)
b = a[i];
return b;
}
-------------------------------------------Program to Illustrate the Concept of Passing 2-D Array to Function
Program to Find Sum of Diagonal Elements of a Matrix
*/
#include <stdio.h>
#define ROW 10
#define COL 10
int diagonal_sum(int [][], int, int);
main()
{
int a[ROW][COL], row, col, i, j, sum;
printf("\nEnter no. of rows and columns of a matrix: ");
scanf("%d %d", &row, &col);
printf("\nEnter elements:\n");
for (i=0; i<row; i++)
for (j=0; j<col; j++)
scanf("%d", &a[i][j]);
printf("\nMatrix is:\n\n");
for (i=0; i<row; i++)
{

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


printf("\t%d", a[i][j]);
printf("\n\n");
}
sum = diagonal_sum(a, row, col);
printf("\nSum: %d", sum);
getch();
}
www.w3professors.com Gursharan Singh Tatla Page No. 2

int diagonal_sum(int x[ROW][COL], int r, int c)


{
int i, j, s=0;
for (i=0; i<r; i++)
for (j=0; j<c; j++)
if (i == j)
s = s + x[i][j];
return s;
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

MULTIPLY TWO 2-D ARRAYS

/**** Program to Multiply Two 2-D Arrays ****/


#include <stdio.h>
void multiply();
int a[10][10], b[10][10], c[10][10], m, n, x, y;
main()
{
int i, j;
printf("\nEnter number of rows & cols of array A: ");
scanf("%d%d", &m, &n);
printf("\nEnter elements of array A:\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
printf("\nEnter number of rows & cols of array B: ");
scanf("%d%d", &x, &y);
printf("\nEnter elements of array B:\n");
for(i=0; i<x; i++)
for(j=0; j<y; j++)
scanf("%d", &b[i][j]);
printf("\n\nArray A:\n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("\t%d", a[i][j]);
printf("\n\n");

}
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");
}

www.eazynotes.com Gursharan Singh Tatla Page No. 2

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:

printf("\nEnter no. of rows and columns of Matrix A: ");


scanf("%d %d", &r1, &c1);
printf("\nEnter no. of rows and columns of Matrix B: ");
scanf("%d %d", &r2, &c2);
if (c1 != r2)
{
printf("\n\nMultiplication is not possible\n");
goto back;
}
printf("\n\nEnter elements of Matrix A:\n");
for (i=0; i<r1; i++)
for (j=0; j<c1; j++)
scanf("%d", &a[i][j]);
printf("\nEnter elements of Matrix B:\n");
for (i=0; i<r2; i++)
for (j=0; j<c2; j++)
scanf("%d", &b[i][j]);
www.w3professors.com Gursharan Singh Tatla Page No. 2

printf("\n\nElements of Matrix A:\n\n");


for (i=0; i<r1; i++)
{
for (j=0; j<c1; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}
printf("\n\nElements of Matrix B:\n");
for (i=0; i<r2; i++)
{
for (j=0; j<c2; j++)
printf("\t%d", b[i][j]);
printf("\n\n");
}
for (i=0; i<r1; i++)
for (j=0; j<c2; j++)
{
c[i][j] = 0;
for (k=0; k<r2; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
printf("\n\nMultiplication of Matrices:\n\n");
for (i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
printf("\t%d", c[i][j]);
printf("\n\n");

}
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])
{

printf("\nItem found at location %d", i+1);


break;
}
if (i > 9)
printf("\nItem doesnot exist.");
getch();
}
-------------------------------------------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;
}
}
-------------------------------------------Program to Sort an Array using Selection Sort *****/
#include <stdio.h>
void selection_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]);
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;
}
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

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;
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

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];
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

MERGE SORTED

/**** Program to Merge Two Sorted Arrays ****/


#include <stdio.h>
void merge_sorted();
int a[50], b[50], c[100], m, n;
main()
{
int i;
printf("\nEnter size of array A: ");
scanf("%d", &m);
printf("\nEnter sorted 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 sorted elements of array B:\n");
for(i=0; i<n; i++)
scanf("%d", &b[i]);
merge_sorted();
printf("\nAfter merging:\n");
for(i=0; i<m+n; i++)
printf("\n%d", c[i]);
getch();
}
void merge_sorted()
{
int i=0, j=0, k=0;
while(i<m && j<n)
{
if(a[i] < b[j])
{
c[k] = a[i];
www.eazynotes.com Gursharan Singh Tatla Page No. 2

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++;
}
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

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;
}
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 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

/**** Program to Insert an Element into an Unsorted Array


****/
#include <stdio.h>
void insert_unsorted();
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 insertion: ");
scanf("%d", &loc);
printf("\nEnter ITEM to insert: ");
scanf("%d", &item);
insert_unsorted();
printf("\n\nAfter insertion:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void insert_unsorted()
{
int i=n-1;
while(i>=loc-1)
{
a[i+1] = a[i];
i--;
}
a[loc-1] = item;
n++;
}
-------------------------------------------www.eazynotes.com Gursharan Singh Tatla Page No. 1

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

/** Program to Implement Structure **/


#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
main()
{
struct student s1 = {"abc", 1, 450};
struct student s2;
printf("Enter student Name, Rollno, Marks:\n");
scanf("%s%i%f", &s2.name, &s2.rollno, &s2.marks);
printf("\nStudent Name\tRollno\tMarks\n");
printf("%s\t%i\t%f", s1.name, s1.rollno, s1.marks);
printf("\n");
printf("%s\t%i\t%f",s2.name,s2.rollno,s2.marks);
getch();
}
-------------------------------------------www.w3professors.com Neeru Babber Page No. 1

/** Program to Implement Structure with Function **/


#include<stdio.h>
struct student
{
char name;
int rollno;
float marks;
};
struct student std_func(char, int, float);
main()

{
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

/** Program to Implement Structure with Pointers **/


#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
void show(struct student *);
main()
{
struct student s1 = {"xyz", 1, 450};
show(&s1);
getch();
}
void show(struct student *ptr)
{
printf("Student name\tRollno\tMarks\n");
printf("%s\t\t%i\t%f",ptr->name,ptr->rollno,ptr->marks);
}
---------------------------------------------------------------------------------------

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;

struct node *link;


} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_first();
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_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++;
}

}
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

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;

printf("\n\nItem inserted: %d", item);


return;
}
else
ptr = ptr->link;
}
}
}
void display()
{
struct node *ptr = start;
int i=1;
if (ptr == NULL)
printf("\nLinklist is empty.\n");

www.eazynotes.com Gursharan Singh Tatla Page No. 4

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

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

DELETE LAST

/**** Program to Delete Last Node in a Linked List ****/


#include <stdio.h>
void insert_last();
void delete_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. Delete Last\n3. Display\n
4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_last();
break;
case 2:
delete_last();
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_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

while (ptr->link != NULL)


{
prev = ptr;
ptr = ptr->link;
}
item = ptr->info;
if (start->link == NULL)
start = NULL;
else
prev->link = NULL;
prev->link = NULL;
free(ptr);
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

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);

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_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

else if (start->info == item)


{
ptr = start;
start = start->link;
free(ptr);
}
else
{

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;

struct node *link;


} *start=NULL;
int item;
main()
{
int ch;
do
{
printf("\n\n\n1. Insert Last\n2. Reverse\n3. Display\n
4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_last();
break;
case 2:
reverse();
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 reverse()
{
struct node *ptr = start, *prev = NULL, *rev;
while (ptr != NULL)
{
rev = prev;
prev = ptr;
ptr = ptr->link;
prev->link = rev;
}
www.eazynotes.com Gursharan Singh Tatla Page No. 3

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;
}

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));
www.eazynotes.com Gursharan Singh Tatla Page No. 3

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++;
}
}
}
---------------------------------------------------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

STACK USING ARRAY


/****** Program to Implement Stack using Array ******/
#include <stdio.h>
#define MAX 50
void push();
void pop();
void display();
int stack[MAX], top=-1, item;
main()
{
int ch;
do
{
printf("\n\n\n\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\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);
default:
printf("\n\nInvalid entry. Please try again...\n");
}
} while(ch!=4);
getch();
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2

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]);
}
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

STACK USING LINKED LIST


/****** Program to Implement Stack using Linked List ******/
#include <stdio.h>
void push();
void pop();
void display();
struct node
{
int info;
struct node *link;
} *top = NULL;
int item;
main()

{
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);
}
}

www.eazynotes.com Gursharan Singh Tatla Page No. 3

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

QUEUE USING ARRAY


/****** Program to Implement Queue using Array ******/
#include <stdio.h>
#define MAX 50
void insert();
void delet();
void display();
int queue[MAX], rear=-1, front=-1, item;
main()
{
int ch;
do
{

printf("\n\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");


printf("\nEnter your choice: ");
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 entry. Please try again...\n");
}
} while(1);
getch();
}
www.eazynotes.com Gursharan Singh Tatla Page No. 2

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);
}
}

www.eazynotes.com Gursharan Singh Tatla Page No. 3

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]);
}
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

QUEUE USING LINKED LIST


/**** Program to Implement Queue using Linked List ****/
#include<stdio.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
void delet();
void display();
int item;
main()
{
int ch;
do
{
printf("\n\n1.\tInsert\n2.\tDelete\n3.\tDisplay\n4.\tExit\n"
);
printf("\nEnter your choice: ");

scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
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);
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;
}
}

www.eazynotes.com Gursharan Singh Tatla Page No. 3

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;
}
}
}
--------------------------------------------

www.eazynotes.com Gursharan Singh Tatla Page No. 1

CIRCULAR QUEUE USING ARRAY


/**** Program to Implement Circular Queue using Array ****/
#include<stdio.h>
#define SIZE 5
void insert();
void delet();
void display();
int queue[SIZE], rear=-1, front=-1, item;
main()
{
int ch;
do
{
printf("\n\n1.\tInsert\n2.\tDelete\n3.\tDisplay\n4.\tExit\n"
);
printf("\nEnter your choice: ");
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. 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);
}
}

www.eazynotes.com Gursharan Singh Tatla Page No. 3

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();
}
--------------------------------------------

Program to Print a File and Count Number of Characters ***/


#include<stdio.h>
#include<windows.h>
main()
{
FILE *fp;
char c;
int noc=0, nol=0, nob=0;
fp = fopen("FileDisplay.C","r");
if (fp == NULL)
{
printf("File can't be opened");
return;
}
while ((c = fgetc(fp)) != EOF)
{
putchar(c);
noc++;
switch (c)
{
case '\t':
case ' ':
nob++;
break;
case '\n':
nol++;
}
sleep(20);
}
www.w3professors.com Gursharan Singh Tatla Page No. 2

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;

fptr = fopen("abc.txt", "r");


fscanf(fptr, "%d %s %f", &rollno, name, &marks);
printf("\nRoll No.: %d", rollno);
printf("\n\nName = %s", name);
printf("\n\nMArks = %.2f", marks);
fclose(fptr);
getch();
}
-------------------------------------------Program to Write and Read a Character from a File ***/
#include <stdio.h>
main()
{
FILE *fptr;
char ch;
printf("\nEnter any character: ");
scanf("%c", &ch);
fptr = fopen("abc.txt", "w");
putc(ch, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
ch = getc(fptr);
printf("\nCharacter: %c", ch);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Write and Read an Integer from a File ***/


#include <stdio.h>
main()
{
FILE *fptr;
int a;
printf("\nEnter any number: ");
scanf("%d", &a);
fptr = fopen("abc.txt", "w");
putc(a, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
a = getc(fptr);
printf("\nCharacter: %d", a);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Write and Read a String from a File */


#include <stdio.h>
#define SIZE 50
main()
{
FILE *fptr;
char s1[SIZE], s2[SIZE];
printf("\nEnter any string: ");
gets(s1);
fptr = fopen("abc.txt", "w");
fputs(s1, fptr);
fclose(fptr);
fptr = fopen("abc.txt", "r");
fgets(s2, SIZE, fptr);
printf("\nYou entered:");
puts(s2);
getch();
}
-------------------------------------------www.w3professors.com Gursharan Singh Tatla Page No. 1

/*** Program to Write Data into a File using fprintf() ***/


#include <stdio.h>
main()
{
FILE *fptr;
int rollno;
char name[20];
float marks;
printf("\nEnter roll no.: ");
scanf("%d", &rollno);
printf("\nEnter name: ");
fflush(stdin);
gets(name);
printf("\nEnter marks: ");
scanf("%f", &marks);
fptr = fopen("abc.txt", "w");
fprintf(fptr, "%d %s %f", rollno, name, marks);
fclose(fptr);
getch();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You might also like