Skip to main content

Posts

Showing posts with the label Programs

Seetha, a maths teacher has started to learn the C programming language. She writes a program to do basic arithmetic operations. Help Seetha to complete her first program.

OUTPUT

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Implement Recursive functions for Binary to Decimal Conversion Program and Algorithm Computer Science Engineering VTU

Binary to Decimal conversion  #include<stdio.h> #include<stdlib.h> int dec=0; int bintodec(int num,int x,int base); void main() { int num,n,r,x=0,base=1; printf("enter a binary number\n"); scanf("%d",&n); num=n; while(n>0) { r=n%10; if(r>1) { printf("enter a proper binary number\n"); exit(0); } x++; n=n/10; } bintodec(num,x,base); printf("decimal value of %d is %d\n",num,dec); } int bintodec (int num,int x,int base) { if(x==0) return 0; else dec=dec+(num%10)*base; bintodec(num/10,x-1,base*2); } Algorithm Binary to Decimal conversion Algorithm : Binary to Decimal conversion Start Declare the required variables Read binary number as n If n has digits...

Develop a program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of N real numbers Program and Algorithm Computer Science Engineering VTU

pointers #include <stdio.h> #include <math.h> void main() { float a[10], *ptr, mean, sum=0, std,sumstd=0; int n,i; printf("Enter the number of elements : "); scanf("%d",&n); printf("Enter the array elements :\n"); for( i=0; i<n; i++) scanf("%f",&a[i]); //assigning address of a to pointer ptr ptr = a; //compute the sum for(i=0;i<n;i++) { sum += *ptr; ptr++; } //compute the mean mean = sum/n; //compute standard deviation ptr = a; for(i=0; i<n; i++) { sumstd = sumstd + pow((*ptr-mean),2); ptr++; } std = sqrt(sumstd/n); //print sum, mean, & standard deviation printf("Sum = %f\n",sum); printf("Mean = %f\n",mean); printf("Standard Deviation : %f\n",std); } POINTERS Algorithm   Start Read number of element...

Implement structures to read, write and compute average- marks and the students scoring above and below the average marks for a class of N students Program and Algorithm Computer Science Engineering VTU

Structures abov below avg #include<stdio.h> struct student { char name [100]; char usn [20]; float marks; }; void main() { int i,j,k,m,n,aboveavg[100],belowavg[100]; float total=0.0,avg=0.0; struct student s[100]; printf("enter number of students\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the details of students%d\n",i+1); printf("enter name\n"); scanf("%s",s[i].name); printf("enter usn\n"); scanf("%s",s[i].usn); printf("enter marks\n"); scanf("%f",&s[i].marks); total+=s[i].marks; } printf("enter the details of students\n"); printf("name\t\t usn\t\t marks\n"); for(i=0;i<n;i++) printf("%s\t\t %s\t\t %f\n",s[i].name,s[i].usn,s[i].marks); avg=total/n; printf("avg=%f\n",avg); j=0; k=0; for(i=0;i<n;i++) { if(s[i].marks>avg) abov...

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

STRINGconcatenation #include<stdio.h> #include<stdlib.h> int length (char str[]); int compare (char str1[],char str2[]); void concatenate (char str1[],char str2[]); void main() { char str1[30],str2[30]; int choice,a,i,j; printf("enter 1-string comparision\n"); printf("enter 2-string length\n"); printf("enter 3-string concatenation\n"); printf("enter 4-exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("enter string 1\n"); scanf("%s",str1); printf("enter string 2\n"); scanf("%s",str2); a=compare(str1,str2); if(a==0) { printf("%s and%s are identical\n",str1,str2); } else { printf("%s and%s are not identical\n",str1,str2); } break; ...

Sort the given set of N numbers using Bubble sort Program and Algorithm Computer Science Engineering VTU

Bubble sort #include<stdio.h> int main() { int i,j,n,a[100],temp; printf("enter the number of elements"); scanf("%d",&n); printf("enter the 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-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("sorted arry\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); return 0; } Algorithm: BubbleSort Start read n for i= 0 to n-1 read a[i] for i=0 to n-1 for j=0 to n-i-1 if (a[j] > a[j+1]) temp = a[j] a[j] = a[j+1] a[j+1] = temp for i=0 to n-1 print a[i] Stop

Compute sin(x)/cos(x) using Taylor series approximation.Program and Algorithm Computer Science Engineering VTU

