Tuesday, 23 October 2012

Function039

Write a C program that will enter a line of text, store in an array and then display backwards. The length of the line should be undefined, (being terminated by ENTER key), but less than 80 characters. 

#include <stdio.h>
#include <conio.h>
void backward ( char    str[] )
{              
        int    i;
        for ( i = 0; str[i] != '\0'; i++ );
        printf("\n\n\t The text in backward direction is..:");
        for ( i = i -1; i >= 0; i-- )
        {                   
                    printf("%c",str[i] );
        }
}                          
                
void main ( )
{                     
        char      str[80];
        clrscr ( );
        printf("\n\n\n\t Enter the line of text..:"); gets( str );
        backward ( str );
        getch ( );
}
Click here to contact us

Monday, 22 October 2012

Function038

Write a C program to accept a date in the format DD / MM / YYYY and add an integer to get the resultant date.

#include <stdio.h>
#include <conio.h>

int leap ( int    yy )
{        
     if ( yy % 400 == 0 || ( yy % 100 != 0 && yy % 4 == 0 ) )
                   return 1;
     return 0;
}         
                
void add_day ( int    dd, int    mm, int    yy, int    day )
{             
     int    days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     dd = dd + day;
     if ( leap ( yy ) )
               days[2] = 29;
     else                  
               days[2]=28;
     while ( dd > days[mm] )
     {                               
               if ( dd > days[mm] )
               {                           
                         dd = dd - days[mm];
                         mm++;
               }                       
               if ( mm > 12 )
               {                                                
                         mm = 1;
                         yy = yy + 1;
                         if ( leap ( yy ) )
                                    days[2] = 29;
                         else               
                                    days[2]=28;
               }
     }
     printf("\n\n\n\t The resultant date is..:%d/%d/%d",dd,mm,yy);
}                     
                   
void main ( )
{
      int         dd, mm, yy, day;
      clrscr ( );
           
      printf("\n\n\n\t Enter the date in (DD/MM/YYYY) format:");
      scanf("%d/%d/%d",&dd,&mm,&yy);
      printf("\n\t Enter the number of days you want to add..:"); scanf("%d",&day);
      add_day ( dd, mm, yy, day );
      getch ( );
}
Click here to contact us 

Sunday, 21 October 2012

Function037

The sequence of Fibonacci numbers is defined as below: f(i)=f(i-1)+f(i-2) with f(0)=1 and f(1)=1. Develop a C program to calculate and display Fibonacci numbers. 

#include <stdio.h>
#include <conio.h>
void main ( )
{                      
        int     n, i, t;
        clrscr ( );
               
        printf("\n\n\n Enter the number of terms of the fibonacci series..:"); scanf("%d",&n);
        printf("\n\n\n OUTPUT....: ");
        for ( i = 0; i < n; i++ )
        {                                
                  t = fibo ( i );
                  printf("%d, ",t);
        }
        getch ( );
}               
                            
int fibo ( int     n )
{                           
        if ( n == 0 || n == 1 )
        {                                
                   return 1;
        }
        return fibo ( n - 1 ) + fibo ( n - 2 );
}
Click here to contact us

Saturday, 20 October 2012

Function036

Write a 'C' function to arrange the elements of an integer array in such a way that all the negative elements are before the positive elements. The array is passed to it as an argument.

#include <stdio.h>
#include <conio.h>
void posneg (int    arr[], int    l );
void main ( )
{                  
        int     arr[10], i;
        clrscr ( );
         
        for ( i = 0; i < 10; i++ )
        {                          
                 printf("\n\t\t Enter a number..:"); scanf("%d",&arr[i]);
        }
        printf("\n\n\n\t The array is..: ");
        for ( i = 0; i < 10; i++ )
        {                           
                  printf("%d ",arr[i]);
        }
        posneg ( arr, 10 );
        printf("\n\n\n\t The resultant array is..: ");
        for ( i = 0; i < 10; i++ )
        {                                   
                 printf("%d ",arr[i]);
        }
        getch ( );
}                 
                    
void posneg ( int     arr[], int    l )
{                            
        int     brr[10], j = 0, i;
        for ( i = 0; i < l; i++ )
        {                                  
                 if ( arr[i] < 0 )
                              brr[j++]=arr[i];
        }
        for ( i = 0; i < l; i++ )
        {                                  
                 if ( arr[i] >= 0 )
                              brr[j++]=arr[i];
        }
        for ( i = 0; i < l; i++ )
        {                                      
                 arr[i]=brr[i];
        }
}
Click here to contact us

Friday, 19 October 2012

Function035

Write a 'C' program to compute the following series: 1 - x + x ^ 2 / 2 - x ^ 3 / 6 + x ^ 4 / 24 + ..... + ( - 1 ) ^ nx ^ n / n ! where n and x is to be accepted by the user.

#include <stdio.h>
#include <conio.h>
#include <math.h>
float series ( int    n, float    x )
{                             
        int         i, sf = 1;
        float      s = 0, t;
        for ( i = 0; i <= n; i++ )
        {                                   
                 t = pow ( x, 2 ) / fact ( i );
                 s = s + sf * t;
                 sf = - sf;
        }
        return s;
}
                                    
int fact ( int    n )
{                                   
        int      f = 1;
        while ( n >= 1 )
        {                               
                  f = f * n;
                  n--;
        }
        return f;
}                            
                    
void main ( )
{                            
        int         n;
        float      f, x;
        clrscr ( );
                                  
        printf("\n\n\n\t\t Enter the value of n ( less than 8 )..:"); scanf("%d",&n);
        printf("\n\n\t\t Enter the value of x ..:"); scanf("%f",&x);
        f = series ( n, x );
        printf("\n\n\n\t\t Value = %f",f);
        getch ( );
}
Click here to contact us

Thursday, 18 October 2012

Function034

Write a C program to calculate the frequencies of different alphabets, present in a given string. The string of alphabets is to be taken as input from the keyboard.

#include<stdio.h>
#include<conio.h>
int frequency ( char    str[], int    j );
void main ( )
{                      
        char       str[70];
        int          i, freq, j;
        clrscr ( );
                       
        printf("\n\n\n\t Enter a string of alphabets..:");  gets(str);
        for ( j = 'a'; j <= 'z'; j++ )
        {                            
                  freq = frequency ( str, j );
                  if ( freq != 0 )
                  {
                             printf("\n\n\t\t\t Frequency of %c = %d",j,freq);
                  }                      
                  freq = frequency ( str, j - 32 );
                  if ( freq != 0 )
                  {                   
                             printf("\n\n\t\t\t Frequency of %c = %d",j-32,freq);
                  }
        }
        getch();
}

int frequency ( char    str[], int    j )
{                                
        int      freq = 0, i;
        for ( i = 0; str[i] != '\0'; i++ )
        {                                
                  if ( str[i] == j )
                  {                     
                               freq++;
                  }
        }
        return freq;
}
Click here to contact us

Wednesday, 17 October 2012

Function033

Write a C function print_upper ( ) to prints its character argument in uppercase.

#include <stdio.h>
#include <conio.h>
void print_upper ( char    str[] )
{                  
        int     i;
        for ( i = 0; str[i] != '\0'; i++ )
        {                                      
                  if ( str[i] >= 'a' && str[i] <= 'z' )
                  {                                 
                               str[i] = str[i] - 32;
                  }
        }
}

void main ( )
{                            
        char       str[40];
        clrscr ( );
                           
        printf("\n\n\n\t\t Enter a string..: "); gets(str);
        print_upper ( str );
        printf("\n\n\t\t The upper case string is..: %s",str);
        getch ( );
}
Click here to contact us
Related Posts Plugin for WordPress, Blogger...