PCD Lab Prog Complete
PCD Lab Prog Complete
Design and develop a flowchart or an algorithm that takes three coefficients (a,
b, and c) of a Quadratic equation (ax 2 +bx+c=0) as input and compute all possible
roots. Implement a C program for the developed flowchart/algorithm and execute
the same to output the possible roots for a given set of coefficients with appropriate
messages.
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2;
clrscr();
printf("Enter the value of a,b,c\n");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
if(a==0||b==0||c==0)
{
printf("Zero entered so roots cannot be determined");
getch();
}
if(d==0)
{
printf("Roots are real and equal\n");
r1=r2=-b/(2*a);
printf("Root1=%f\n",r1);
printf("Root2=%f",r2);
}
if(d>0)
{
printf("Roots are real and distinct\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Root1=%f\n",r1);
printf("Root2=%f",r2);
}
if(d<0)
{
printf("Roots are real and imaginary\n");
r1=-b/(2*a);
r2=sqrt(-d)/(2*a);
printf("Root1=%f+i%f \n",r1,r2);
printf("Root2=%f-i%f",r1,r2);
}
getch();
}
2. Design and develop an algorithm to find the reverse of an integer number NUM
and check whether it is PALINDROME or NOT. Implement a C program for the
developed algorithm that takes an integer number as input and output the reverse
of the same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a
Palindrome
#include<stdio.h>
void main()
{
int n,num,rev,rem;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
num=n;
rev=0;
while(n!=0)
{
rem=n%10;
n=n/10;
rev=rev*10+rem;
}
printf("Reversed number=%d \n",rev);
if(num==rev)
{
printf("Number is palindrome");
}
else
{
printf("Number is not a palindrome");
}
getch();
}
3a. Design and develop a flowchart to find the square root of a given number N.
Implement a C program for the same and execute for all possible inputs with
appropriate messages. Note: Dont use library function sqrt(n).
#include<stdio.h>
void main()
{
float i,j,n;
clrscr();
printf("Enter a number\n");
scanf("%f",&n);
if(n==0)
{
printf("Square root of zero is 0");
}
if(n<0)
{
printf("Cannot find square root of a negative number");
}
if(n>0)
{
j=n;
for(i=0;i<n;i++)
{
j=( j+(n/j) )/2;
}
printf("Square root=%f",j);
}
getch();
}
3b. Design and develop a 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>
void main()
{
int year;
clrscr();
printf("Enter the year\n");
scanf("%d",&year);
if( (year%4==0 && year%100!=0) || (year%400==0) )
{
printf("leap year");
}
else
{
printf("Not a leap year");
}
getch();
}
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
sum=a[4];
for(i=3;i>=0;i--)
{
sum=sum*x+a[i];
}
printf("The sum of polynomial=%d",sum);
getch();
}
5. Draw the flowchart and Write a 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 i,m,deg;
float rad,term,sum;
clrscr();
printf("Enter degree \n");
scanf("%d",°);
printf("Enter the number of terms\n");
scanf("%d",&m);
rad=deg*(3.14/180);
term=sum=rad;
for(i=1;i<m;i++)
{
term=term* -rad*rad/(2*i*(2*i+1));
sum=sum+term;
}
printf("Using taylor's series =%f\n",sum);
printf("Using inbuilt function=%f",sin(rad));
getch();
}
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("MATRIX A \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("MATRIX B \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("MATRIX C \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
getch();
}
}
}
if(flag==1)
{
printf("Successful search and name found at position %d ",mid+1);
}
if(flag==0)
{
printf("Unsuccessful search");
}
getch();
}
9a. Write and execute a C program that implements string copy operation
STRCOPY(str1,str2) that copies a string str1 to another string str2 without using
library function.
#include<stdio.h>
void strcopy(char s1[20],char s2[20])
{
int i;
for(i=0;s1[i]!='\0';i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
}
void main()
{
char s1[20],s2[20];
clrscr();
printf("Enter the string to be copied \n");
scanf("%s",s1);
strcopy(s1,s2);
printf("Copied string is %s",s2);
getch();
9.b Write and execute a C program that Read a sentence and print frequency of
vowels and total count of consonants.
#include<stdio.h>
#include<ctype.h>
void main()
{
int i,vowels=0,consonants=0;
char s[30],ch;
clrscr();
printf("Enter the sentence \n");
gets(s);
for(i=0;i<strlen(s);i++)
{
if(isalpha(s[i]))
{
ch=tolower(s[i]);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowels++;
}
else
{
consonants++;
}
}
}
printf("Number of vowels=%d\n",vowels);
printf("Number of consonants=%d",consonants);
getch();
}
10.a Design and develop a 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>
unsigned int rightrotate(unsigned int x,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(x%2==0)
{
x=x>>1;
}
else
{
x=x>>1;
x=x+32768;
}
}
return x;
}
void main()
{
unsigned int x,res;
int n;
clrscr();
printf("Enter an unsigned integer \n");
scanf("%u",&x);
printf("Enter the value of n \n");
scanf("%d",&n);
res=rightrotate(x,n);
printf("Result=%u",res);
getch();
}
11.Draw the flowchart and write a recursive 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 nCr . 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;
}
else
{
return n*fact(n-1);
}
}
void main()
{
int n,r,res;
clrscr();
printf("Enter the value of n \n");
scanf("%d",&n);
printf("Enter the value of r \n");
scanf("%d",&r);
res=fact(n)/(fact(n-r)*fact(r));
printf("The nCr is=%d",res);
getch();
}
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s",usn);
printf("%s%15s\n",name,usn);
}
fclose(fp3);
getch();
}
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>
void main()
{
int i,n;
float a[10],*ptr, mean, stddev, variance=0, sum=0;
clrscr();
printf("Enter the value of n \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++)
{
variance=variance+pow((*ptr-mean),2);
ptr++;
}
stddev=sqrt(variance/n);
printf("Sum=%f\n",sum);
printf("Mean=%f\n",mean);
printf("Standard deviation=%f",stddev);
getch();
}