0% found this document useful (0 votes)
90 views26 pages

C PROGRAMMING LAB MANUAL at KALYAN

This document contains 15 experiments for a C programming laboratory manual. Each experiment includes the objective, sample code, and comments for a program related to basic C programming concepts like: calculating sums and factorials, generating Fibonacci sequences and prime numbers, matrix operations, file I/O, complex number arithmetic, and more. The experiments provide hands-on practice with essential programming skills using structured programming principles in C.

Uploaded by

karanmandrai8643
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views26 pages

C PROGRAMMING LAB MANUAL at KALYAN

This document contains 15 experiments for a C programming laboratory manual. Each experiment includes the objective, sample code, and comments for a program related to basic C programming concepts like: calculating sums and factorials, generating Fibonacci sequences and prime numbers, matrix operations, file I/O, complex number arithmetic, and more. The experiments provide hands-on practice with essential programming skills using structured programming principles in C.

Uploaded by

karanmandrai8643
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PARALA MAHARAJA ENGINEERING COLLEGE, BERHAMPUR

Dept. of Computer Sc. & Engg.

“C PROGRAMMING”
LABORATORY MANUAL

Kalyan Kumar Jena


C PROGRAMMING LAB
Experiment No. 1
Write a C program to find the sum of individual digits of a positive integer.

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

Using Recursive Function

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

Using Non-Recursive Function

#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

Using Recursive Function

#include<stdio.h>

#include<conio.h>

int gcd (int,int);

void main( )

int a,b,k;

clrscr( );

printf(“enter two numbers”);

scanf(“%d%d”,&a,&b);

k=gcd(a,b);

printf(“the gcd of two numbers is %d”,k);

getch( );

int gcd(int x,int y)

if(y= =0)

return (x);

else
{

gcd(y,x%y);

PROGRAM

Using Non-Recursive Function

#include<stdio.h>

#include<conio.h>

int gcd (int,int);

void main( )

int m,n,k;

clrscr( );

printf(“enter two numbers”);

scanf(“%d%d”,&m,&n);

k=gcd(m,n);

printf(“the gcd of two numbers is %d”,k);

getch( );

int gcd(int x,int y)

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;

printf(“specify the value of n”);

scanf(“%d”,&n);

printf(“enter the elements”);

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

printf(“the greast number=%d”,g);

printf(“the smallest number=%d”,s);

getch();

}
Experiment No.9

PROGRAM

#include<stdio.h>

#include<conio.h>

void add(int [ ][ ],int [ ][ ]);

void main( )

int x[3][3],y[3][3],i,j;

clrscr();

printf(“enter elements to 1st matrix”);

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

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

scanf(“%d”,&x[i][j]);

printf(“enter elements to 2nd matrix”);

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

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

scanf(“%d”,&y[i][j]);

}
}

add(x,y);

getch( );

void add(int a[ ][ ],int b[ ][ ])

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

printf(“the resultant matrix is \n \n”);

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 mul(int [ ][ ],int [ ][ ]);

void main( )

int x[3][3],y[3][3],i,j;

clrscr();

printf(“enter elements to 1st matrix”);

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

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

scanf(“%d”,&x[i][j]);

printf(“enter elements to 2nd matrix”);

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

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

scanf(“%d”,&y[i][j]);

}
mul(x,y);

getch( );

void mul(int a[ ][ ],int b[ ][ ])

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

printf(“the resultant matrix is \n \n”);

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)

printf(“the string is palindrome”);

else

printf(“the string is not palindrome”);

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

printf(“total number of characters=%d \n”,c);

printf(“total number of spaces=%d \n”,s);

printf(“total number of lines=%d \n”,l);

getch( );

}
Experiment No.14

PROGRAM

#include<stdio.h>

#include<conio.h>

struct complex

float real,imag;

};

void main( )

struct complex c1,c2,c3,c4;

printf(“enter the real and imaginary part of 1st complex no”);

scanf(“%f%f”.&c1.real,&c1.imag);

printf(“enter the real and imaginary part of 2nd complex no”);

scanf(“%f%f”.&c2.real,&c2.imag);

printf(“the first complex number is %f +i %f \n”,c1.real,c1.imag);

printf(“the second complex number is %f +i %f \n”,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;

printf(“\n the result of addition of two complex no is %f +i %f \n”,c3.real,c3.imag);

printf(“\n the result of subtraction of two complex no is %f +i %f \n”,c4.real,c4.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( );

You might also like