C Lab Manual
C Lab Manual
LAB MANUAL
Course Name : C-Programming Lab
Year : I Year BCA / I Year B.Sc
Semester : I Semester
Prepared by
Mamatha C MCA
Dept of Computer Science and Applications
Govt Science College(Autonomous)
Hassan
List of programs
PART A
1. Write a C Program to read radius of a circle and to find area and
circumference
2. Write a C Program to read three numbers and find the biggest of three
3. Write a C Program to demonstrate library functions in math.h
4. Write a C Program to generate n primes
5. Write a C Program to read a number, find the sum of the digits,
reverse the number and check it for palindrome
6. Write a C Program to read numbers from keyboard continuously
till the user presses 999 and to find the sum of only positive
numbers
7. Write a C Program to read percentage of marks and to display
appropriate message (Demonstration of else-if ladder)
8. Write a C Program to find the roots of quadratic equation
(demonstration of switch-case statement)
9. Write a C Program to remove Duplicate Element in a single dimensional
Array
10. Program to perform addition and subtraction of Matrices
PART B
1. Write a C Program to find the length of a string without using built in
function
2. Write a C Program to demonstrate string functions.
3. Write a C Program to check a number for prime by defining isprime( )
function
4. Write a C Program to read, display and to find the trace of a square matrix
5. Write a C Program to read, display and multiply two matrices using functions
6. Write a C Program to read a string and to find the number of
alphabets, digits, vowels, consonants, spaces and special
characters.
7. Write a C Program to Reverse a String using Pointer
8. Write a C Program to Swap Two Numbers using Pointers
9. Write a C Program to demonstrate student structure to read &
display records of n students.
10. Write a C Program to demonstrate the difference between structure & union.
PART A
1.Write a C Program to read radius of a circle and to find area and circumference
PROGRAM IMPLEMENTATION:
/* Program to read radius of a circle and to find area and
circumference */
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float PI=3.14,area,circum;
clrscr();
printf(“Enter the radius of a circle\n”);
scanf(“%d”,&r);
area=PI*r*r;
circum=2*PI*r;
printf(“Area of a circle=%f\n”,area);
printf(“Circumference of a circle =%f\n”,circum);
getch();
}
OUTPUT:
2.Write a C Program to read three numbers and find the biggest of three
PROGRAM IMPLEMENTATION :
/* C Program to read three numbers and find the biggest of three */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter any 3 integer numbers\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b && a>c)
printf(“%d is biggest\n”,a);
else if(b>c)
printf(“%d is biggest\n”,b);
else
printf(“%d is biggest\n”,c);
getch();
}
3. Write a C Program to demonstrate library functions in math.h
ALGORITHM: To find squareroot, power using library function
math.h
Step 1: Start
Step 2: Input x
Step 3: Calculate SqRt, Powr
SqRt=sqrt(x);
Powr=pow(x,3);
Step 4: print SqRt
Step 5: print Powr
Step 6: Stop
PROGRAM IMPLEMENTATION:
/* C Program to demonstrate library functions using math.h */
#include<stdio.h>
#include<math.h>
void main()
{
int x;
float SqRt,Powr;
clrscr();
printf("Enter any integer value for x\n");
scanf("%d",&x);
SqRt=sqrt(x);
printf("The Square Root of %d is %f\n",x,SqRt);
Powr=pow(x,3); //Returns the value of x power 3(x*x*x)
printf("The value of %d power 3 is %f\n",x,Powr);
getch();
}
OUTPUT
4.Write a C Program to generate n primes
PROGRAM IMPLEMENTATION
/* C Program to generate n primes */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,count=1,flag,i=2,j;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("The first %d prime numbers are:\n",n);
while(count<=n)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j == 0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
return 0;
}
OUTPUT:
5.Write a C Program to read a number, find the sum of the digits, reverse
the number and check it for palindrome
ALGORITHM: Find the sum of the digits, reverse the number and check it
for palindrome.
Step 1: Start
Step 2 : Read a valid integer number ‘n’
Step 3: Set rev=0,count=0,sum=0
Step 4: Let temp= n
Step 5: do
Rem= n%10
Rev=rev*10+rem
Sum=sum+rem
Count=count+1
N=n/10
While n>0
done
print “The given number(temp)”
print “Reverse of a given number(rev)”
print “Sum of digits of the given number(sum)”
print “The number of digits(count)”
if t=rev then
print “The given number is palindrome”
else
print “The given number is not palindrome”
fi
Step 6: Stop
PROGRAM IMPLEMENTATION
/* C Program to read a number, find the sum of the digits, reverse the number and
check it for palindrome */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev=0, count=0,sum=0,t,rem;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
t=n;
do
{
rem=n%10;
rev=rev*10+rem;
sum=sum+rem;
count=count+1;
n=n/10;
}
while(n>0);
printf("The given number is = %d\n",t);
printf("Reverse of a given number is = %d\n",rev);
printf("Sum of digits of the given number = %d\n",sum);
printf("Number of digits = %d\n",count);
if(t==rev)
printf("The given number is palindrome\n");
else
printf("The given number is not a palindrome\n");
getch();
}
OUTPUT:
6.Write a C Program to read numbers from keyboard continuously till the
user presses 999 and to find the sum of only positive numbers
PROGRAM IMPLEMENTATION :
/* C Program to read numbers from keyboard continuously till the user
presses 999 and to find the sum of only positive numbers */
#include <stdio.h>
#include <conio.h>
int main()
{
int a[50],sum=0,j,n,i,count=0;
clrscr();
printf("Input the size of the arry \n");
scanf("%d",&n);
printf("Input the numbers:\n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
count++;
if(a[i]==999)
{
printf("\n The condition of termination occured\n");
break;
}
}
for(i=0; i<count; i++)
{
if(a[i] > 0)
{
sum += a[i];
}
}
printf("\nThe sum of positive integer numbers is : %d",sum);
printf("\n");
getch();
return 0;
}
OUTPUT:
7.Write a C Program to read percentage of marks and to display
appropriate message (Demonstration of else-if ladder)
PROGRAM IMPLEMENTATION
/* C Program to read percentage of marks and to display appropriate
message (Demonstration of else-if ladder) */
#include<stdio.h>
void main()
{
float per;
clrscr();
printf("Enter a valid percentage of marks\n");
scanf("%f",&per);
if(per>0 && per<=100)
{
if(per>=75 && per<=100)
printf("It's a DISTINCTION!!!\n");
else if(per>=60 && per<75)
printf("It's a FIRST CLASS!!!\n");
else if(per>=35 && per<60)
printf("It's a SECOND CLASS!!!\n");
else
printf("FAIL!!!!\n");
}
else
{
printf("INVALID PERCENTILE!!!\n");
getch();
}
getch();
}
OUTPUT:
8.Write a C Program to find the roots of quadratic equation
(demonstration of switch-case statement)
PROGRAM IMPLEMENTATION:
/* C Program to find the roots of quadratic equation*/
#include<stdio.h>
#include<math.h> /* Used for sqrt() */
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
clrscr();
/* Calculate discriminant */
discriminant = (b * b) - (4 * a * c);
case 0:
/* If discriminant is not positive */
switch(discriminant < 0)
{
case 1:
/* If discriminant is negative */
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);
getch();
break;
case 0:
/* If discriminant is zero */
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
getch();
break;
}
}
return 0;
}
OUTPUT:
9.Write a C Program to remove Duplicate Element in a single dimensional Array
PROGRAM IMPLEMENTATION:
/* C Program to remove Duplicate Element in a single dimensional Array*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,j,k,n;
clrscr();
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
j--;
}
}
}
printf("Array elements after deletion of duplicate elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
return 0;
}
OUTPUT:
10.Program to perform addition and subtraction of Matrices
PROGRAM IMPLEMENTATION:
/* C Program to perform addition and subtraction of Matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10],i,j,m,n,p,q;
clrscr();
printf("Enter the order of the first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the order of the second matrix\n");
scanf("%d%d",&p,&q);
if(m==p && n==q)
{
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of second matrix\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("The first matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The second matrix is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
d[i][j]=0;
d[i][j]=a[i][j]-b[i][j];
}
}
printf("Addition of two matrices is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("Subtraction of two matrices is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
}
else
printf("Addition and Subtraction of two matrices cannot be performed\n");
getch();
}
OUTPUT:
PART B
1. Write a C Program to find the length of a string without using built in function
PROGRAM IMPLEMENTATION:
/* C Program to find the length of a string without using built in function */
#include<stdio.h>
int main()
{
char s[]= "I am doing good";
int i;
clrscr();
for(i=0;s[i]!='\0';i++);
printf("Length of the String is %d\n",i);
getch();
return 0;
}
OUTPUT:
2. Write a C Program to demonstrate string functions.
PROGRAM IMPLEMENTATION:
/* C Program to demonstrate string functions*/
#include<stdio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int l1,l2;
clrscr();
puts("Enter 1st string");
gets(s1);
puts("Enter 2nd string");
gets(s2);
l1=strlen(s1);
printf("Length of the string S1 is %d\n", l1);
l2=strlen(s2);
printf("Length of the string S2 is %d\n",l2);
strcat(s1,s2);
printf("The concatenated String is: %s\n",s1);
strcpy(s1,s2);
printf("The copied string is : %s\n",s1);
getch();
}
OUTPUT:
3.Write a C Program to check a number for prime by defining isprime( ) function
PROGRAM IMPLEMENTATION:
/* C Program to check a number for prime by defining isprime( ) function*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,flag=0;
clrscr();
printf("Enter any integer number\n");
scanf("%d",&n);
flag=isPrime(n);
if(flag==0)
printf("\n %d is a Prime number\n",n);
else
printf("\n %d is not a Prime number\n",n);
getch();
}
int isPrime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i != 0)
continue;
else
return 1;
}
return 0;
}
OUTPUT:
4. Write a C Program to read, display and to find the trace of a square matrix
PROGRAM IMPLEMENTATION:
/* C Program to read, display and to find the trace of a square matrix */
#include<stdio.h>
void main()
{
int a[10][10],m,i,j,trace;
clrscr();
printf("Enter the order of a matrix\n");
scanf("%d",&m);
printf("Enter the elements for a square matrix\n");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("The square matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
trace=0;
for(i=0;i<m;i++)
trace=trace+a[i][i];
printf("\n Trace of the given square matrix = %d\n",trace);
getch();
}
OUTPUT:
5. Write a C Program to read, display and multiply two matrices using functions
PROGRAM IMPLEMENTATION:
/* C Program to read, display and multiply two matrices using functions */
#include<stdio.h>
void readMat(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void mulMat(int a[][10],int b[][10],int prod[][10],int m,int n,int q)
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<q;j++)
prod[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
prod[i][j]=prod[i][j]+a[i][k]*b[k][j];
}
}
}
}
void dispMat(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
int main()
{
int a[10][10],b[10][10],prod[10][10],m,n,p,q;
clrscr();
printf("Enter order for 1st matrix\n");
scanf("%d%d",&m,&n);
printf("Enter order for 2nd matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Multiplication of 2 matrices is impossible\n");
getch();
exit();
}
printf("Enter elements for 1st matrix\n");
readMat(a,m,n);
printf("Enter elements for 2nd matrix\n");
readMat(b,p,q);
printf("1st matrix is:\n");
dispMat(a,m,n);
printf("2nd matrix is:\n");
dispMat(b,p,q);
mulMat(a,b,prod,m,n,q);
printf("The product matrix is:\n");
dispMat(prod,m,q);
getch();
return 0;
}
OUTPUT:
6. Write a C Program to read a string and to find the number of alphabets,
digits, vowels, consonants, spaces and special characters.
PROGRAM IMPLEMENTATION:
/* C 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>
int main()
{
char s[100];
int i, vow=0,cons=0,digit=0,space=0,symb=0,alph=0;
clrscr();
printf("Enter the string\n");
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]== 'u' || s[i]=='A' || s[i]=='E' || s[i]=='I'
|| s[i]=='O' || s[i]=='U')
vow++;
else if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'))
cons++;
else if(s[i]>='0' && s[i]<='9')
digit++;
else if(s[i]==' ')
space++;
else
symb++;
if((s[i]>=65 && s[i]<=90) || (s[i]>=97 && s[i]<=122))
alph++;
}
printf("No. of alphabets=%d\n",alph);
printf("No. of Vowels=%d\n",vow);
printf("No. of Consonents=%d\n",cons);
printf("No. of digits=%d\n",digit);
printf("No. of Spaces=%d\n",space);
printf("No. of Symbols=%d\n",symb);
getch();
return 0;
}
OUTPUT:
7.Write a C Program to Reverse a String using Pointer
PROGRAM IMPLEMENTATION:
/* C Program to Reverse a String using Pointer */
#include<stdio.h>
#include<string.h>
void strRev(char *s)
{
int l,i;
char *start,*end,temp;
l=strlen(s);
start=s;
end=s;
for(i=0;i<l-1;i++)
end++;
for(i=0;i<l/2;i++)
{
temp=*end;
*end=*start;
*start=temp;
start++;
end--;
}
}
int main()
{
char s[100];
clrscr();
printf("Enter a string\n");
gets(s);
strRev(s);
printf("The reverse of a string is :%s\n",s);
getch();
return 0;
}
OUTPUT:
8.Write a C Program to Swap Two Numbers using Pointers
PROGRAM IMPLEMEMNTATION:
/* C Program to Swap Two Numbers using Pointers*/
#include<stdio.h>
void main()
{
int x,y,*a,*b,temp;
clrscr();
printf("Enter any 2 integer numbers\n");
scanf("%d%d",&x,&y);
printf("Before swapping\nx=%d\ty=%d\n",x,y);
a=&x;
b=&y;
temp=*b;
*b=*a;
*a=temp;
printf("After swapping\nx=%d\ty=%d\n",x,y);
getch();
}
OUTPUT:
9.Write a C Program to demonstrate student structure to read & display
records of n students.
PROGRAM IMPLEMENTATION:
/* C Program to demonstrate student structure to read & display records
of n students.*/
#include<stdio.h>
#include<conio.h>
struct student
{
char name[100];
int rollno;
int sem;
int marks;
}s[100];
int main()
{
int i,n;
clrscr();
printf("\nEnter the number of students\n");
scanf("%d",&n);
printf("\nEnter student's information one by one:\n");
for(i=0;i<n;i++)
{
printf("Enter name\n");
scanf("%s",s[i].name);
printf("Enter roll number\n");
scanf("%d",&s[i].rollno);
printf("Enter the semester\n");
scanf("%d",&s[i].sem);
printf("Enter percentage of marks obtained\n");
scanf("%d",&s[i].marks);
}
printf("\tTHE STUDENT DETAILS ARE:\n\n");
for(i=0;i<n;i++)
{
printf("\t Name = %s\n",s[i].name);
printf("\t Roll Number = %d\n",s[i].rollno);
printf("\t Semester = %d\n",s[i].sem);
printf("\t Percentage of marks scored = %d\n\n",s[i].marks);
}
getch();
return 0;
}
OUTPUT:
10.Write a C Program to demonstrate the difference between structure &
union
PROGRAM IMPLEMENTATION:
/* C Program to demonstrate the difference between structure & union */
#include<stdio.h>
struct student
{
int age;
char name[50];
int sem;
float marks;
};
union students
{
int ages;
char names[50];
int semester;
float markss;
};
void main()
{
struct student s;
union students s1;
printf("The size of Student STRUCTURE = %d\n",sizeof(s));
printf("The size of Students UNION = %d\n",sizeof(s1));
getch();
}
OUTPUT: