Skip to main content

Posts

Showing posts from 2015

Writing Data Into A File using C.

Writing Data Into A File We can write data into a file. The following is the algorithm for writing data: Create a file using fopen() function Write data into a file using fprintf() and fputs() functions Close a file using fclose() function For testing, we create data into a file, demo.txt. For implementation, create a file, called filewrite.c, and write this code. #include <stdio.h> int main( int argc , const char* argv[]) { int i; FILE *f; f = fopen("demo.txt", "w+");   for (i= 0 ;i< 5 ;i++){    fprintf(f, "fprintf message %d\n",i);    fputs("fputs message\n", f); // no format } fclose(f); printf("Data was written into a file\n"); return 0 ; } Save this code. Compile and run this program.  

C program to add two matrix

C program to add two matrix This c program add two matrices i.e. compute the sum of two matrices and then print it. Firstly user will be asked to enter the order of matrix (number of rows and columns) and then two matrices. For example if the user entered order as 2, 2 i.e. two rows and two columns and matrices as First Matrix :- 1 2 3 4 Second matrix :- 4 5 6 7 then output of the program (sum of First and Second matrix) will be 5  7 9 11 #include <stdio.h>   int main ( ) { int m , n , c , d , first [ 10 ] [ 10 ] , second [ 10 ] [ 10 ] , sum [ 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 (...

Deen Dayal Upadhyay Gorakhpur University RESULTS

DDU Gorakhpur results chahiye to yaha click kare                                                                       RESULTS  

C Program to swap two numbers using pointers and function.

C Program to swap two numbers using pointers and function.   #include <stdio.h> void swap(int *a,int *b); int main() {   int num1=5,num2=10;   swap(&num1,&num2);        /* address of num1 and num2 is passed to swap function */      printf( "Number1 = %d\n" ,num1);   printf( "Number2 = %d" ,num2);      return 0; } void swap(int *a,int *b) {  /* pointer a and b points to address of num1 and num2 respectively */   int temp;   temp=*a;   *a=*b;   *b=temp; }

To check if given string is palindrome or not.

To check if given string is palindrome or not. #include <stdio.h> #include <string.h> #include <conio.h>   int main()   {   char a[100], b[100]; printf ( "Enter the string to check if it is a palindrome\n" );   gets (a);   strcpy (b,a);   strrev (b);   if ( strcmp(a,b) == 0 )   printf ( "Entered string is a palindrome.\n" );   else   printf ( "Entered string is not a palindrome.\n" );   return 0;   }

C Program to find transpose of a Matrix

Transpose : Transpose of a Matrix means changing Rows into Columns and vice-versa.                                                          Statement of C Program : This Program accepts the Matrix and prints its Transpose.         #include<stdio.h> #include<conio.h> void main() { int A[2][3] , B[3][2]; int i, j;                                    /* 'i ' used for rows and ' j ' used for columns */ clrscr(); printf(" Enter the elements of A\n"); for(i=0 ; i<2 ; i+...

C Program to Check Leap Year

C program to check whether a year is leap year or not using if else statement .      #include <stdio.h>      int main()      {       int year;       printf( "Enter a year: " );       scanf( "%d" ,&year);       if (year%4 == 0)       {           if ( year%100 == 0)   /* Checking for a century year */           {               if ( year%400 == 0)                  printf( "%d is a leap year." , year);               else            ...

Multiplication Table in C

   Multiplication Table         #include <stdio.h>   int main ( ) {         int n , i = 1 ;         int multiply ;         printf ( "Enter a number : " ) ;         scanf ( "%d" ,& n ) ;           while ( i <= 10 )         {                 multiply = i * n ;                 printf ( "%d x %d = %d \n " , n , i , multiply ) ;                 i ++;         }         return 0 ; }   Output:     

C program to accept number and display it in words.

C program to accept number and display it in words. #include <stdio.h> #include <string.h> #include <conio.h>   void main(){      char init[ 27 ][ 12 ] = {" one "," two "," three ",                 " four "," five "," six ",                 " seven "," eight "," nine ",                 " ten "," eleven "," twelve ",                 " thirteen "," fourteen "," fifteen ",                 " sixteen "," seventeen "," eighteen ",                 " nineteen "," twenty "," thirty ",    ...

