















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
2014 Scheme PCD lab programs for VTU
Typology: Study notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
On special offer
#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.
#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.
#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
#include<stdio.h> #include<math.h> main() { float degree, x, term, sum, i; printf("\nEnter the value in degree:"); scanf("%f", °ree); 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.
#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
#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
#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
#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
#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:
person1 414CS person2 414CS person3 414CS output.txt person person person
person1 4 14CS person2 4 14CS person3 4 14CS