Vous êtes sur la page 1sur 23

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>
int main()
{
float a, b, c, d;
float root1, root2, real, img;
printf("\nEnter the values of a b and c:\t");
scanf("%f%f%f",&a, &b, &c);
if((a == 0)&&(b == 0))
{
printf("\nInvalid inputs");
}
else
{
d = (b*b)-(4*a*c);
if(d == 0)
{
printf("\nRoots are real and equal:::");
root1 = root2 = -b/(2*a);
printf("\nRoot1 = %.3f \nRoot2 = %.3f", root1, root2);
}
else if(d > 0)
{
printf("\nRoots are real and distinct:::");
root1 = (-b+sqrt(d))/(2*a);
root2 = (-b-sqrt(d))/(2*a);
printf("\nRoot1 = %.3f \nRoot2 = %.3f", root1, root2);
}
else
{
printf("\nRoots are real and imaginary:::");
real = -b/(2*a);
img = (sqrt(fabs(d)))/(2*a);
printf("\nRoot1 = %.3f+i%.3f", real, img);
printf("\nRoot2 = %.3f-i%.3f", real, img);
}
}
}

Output:
1)
Enter the values of a b and c: 1 6 9
Roots are real and equal:::
Root1 = -3.000
Root2 = -3.000

2)
Enter the values of a b and c: 1 -5 3
Roots are real and distinct:::
Root1 = 4.303
Root2 = 0.697
3)
Enter the values of a b and c: 1 4 7
Roots are real and imaginary:::
Root1 = -2.000+i1.732
Root2 = -2.000-i1.732

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.
#include<stdio.h>
main()
{
int num, rev=0, rem, n;
printf("\nEnter a number");
scanf("%d", &num);
n = num;
while(num != 0)
{
rem = num%10;
rev = rev*10+rem;
num = num/10;
}
printf("\nReversed number is %d", rev);
if(rev == n)
{
printf("\nGiven number is Palindrome");
}
else
{
printf("\nGiven number is not Palindrome ");
}
}

Output:
1)
Enter a number:

1221

Reversed number is 1221


Given number is Palindrome

2)
Enter a number:

1234

Reversed number is 4321


Given number is not Palindrome

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.
#include<stdio.h>
main()
{
double j;
int i, n;
printf("\nEnter a number:");
scanf("%d", &n);
j = n;
for(i=1; i<=n*2; i++)
{
j = (j+(n/j))/2.0;
}
printf("\nSquare root of given number is %lf", j);
}

Output:
1)
Enter a number: 4
Square root of given number is 2.000000

2)
Enter a number: 3.2
Square root of given number is 1.732051

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>
main()
{
int year;
printf("\nEnter valid year:");
scanf("%d", &year);
if(((year%4==0) && (year%100!=0)) | |(year%400==0))
{
printf("\n%d is a leap year", year);
}
else
{
printf("\n%d is not a leap year", year);
}
}

Output:
1)
Enter valid year:

2012

2012 is a leap year


2)
Enter valid year:

2200

2200 is not a leap year

4. Design and develop algorithm to evaluate polynomial f(x) = a4x4 + a3x3 + a2x2 +a1x +
a0, for a given value of x and its coefficients using Horners method. Implement C program
for the same and execute the program with different set of values of coefficients and x.
#include<stdio.h>
main()
{
int n,sum=0, i ,a[50], x;
printf("\nEnter the degree of polynomial:");
scanf("%d", &n);
printf("\nEnter the n+1 coeffients:");
for(i=n; i>=0; i--)
{
scanf("%d", &a[i]);
}
printf("\nEnter value of x:");
scanf("%d", &x);
for(i=n; i>=1; i--)
{
sum = x*(sum+a[i]);
}
sum = sum+a[0];
printf("\nSum of given polynomial is %d", sum);
}

Output:
Enter the degree of polynomial:
Enter the n+1 coeffients:
Enter value of x:

12345

Sum of given polynomial is 57

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>
main()
{
float degree, x, term, sum, i;
printf("\nEnter the value in degree:");
scanf("%f", &degree);
x = degree*(3.14/180);
sum = 0;
term = x;
i = 1;
do
{
sum = sum+term;
term = ((-1)*term*x*x)/((i+2)*(i+1));
i = i+2;
}while(fabs(term)>=0.000001);
printf("\n Value of sin(%0.2f) is %0.2f", degree, sum);
printf("\n Using library function value of sin(%0.2f) is %0.2f", degree, sin(x));
}
Output:
1)
Enter the value in degree:

45

Value of sin(45.00) is 0.71


Using library function value of sin(45.00) is 0.71
2)
Enter the value in degree:

30

Value of sin(30.00) is 0.50


