C-Programming Lab Programs
Part- A
1. Program to read radius of a circle and to find area and circumference
#include<stdio.h>
#define pi 3.14
void main()
{
float r,c,a;
clrscr();
printf("Enter the radius of the circle:\n");
scanf("%f",&r);
c=2*pi*r;
printf("The circumference of the given radius is %f\n",c);
a=pi*r*r;
printf("The area of the given radius is %f\n",a);
getch();
}
2. Program to read three numbers and find the biggest of three
#include<stdio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the three integers:\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
printf("%d is the largest integer",a);
else if(b>a&&b>c)
printf("%d is the largest integer",b);
else
printf("%d is the largest integer",c);
getch();
}
3. Program to demonstrate library functions in math.h
#include<stdio.h>
#include<math.h>
void main()
{
float n;
clrscr();
printf("Enter a number:\n");
scanf("%f",&n);
printf("The square root of the given number is:%f\n",sqrt(n));
printf("The absolute value of the given number is:%f\n",fabs(n));
printf("The cube of the given numberis:%f\n",pow(n,3));
printf("The floor of the given number is : %f\n",floor(n));
printf("The ceil of the given number is : %f\n",ceil(n));
getch();
}
4. Program to check for prime
#include<stdio.h>
void main()
{
int n,i,flag=0;
clrscr();
printf("Enter a number:\n");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(n==1)
printf("1 is neither prime nor composite");
else
{
if(flag==0)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
}
getch();
}
5. Program to generate n primes
#include<stdio.h>
void main()
{
int low,high,i,flag;
clrscr();
printf("Enter two numbers(range):\n");
scanf("%d%d",&low,&high);
printf("Prime numbers between %d and %d are:\n",low,high);
while(low<high)
{
flag=0;
if(low<=1)
{
++low;
continue;
}
for(i=2;i<=low/2;i++)
{
if(low%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d\t",low);
low++;
}
getch();
}
6. Program to read a number, find the sum of the digits, reverse the number and check it for
palindrome
#include<stdio.h>
void main()
{
int n,m,r,sum,rev;
clrscr();
printf("Enter a number:\n");
scanf("%d",&n);
m=n;
sum=0;
rev=0;
while(n!=0)
{
r=n%10;
sum=sum+r;
rev=rev*10+r;
n=n/10;
}
printf("\nThe sum of the digits in the given integer is %d",sum);
printf("\nThe reverse of the given number is %d",rev);
if(m==rev)
printf("\n Is a polyndrome number");
else
printf("\n Is NOT a polyndrome number");
getch();
}
7. Program to read numbers from keyboard continuously till the user presses 999 and to find
the sum of only positive numbers
#include<stdio.h>
void main()
{
int n,sum=0;
clrscr();
printf("Enter numbers and press 999 to stop the input:\n");
do
{
scanf("%d",&n);
if((n>0)&&(n!=999))
sum=sum+n;
}while(n!=999);
printf("Sum of numbers = %d", sum);
getch();
}
8. Program to read percentage of marks and to display appropriate message (Demonstration
of else-if ladder)
#include<stdio.h>
void main()
{
int per;
clrscr();
printf("Enter the percentage obtained by the student:\n");
scanf("%d",&per);
if(per>=80)
printf("Distinction");
else if(per>=60)
printf("First class");
else if(per>=50)
printf("Second class");
else if(per>=40)
printf("Pass");
else
printf("Fail");
getch();
}
9. Program to find the roots of quadratic equation (demonstration of switch-case statement)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
int n;
clrscr();
printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");
scanf("%f%f%f", &a, &b, &c);
if(a==0)
{
printf("It is not a quadratic equation!");
exit(0);
}
discriminant = (b * b) - (4 * a * c);
if(discriminant>0)
n=0;
else if(discriminant<0)
n=1;
else
n=2;
switch(n)
{
case 0:
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct and real roots exists: %.2f and %.2f",root1, root2);
break;
case 1:
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",root1,
imaginary, root2, imaginary);
break;
case 2:
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
break;
}
getch();
}
10. Program to read marks scored by n students and find the average of marks
(Demonstration of single dimensional array)
#include <stdio.h>
void main()
{
int n,i,a[100],sum=0;
float avg;
clrscr();
printf("Enter the total number of students(n):\n");
scanf("%d",&n);
printf("Enter the total marks of %d students\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
avg=sum/n;
}
printf("The average marks is %f",avg);
getch();
}
11. Program to remove Duplicate Element in a single dimensional Array
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[50],i,j,k, count = 0, dup[50], number;
clrscr();
printf("Enter size of the array\n");
scanf("%d",&number);
printf("Enter Elements of the array:\n");
for(i=0;i<number;i++)
{
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: \n");
for(i=0;i<number;i++)
printf("%d ",a[i]);
for(i=0;i<number;i++)
{
for(j = i+1; j < number; j++)
{
if(a[i] == a[j])
{
for(k = j; k <number; k++)
{
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("\nAfter deleting the duplicate element the Array is:\n");
for(i=0;i<number;i++)
printf("%d ",a[i]);
getch();
}
12. Program to perform addition and subtraction of Matrices
#include<stdio.h>
void main()
{
int n, m, c, d;
int A[10][10], B[10][10], sum[10][10], diff[10][10];
clrscr();
printf("\nEnter the order of the matrix \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &A[c][d]);
printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &B[c][d]);
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", A[c][d]);
}
printf("\n");
}
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", B[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
{
sum[c][d] = A[c][d] + B[c][d];
diff[c][d] = A[c][d] - B[c][d];
}
printf("\n\nThe sum of the two matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}
printf("\n\nThe difference(subtraction) of the two matrices is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}
getch();
}
PART-B
1. Program to find the length of a string without using built in function
#include<stdio.h>
void main()
{
char str[50];
int i,length=0;
clrscr();
printf("Enter a string\n");
gets(str);
for(i=0;str[i]!='\0';i++)
length++;
printf("The length of the string is %d",length);
getch();
}
2. Program to demonstrate string functions.
3. Program to demonstrate pointers in C
4. Program to check a number for prime by defining isprime( ) function
#include<stdio.h>
void main()
{
int n,i;
clrscr();
printf("Enter a number:\n");
scanf("%d",&n);
isprime(n);
getch();
}
isprime(int n)
{
int flag=0,i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(n==1)
printf("1 is neither prime nor composite");
else
{
if(flag==0)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
}
}
5. Program to read, display and to find the trace of a square matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],r,c,i,j,sum=0;
clrscr();
printf("Enter the number of rows and columns:\n");
scanf("%d%d",&r,&c);
if(r==c)
{
printf("Enter %dX%d elements in the matrix\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("The matrix entered is:\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
printf("%6d",a[i][j]);
printf("\n");
}
for(i=0;i<r;i++)
sum=sum+a[i][i];
printf("The trace of the matrix is %d",sum);
}
else
printf("It is not a square matrix!");
getch();
}
6. Program to read, display and add two mxn matrices using functions
#include<stdio.h>
#include<conio.h>
void readarray(int a[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void addarray(int m1[10][10],int m2[10][10],int m3[10][10],int row, int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j]=(m1[i][j]+m2[i][j]);
}
}
}
void printarray(int m[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%6d",m[i][j]);
}
printf("\n");
}
}
main()
{
int m1[10][10],m2[10][10],m3[10][10],row,col;
clrscr();
printf("Enter order of rows:\n");
scanf("%d%d",&row,&col);
printf("Enter %d elements for martix 1: \n",row*col);
readarray(m1,row,col);
printf("Enter %d elements for matrix 2: \n",row*col);
readarray(m2,row,col);
addarray(m1,m2,m3,row,col);
printf("The entered matrix 1 is:\n");
printarray(m1,row,col);
printf("The entered matrix 2 is:\n");
printarray(m2,row,col);
printf("The Additon of matrix is:\n");
printarray(m3,row,col);
getch();
}
7. Program to read, display and multiply two mxn matrices using functions
#include<stdio.h>
#include<conio.h>
void readarray(int a[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void addarray(int m1[10][10],int m2[10][10],int m3[10][10],int row, int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j]=(m1[i][j]+m2[i][j]);
}
}
}
void printarray(int m[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%6d",m[i][j]);
}
printf("\n");
}
}
main()
{
int m1[10][10],m2[10][10],m3[10][10],row,col;
clrscr();
printf("Enter order of rows:\n");
scanf("%d%d",&row,&col);
printf("Enter %d elements for martix 1: \n",row*col);
readarray(m1,row,col);
printf("Enter %d elements for matrix 2: \n",row*col);
readarray(m2,row,col);
addarray(m1,m2,m3,row,col);
printf("The entered matrix 1 is:\n");
printarray(m1,row,col);
printf("The entered matrix 2 is:\n");
printarray(m2,row,col);
printf("The Additon of matrix is:\n");
printarray(m3,row,col);
getch();
}
8. Program to read a string and to find the number of alphabets, digits, vowels, consonants,
spaces and special characters.
#include<stdio.h>
#include<string.h>
void main()
{
char s[1000];
int i,alphabets=0,digits=0,specialchar=0;
int blankspaces=0,vowels=0,consonents=0;
clrscr();
printf("Enter the string:\n");
gets(s);
for(i=0;s[i]!=0;i++)
{
if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122))
{
alphabets++;
if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'||s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U')
vowels++ ;
else
consonents++;
}
else if(s[i]>=48&&s[i]<=57)
digits++;
else if(s[i]==32)
blankspaces++;
else
specialchar++;
}
printf("The number of alphabets are %d\n",alphabets);
printf("The number of vowels are %d\n",vowels);
printf("The number of consonents are %d\n",consonents);
printf("The number of digits are %d\n",digits);
printf("The number of blankspaces are %d\n",blankspaces);
printf("The number of special characters are %d\n",specialchar);
getch();
}
9. Program to Reverse a String using Pointer
#include<stdio.h>
int strlength(char*);
void reverse(char*);
main()
{
char s[100];
clrscr();
printf("Enter a string\n");
gets(s);
reverse(s);
printf("The reverse of the string entered is\n %s\n",s);
getch();
return 0;
}
void reverse(char *s)
{
int len,c;
char *begin,*end,temp;
len=strlength(s);
begin=s;
end=s;
for(c=0;c<len-1;c++)
end++;
for(c=0;c<len/2;c++)
{
temp=*end;
*end=*begin;
*begin=temp;
begin++;
end--;
}
}
int strlength(char *ptr)
{
int c=0;
while(*(ptr+c)!='\0')
c++;
return c;
}
10. Program to Swap Two Numbers using Pointers
#include<stdio.h>
void swap(int*,int*);
void main()
{
int a,b;
clrscr();
printf("Enter values for a and b\n");
scanf("%d%d",&a,&b);
printf("Before swapping:a=%d and b=%d\n",a,b);
swap(&a,&b);
printf("After swapping :a=%d and b=%d\n",a,b);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
11. Program to demonstrate student structure to read & display records of n students.
#include<stdio.h>
struct student
{
char name[50];
int roll;
}s[2];
void main()
{
int i=0,n;
char ch='y';
clrscr();
printf("Enter student information\n");
while(ch=='y'||ch=='Y')
{
printf("Enter the roll number of the student:\n");
scanf("%d",&s[i].roll);
printf("Enter the name of the student:\n");
scanf("%s",s[i].name);
i++;
printf("\n Do you Want to continue [y/n] :");
fflush(stdin);
scanf("%c",&ch);
}
n=i;
printf("\nThe students information entered are:\n");
printf("\nRoll Number \t NAME \n");
for(i=0;i<n;i++)
printf("%-5d\t\t%-20s \n",s[i].roll,s[i].name);
getch();
}
12. Program to demonstrate the difference between structure & union.