├── assig1 ├── 1.c ├── 2.c ├── 3.c ├── 4.c └── a.out ├── assig2 ├── 1.c ├── 2.c ├── 3.c ├── 4.c └── a.out ├── assig3 ├── 1.c ├── 2.c ├── 3.c ├── 4.c └── a.out ├── assig4 ├── 1.c ├── 2.c ├── 3.c ├── 4.c └── a.out ├── assig5 ├── 1.c ├── 2.c ├── 3.c └── a.out ├── assig6 ├── 1.c ├── 2.c ├── 3.c ├── 4.c └── a.out └── assig7 ├── 1.c ├── 2.c ├── 3.c ├── a.out ├── dimensions.txt └── input.txt /assig1/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int a, d, n,nth_term; 4 | printf("Enter values 'a', 'd', 'n' respectively\n"); 5 | scanf("%d%d%d",&a,&d,&n); 6 | nth_term = a+(n-1)*d; 7 | printf("%dth term is : %d\n",n,nth_term); 8 | printf("Sum up to %dth term is: %d\n",n, (n*(a+nth_term))/2); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /assig1/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | float pi=3.14; 4 | float l; 5 | printf("Enter the side of square\n"); 6 | scanf("%f",&l); 7 | printf("The radius of circle is: %f\n", (2*l)/pi); 8 | printf("The area of circle is: %f\n",(4*l*l)/pi); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /assig1/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(int argc, char const *argv[]) 3 | { 4 | int C, D; 5 | printf("Enter C, D values respectively:\n"); 6 | scanf("%d,%d",&C,&D); 7 | printf("Before swaping C=%d, D=%d",C,D); 8 | C = C + D; // this will fail if the addition of two digits exceeds int range(>2^32 (if size of int is 32)) 9 | D = C - D; // D = C + D - D = C 10 | C = C - D; // C = C + D - C = D 11 | /* 12 | C = C ^ D; // best solution 13 | D = C ^ D; // D = C ^ D ^ D = C 14 | C = C ^ D; // C = C ^ D ^ C = D 15 | */ 16 | printf("After swaping C=%d, D=%d",C,D); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /assig1/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(){ 5 | int x1,y1; 6 | int x2,y2; 7 | int x3,y3; 8 | int side; 9 | printf("Enter the coordinates of first vertex\n"); 10 | scanf("%d%d",&x1,&y1); 11 | printf("Enter the coordinates of second vertex\n"); 12 | scanf("%d%d",&x2,&y2); 13 | printf("Enter the coordinates of third vertex\n"); 14 | scanf("%d%d",&x3,&y3); 15 | printf("Center of inner circle is: (%d,%d)\n",(x1+x2+x3)/3,(y1+y2+y3)/3); 16 | side= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); 17 | printf("The area of inner circle is:%f\n", (sqrt(3) * side)/6); 18 | return 0; 19 | // we didn't check if the cordinates form triangle please include that check also 20 | } -------------------------------------------------------------------------------- /assig1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoursesRavindraBabu/CProgramming/8f70051b2051a10a2d2e54df471746d44864983a/assig1/a.out -------------------------------------------------------------------------------- /assig2/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int x1,y1; 4 | int x2,y2; 5 | int x3,y3; 6 | int slop_ab,slop_bc; 7 | printf("Enter the coordinates of first vertex\n"); 8 | scanf("%d%d",&x1,&y1); 9 | printf("Enter the coordinates of second vertex\n"); 10 | scanf("%d%d",&x2,&y2); 11 | printf("Enter the coordinates of third vertex\n"); 12 | scanf("%d%d",&x3,&y3); 13 | slop_ab= (y2-y1)/(x2-x1); 14 | slop_bc= (y3-y2)/(x3-x2); 15 | if(slop_ab==slop_bc){ 16 | printf("YES\n"); 17 | }else{ 18 | printf("NO\n"); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /assig2/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int a,b,c; 4 | int min,max; 5 | printf("Enter three numbers a,b,c :\n"); 6 | scanf("%d%d%d",&a,&b,&c); 7 | max=(a>b?((a>c)?a:c):((b>c)?b:c)); 8 | min=(a>b?((b>c)?c:b):((a>c)?c:a)); 9 | printf("The maximum of given 3 numbers is:%d\n",max); 10 | printf("The minimum of given 3 numbers is:%d\n",min); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /assig2/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int a=1+rand()%11; 5 | if(a<3){ 6 | printf("a\n"); 7 | }else if(a<6){ 8 | printf("b\n"); 9 | }else if(a<9){ 10 | printf("c\n"); 11 | }else{ 12 | printf("d\n"); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /assig2/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(int argc, char const *argv[]) 3 | { 4 | int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10; 5 | int count=0; 6 | printf("Enter ten digits in single line. each digit should be separeted by space\n"); 7 | //0 1 0 0 1 0 0 1 0 0 8 | scanf("%d %d %d %d %d %d %d %d %d %d",&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9,&a10); 9 | if(a1==0 && a1 == 1 && a2 == 0 && a3 == 0){ 10 | count++; 11 | }else if(a2==0 && a3 == 1 && a4 == 0 && a5 == 0){ 12 | count++; 13 | }else if(a3==0 && a4 == 1 && a5 == 0 && a6 == 0){ 14 | count++; 15 | }else if(a4==0 && a5 == 1 && a6 == 0 && a7 == 0){ 16 | count++; 17 | }else if(a5==0 && a6 == 1 && a7 == 0 && a8 == 0){ 18 | count++; 19 | }else if(a6==0 && a7 == 1 && a8 == 0 && a9 == 0){ 20 | count++; 21 | }else if(a7==0 && a8 == 1 && a9 == 0 && a10 == 0){ 22 | count++; 23 | } 24 | printf("Number of time the pattern occured is:%d\n", count); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /assig2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoursesRavindraBabu/CProgramming/8f70051b2051a10a2d2e54df471746d44864983a/assig2/a.out -------------------------------------------------------------------------------- /assig3/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int rows, coef = 1, space, i, j; 4 | printf("Enter number of rows: "); 5 | scanf("%d",&rows); 6 | for(i=0; i 2 | int main(int argc, char const *argv[]) 3 | { 4 | int n,sum=0; 5 | printf("Enter the positive number\n"); 6 | scanf("%d",&n); 7 | while(n){ 8 | sum+=n%10; 9 | n/=10; 10 | } 11 | printf("Sum of digits is:%d\n", sum); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /assig3/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | long int fact(long int n){ 5 | long int fact=1; 6 | for(int i=1;i<=n;i++){ 7 | fact=fact*i; 8 | } 9 | return fact; 10 | } 11 | int main(){ 12 | float x; 13 | float perv_sin,sin; 14 | long int k=1; 15 | printf("Enter value of x:\n"); 16 | scanf("%f",&x); 17 | sin=x; 18 | do{ 19 | perv_sin=sin; 20 | printf("%f,%ld,%f\n",pow(-1,k),k,sin); 21 | sin += pow(-1,k) * ((pow(x,(2*k+1)))/fact(2*k+1)); 22 | k++; 23 | }while(fabs(perv_sin-sin)>0.001); 24 | printf("The value of sin(%f) is : %f\n",x,sin); 25 | } 26 | -------------------------------------------------------------------------------- /assig3/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(){ 5 | int n; 6 | printf("Enter value of n:\n"); 7 | scanf("%d",&n); 8 | for (int i = 0; i <= n; ++i){ 9 | int a=5*i*i+4; 10 | int b=5*i*i-4; 11 | int sqrt_a=sqrt(a); 12 | int sqrt_b=sqrt(b); 13 | if(!(sqrt_a*sqrt_a==a || sqrt_b*sqrt_b==b)){ 14 | printf("%d\n",i); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /assig3/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoursesRavindraBabu/CProgramming/8f70051b2051a10a2d2e54df471746d44864983a/assig3/a.out -------------------------------------------------------------------------------- /assig4/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int n,count=0; 4 | printf("Enter value of n:\n"); 5 | scanf("%d",&n); 6 | while(n){ 7 | int x = n & -n; 8 | if (x==1){ 9 | count++; 10 | } 11 | n>>=1; 12 | } 13 | if(count!=1){ 14 | printf("The given number is not power of two\n"); 15 | }else{ 16 | printf("The given number is power of two\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /assig4/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int n1, n2; 4 | printf("Enter two positive integers: "); 5 | scanf("%d %d",&n1,&n2); 6 | while(n1!=n2){ 7 | if(n1 > n2) 8 | n1 -= n2; 9 | else 10 | n2 -= n1; 11 | } 12 | printf("GCD = %d",n1); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /assig4/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, reversedInteger = 0, remainder, originalInteger; 5 | printf("Enter an integer: "); 6 | scanf("%d", &n); 7 | originalInteger = n; 8 | while( n!=0 ){ 9 | remainder = n%10; 10 | reversedInteger = reversedInteger*10 + remainder; 11 | n /= 10; 12 | } 13 | if (originalInteger == reversedInteger) 14 | printf("%d is a palindrome.", originalInteger); 15 | else 16 | printf("%d is not a palindrome.", originalInteger); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /assig4/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int a[]={1,2,3,4,5,6,7,8,9,10}; 5 | // int a[]={10,9,8,7,6,5,4,3,2,1}; 6 | int n=10; 7 | int left=0, right=9,m; 8 | while(left<=right && right<10 && left>=0){ 9 | m=floor((right+left)>>1); 10 | printf("%d\n",m ); 11 | if(m>0 && m<10){ 12 | if(a[m-1]<=a[m] && a[m]>=a[m+1]){ 13 | printf("The peak value is:%d\n",a[m]); 14 | return 0; 15 | }else if(a[m-1]>a[m]){ 16 | right=m-1; 17 | }else if(a[m-1] 2 | #include 3 | #define INT_MAX 1000 4 | int max(int a,int b){ 5 | return a>b?a:b; 6 | } 7 | int min(int a,int b){ 8 | return a>b?b:a; 9 | } 10 | void MaxMin(int a[],int size,int *max_prt,int *min_ptr){ 11 | *max_prt=-1; 12 | *min_ptr=INT_MAX; 13 | for (int i = 0; i < size; ++i){ 14 | *max_prt=max(*max_prt,a[i]); 15 | *min_ptr=min(*min_ptr,a[i]); 16 | } 17 | } 18 | int main(){ 19 | int a[]={1,124,21,4,5,8,3,56,52,0}; 20 | int size=sizeof(a)/sizeof(a[0]); 21 | int *max_prt=(int*)malloc(sizeof(int)); 22 | int *min_ptr=(int*)malloc(sizeof(int*)); 23 | MaxMin(a,size,max_prt,min_ptr); 24 | printf("Maximum value is:%d, Manimum value is:%d\n",*max_prt,*min_ptr); 25 | } -------------------------------------------------------------------------------- /assig5/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void swap(int *a,int *b){ 4 | int temp=*a; 5 | *a=*b; 6 | *b=temp; 7 | } 8 | void reverse(int a[],int size){ 9 | for (int i = 0,j=size-1; i < j; ++i,--j){ 10 | swap(&a[i],&a[j]); 11 | } 12 | } 13 | void print_array(int a[],int size){ 14 | for (int i = 0; i < size; ++i){ 15 | printf("%d\t",a[i]); 16 | } 17 | printf("\n"); 18 | } 19 | int main(){ 20 | int a[]={1,124,21,4,5,8,3,56,52}; 21 | int size=sizeof(a)/sizeof(a[0]); 22 | print_array(a,size); 23 | reverse(a,size); 24 | print_array(a,size); 25 | } -------------------------------------------------------------------------------- /assig5/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | int OrthoNormal(int N,float product[][N]){ 3 | for (int i = 0; i < N; ++i){ 4 | for (int j = 0; j < N; ++j){ 5 | if(i==j && product[i][j]!=1){ 6 | return 0; 7 | }else if(i!=j && product[i][j]!=0){ 8 | return 0; 9 | } 10 | } 11 | } 12 | return 1; 13 | } 14 | void product_of(int N,float A[][N],float AT[][N],float product[][N]){ 15 | int sum=0; 16 | for ( int i = 0 ; i < N ; i++ ){ 17 | for (int j = 0 ; j < N ; j++ ){ 18 | for (int k = 0 ; k < N ; k++ ){ 19 | sum = sum + A[i][k] * AT[k][j]; 20 | } 21 | product[i][j] = sum; 22 | sum = 0; 23 | } 24 | } 25 | } 26 | void matTranspose(int N,float A[][N],float AT[][N]){ 27 | for (int i = 0; i < N; ++i){ 28 | for (int j = 0; j < N; ++j){ 29 | AT[j][i]=A[i][j]; 30 | } 31 | } 32 | } 33 | void print_matrix(int N,float A[][N]){ 34 | for (int i = 0; i < N; ++i){ 35 | for (int j = 0; j < N; ++j){ 36 | printf("%f\t", A[i][j]); 37 | } 38 | printf("\n"); 39 | } 40 | } 41 | void main(){ 42 | int N=3; 43 | float A[][3]={ 44 | {1,2,3}, 45 | {3,4,5}, 46 | {5,6,7} 47 | }; 48 | float AT[N][N],product[N][N]; 49 | 50 | printf("Matrix A is:\n"); 51 | print_matrix(N,A); 52 | 53 | matTranspose(N,A,AT); 54 | printf("Transpose of matrix A is:\n"); 55 | print_matrix(N,AT); 56 | 57 | product_of(N,A,AT,product); 58 | printf("Product of A and AT is:\n"); 59 | print_matrix(N,product); 60 | 61 | if(OrthoNormal(N,product)){ 62 | printf("Matrix is orthogonal.\n"); 63 | }else{ 64 | printf("Matrix is not orthogonal.\n"); 65 | } 66 | } -------------------------------------------------------------------------------- /assig5/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoursesRavindraBabu/CProgramming/8f70051b2051a10a2d2e54df471746d44864983a/assig5/a.out -------------------------------------------------------------------------------- /assig6/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | char str1[10]; 5 | char str2[10]; 6 | int a[26]={0}; 7 | printf("Enter first string:\n"); 8 | scanf("%s",str1); 9 | printf("Enter second string:\n"); 10 | scanf("%s",str2); 11 | int str2_len=strlen(str2); 12 | for (int i = 0; i < str2_len; ++i){ 13 | a[str2[i]-'a']=1; 14 | } 15 | int str1_len=strlen(str1); 16 | for (int i = 0; i < str1_len; ++i){ 17 | if(a[str1[i]-'a']!=1){ 18 | printf("NO\n"); 19 | return 0; 20 | } 21 | } 22 | printf("YES\n"); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /assig6/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int n; 5 | int *a; 6 | int sum=0; 7 | printf("Enter number of elements:\n"); 8 | scanf("%d",&n); 9 | a=(int*)malloc(n*sizeof(int)); 10 | printf("Enter n integers:\n"); 11 | for (int i = 0; i < n; ++i){ 12 | scanf("%d",&a[i]); 13 | sum+=a[i]; 14 | } 15 | printf("The avg of given %d numbers is:%f\n", n,(float)sum/n); 16 | free(a); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /assig6/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void remove_non_chars(char *str,int n){ 4 | char *new_str=(char*)malloc(n*sizeof(char)); 5 | int k=0; 6 | for(int i=0;i= 'a' && str[i] <='z')||(str[i] >= 'A' && str[i] <='Z') || (str[i] >= '0' && str[i] <='9')){ 8 | new_str[k++]=str[i]; 9 | } 10 | } 11 | printf("After removal of non chars the new string is:%s\n",new_str); 12 | } 13 | int main(){ 14 | char *str; 15 | int n; 16 | printf("Enter the length of string:\n"); 17 | scanf("%d",&n); 18 | str=(char*)malloc(n*sizeof(char)); 19 | printf("Enter string\n"); 20 | scanf("%s",str); 21 | remove_non_chars(str,n); 22 | free(str); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /assig6/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(){ 5 | char *s[5], *t; 6 | printf("Enter any five strings : \n"); 7 | for (int i = 0; i < 5; i++){ 8 | int len; 9 | printf("Enter the number of char's in %dth string\n",i); 10 | scanf("%d",&len); 11 | s[i]=(char*)malloc(len*sizeof(char*)); 12 | scanf("%s", s[i]); 13 | } 14 | for (int i = 1; i < 5; i++){ 15 | for (int j = 1; j < 5; j++){ 16 | if (strcmp(s[j - 1], s[j]) > 0) { 17 | t=(char*)malloc(strlen(s[j-1])*sizeof(char)); 18 | strcpy(t, s[j - 1]); 19 | strcpy(s[j - 1], s[j]); 20 | strcpy(s[j], t); 21 | free(t); 22 | } 23 | } 24 | } 25 | printf("Strings in order are : \n"); 26 | for (int i = 0; i < 5; i++) 27 | printf("%s\n", s[i]); 28 | for (int i=0;i<5;i++) 29 | free(s[i]); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /assig6/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoursesRavindraBabu/CProgramming/8f70051b2051a10a2d2e54df471746d44864983a/assig6/a.out -------------------------------------------------------------------------------- /assig7/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct box 5 | { 6 | float l; 7 | float w; 8 | float h; 9 | }; 10 | float max(struct box one_box){ 11 | return (one_box.h>one_box.l)?((one_box.h>one_box.w)?one_box.h:one_box.w):((one_box.l>one_box.w)?one_box.l:one_box.w); 12 | } 13 | 14 | float find_maximum_height (struct box *box_array, int num_boxes){ 15 | float sum=0.0; 16 | for (int i = 0; i < num_boxes; ++i){ 17 | sum+=max(box_array[i]); 18 | } 19 | return sum; 20 | } 21 | 22 | int main(){ 23 | FILE *fp=fopen("dimensions.txt","r"); 24 | const size_t line_size = 100; 25 | char *line =malloc(line_size*sizeof(char)); 26 | char *tok; 27 | int number_of_cubes; 28 | if(fgets(line, line_size, fp) != NULL){ 29 | number_of_cubes=atoi(line); 30 | } 31 | struct box *box_ptr=malloc(number_of_cubes*sizeof(struct box)); 32 | printf("%d\n",number_of_cubes ); 33 | int i=0; 34 | while (fgets(line, line_size, fp) != NULL){ 35 | tok=strtok(line," "); 36 | box_ptr[i].l=atof(tok); 37 | tok=strtok(NULL," "); 38 | box_ptr[i].w=atof(tok); 39 | tok=strtok(NULL," "); 40 | box_ptr[i++].h=atof(tok); 41 | } 42 | for (int i = 0; i < number_of_cubes; ++i){ 43 | printf("length:%f,width:%f,height:%f\n",box_ptr[i].l,box_ptr[i].w,box_ptr[i].h); 44 | } 45 | printf("the maximum height of the arrangement is:%f\n",find_maximum_height(box_ptr,number_of_cubes)); 46 | free(line); 47 | free(box_ptr); 48 | fclose(fp); 49 | } -------------------------------------------------------------------------------- /assig7/2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(int argc, char const *argv[]) 5 | { 6 | char *str=malloc(100*sizeof(char)); 7 | strcpy(str,"wc "); 8 | strcat(str,argv[1]); 9 | if(argc>1){ 10 | FILE *fp=fopen(argv[1],"r"); 11 | if(fp!=NULL){ 12 | system(str); 13 | } 14 | fclose(fp); 15 | 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /assig7/3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void insertionSort(int arr[], int n){ 5 | int i, key, j; 6 | for (i = 1; i < n; i++){ 7 | key = arr[i]; 8 | j = i-1; 9 | while (j >= 0 && arr[j] > key){ 10 | arr[j+1] = arr[j]; 11 | j = j-1; 12 | } 13 | arr[j+1] = key; 14 | } 15 | } 16 | void print_array(int array[],int n){ 17 | for (int i = 0; i < n; ++i){ 18 | printf("%d\t",array[i] ); 19 | } 20 | printf("\n"); 21 | } 22 | int main(int argc, char const *argv[]) 23 | { 24 | int n=argc-1; 25 | int array[argc]; 26 | for (int i = 1; i