Using library function value of sin(30.00) is 0.50

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>
main()
{
int n, temp, a[50], i, j;
printf("\nEnter number of elements to be taken:");
scanf("%d", &n);
printf("\nEnter elements:");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(i=0; i<n-1; i++)
{
for(j=0; j<(n-1-i); j++)
{
if(a[j+1] < a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("\nElements after sorting:");
for(i=0; i<n; i++)
printf("%d\t", a[i]);
}

Output:
Enter number of elements to be taken:
Enter elements:

22 33 11 44 22

Elements after sorting:

11

22

22

33

44

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 an appropriate
message in case of incompatibility.
#include<stdio.h>
main()
{
int m, n, p, q, a[50][50], b[50][50], c[50][50], i, j, k;
printf("\nEnter number of rows and columns of Matrix A:");
scanf("%d %d", &m, &n);
printf("\nEnter matrix elements A:");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nEnter number of rows and columns of Matrix B:");
scanf("%d %d", &p, &q);
printf("\nEnter matrix elements B:");
for(j=0; j<q; j++)
{
for(i=0; i<p; i++)
{
scanf("%d", &b[i][j]);
}
}
printf("\nMatrix elements A:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}

printf("\nMatrix elements B:\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("\nMultiplication not possible");
}
else
{
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("\nMatrix elements C:\n");
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
}

Output:
(1)
Enter number of rows and columns of Matrix A:
Enter matrix elements A:
1234
Enter number of rows and columns of Matrix B:
Enter matrix elements B:
5 8 6 9 7 10

22

23

Matrix elements A:
1
2
3
4
Matrix elements B:
5
6
7
8
9
10
Matrix elements C:
21 24 27
47 54 61

(2)

Enter number of rows and columns of Matrix A:


Enter matrix elements A:

22

1234

Matrix elements A:
1
2
3
4
Enter number of rows and columns of Matrix B:
Enter matrix elements B:
Matrix elements B:
1
2
3
4
5
6
Multiplication not possible

123456

32

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>
main()
{
int n, i, low, high, mid;
char name[100][100], key[100];
printf("\nEnter number of names to be taken:");
scanf("%d", &n);
printf("\nEnter the names in alphabetical order:");
for(i=0; i<n; i++)
{
scanf("%s", name[i]);
}
printf("\nEnter key name to be searched:");
scanf("%s", key);
low = 0;
high = n-1;
while(low <= high)
{
mid = (low+high)/2;
if(strcmp(name[mid],key) == 0)
{
printf("\nKey name is found at pos %d", mid+1);
break;
}
else if(strcmp(name[mid],key) > 0)
high = mid-1;
else
low = mid+1;
}
if(low>high)
{
printf("\nKey name not found");
}
}
Output:
Enter number of names to be taken: 5
Enter the names in alphabetical order:
Enter key name to be searched:
Key name is found at pos 2

bcd

aaa bcd eef ghi mno

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[], char s2[]);
main()
{
char s1[20], s2[20];
printf("\nEnter a string:");
gets(s1);
printf("\nString s1:");
puts(s1);
STRCOPY(s1, s2);
printf("\nString s2:");
puts(s2);
}
void STRCOPY(char s1[], char s2[])
{
int i = 0;
while(s1[i] != '\0')
{
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
}
Output:
Enter a string: hello how are you
String s1: hello how are you
String s2: hello how are you

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<string.h>
#include<ctype.h>
main()
{
char s[50], ch;
int ac=0, ec=0, ic=0, oc=0, uc=0, con=0, n, i;
printf("\nEnter a sentence:");
gets(s);
n = strlen(s);
for(i=0; i<n; i++)
{
if(isalpha(s[i]))
{
ch = tolower(s[i]);
switch(ch)
{
case 'a':
ac++; break;
case 'e':
ec++; break;
case 'i':
ic++; break;
case 'o':
oc++; break;
case 'u':
uc++; break;
default:
con++; break;
}
}
}
printf("\n Number of occurences of a is %d" , ac);
printf("\n Number of occurences of e is %d", ec);
printf("\n Number of occurences of i is %d", ic);
printf("\n Number of occurences of o is %d", oc);
printf("\n Number of occurences of u is %d", uc);
printf("\n Number of occurences of consonants is %d ", con);
}
Output:
Enter a sentence:

C Programming Lab

Number of occurences of a is 2
Number of occurences of e is 0
Number of occurences of i is 1
Number of occurences of o is 1
Number of occurences of u is 0
Number of occurences of consonants is 11

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 RightShift(unsigned int x, unsigned int n);
void main()
{
unsigned int x, n, val;
char ch;
do
{
printf("\nEnter the value: ");
scanf("%u", &x);
printf("\nEnter the number of rotations: ");
scanf("%u", &n);
val=RightShift(x, n);
printf("\nValue is %u", val);
fflush(stdin);
printf("\nDo you want to continue?: ");
ch = getchar();
}while(ch != 'n');
}
unsigned RightShift(unsigned int x, unsigned 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;
}

Output:
Enter the value: 4
Enter the number of rotations: 2
Value is 1
Do you want to continue?: y
Enter the value: 9
Enter the number of rotations: 2
Value is 16386
Do you want to continue?: y
Enter the value: 8
Enter the number of rotations: 2
Value is 2
Do you want to continue?: y
Enter the value: 7
Enter the number of rotations: 1
Value is 32771
Do you want to continue?: n

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>
#include<math.h>
int isprime(int n);
void main()
{
int x, y, i, flag = 0;
printf("\nEnter the range: ");
scanf("%d %d", &x, &y);
for(i=x; i<=y; i++)
{
if(isprime(i))
{
printf("%d ", i);
flag=1;
}
}
if(flag == 0)
printf("\nThere are no prime numbers in this range");
}
int isprime(int n)
{
int i;
if( n==0 || n==1)
return 0;
for(i=2; i<=sqrt(n); i++)
{
if(n%i == 0)
return 0;
}
return 1;
}
Output:
(1)
Enter the range: 24 26
There are no prime numbers in this range
(2)
Enter the range: 4 15
5 7 11 13

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 rnC . 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;
printf("\nEnter value of n: ");
scanf("%d %d", &n, &r);
res = fact(n)/(fact(n-r)*fact(r));
printf("nCr is = %d", res);
}
Output:
Enter value of n: 4 2
nCr is = 6

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.
#include<stdio.h>
void main()
{
FILE *fp1, *fp2, *fp3;
char name[50], usn[50];
fp1 = fopen("student.txt", "r");
if(fp1 == NULL)
printf("File1 Not found");
fp2 = fopen("usn.txt", "r");
if(fp2 == NULL)
printf("File2 Not found");
fp3 = fopen("output.txt", "w");
while(!feof(fp1) && !feof(fp2))
{
fscanf(fp1, "%s", name);
fscanf(fp2, "%s", usn);
fprintf(fp3, "\n%s %s", name, usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3 = fopen("output.txt", "r");
printf("-----------------------------\n");
printf("NAME\t\tUSN\n");
printf("-----------------------------\n");
while(!feof(fp3))
{
fscanf(fp3, "%s", name);
fscanf(fp3, "%s", usn);
printf("\n%s\t\t%s", name, usn);
}
fclose(fp3);
}

Input files:

student.txt

usn.txt

person1
person2
person3

414CS001
414CS002
414CS003

Output:

----------------------------NAME
USN
----------------------------person1
person2
person3

414CS001
414CS002
414CS003

output.txt
person1 414CS001
person2 414CS002
person3 414CS003

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 rollno, marks;
char name[20], grade;
};
void main()
{
struct student s[10];
int n, i, found=0;
char keyname[20];
printf("\nEnter the number of students: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nEnter the details of student %d", i+1);
printf("\nEnter roll num: ");
scanf("%d", &s[i].rollno);
printf("\nEnter name: ");
scanf("%s", s[i].name);
printf("\nEnter marks: ");
scanf("%d", &s[i].marks);
printf("\nEnter grade: ");
fflush(stdin);
scanf("%c", &s[i].grade);
}
printf("\nStudents details are: ");
printf("\nRoll No \t Name \t Marks \t Grade\n");
for(i=0; i<n; i++)
{
printf("\n%d \t %s \t %d \t %c", s[i].rollno, s[i].name, s[i].marks, s[i].grade);
}
printf("\nEnter the student name whose mark has to be displayed: ");
scanf("%s", keyname);

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


{
if(strcmp(s[i].name, keyname) == 0)
{

printf("\nMarks of student %s is: %d", s[i].name, s[i].marks);


found = 1;
}
}
if(found == 0)
printf("\nGiven student name not found");
}
Output:
Enter the number of students: 2
Enter the details of student 1
Enter roll num: 11
Enter name: John
Enter marks: 60
Enter grade: a
Enter the details of student 2
Enter roll num: 22
Enter name: Sam
Enter marks: 50
Enter grade: b
Students details are:
Roll No
Name Marks Grade
11
22

John
Sam

60
50

a
b

Enter the student name whose mark has to be displayed: John


Marks of student John is: 60

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()
{
float a[40], sum=0, mean, sumvar=0, sd, var, *ptr;
int n, i;
printf("\nEnter the value of n:");
scanf("%d", &n);
printf("\nEnter the n array elements:");
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++)
{
sumvar = sumvar+(pow((*ptr-mean), 2));
ptr++;
}
var = sumvar/n;
sd = sqrt(var);
printf("\nSum is = %f", sum);
printf("\nMean is = %f", mean);
printf("\nStandard deviation is = %f", sd);
}
Output:
Enter the value of n: 5
Enter the n array elements: 1 5 6 7 9
Sum is = 28.000000
Mean is = 5.600000
Standard deviation is = 2.653300

Vous aimerez peut-être aussi