Program To Reverse The Entered String
Program To Reverse The Entered String
h> void main() { char*str,*str1; int l,i,k=0; clrscr(); puts("Enter a string\n"); gets(str); l=strlen(str); for(i=l-1;i>=0;i--) { str1[k]=str[i]; k++; } str1[k]=NULL; printf("The reverse is %s",str1); getch(); }
Program in C to calculate or find the day on 1st january of any year #include<stdio.h> #include<conio.h> void main() { int leapdays,firstday,yr; long int normaldays,totaldays; clrscr(); printf("Enter year "); scanf("%d",&yr); normaldays=(yr-1)*365L; leapdays=(yr-1)/4-(yr-1)/100+(yr-1)/400; totaldays=normaldays+leapdays; firstday=totaldays%7; if(firstday==0) printf("\nMonday"); if(firstday==1) printf("\Tuesday"); if(firstday==2) printf("\nWednesday"); if(firstday==3) printf("\nThrusday"); if(firstday==4) printf("\nFriday"); if(firstday==5) printf("\nSaturday"); if(firstday==6) printf("\nSunday");
Program in C to check whether given triangle is valid or not by entering three angles #include<stdio.h> #include<conio.h> void main() { float angle1,angle2,angle3; clrscr(); printf("Enter three angles of triangle: "); scanf("%f%f%f",&angle1,&angle2,&angle3); if((angle1+angle2+angle3)==180) printf("The triangle is valid"); else printf("The triangle is Invalid"); printf("\n\n\n\n\n\nPress any key to exit......."); getch(); }
//Binary equivalent of a binary number #include<stdio.h> #include<conio.h> int binary (int); void main() { int num; clrscr(); printf("\nEnter The Number: "); scanf("%d",&num); binary(num); printf("\n\n\n\n\nPress any key to exit....."); getch(); } //function to convert deciaml to binary int binary (int n) { int r; r=n%2;
n=n/2; if (n==0) { printf("\nThe binary equivalent is %d",r); return(r); } else binary(n); printf("%d",r); }
//To find absolute value of number entered through keyboard #include<stdio.h> #include<conio.h> void main() { int no; clrscr(); printf("\nEnter any number:"); scanf("%d",&no); no=no*-1; printf("\nThe absolute value of the given number is %d", no); printf("\n\n\n\n\nPress any key to exit..."); getch(); }
Generate all ARMSTRONG numbers between 1 to 500 #include <stdio.h> #include <conio.h> void main() { int i=1,a,b,c; clrscr(); printf("ARMSTRONG NUMBERS BETWEEN 1 to 500 ARE \n"); while (i<=500) { a=i%10; /*Extract Last Digit */ b=i%100; b=(b-a)/10; /*Extract First Digit */ c=i/100;/*Extract first Digit*/ if ((a*a*a)+(b*b*b)+(c*c*c)==i) printf("%d\n",i);
/*program checking for collinear points*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int x1,y1,x2,y2,x3,y3; int s1,s2,s3; clrscr(); printf("nEnter the value of x1 and y1 of first point:"); scanf("%d%d",&x1,&y1); printf("nEnter the value of x2 and y2 of second point:"); scanf("%d%d",&x2,&y2); printf("nEnter the value of x3 and y3 of third point:"); scanf("%d%d",&x3,&y3); /*calculate slope of line between each pair of point*/ s1=abs(x2-x1)/abs(y2-y1); s2=abs(x3-x1)/abs(y3-y1); s3=abs(x3-x2)/abs(y3-y2); if((s1==s2)&&(s1==s3)) printf("ntGiven points are collinear"); else printf("ntGiven points are NoT collinear"); printf("nnnnnnPress any key to exit...."); getch(); }
23) write a program in C to find out if the given point lies on x axis,y axis or on the origin
#include<stdio.h> #include<conio.h> void main() { int x,y; clrscr(); printf("nEnter the x and y coordinates of a point:n"); scanf("%d%d",&x,&y); if(x==0&&y==0) printf("point lies on origin"); else if(x==0&&y!=0) printf("nPoint lies on y-axis"); else if(x!=0&&y==0) printf("nPoint lies on x-axis"); else printf("nPoint doesn't lie on any axis,nor origin"); printf("nnnnPress any key to exit..."); getch(); } write a program in C which determine whether a given point lies inside the circle, on the circle or outside the circle #include<stdio.h> #include<conio.h> void main() { int x,y,r; int dis,d; clrscr(); printf("nEnter radius and coordinate of points of circle(x,y):n"); scanf("%d%d%d",&r,&x,&y); dis=x*x+y*y; d=r*r; if(dis==d) printf("nPoint is on the circle"); else { if(dis>d)
printf("Points is outside the circle"); else printf("Point is inside the circle"); } printf("nnnnnPress any key to exit...."); getch(); }
Pascal triangle in c
#include<stdio.h> long factorial(int);
main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } } return 0;
long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }
printf("%d is present at location %d.\n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.\n", search); return 0;
int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]);
scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for ( c = n - 1 ; c >= position - 1 ; c-- ) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n"); for( c = 0 ; c <= n ; c++ ) printf("%d\n", array[c]); return 0; }
int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]);
} }
return 0;
#include<stdio.h> main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) { for ( d = 0 ; d < n - c - 1 ; d++ ) { if ( array[d] > array[d+1] ) { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; }
if ( array[c] < array[d] ) { swap = array[d]; array[d] = array[c]; for ( k = c ; k > d ; k-- ) array[k] = array[k-1]; array[k+1] = swap; }
} }
printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; }
} getch(); return 0; }
Subtract matrices
C code to subtract matrices of any order. This program finds difference between corresponding elements of two matrices and then print the resultant matrix.
C code
#include<stdio.h> #include<conio.h> main() { int m, n, c, d, first[10][10], second[10][10], difference[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d",&m,&n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d",&first[c][d]); printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d",&second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) difference[c][d] = first[c][d] - second[c][d]; printf("difference of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t",difference[c][d]); printf("\n"); } getch(); return 0; }
main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], mul[10][10]; printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d",&m,&n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d",&first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d",&p,&q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d",&second[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; } mul[c][d] = sum; sum = 0;
} }
printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t",mul[c][d]); printf("\n"); } }
getch(); return 0;
char first[100], second[100], result; printf("Enter first string\n"); gets(first); printf("Enter second string\n"); gets(second); result = compare_string(first, second); if ( result == 0 ) printf("Both strings are same.\n"); else printf("Entered strings are not equal.\n"); return 0; } int compare_string(char *first, char *second) { while(*first==*second) { if ( *first == '\0' || *second == '\0' ) break; first++; second++;
} if( *first == '\0' && *second == '\0' ) return 0; else return -1;
C substring code
#include<stdio.h> #include<malloc.h> char* substring(char*, int, int); main() { char string[100], *pointer; int position, length; printf("Enter a string\n"); gets(string); printf("Enter the position and length of substring\n"); scanf("%d%d",&position, &length); pointer = substring( string, position, length); printf("Required substring is \"%s\"\n", pointer);
free(pointer); return 0; } char *substring(char *string, int position, int length) { char *pointer; int c; pointer = malloc(length+1); if( pointer == NULL ) { printf("Unable to allocate memory.\n"); exit(EXIT_FAILURE); } for( c = 0 ; c < position -1 ; c++ ) string++; for( c = 0 ; c < length ; c++ ) { *(pointer+c) = *string; string++; } *(pointer+c) = '\0'; return pointer; }
} }
return 0;
there are two spaces in this string, so our program will print a string "c programming". It will remove spaces when they occur more than one time consecutively in string anywhere.
C code
#include<stdio.h> #include<string.h> #include<malloc.h> #define SPACE ' ' main() { char string[100], *blank, *start, *end; int length; printf("Enter a string\n"); gets(string); length = strlen(string); blank = string; end = start = (char*)malloc(length+1); while(*blank) { if ( *blank == SPACE && *(blank+1) == SPACE ) {} else { *start = *blank; start++; } blank++;
strupr in c
#include<stdio.h> #include<string.h> main() { char string[] = "strupr in c"; printf("%s\n",strupr(string)); } return 0;
#include<stdio.h> void lowerString(char*); main() { char string[100]; printf("Enter a string to convert it into lower case\n"); gets(string); lowerString(string); printf("Entered string in lower case is \"%s\"\n", string); return 0; } void lowerString(char *string) { while(*string) { if ( *string >= 'A' && *string <= 'Z' ) { *string = *string + 32; } string++; } }
C programming code
#include<stdio.h> #include<string.h> #include<malloc.h> #include<conio.h> main() { char first[100], second[100], *temp; printf("Enter the first string "); gets(first); printf("Enter the second string "); gets(second); printf("\nBefore Swapping\n"); printf("First string: %s\n",first); printf("Second string: %s\n\n",second); temp = (char*)malloc(100); strcpy(temp,first); strcpy(first,second); strcpy(second,temp);
printf("After Swapping\n"); printf("First string: %s\n",first); printf("Second string: %s\n",second); getch(); return 0; }
printf("Enter second string\n"); gets(b); flag = check(a, b); if ( flag == 1 ) printf("\"%s\" and \"%s\" are anagrams.\n", a, b); else printf("\"%s\" and \"%s\" are not anagrams.\n", a, b); return 0; } int check(char a[], char b[]) { int first[26] = {0}, second[26] = {0}, c = 0; while ( a[c] != '\0' ) { first[a[c]-'a']++; c++; } c = 0; while ( b[c] != '\0' ) { second[b[c]-'a']++; c++; } for ( c = 0 ; c < 26 ; c++ ) { if( first[c] != second[c] ) return 0; } } return 1;
for ( c = 0 ; c < ( n - 1) ; c++ ) { for ( d = ( c + 1 ) ; d <= ( n - 1 ) ; d++ ) { if ( array[c] > array[d] ) { swap = array[c]; array[c] = array[d]; array[d] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); } return 0;
gets(file1); fs = fopen(file1,"r"); if( fs == NULL ) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } printf("Enter name of target file "); gets(file2); ft = fopen(file2,"w"); if( ft == NULL ) { perror("Error "); fclose(fs); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } while( ( ch = fgetc(fs) ) != EOF ) fputc(ch,ft); printf("File copied successfully.\n"); getch(); fclose(fs); fclose(ft); return 0;
fs2 = fopen(file2,"r"); if( fs1 == NULL || fs2 == NULL ) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } ft = fopen(file3,"w"); if( ft == NULL ) { perror("Error "); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while( ( ch = fgetc(fs1) ) != EOF ) fputc(ch,ft); while( ( ch = fgetc(fs2) ) != EOF ) fputc(ch,ft); printf("Two files were merged into %s file successfully.\n",file3); fclose(fs1); fclose(fs2); fclose(ft); getch(); return 0;
getch(); return 0;
getch(); return 0;