Taylor series  #include<stdio.h> #include<stdlib.h> #include<math.h> int main() {  int ch,i;  float num,den,x,degree,sum=0,term;  printf("enter 1 for sin(x)\n enter 2 for cos(x)\n");  scanf("%d",&ch);  printf("enter the degree\n");  scanf("%f",&degree);  x=(degree*3.14)/180.0;  switch(ch) {   case 1:num=x;   den=1;   i=2; break;   case 2:num=1;   den=1;   i=1;   break;   default:printf("enter the proper choice\n");   exit(0); }   do {   term=num/den;   num=-num*x*x;   den=den*i*(i+1);   sum=sum+term;   i=i+2; } while(fabs(term)>0.000001);   if(ch==1) {   printf("sin(%f)without using built in function=%f\n",degree,sum);   printf("sin(%f)with using built in function=%f\n",degree,sin(x)); } else {  printf("cos(%f) without using buil...

Implement Matrix multiplication and validate the rules of multiplication Program and Algorithm Computer Science Engineering VTU

Matrix Multiplication #include<stdio.h> #include<stdlib.h> void main() { int a[10] [10],b[10] [10],c[10] [10],i,j,k,m,n,p,q; printf("enter the order of the matrix A\n"); scanf("%d%d",&m,&n); printf("enter the order of the matrix B\n"); scanf("%d%d",&p,&q); if(n!=p) {     printf("matrix multiplication is not possible\n");     exit(0); } printf("enter the elements of A\n"); for(i=0;i<m;i++) {     for(j=0;j<n;j++)     {         scanf("%d",&a[i][j]);     } } printf("enter the elements of 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++)         ...

Implement Binary Search on Integers / Names Program and Algorithm Computer Science Engineering VTU

BinarySearch #include<stdio.h> #include<stdlib.h> #include<string.h> void main() { int a[20],low,mid,high,i,n,k,ch; char b[20][20],key[20]; printf("1. binary search for integer\n2. Binary search for string/name\n"); printf("Enter your choice:\t"); scanf("%d",&ch); switch(ch) { case 1: /* For integers */              printf("Enter the number of elements  in ascending order  \n");              scanf("%d",&n);                          printf("Enter the elements\n");              for(i = 0 ; i < n ; i++)                  ...

Compute the roots of a quadratic equation by accepting the coefficients Program and Algorithm Computer Science Engineering VTU

#include<stdio.h> #include<stdlib.h> #include<math.h> void main() {     float a,b,c,x1,x2,sp,ip,disc;     printf("enter the co-efficients\n");     scanf("%f%f%f",&a,&b,&c);     if(a==0)     {         printf("a cannot be zero");         exit(0);     }     disc=b*b-4*a*c;     if(disc==0)     x1=x2=-b/(2*a);     printf("the roots are equal\n");     printf("x1=%f\n x2=%f\n",x1,x2); }     else if(disc>0) {     x1=(-b+sqrt(disc))/(2*a);     x2=(-b-sqrt(disc))/(2*a);     printf("the roots are real and distinct\n");     printf("x1=%f\n x2=%f\n";x1,x2); } else  {     sp=-b/(2*a);     ip=sqrt(fabs(disc))/(2*a);     printf("the roots are Complex\n");     printf("first root=%f+i%f\n",sp,ip);     printf("second root=...

An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs 400, then an additional surcharge of 15% of total amount is charged. Write a program to read the name of the user, number of units consumed and print out the charges. Program and Algorithm Computer Science Engineering VTU

Electricity sort #include<stdio.h> void main() { int units; float charges; char name[20]; printf("Enter the name of the customer\n"); scanf("%s",name); printf("Enter the units consumed\n"); scanf("%d",&units); charges=100; if(units>0&&units<=200) charges=charges+0.8*units; else if (units>200&&units<=300) charges=charges+0.8*200+(300-units)*0.9; else charges=charges+0.8*200+100*0.9+(units-300)*1; if(charges>400) charges=charges+0.15*charges; printf("name=%s\n",name); printf("units consumed=%d\n",units); printf("charges=%f\n",charges); } Algorithm: Electricity bill   Start Read name, units consumed as units charge = 100 if units is <= 100 then comp...

Simulation of a Simple Calculator | COMPUTER PROGRAMMING LABORATORY | 21CPL27/17

program #include<stdio.h> #include<stdlib.h> void main() {     float a,b,c;     char op;     printf("enter the two operands\n");     scanf("%f%f",&a,&b);     printf("enter an operator\n");     scanf("%f",&op);     switch (op) {     case'+': c=a+b;     printf("%f+%f=%f\n",a,b,c);     break;     case'-': c=a-b;     printf("%f-%f=%f\n",a,b,c);     break;     case'*': c=a*b;     printf("%f*%f=%f\n",a,b,c);     break;     case'/': if(b!=0) {     c=a/b;     printf("%f/%f=%f\n",a,b,c);     exit(0); }     else     printf("division by 0 error");     break;     default:     printf("enter valid operation"); } }  Algorithm Start  Read 2 numbers as a and b Read operator (+ or – or * or /) as op If op is + then display a ...