You are on page 1of 44

Sr No Name of Programe

Basic Program
1 Program to Find Sum and Average of Three Real Numbers
2 Program to Find Area of Square and Circumference of a Circle
3 Program to find Sphere Surface Area and Volume of a Sphere
4 Program to Find Area of a Triangle using Hero Formula
5 Program to Find Simple Interest and Compound Interest
6 Program to Convert Temperature from Degree Centigrade to Fahrenheit
7 Program to Convert Time in Seconds to Hours Minutes and Seconds
8 Program to Swap Values of Two Variables using Third Variable
9 Program to Swap Values of Two Variables Without using Third Variable
10 Program to Calculate the Net Salary
1 Program to Find Largest of Three Numbers
Control Statements & Loop
1 Program to Check Whether a Character is Vowel or not by using switch
Statement1
2 Program to Find the Sum of First 100 Positive Integers2
3 Program to Find the Sum of Even and Odd Numbers from First 100 Positive
Integers
4 Program to Find the Sum of Digits of a Positive Integer
5 Program to Find Whether the Given Number is a Prime Number
6 Program to Print First N Prime Numbers
7 Program to Print a Table of any Number
8 Program to Check Whether the Given Number is an Armstrong Number
9 Program to Print the Numbers Which are Divisible by 3 and 5 from First 100
Natural Numbers
10 Program to Find Whether a Number is Palindrome or Not
11 Program to Find Factorial of a Number without using Recursion
12 Program to Find Factorial of a Number using Recursion
13 Program to Print Fibonacci Series without Recursion
14 Program to Print Fibonacci Series using Recursion
15 Program to Reverse a Given Number
16 Program to Find Value of sinx using Expansion Series given below:
17 sin(x) = x - x3/3 + x5/5 - x7/7......
18 Program to Find HCF of Two Numbers using Recursion
19 Program to Find HCF of Two Numbers Without Recursion
20 Program to Find Vowels in a String
21 Program to Count Number of Words and Number of Characters in a String
22 Program to Implement break Statement
23 Program to Implement continue Statement
Array
1 Program to Print Transpose of a Matrix
2 Program to Add Two Matrices
3 Program to Multiply Two Matrices
4 Program to Find Smallest Among N Numbers
5 Program to Illustrate the Concept of Passing 1-D Array to Function
6 Program to Illustrate the Concept of Passing 2-D Array to Function
7 Program to Sort an Array Using Bubble Sort
8 Program to Sort an Array Using Selection Sort
9 Program to Search an Array using Linear Search
Strings
1 Program to Concatenate Two Strings using strcat( )
2 Program to Concatenate Two Strings without using strcat( )
3 Program to Compare Two Strings using strcmp( )
4 Program to Compare Two Strings without using strcmp( )
5 Program to Copy String using strcpy( )
6 Program to Copy String without using strcpy( )
7 Program to Find Length of a String using strlen( )
8 Program to Reverse a String using strrev( )
9 Program to Reverse a String without using strrev( )
10 Program to Input-Output Strings using Character Functions
11 Program to Input-Output Strings using gets( ) and puts( )
12 Program to Input-Output Strings using printf( ) and scanf( )
13 Program to Find Length of a String Without using strlen( )
14 Program to Find Whether a String is Palindrome or Not
15 Program to Find Whether a String is Palindrome or Not without using String
Functions
Functions
1 Program to Show Call by Value
2 Program to Show Call by Reference
Recursion
1 Program to Find Factorial of a Number using Recursion
2 Program to Print Fibonacci Series using Recursion
3 Program to Find HCF of Two Numbers using Recursion
Pointers
1 Program to Illustrate the Concept of Pointers
1 Program to Implement Structure
2 Program to Implement Structure with Array
3 Program to Implement Structure with Function
4 Program to Implement Structure with Pointers
Searching & Sorting
1 Program to Search an Element in the Array using Linear Search
2 Program to Search an Array using Binary Search
3 Program to Sort an Array using Bubble Sort
4 Program to Sort an Array using Selection Sort
File Handling
1 Program to Write and Read a Character from a File
2 Program to Write and Read an Integer from a File
3 Program to Write and Read a String from a File
4 Program to Write Data into a File using fprintf( )
5 Program to Read Data from File using fscanf( )
6 Program to Copy Contents of One File to Another
7 Program to Print a File and Count Number of Characters

Simple C Programs

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

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

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

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

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

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

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

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

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

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

Control Statements

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

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 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 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 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 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 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 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 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 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);
if (i <= pwr)
goto ab;
printf("\n\nSum = %f", sum);
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--)
{
if (j <= k)
printf("%d", j);
else
printf("%d", k);
}
putchar('\n');
}
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;
}
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 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 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 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 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("\ns\n");
}
getch();
}

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

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

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

Strings

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Pointers

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

Structures

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

/** Program to Implement Structure with Array **/


#include<stdio.h>
struct student
{
char name[20];
int rollno;
float marks;
};
struct student s1[3];
main()
{
int i;
printf("Enter Name, RollNo, Marks:\n");
for(i=0; i<=2; i++)
scanf("%s %d %f",&s1[i].name,&s1[i].rollno,&s1[i].marks);
printf("Student Name\tStudent Rollno\tStudent Marks:");
for(i=0; i<=2; i++)
printf("\n%s\t\t%d\t\t%f", s1[i].name, s1[i].rollno,
s1[i].marks);
getch();

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

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

Searching & Sorting

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

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

File Handling

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

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

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

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

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

You might also like