C PROGRAMMING LAB MANUAL at KALYAN
C PROGRAMMING LAB MANUAL at KALYAN
“C PROGRAMMING”
LABORATORY MANUAL
Experiment No. 2
A Fibonacci sequence is defined as follows: the first and second terms in the sequence are 0 and
1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.
Experiment No. 3
Write a C program to generate all the prime numbers between 1 and n, where n is a value
supplied by the user.
Experiment No. 4
Write a C program to calculate the following Sum:
Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! ……………
Experiment No. 5
Write a C program to find the roots of a quadratic equation.
Experiment No. 6
Write C programs that use both recursive and non-recursive functions to find the factorial of a
given integer.
Experiment No. 7
Write C programs that use both recursive and non-recursive functions to find the GCD (greatest
common divisor) of two given integers.
Experiment No. 8
Write a C program to find both the larges and smallest number in a list of integers.
Experiment No. 9
Write a C program that uses functions to perform the following:
Addition of Two Matrices
Experiment No. 10
Write a C program that uses functions to perform the following:
Multiplication of Two Matrices
Experiment No. 11
Write a C program to determine if the given string is a palindrome or not.
Experiment No. 12
Write a C program to construct a pyramid of numbers.
Experiment No. 13
Write a C program to count the lines, spaces and characters in a given text.
Experiment No.14
Write a C program that uses the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv)Subtraction of two complex numbers
( represent complex number using a structure.)
Experiment No. 15
Write a program which copies one file to another.
Experiment No.1
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,s;
clrscr( );
s=0;
printf(“Enter the number”);
scanf(“%d”,&a);
while(a>0)
{
b=a%10;
s=s+b;
a=a/10;
}
printf(“The sum of digits of the entered number=%d”,s);
getch( );
}
Experiment No.2
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
{
int a=0,b=1,c=0,i,n;
clrscr( );
printf(“enter the range”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“%d”,c);
a=b;
b=c;
c=a+b;
}
getch( );
}
Experiment No.3
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,k;
clrscr( );
printf(“enter the upper limit”);
scanf(“%d”,&n);
printf(“the prime number in between 1 and %d are \n”,n);
for(i=1;i<=n;i++)
{
k=2;
while(k<i)
{
if(i%k= =0)
{
break;
}
k++;
}
if(k= =i)
printf(“%d”,i);
}
getch( );
}
Experiment No.4
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int x,n,i,j=0,f=1;
float s=0;
clrscr( );
printf(“enter the value of x and n”);
scanf(“%d%d”,&x,&n);
for(i=1;i<=n;i++)
{
if(i= =1)
{
s=s+f;
}
else
{
f=f*(j+1)*(j+2);
s=s+pow(-1,i+1)*pow(x,j+2)/(float)f;
j=j+2;
}
}
printf(“the series output is %f”,s);
getch( );
}
Experiment No.5
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int a,b,c;
float r1,r2,d;
clrscr( );
printf(“enter the value of a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);
d=sqrt(b*b-4*a*c);
r1=(-b+d)/2*a;
r2=(-b-d)/2*a;
printf(“root 1=%f”,r1);
printf(“root2=%f”,r2);
getch( );
}
Experiment No.6
PROGRAM
#include<stdio.h>
#include<conio.h>
int fact(int);
void main( )
{
int n,k;
clrscr( );
printf(“enter the number”);
scanf(“%d”,&n);
k=fact(n);
printf(“the factorial of number=%d”,k);
getch();
}
int fact(int x)
{
if(x= =0)
{
return(1);
}
else
{
return(x*fact(x-1));
}
}
#include<stdio.h>
#include<conio.h>
int fact(int);
void main( )
{
int n,k;
clrscr( );
printf(“enter the number”);
scanf(“%d”,&n);
k=fact(n);
printf(“the factorial of number=%d”,k);
getch();
}
int fact(int x)
{
int i,f=1;
for(i=1;i<=x;i++)
{
f=f*i;
}
return(f);
}
Experiment No.7
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
int a,b,k;
clrscr( );
scanf(“%d%d”,&a,&b);
k=gcd(a,b);
getch( );
if(y= =0)
return (x);
else
{
gcd(y,x%y);
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
int m,n,k;
clrscr( );
scanf(“%d%d”,&m,&n);
k=gcd(m,n);
getch( );
int a,b;
a=x;
b=y;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
return (a);
}
Experiment No.8
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
int a[30],i,n,g,s;
scanf(“%d”,&n);
for(i=0;i<n;i++)
Scanf(“%d”,&a[i]);
g=a[0];
s=a[0];
for(i=1;i<n;i++)
if(a[i]>g)
g=a[i];
if(a[i]<s)
{
s=a[i];
getch();
}
Experiment No.9
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
int x[3][3],y[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&x[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&y[i][j]);
}
}
add(x,y);
getch( );
int i,j,c[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,c[i][j]);
printf(“\n”);
}
Experiment No.10
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
int x[3][3],y[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&x[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”,&y[i][j]);
}
mul(x,y);
getch( );
int i,j,k,c[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=0;
for(k=;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,c[i][j]);
printf(“\n”);}
Experiment No.11
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
char a[50],b[50];
int i,k=0;
printf(“enter a string”);
gets(a);
for(i=strlen(a)-1;i>=0;i- -)
b[k++]=a[i];
b[k]=’\0’;
if(strcmp(a,b)= = 0)
else
getch( );}
Experiment No.12
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
int i,j;
clrscr( );
for(i=1;i<=4;i++)
for(j=4-i;j>=1;j- -)
Printf(“ “);
for(j=1;j<=i;j++)
printf(“%d”,j);
for(j=i-1;j>=1;j- -)
printf(“%d”,j);
printf(“\n”);
getch( );}
Experiment No.13
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
FILE *fp;
int l=0;s=0;c=0;
char ch;
fp=fopen(“x.txt”,”r”);
if (fp= = NULL)
printf(“error in reading”);
exit(0);
while(ch=fgetc(fp)!=EOF)
c++;
if(ch= =”\n”)
l++;
if(ch==32)
s++;
}
fclose(fp);
getch( );
}
Experiment No.14
PROGRAM
#include<stdio.h>
#include<conio.h>
struct complex
float real,imag;
};
void main( )
scanf(“%f%f”.&c1.real,&c1.imag);
scanf(“%f%f”.&c2.real,&c2.imag);
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
c4.real=c1.real-c2.real;
c4.imag=c1.imag-c2.imag;
getch( );}
Experiment No.15
PROGRAM
#include<stdio.h>
#include<conio.h>
void main( )
FILE *fs,*fd;
char ch;
fs=fopen(“x.txt”,”r”);
if (fs= = NULL)
printf(“error in reading”);
exit(0);
fd=fopen(“y.txt”,”w”);
if (fd= = NULL)
printf(“error in reading”);
exit(0);
while(ch=fgetc(fs)!=EOF)
fputc(ch,fd);
}
fclose all ( );
getch( );