PCD Lab Programs
PCD Lab Programs
PCD Lab Programs
#include<stdio.h>
int main()
{
int n,m,rev=0,digit;
printf("Enter the Integer number\n");
scanf("%d",&n);
m=n;
while(n!=0)
{
digit = n%10;
n = n/10;
rev = rev*10+digit;
}
printf("Reverse of a given number=%d\n",rev);
if(m == rev)
printf("Given number %d is Palindrome\n",m);
else
printf("Given number %d is not Palindrome\n",m);
}
Output:
/*Program 3a: C Program to find the square root of a given number N */
#include<stdio.h>
void main()
{
float n,x,i;
printf("Enter the value n \n");
scanf("%f",&n);
x=n;
for(i=0;i<20;i++)
{
x=(x*x+n)/(2*x);
}
printf("Square Root of %f is %f\n",n,x);
}
Output:
/*Program 3b: C Program to read a year as an input and find whether
it is leap year or not. Also consider end of the centuries. */
#include<stdio.h>
int main()
{
int year;
printf("Enter valid year\n");
scanf("%d",&year);
if((year%4==0) && ((year%100!=0) || (year%400==0)))
{
printf("%d is a leap year\n",year);
}
else
{
printf("%d is not a leap year\n",year);
}
}
Output:
/* Program 4: C Program to evaluate polynomial f(x) = a 4 x 4 + a 3 x
3 + a 2 x 2 + a 1 x + a 0 , for a given value of x and its
coefficients using Horner’s method. */
#include<stdio.h>
int main()
{
int n,a[100],x,sum,i;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter n+1 co efficients\n");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the value of x\n");
scanf("%d",&x);
sum = 0;
sum = a[n]*x;
for(i=n-1;i>=1;i--)
{
sum = (sum+a[i])*x;
}
sum = sum + a[0];
printf("Sum = %d\n",sum);
}
Output:
/* Program 5: C Program to compute Sin(x) using Taylor series
approximation given by Sin(x) = x - (x3 /3!) + (x5 /5!) - (x7 /7!) +
....... Compare your result with the built- in Library function. Print
both the results with appropriate messages. */
#include<stdio.h>
#include<math.h>
void main()
{
int n,i;
float x,deg,sum,term;
printf("Enter degree and number\n");
scanf("%f%d",°,&n);
x = deg*3.14/180;
term = x;
sum = term;
for(i=3;i<=n;i+=2)
{
term =- term*x*x/((i-1)*i);
sum = sum+term;
}
printf("Sin(%f)=%f\n",deg,sum);
printf("Sin(%f)=%f\n",deg,sin(x));
}
Output:
/* Prohram 6: C program to reads N integer numbers and arrange them in
ascending order using Bubble Sort. */
#include<stdio.h>
int main()
{
int a[10],i,j,n,temp;
printf("Enter the array size\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Entered array elements are as follows\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=1;i<=n-1;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(a[j] >= a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Sorted array elements are as follows\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
Output:
/* Program 7: C program to reads two matrices A (m x n ) and B (p x q
) and Compute product of matrices A and B. Read matrix A and matrix B
in row major order and in column major order respectively. Print both
the input matrices and resultant matrix with suitable headings and
output should be in matrix format only. Program must check the
compatibility of orders of the matrices for multiplication. Report
appropriate message in case of incompatibility. */
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q,sum;
printf("Enter the value of m and n\n");
scanf("%d%d",&m,&n);
printf("Enter the elements for Matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf(" The Matrix A is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the value of p and q\n");
scanf("%d%d",&p,&q);
printf("Enter the elements for Matrix B\n");
for(j=0;j<q;j++)
{
for(i=0;i<p;i++)
{
scanf("%d",&b[i][j]);
}
}
printf(" The Matrix B is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
if(n != p)
{
printf("The Matrix Multiplication Not Possible\n");
exit(0);
}
printf("\n Matrix Multiplication Possible");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum = 0;
for(k=0;k<n;k++)
{
sum = sum + (a[i][k] * b[k][j]);
}
c[i][j] = sum;
}
}
printf("The Resultant Matrix C is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
/* Program 8: C Program to search a Name in a list of names using
Binary searching Technique. */
#include<stdio.h>
#include<string.h>
int main()
{
char name[10][20],key[20];
int mid,high,low,n,found=0,i;
printf("Enter the number of names to be read,n = ");
scanf("%d",&n);
printf("Enter the names ascending order:\n");
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
printf("Enter the name to be Searched:\n");
scanf("%s",key);
low = 0;
high = n-1;
while(low <= high)
{
mid = (low+high)/2;
if(strcmp(name[mid],key)==0)
{
printf("Search is Successful\n");
printf("The %s is found in the Position %d\n",name[mid],mid+1);
found = 1;
break;
}
else if(strcmp(name[mid],key)<0)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if(found == 0)
{
printf("Unsuccessful Search\n");
printf(" %s is not present in the list\n",key);
}
}
Output:
/* Program 9a: C Program to Implements string copy operation
STRCOPY(str1,str2) that copies a string str1 to another string str2
without using library function. */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void STRCOPY(char s1[50],char s2[50])
{
int i=0;
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
}
void main()
{
char str1[50],str2[50];
printf("Enter the source String\n");
gets(str1);
STRCOPY(str1,str2);
printf("Destination String is \n");
puts(str2);
}
Output:
/* Program 9b: C Program to Read a sentence and print frequency of
vowels and total count of consonants. */
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
int ac=0,ec=0,ic=0,oc=0,uc=0,cc=0,i;
printf("Enter the sentence\n");
gets(s);
for(i=0;i<strlen(s);i++)
{
if(isalpha(s[i]))
{
if(s[i]=='a' || s[i]=='A')
{
ac++;
}
else if(s[i]=='e' || s[i]=='E')
{
ec++;
}
else if(s[i]=='i' || s[i]=='I')
{
ic++;
}
else if(s[i]=='o' || s[i]=='O')
{
oc++;
}
else if(s[i]=='u' || s[i]=='U')
{
uc++;
}
else
{
cc++;
}
}
}
printf("Number of vowel a is = %d\n",ac);
printf("Number of vowel e is = %d\n",ec);
printf("Number of vowel i is = %d\n",ic);
printf("Number of vowel o is = %d\n",oc);
printf("Number of vowel u is = %d\n",uc);
printf("Number consonants = %d\n",cc);
}
Output:
/* Program 10a. C function RightShift(x ,n) that takes two integers x
and n as input and returns value of the integer x rotated to the right
by n positions. Assume the integers are unsigned. Write a C program
that invokes this function with different values for x and n and
tabulate the results with suitable headings. */
#include<stdio.h>
#include<math.h>
unsigned int rightshift(unsigned int x, int n)
{
int i;
for(i=0;i<n;i++)
{
if(x%2 == 0)
{
x = x>>1;
}
else
{
x = x>>1;
//x=x+32768;
x = x + pow(2,31);
}
}
return x;
}
int main()
{
unsigned int x[10],res;
int n[10],p,choice,i;
printf("Enter the size\n");
scanf("%d",&p);
for(i=0;i<p;i++)
{
printf("Enter an unsigned integer value <= 65535\n");
scanf("%u",&x[i]);
printf("Enter how many bits you want to rotate\n");
scanf("%d",&n[i]);
}
for(i=0;i<p;i++)
{
res = rightshift(x[i],n[i]);
printf("Rightrotate(%u,%d)=%u\n",x[i],n[i],res);
}
}
Output:
/* Program 10b. C function isprime(num) that accepts an integer
argument and returns 1 if the argument is prime, a 0 otherwise. Write
a C program that invokes this function to generate prime numbers
between the given range. */
#include<stdio.h>
int isprime(int x)
{
int i;
if(x==0 || x==1)
{
return 0;
}
for(i=2;i<=x/2;i++)
{
if(x%i==0)
{
return 0;
}
}
return 1;
}
int main()
{
int p,q,i;
printf("Enter the range \n");
scanf("%d%d",&p,&q);
printf("The prime numbers between %d and %d are \n",p,q);
for(i=p;i<=q;i++)
{
if(isprime(i)==1)
{
printf("%d \n",i);
}
}
return 0;
}
Output:
/* Program 11: C function to find the factorial of a number, n!,
defined by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using
this function, write a C program to compute the binomial coefficient n
C r . Tabulate the results for different values of n and r with
suitable messages. */
#include<stdio.h>
int fact(int n)
{
if(n == 0)
{
return 1;
}
return (n*fact(n-1));
}
int main()
{
int n,r,res,m,i;
printf("Enter the different set of numbers\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the value of n and r \n");
scanf("%d%d",&n,&r);
if(n>=r)
{
res = fact(n)/(fact(n-r)*fact(r));
printf("%dC%d(nCr) = %d \n",n,r,res);
}
else
{
printf(" n sholud be <= r\n");
}
}
return 0;
}
Output:
/* Program 12: Given two university information files
“studentname.txt” and “usn.txt” that contains students Name and USN
respectively. C program to create a new file called “output.txt” and
copy the content of files “studentname.txt” and “usn.txt” into output
file in the sequence shown below. Display the contents of output file
“output.txt” on to the screen. */
#include<stdio.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char usn[20],name[20];
fp1 = fopen("studname.txt","r");
if(fp1 == NULL)
{
printf("File not found \n");
}
fp2 = fopen("studusn.txt","r");
if(fp2== NULL)
{
printf("File not found \n");
}
fp3 = fopen("output.txt","w");
while( !feof(fp1) && !feof(fp2))
{
fscanf(fp1,"%s",name);
fscanf(fp2,"%s \n",usn);
fprintf(fp3,"%-15s %10s \n",name,usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("output.txt","r");
printf("\n------------------------------\n");
printf(" NAME\t\t\tUSN\n");
printf("--------------------------------\n");
while(!feof(fp3))
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s \n",usn);
printf("%-15s %10s \n",name,usn);
}
fclose(fp3);
return 0;
}
Output:
/* Program 13. C Program to maintain a record of n student details
using an array of structures with four fields (Roll number, Name,
Marks, and Grade). Assume appropriate data type for each field. Print
the marks of the student, given the student name as input. */
#include<stdio.h>
#include<string.h>
struct student
{
int roll_num,marks;
char name[20],grade[5];
};
int main()
{
int i,n,found=0;
struct student s[10];
char sname[20];
printf("Enter the students n = ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter the %d student details\n",i+1);
printf(" Enter the roll number:");
scanf("%d",&s[i].roll_num);
printf(" Enter the student name without spaces:");
scanf("%s",s[i].name);
printf(" Enter the marks:");
scanf("%d",&s[i].marks);
printf(" Enter the Grade:");
scanf("%s",s[i].grade);
}
printf("\n The student details are \n");
printf("---------------------------------------------------------\n");
printf(" Roll Num\tName\t\tMarks\tGrade\n");
printf("---------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\t%d\t%s\t\t%d\t%s\n",s[i].roll_num,s[i].name,s[i].marks,
s[i].grade);
}
printf("\n Enter the student name to print the marks\n");
scanf("%s",sname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name,sname)==0)
{
printf("\n The marks of %s is %d\n",sname,s[i].marks);
found = 1;
}
}
if(found == 0)
{
printf(" Given student name %s is not present in the list\n",sname);
}
return 0;
}
Output:
/* Program 14: Write a C program using pointers to compute the sum,
mean and standard deviation of all elements stored in an array of n
real numbers. */
#include<stdio.h>
#include<math.h>
int main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
printf("Enter the no of elements n =");
scanf("%d",&n);
printf(" Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+ pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard Deviation=%.3f\n",std);
return 0;
}
Output: