Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Pcd lab programs 2014 Scheme , Study notes of C programming

2014 Scheme PCD lab programs for VTU

Typology: Study notes

2014/2015
On special offer
40 Points
Discount

Limited-time offer


Uploaded on 08/10/2015

tejaswini
tejaswini 🇮🇳

4.7

(6)

2 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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);
}
}
}
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
Discount

On special offer

Partial preview of the text

Download Pcd lab programs 2014 Scheme and more Study notes C programming in PDF only on Docsity!

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 = (bb)-(4ac); if(d == 0) { printf("\nRoots are real and equal:::"); root1 = root2 = -b/(2a); printf("\nRoot1 = %.3f \nRoot2 = %.3f", root1, root2); } else if(d > 0) { printf("\nRoots are real and distinct:::"); root1 = (-b+sqrt(d))/(2a); root2 = (-b-sqrt(d))/(2a); printf("\nRoot1 = %.3f \nRoot2 = %.3f", root1, root2); } else { printf("\nRoots are real and imaginary:::"); real = -b/(2a); img = (sqrt(fabs(d)))/(2a); printf("\nRoot1 = %.3f+i%.3f", real, img); printf("\nRoot2 = %.3f-i%.3f", real, img); } } }

Output:

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

Enter the values of a b and c: 1 -5 3 Roots are real and distinct::: Root1 = 4. Root2 = 0.

Enter the values of a b and c: 1 4 7 Roots are real and imaginary::: Root1 = -2.000+i1. Root2 = -2.000-i1.

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:

Enter a number: 4 Square root of given number is 2.

Enter a number: 3. Square root of given number is 1.

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:

Enter valid year: 2012 2012 is a leap year

Enter valid year: 2200 2200 is not a leap year

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)termxx)/((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:

Enter the value in degree: 45 Value of sin(45.00) is 0. Using library function value of sin(45.00) is 0.

Enter the value in degree: 30 Value of sin(30.00) is 0. Using library function value of sin(30.00) is 0.

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: 5 Enter elements: 22 33 11 44 22 Elements after sorting: 11 22 22 33 44

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: 2 2 Enter matrix elements A: 1 2 3 4 Enter number of rows and columns of Matrix B: 2 3 Enter matrix elements B: 5 8 6 9 7 10 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: 2 2 Enter matrix elements A: 1 2 3 4 Matrix elements A: 1 2 3 4 Enter number of rows and columns of Matrix B: 3 2 Enter matrix elements B: 1 2 3 4 5 6 Matrix elements B: 1 2 3 4 5 6 Multiplication not possible

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

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

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 Output:


NAME USN

person1 414CS person2 414CS person3 414CS output.txt person person person

4 14CS

4 14CS

4 14CS

person1 4 14CS person2 4 14CS person3 4 14CS