Saturday, 28 February 2015
Program to Convert Numbers into Words in c
#include<stdio.h>
void pw(long,char[]);
char *one[]={" "," one"," two"," three"," four"," five"," six"," seven","eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
char *ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","seventy"," eighty"," ninety"};
void main()
{
long n;
clrscr();
printf("Enter any 9 digit no: ");
scanf("%9ld",&n);
if(n<=0)
printf("Enter numbers greater than 0");
else
{
pw((n/10000000),"crore");
pw(((n/100000)%100),"lakh");
pw(((n/1000)%100),"thousand");
pw(((n/100)%10),"hundred");
pw((n%100)," ");
}
getch();
}
void pw(long n,char ch[])
{
(n>19)?printf("%s %s ",ten[n/10],one[n%10]):printf("%s ",one[n]);
if(n)printf("%s ",ch);
}
void pw(long,char[]);
char *one[]={" "," one"," two"," three"," four"," five"," six"," seven","eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen","fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
char *ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty","seventy"," eighty"," ninety"};
void main()
{
long n;
clrscr();
printf("Enter any 9 digit no: ");
scanf("%9ld",&n);
if(n<=0)
printf("Enter numbers greater than 0");
else
{
pw((n/10000000),"crore");
pw(((n/100000)%100),"lakh");
pw(((n/1000)%100),"thousand");
pw(((n/100)%10),"hundred");
pw((n%100)," ");
}
getch();
}
void pw(long n,char ch[])
{
(n>19)?printf("%s %s ",ten[n/10],one[n%10]):printf("%s ",one[n]);
if(n)printf("%s ",ch);
}
selection sort in c
This code implements selection sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
#include<stdio.h> main() { int array[100], n, c, d, position, 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++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; }
insertion sort in c
This code implements insertion sort algorithm to arrange numbers of an array in ascending order. With a little modification it will arrange numbers in descending order.
/* insertion sort ascending order */ #include<stdio.h> main() { int array[100], n, c, d, swap, k; 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 = 1 ; c <= n - 1 ; c++ ) { for ( d = 0 ; d <= c - 1 ; d++ ) { 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; }
c program for bubble sort
c program for bubble sort: c programming code for bubble sort to sort numbers or arrange them in ascending order. You can easily modify it to print numbers in descending order.
/* Bubble sort code */ #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] ) /* For decreasing order use < */ { 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; }
c program to delete an element from an array
This program delete an element from an array. Deleting an element does not affect the size of array. It is also checked whether deletion is possible or not, For example if array is containing five elements and you want to delete element at position six which is not possible.
c programming code
#include <stdio.h> main() { 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; }
c program to insert an element in an array
This code will insert an element into an array, For example consider an array a[10] having three elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to insert a number 45 at location 1 i.e. a[0] = 45, so we have to move elements one step below so after insertion a[1] = 1 which was a[0] initially, and a[2] = 2 and a[3] = 3. Array insertion does not mean increasing its size i.e array will not be containing 11 elements.
C programming code
#include <stdio.h> main() { 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]); printf("Enter the location where you wish to insert an element\n"); 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; }
c program to reverse an array
This program reverses the array elements. For example if a is an array of integers with three elements
such that
a[0] = 1
a[1] = 2
a[2] = 3
then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1
Given below is the c code to reverse an array .
such that
a[0] = 1
a[1] = 2
a[2] = 3
then on reversing the array will be
a[0] = 3
a[1] = 2
a[0] = 1
Given below is the c code to reverse an array .
C programming code
#include<stdio.h> main() { int n, c, d, a[100], b[100]; printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter the array elements\n"); for ( c = 0 ; c < n ; c++ ) scanf("%d",&a[c]); for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ ) b[d] = a[c]; for ( c = 0 ; c < n ; c++ ) a[c] = b[c]; printf("Reverse array is\n"); for( c = 0 ; c < n ; c++ ) printf("%d\n", a[c]); return 0; }
c program to reverse an array using pointers
#include<stdio.h> #include<stdlib.h> void reverse_array(int*, int); main() { int n, c, *pointer; scanf("%d",&n); pointer = (int*)malloc(sizeof(int)*n); if( pointer == NULL ) exit(EXIT_FAILURE); for ( c = 0 ; c < n ; c++ ) scanf("%d",(pointer+c)); reverse_array(pointer, n); printf("Original array on reversal is\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n",*(pointer+c)); free(pointer); return 0; } void reverse_array(int *pointer, int n) { int *s, c, d; s = (int*)malloc(sizeof(int)*n); if( s == NULL ) exit(EXIT_FAILURE); for ( c = n - 1, d = 0 ; c >= 0 ; c--, d++ ) *(s+d) = *(pointer+c); for ( c = 0 ; c < n ; c++ ) *(pointer+c) = *(s+c); free(s); }
c program to find minimum element in array
C code to find minimum or smallest element present in an array. It also prints the location or index at which minimum element occurs in array. This can also be done by using pointers (see both the codes).
C programming code
#include <stdio.h> main() { int array[100], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) { minimum = array[c]; location = c+1; } } printf("Minimum element is present at location number %d and it's value is %d.\n", location, minimum); return 0; }
C programming code using pointers
#include <stdio.h> main() { int array[100], *minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array; *minimum = *array; for ( c = 1 ; c < size ; c++ ) { if ( *(array+c) < *minimum ) { *minimum = *(array+c); location = c+1; } } printf("Minimum element is present at location number %d and it's value is %d.\n", location, *minimum); return 0; }
c program to find maximum element in array
This code find maximum or largest element present in an array. It also prints the location or index at which maximum element occurs in array. This can also be done by using pointers (see both codes).
C programming code
#include <stdio.h> main() { int array[100], maximum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); maximum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] > maximum ) { maximum = array[c]; location = c+1; } } printf("Maximum element is present at location number %d and it's value is %d.\n", location, maximum); return 0; }
C programming code using pointers
#include <stdio.h> main() { int array[100], *maximum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); maximum = array; *maximum = *array; for ( c = 1 ; c < size ; c++ ) { if ( *(array+c) > *maximum ) { *maximum = *(array+c); location = c+1; } } printf("Maximum element is present at location number %d and it's value is %d.\n", location, *maximum); return 0; }
c program to print Pascal triangle
Pascal Triangle in c: C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below :-
1 1 1 1 2 1 1 3 3 1
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 ); }
c program to delete a file
This c program deletes a file which is entered by the user, the file to be deleted should be present in the directory in which the executable file of this program is present. Extension of the file should also be entered, also note that deleted file doesn't go to recycle bin, remove macro is used to delete the file. If there is an error in deleting the file then an error will be displayed using perror function.
C programming code
#include<stdio.h> main() { int status; char file_name[25]; printf("Enter the name of file you wish to delete\n"); gets(file_name); status = remove(file_name); if( status == 0 ) printf("%s file deleted successfully.\n",file_name); else { printf("Unable to delete the file\n"); perror("Error"); } return 0; }
c program to list files in directory
This program list all files present in a directory/folder in which this executable file is present. For example if this executable file is present in C:\\TC\\BIN then it will lists all the files present in C:\\TC\\BIN.
C programming code
#include<stdio.h> #include<conio.h> #include<dir.h> main() { int done; struct ffblk a; printf("Press any key to view the files in the current directory\n"); getch(); done = findfirst("*.*",&a,0); while(!done) { printf("%s\n",a.ff_name); done = findnext(&a); } getch(); return 0; }
c program to merge two files
This c program merges two files and store their contents in an another file. The files which are to be merged are opened in read mode and the file which contains content of both the files is opened in write mode. To merge two files first we open a file and read it character by character and store the read contents in another file then we read the contents of another file and store it in file, we read two files until EOF ( end of file ) is reached.
C programming code
#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { FILE *fs1, *fs2, *ft; char ch, file1[20], file2[20], file3[20]; printf("Enter name of first file "); gets(file1); printf("Enter name of second file "); gets(file2); printf("Enter name of file which will store contents of two files "); gets(file3); fs1 = fopen(file1,"r"); 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; }
Subscribe to:
Posts (Atom)