Skip to main content

Posts

Showing posts from April, 2022

All programs computer science 1sem Engineering

Simple calculator Quadratic equation Electricty Bill Binary Search on Integers / Names Matrix multiplication Taylor series Bubble sort.  string operations such as compare, concatenate, string length.  above and below the average   pointers  Binary to Decimal Conversion. engineering computer all programs 1sem 2022

STRING concatenation77

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; case 2:pr...

Taylor series77

Taylorseries #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 built i...

Binary Search on Integers / Names77

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++)                   ...

simple calculater77

simple calculater #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(" %c",&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"); } } simple calculater Start  Read 2 numbers as a and b Read operator (+ or – or * or /) as op If op is + then di...

Structures abov below avg77

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) aboveavg[j++]=i; else belowavg[k++]=i; } printf(...

Quadratic Equation77

Quadratic Equation #include<stdio.h> #include<stdlib.h> #include<math.h> void main() {  float a,b,c,x1,x2,rp,ip,disc;  printf("enter the co-efficients\n");  scanf("%f%f%f",&a,&b,&c);  if((a*b*c)==0)  {  printf("a cannot be zero");  exit(0);  }  disc=b*b-4*a*c;  if(disc==0) {  printf("the roots are equal\n"); x1=x2=-b/(2*a);  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  {  rp=-b/(2*a);  ip=sqrt(fabs(disc))/(2*a);  printf("the roots are Complex\n");  printf("first root=%f+i%f\n",rp,ip);  printf("second root=%f-i%f\n",rp,ip); } } Quadratic Equation Start Read the co-efficient as a, b...

pointers777

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); } POINTERSALG Start Read number of elements as n Read array eleme...

Matrix Multiplication77

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++)         {   ...

Electricity Bill77

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: Electricitybill Start Read name, units consumed as units charge = 100 if units is <= 100 then compute charge = charge + units * 0.8 else If units ...

Binary to Decimal conversion77

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 other than 0 and 1 display error message and stop x = l...

BubbleSort77

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

electri

HTML <p> #include 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 charges=charges+0.8*units; else if (units>200&&units 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); } </p>