1. Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c)of a Quadratic equation (ax2+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, rp, ip, r1, r2;
printf("Enter three co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0 && b==0)
{
printf("Invalid coefficients\n");
printf("Try again with valid inputs!!!!\n");
}
else if(a==0)
{
printf("Linear Equation\n");
r1=-c/b;
printf("Root=%f",r1);
}
else
{
d=b*b-4*a*c;
if(d==0)
{
printf("Root are real and equal\n");
r1=-b/(2*a);
r2=r1;
printf("Root1=%f\nRoot2=%f",r1,r2);
}
else if(d>0)
{
printf("Roots are real and distict\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Root1=%f\nRoot2=%f",r1,r2);
}
else
{
printf("Roots are real and imaginary\n");
rp=-b/(2*a);
ip=sqrt(fabs(d))/(2*a);
printf("Root1=%.3f+i%.3f\n",rp,ip);
printf("Root2=%.3f-i%.3f\n",rp,ip);
}
}
}
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 num, digit, rev=0, temp;
printf("Enter the number\n");
scanf("%d",&num);
temp=num;
while(num!=0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
printf("Given number: %d\n", temp);
printf("Reverse of a given number: %d\n", rev);
if(rev==temp)
printf("Given number is Palindrome\n");
else
printf("Given number is not a Palindrome\n");
}
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: Don’t use library function sqrt(n).
#include<stdio.h>
#include<math.h>
void main()
{
float i, number,sqroot,temp;
printf("Enter the number\n");
scanf("%f", &number);
if(number>0)
{
sqroot=number/2;
temp=0;
while(sqroot!=temp)
{
temp=sqroot;
sqroot=(number/sqroot+sqroot)/2;
}
printf("Square root of %.3f = %.3f\n", number, sqroot);
printf("Square root of %.3f using inbuilt function = %.3f\n",number,sqrt(number));
}
else
printf("Square root of negative number doesnot exist\n");
}
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;
printf("Enter a 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);
}
4. Design and develop an algorithm for evaluating the polynomial f(x) = a4x4+ a3x3+ a2x2+ a1x + a0, for a given value of x and its coefficients using Horner’s method. Implement a C program for the same and execute the program for different sets of values of coefficients and x.
#include<stdio.h>
void main()
{
int a[10], x, n, i, sum=0;
printf("Enter the number of co-efficients\n");
scanf("%d",&n);
printf("Enter %d elements\n",n+1);
for(i=n;i>=0;i--)
{
scanf("%d",&a[i]);
}
printf("Enter the value of x\n");
scanf("%d",&x);
for(i=n;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("Sum=%d",sum);
}
5. Draw the flowchart and Write C Program to compute Sin(x) using Taylor series approximation given by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + ……. Compare the result with the built- in Library function and print both the results with appropriate messages.
#include<stdio.h>
#include<math.h>
#define PI 3.142
void main()
{
int i=2, deg;
float x, sum=0, term, num, den=1;
printf("Enter the value of degree\n");
scanf("%d",°);
x=deg*(PI/180);
num=x;
do
{
term=num/den;
num=-num*x*x;
den=den*(i*(i+1));
sum=sum+term;
i=i+2;
}
while(fabs(term)>=0.00001);
printf("The sin(%d) using Taylors series = %.3f\n",deg, sum);
printf("The sin(%d) using inbuilt function = %.3f\n", deg, sin(x));
}
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, a[20], temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements to array\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Input elements are\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=0;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 elements are\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
7. Develop, implement and execute a C program that reads two matrices A (m x n ) and B (p x q ) and Compute the product 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[3][3], b[3][3], c[3][3], i, j, k, m, n, p, q;
printf("Enter the size of matrix A\n");
scanf("%d%d",&m, &n);
printf("Enter the size of matrix B\n");
scanf("%d%d",&p, &q);
if(n!=p)
{
printf("Matrix multiplication is not possible\n");
}
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("%4d", a[i][j]);
}
printf("\n");
}
printf("Matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%4d", b[i][j]);
}
printf("\n");
}
printf("Matrix C\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%4d", c[i][j]);
}
printf("\n");
}
}
}
8. Develop, implement and execute a C program to search a Name in a list of names usingBinary searching Technique.
#include<stdio.h>
#include<string.h>
void main()
{
char name[10][20], keyname[20];
int n, i, low, high, mid, found=0;
printf("Enter number of names to read\n");
scanf("%d",&n);
printf("Enter the names in ascending order\n");
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
printf("Enter the name to be searched\n");
scanf("%s",keyname);
low=0;
high=n-1;
while(low<=high && !found)
{
mid=(low+high)/2;
if(strcmp(name[mid],keyname)==0)
{
found=1;
}
else if(strcmp(name[mid],keyname)<0)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
if(found==1)
{
printf("Name found in position: %d\n",mid+1);
}
else
{
printf("Name not found\n");
}
}
9. Write and execute a C program that
i. 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[], char s2[]);
void main()
{
char s1[50],s2[50];
printf("Enter the source string\n");
gets(s1);//or use this "fgets(s1,100,stdin)".syntax fgets(string,size,stdin);//
strcopy(s1, s2);
}
void strcopy(char s1[50], char s2[50])
{
int i=0;
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
printf("Source string:");
puts(s1);
printf("Destination string:");
puts(s2);
}
ii. Reads a sentence and prints frequency of each of the vowels and total count of consonants.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char s[100];
int i, vc=0, cc=0;
printf("Enter the sentence\n");
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')
vc++;
else
cc++;
}
}
printf("Number of vowels=%d\n", vc);
printf("Number of consonants=%d\n", cc);
}
10a. 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 rightrot(unsigned int, int);
void main()
{
unsigned int x, m;
int n;
printf("Enter the number to be rotated\n");
scanf("%u", &x);
printf("Enter number of bits to be rotated\n");
scanf("%d", &n);
m=rightrot(x,n);
printf("%u rotated to the right by %d bits=%u\n",x,n,m);
}
/*Function to right rotate*/
unsigned int rightrot(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;
}
}
return(x);
}
10b. 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>
#include<math.h>
int isprime(int);
void main()
{
int n1,i,n2,flag=0;
printf("Enter the range n1 and n2\n");
scanf("%d%d", &n1, &n2);
printf("prime numbers between %d and %d are:\n", n1, n2);
for(i=n1;i<n2;i++)
{
if(isprime(i))
{
printf("%d\n",i);
flag=1;
}
}
if(flag==0)
{
printf("There are no prime numbers between this range\n");
}
}
/*Function to check the given number is prime or not*/
int isprime(int num)
{
int i;
if(num==0 || num==1)
{
return 0;
}
for(i=2;i<=sqrt(num);i++)
{
if(num%i==0)
return(0);
}
return(1);
}
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);
void main()
{
int n,r,res;
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("NCR=%d\n", res);
}
else
printf("n should be greater than r\n");
}
int fact(int n)
{
if(n==0)
{
return 1;
}
return (n*fact(n-1));
}
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.
Student Name USN Heading
Name 1 USN1
Name 2 USN2
……. ……..
…..... ……..
#include<stdio.h>
void main()
{
FILE *fp1, *fp2, *fp3;
char usn[15], name[20];
fp1=fopen("studname.txt", "r");
if(fp1==NULL)
printf("studname.txt file not found\n");
fp2=fopen("studusn.txt", "r");
if(fp2==NULL)
printf("studusn.txt 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("Name\t\t\tUSN\n");
while(!feof(fp3))
{
fscanf(fp3, "%s", name);
fscanf(fp3, "%s\n", usn);
printf("%-15s %10s \n", name, usn);
}
fclose(fp3);
}
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>
#include<string.h>
struct student
{
int rollno,marks;
char name[20],grade;
};
void main()
{
int i, n,found=0;
struct student s[10];
char keyname[20];
printf("Enter number of students\n");
scanf("%d", &n);
printf("Enter %d student details\n", n);
for(i=0;i<n;i++)
{
printf("Enter details of student %d\n\n",i+1);
printf("Enter the roll number\n");
scanf("%d", &s[i].rollno);
printf("Enter the name\n");
scanf("%s", s[i].name);
printf("Enter the marks\n");
scanf("%d", &s[i].marks);
printf("Enter the grade\n");
fflush(stdin);
scanf(" %c", &s[i].grade);
}
printf("Student details are\n");
printf("Roll No\tName\tMarks\tGrade\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%c\n", s[i].rollno, s[i].name, s[i].marks, s[i].grade);
}
printf("Enter the name to print the marks\n");
scanf("%s", keyname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name, keyname)==0)
{
printf("Marks of the student: %d\n", s[i].marks);
found=1;
}
}
if(found==0)
printf("Given student name not found\n");
}
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 n, i;
float a[20], *ptr, mean, std, sum=0, stdsum=0;
printf("Enter the number of elements\n");
scanf("%d", &n);
printf("Enter %d elements\n", 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++)
{
stdsum=stdsum+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(stdsum/n);
printf("Sum=%.3f\n", sum);
printf("Mean=%.3f\n", mean);
printf("Standard Deviation=%.3f\n",std);
}
for any query: mail @ gouthamcp011@gmail.com
credit: SDMIT CSE DEPT.
*******************************ERROR FREE**************************************
#include<stdio.h>
#include<math.h>
void main()
{
float a, b, c, d, rp, ip, r1, r2;
printf("Enter three co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0 && b==0)
{
printf("Invalid coefficients\n");
printf("Try again with valid inputs!!!!\n");
}
else if(a==0)
{
printf("Linear Equation\n");
r1=-c/b;
printf("Root=%f",r1);
}
else
{
d=b*b-4*a*c;
if(d==0)
{
printf("Root are real and equal\n");
r1=-b/(2*a);
r2=r1;
printf("Root1=%f\nRoot2=%f",r1,r2);
}
else if(d>0)
{
printf("Roots are real and distict\n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Root1=%f\nRoot2=%f",r1,r2);
}
else
{
printf("Roots are real and imaginary\n");
rp=-b/(2*a);
ip=sqrt(fabs(d))/(2*a);
printf("Root1=%.3f+i%.3f\n",rp,ip);
printf("Root2=%.3f-i%.3f\n",rp,ip);
}
}
}
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 num, digit, rev=0, temp;
printf("Enter the number\n");
scanf("%d",&num);
temp=num;
while(num!=0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
printf("Given number: %d\n", temp);
printf("Reverse of a given number: %d\n", rev);
if(rev==temp)
printf("Given number is Palindrome\n");
else
printf("Given number is not a Palindrome\n");
}
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: Don’t use library function sqrt(n).
#include<stdio.h>
#include<math.h>
void main()
{
float i, number,sqroot,temp;
printf("Enter the number\n");
scanf("%f", &number);
if(number>0)
{
sqroot=number/2;
temp=0;
while(sqroot!=temp)
{
temp=sqroot;
sqroot=(number/sqroot+sqroot)/2;
}
printf("Square root of %.3f = %.3f\n", number, sqroot);
printf("Square root of %.3f using inbuilt function = %.3f\n",number,sqrt(number));
}
else
printf("Square root of negative number doesnot exist\n");
}
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;
printf("Enter a 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);
}
4. Design and develop an algorithm for evaluating the polynomial f(x) = a4x4+ a3x3+ a2x2+ a1x + a0, for a given value of x and its coefficients using Horner’s method. Implement a C program for the same and execute the program for different sets of values of coefficients and x.
#include<stdio.h>
void main()
{
int a[10], x, n, i, sum=0;
printf("Enter the number of co-efficients\n");
scanf("%d",&n);
printf("Enter %d elements\n",n+1);
for(i=n;i>=0;i--)
{
scanf("%d",&a[i]);
}
printf("Enter the value of x\n");
scanf("%d",&x);
for(i=n;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("Sum=%d",sum);
}
5. Draw the flowchart and Write C Program to compute Sin(x) using Taylor series approximation given by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + ……. Compare the result with the built- in Library function and print both the results with appropriate messages.
#include<stdio.h>
#include<math.h>
#define PI 3.142
void main()
{
int i=2, deg;
float x, sum=0, term, num, den=1;
printf("Enter the value of degree\n");
scanf("%d",°);
x=deg*(PI/180);
num=x;
do
{
term=num/den;
num=-num*x*x;
den=den*(i*(i+1));
sum=sum+term;
i=i+2;
}
while(fabs(term)>=0.00001);
printf("The sin(%d) using Taylors series = %.3f\n",deg, sum);
printf("The sin(%d) using inbuilt function = %.3f\n", deg, sin(x));
}
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, a[20], temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements to array\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Input elements are\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=0;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 elements are\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
7. Develop, implement and execute a C program that reads two matrices A (m x n ) and B (p x q ) and Compute the product 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[3][3], b[3][3], c[3][3], i, j, k, m, n, p, q;
printf("Enter the size of matrix A\n");
scanf("%d%d",&m, &n);
printf("Enter the size of matrix B\n");
scanf("%d%d",&p, &q);
if(n!=p)
{
printf("Matrix multiplication is not possible\n");
}
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("%4d", a[i][j]);
}
printf("\n");
}
printf("Matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%4d", b[i][j]);
}
printf("\n");
}
printf("Matrix C\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%4d", c[i][j]);
}
printf("\n");
}
}
}
8. Develop, implement and execute a C program to search a Name in a list of names usingBinary searching Technique.
#include<stdio.h>
#include<string.h>
void main()
{
char name[10][20], keyname[20];
int n, i, low, high, mid, found=0;
printf("Enter number of names to read\n");
scanf("%d",&n);
printf("Enter the names in ascending order\n");
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
printf("Enter the name to be searched\n");
scanf("%s",keyname);
low=0;
high=n-1;
while(low<=high && !found)
{
mid=(low+high)/2;
if(strcmp(name[mid],keyname)==0)
{
found=1;
}
else if(strcmp(name[mid],keyname)<0)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
if(found==1)
{
printf("Name found in position: %d\n",mid+1);
}
else
{
printf("Name not found\n");
}
}
9. Write and execute a C program that
i. 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[], char s2[]);
void main()
{
char s1[50],s2[50];
printf("Enter the source string\n");
gets(s1);//or use this "fgets(s1,100,stdin)".syntax fgets(string,size,stdin);//
strcopy(s1, s2);
}
void strcopy(char s1[50], char s2[50])
{
int i=0;
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
printf("Source string:");
puts(s1);
printf("Destination string:");
puts(s2);
}
ii. Reads a sentence and prints frequency of each of the vowels and total count of consonants.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char s[100];
int i, vc=0, cc=0;
printf("Enter the sentence\n");
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(isalpha(s[i]))
{
s[i]=tolower(s[i]);
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')
vc++;
else
cc++;
}
}
printf("Number of vowels=%d\n", vc);
printf("Number of consonants=%d\n", cc);
}
10a. 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 rightrot(unsigned int, int);
void main()
{
unsigned int x, m;
int n;
printf("Enter the number to be rotated\n");
scanf("%u", &x);
printf("Enter number of bits to be rotated\n");
scanf("%d", &n);
m=rightrot(x,n);
printf("%u rotated to the right by %d bits=%u\n",x,n,m);
}
/*Function to right rotate*/
unsigned int rightrot(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;
}
}
return(x);
}
10b. 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>
#include<math.h>
int isprime(int);
void main()
{
int n1,i,n2,flag=0;
printf("Enter the range n1 and n2\n");
scanf("%d%d", &n1, &n2);
printf("prime numbers between %d and %d are:\n", n1, n2);
for(i=n1;i<n2;i++)
{
if(isprime(i))
{
printf("%d\n",i);
flag=1;
}
}
if(flag==0)
{
printf("There are no prime numbers between this range\n");
}
}
/*Function to check the given number is prime or not*/
int isprime(int num)
{
int i;
if(num==0 || num==1)
{
return 0;
}
for(i=2;i<=sqrt(num);i++)
{
if(num%i==0)
return(0);
}
return(1);
}
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);
void main()
{
int n,r,res;
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("NCR=%d\n", res);
}
else
printf("n should be greater than r\n");
}
int fact(int n)
{
if(n==0)
{
return 1;
}
return (n*fact(n-1));
}
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.
Student Name USN Heading
Name 1 USN1
Name 2 USN2
……. ……..
…..... ……..
#include<stdio.h>
void main()
{
FILE *fp1, *fp2, *fp3;
char usn[15], name[20];
fp1=fopen("studname.txt", "r");
if(fp1==NULL)
printf("studname.txt file not found\n");
fp2=fopen("studusn.txt", "r");
if(fp2==NULL)
printf("studusn.txt 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("Name\t\t\tUSN\n");
while(!feof(fp3))
{
fscanf(fp3, "%s", name);
fscanf(fp3, "%s\n", usn);
printf("%-15s %10s \n", name, usn);
}
fclose(fp3);
}
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>
#include<string.h>
struct student
{
int rollno,marks;
char name[20],grade;
};
void main()
{
int i, n,found=0;
struct student s[10];
char keyname[20];
printf("Enter number of students\n");
scanf("%d", &n);
printf("Enter %d student details\n", n);
for(i=0;i<n;i++)
{
printf("Enter details of student %d\n\n",i+1);
printf("Enter the roll number\n");
scanf("%d", &s[i].rollno);
printf("Enter the name\n");
scanf("%s", s[i].name);
printf("Enter the marks\n");
scanf("%d", &s[i].marks);
printf("Enter the grade\n");
fflush(stdin);
scanf(" %c", &s[i].grade);
}
printf("Student details are\n");
printf("Roll No\tName\tMarks\tGrade\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%c\n", s[i].rollno, s[i].name, s[i].marks, s[i].grade);
}
printf("Enter the name to print the marks\n");
scanf("%s", keyname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name, keyname)==0)
{
printf("Marks of the student: %d\n", s[i].marks);
found=1;
}
}
if(found==0)
printf("Given student name not found\n");
}
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 n, i;
float a[20], *ptr, mean, std, sum=0, stdsum=0;
printf("Enter the number of elements\n");
scanf("%d", &n);
printf("Enter %d elements\n", 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++)
{
stdsum=stdsum+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(stdsum/n);
printf("Sum=%.3f\n", sum);
printf("Mean=%.3f\n", mean);
printf("Standard Deviation=%.3f\n",std);
}
for any query: mail @ gouthamcp011@gmail.com
credit: SDMIT CSE DEPT.
*******************************ERROR FREE**************************************
No comments:
Post a Comment