Вы находитесь на странице: 1из 18

1.

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

4. Design and develop an algorithm to evaluate polynomial f(x) = a4x 4 + a3x 3 +


a2x 2 + a1x + a0, for a given value of x and its coefficients using Horners method.
Implement a C program for the same and execute the program with different set of
values of coefficients and x
#include<stdio.h>
void main()
{
int i,n,x,sum,a[10];
clrscr();
printf("Enter the degree of polynomial \n");
scanf("%d",&n);
printf("Enter the value of x \n");
scanf("%d",&x);
printf("Enter the coefficients \n");

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",&deg);
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();
}

6. Develop an algorithm, implement and execute a C program that reads N integer


numbers and arrange them in ascending order using Bubble Sort.
#include<stdio.h>
void main()
{
int n,i,j,temp,a[10];
clrscr();
printf(" Enter the number \n");
scanf("%d",&n);
printf("Enter number of elements \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Sorted numbers are \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

7. Develop, implement and execute a C program that 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>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
clrscr();
printf("Enter the order of matrix A \n");
scanf("%d%d",&m,&n);
printf("Enter the order of matrix B \n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication not possible");
}
else
{
printf("Enter the elements of matrix A \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{

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

8. Develop, implement and execute a C program to search a Name in a list of names


using Binary searching Technique.
#include<stdio.h>
#include<string.h>
void main()
{
int i,n,low,high,mid,flag;
char names[20][20],key[20];
clrscr();
printf("Enter the number of names \n");
scanf("%d",&n);
printf("Enter names in ascending order \n");
for(i=0;i<n;i++)
{
scanf("%s",names[i]);
}
printf("Enter the key name to search \n");
scanf("%s",key);
flag=0;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(names[mid],key)==0)
{
flag=1;
break;
}
if(strcmp(names[mid],key)<0)
{
low=mid+1;
}
if(strcmp(names[mid],key)>0)
{
high=mid-1;

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

10.b Design and develop a 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;i++)
{
if(x%i==0)
{
return 0;
}
}
return 1;
}
void main()
{
int n1,n2,i,flag=0;
clrscr();
printf("Enter a range \n");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
if(isprime(i))
{
printf("The prime number is %d\n",i);
flag=1;
}
}
if(flag==0)
{
printf("There are no prime numbers");
}
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();
}

12 Given two university information files studentname.txt and usn.txt that


contains students Name and USN respectively. Write a 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.

Before executing create studentname.txt and studentusn.txt files


#include<stdio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char usn[20],name[20];
clrscr();
fp1=fopen("studentname.txt","r");
if(fp1==NULL)
{
printf("File not found\n");
}
fp2=fopen("studentusn.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",usn);
fprintf(fp3,"%15s%15s",name,usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
printf("NameUsn\n");
fp3=fopen("output.txt","r");
while(!feof(fp3))

{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s",usn);
printf("%s%15s\n",name,usn);
}
fclose(fp3);
getch();
}

13 Write a 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>
struct student
{
int rollnum,marks;
char name[20],grade[3];
};
void main()
{
int i,n;
char sname[20];
struct student s[10];
clrscr();
printf("Enter the number of students\n");
scanf("%d",&n);
printf("Enter students details \n");
for(i=0;i<n;i++)
{
printf("Enter the student name \n");
scanf("%s",s[i].name);
printf("Enter the grade \n");
scanf("%s",s[i].grade);
printf("Enter the roll number \n");
scanf("%d",&s[i].rollnum);

printf("Enter the marks \n");


scanf("%d",&s[i].marks);
}
printf("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("Marks is %d",s[i].marks);
getch();
exit(0);
}
}
printf("Name not found");
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();
}

Вам также может понравиться