Bubble sort

  Bubble sort   Bubble sort , sometimes referred to as sinking sort , is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort , is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort . It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position. Bubble sort has worst-case and average complexity both О ( n 2 ), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O ( n  log  n ). Even other О ( n 2 ) ...

Program to find factorial of a number using recursive function.

Program to find factorial of a number using recursive function. #include <stdio.h>   int factorial(int n); int main() {     int n,f;     printf( "Enter an positive integer: " );     scanf( "%d" ,&n);     f=factorial(n);     printf( "Factorial of %d = %d" , n,f );     return 0 ; } int factorial(int n) {     if (n!= 1 )      return (n*factorial(n-1)) ; } Output:  

Program to search the element in an array of N element using Linear search method.

Program to search the element in an array  of N element  using Linear search method. #include <stdio.h> int main() {    int array[ 100 ], search, c, n;    printf( "Enter the number of elements in array\n" );    scanf( "%d" ,&n);    printf( "Enter %d integer(s)\n" , n);     for (c = 0 ; c < n; c++)       scanf( "%d" , &array[c]);    printf( "Enter the number to search\n" );    scanf("%d", &search);    for (c = 0 ; c < n; c++)    {       if (array[c] == search)          {          printf( "%d is present at location %d.\n" , search, c+ 1 );          break;       }    }    if (c == n)       printf( "%d is not present in a...

Program to find if a given number is prime or not.

C Program to find if a given number is prime or not.   #include <stdio.h> int main() {   int n, i, check= 0 ;   printf( "Enter a positive integer: " );   scanf( "%d" ,&n);   for (i= 2 ;i<=n/ 2 ;++i)   {        if (n%i== 0 )       {           check= 1 ;           break;       }   }   if (check== 0 )       printf("%d is a prime number.",n);   else       printf("%d is not a prime number.",n);   getch();   return 0 ; } Output:

Program to insert a element in one dimensional array at a given position.

Program to insert a element in one dimensional array at a given position.      #include <stdio.h> int main ()  {    int arr [ 30 ] , element, num, i, location;    printf ( "\nEnter no of elements :" ) ;    scanf ( "%d" , & num ) ;    printf ( "\nEnter the %d elements:" ,num ) ;    for ( i = 0 ; i < num; i ++)       scanf("%d", & arr [ i ] );    printf ( "\nEnter the element to be inserted :" ) ;    scanf ( "%d" , & element ) ;    printf ( "\nEnter the location" ) ;    scanf ( "%d" , & location ) ;    //Create space at the specified location    for ( i = num; i > = location; i-- )       arr [ i ] = arr [ i - 1 ] ;    num ++ ;    arr [ location - 1 ] = element;    //Print out the result of insertion    for ( i = 0 ; i < ...

Program to reverse the array.

Program to reverse the array. #include <stdio.h> void main() {    int n, c, a[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] );       pdrintf( "Reversed array is \n" );       for (c=n;c>=0;c--)    printf( "%5d" , a[c] );    getch(); } Output:

Program to reverse the digit

Program to reverse the digit. #include <stdio.h> int main() {     int n, reverse = 0 ;    printf( "Enter a number to reverse\n" );    scanf( "%d" ,&n);       while (n != 0 )     {       reverse = reverse * 10 ;       reverse = reverse + n% 10 ;       n = n/ 10 ;    }    printf( "Reverse of entered number is = %d\n" , reverse);    return 0 ; }

Program to find sum of digit of any entered number

Program to find sum of digit of any entered number. #include <stdio.h> int main() {    int n, t, sum = 0 , remainder;    printf( "Enter an integer\n" );    scanf( "%d" , &n);    t = n;    while (t != 0 )    {       remainder = t % 10 ;       sum       = sum + remainder;       t         = t / 10 ;    }    printf( "Sum of digits of %d = %d\n" , n, sum);    return 0 ; }