Sunday 4 November 2012

Pointer005

Input three numbers in main function calculate there sum & average in a user defined function and print the result in the main function.

#include <stdio.h>
#include <conio.h>
void sumavg ( int   n1, int    n2, int    n3, int*s, float*avg )
{              
        *s = n1 + n2 + n3;
        *avg = ( *s ) / 3.0;
}                   
             
void main ( )
{                           
        int          n1, n2, n3, s = 0;
        float       avg;
        clrscr ( );
        printf("\n\n\n\t\t Enter 1st number..:"); scanf("%d",&n1);
        printf("\n\t\t Enter 2nd number..:"); scanf("%d",&n2);
        printf("\n\t\t Enter 3rd number..:"); scanf("%d",&n3);
        sumavg ( n1, n2, n3, &s, &avg );
        printf("\n\n\n\t\t Sum = %d",s);
        printf("\n\t\t Average = %f",avg);
        getch();
}
Click here to contact us

Saturday 3 November 2012

Pointer004

Input some elements in an array and copy that array into another array using pointer.

#include <stdio.h>
#include <conio.h>
void input ( int    arr[], int    l )
{                         
       int    i;
       for ( i = 0; i < l; i++ )
       {                 
                 printf("\n\t\t Enter a number..:"); scanf("%d",arr+i);
       }
}                        
                         
void print ( int    arr[], int    l )
{                   
        int    i;
        for ( i = 0; i < l; i++ )
        {                                 
                 printf("%d, ", *(arr+i) );
        }                       
}                

void copy ( int    brr[], int    arr[], int    l )
{                              
        int    i;
        for ( i = 0; i < l; i++ )
        {                                  
                 *( brr +i ) = *( arr + i );
        }
}                        
                       
void main ( )
{                                
        int     arr[10], brr[10];
        clrscr ( );
        input ( arr, 10 );
        printf("\n\n\n The array is..:");
        print ( arr, 10 );
        copy ( brr, arr, 10 );
        printf("\n\n The copied array is..:");
        print ( brr, 10 );
        getch ( );
}
Click here to contact us

Wednesday 31 October 2012

Pointer003

Input two numbers and swap them.

#include<stdio.h>
#include<conio.h>
void swap ( int*, int* );
void main ( )
{                  
        int      m, n;
        clrscr ( );
                                     
        printf("\n\n\n\t\t   Enter 1st number..:"); scanf("%d",&m);
        printf("\n\t\t   Enter 2nd number..:"); scanf("%d",&n);
                                
        swap ( &m, &n );
        printf("\n\n\n\n\t\t -- Values after swapping --");
        printf("\n\n\t\t      First value   = %d",m);
        printf("\n\t\t      Second value = %d",n);
        getch ( );
}                           
                   
void swap ( int     *p, int     *q )
{                 
        int     t;
        t = *p;
        *p = *q;
        *q = t;
}
Click here to contact us

Tuesday 30 October 2012

Pointer002

Input some numbers in an array. Print the array using pointer.

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

Monday 29 October 2012

Pointer001

Concept of pointer.

#include <stdio.h>
#include <conio.h>
void main ( )
{           
        int       n, *p;
        p = &n;
        n = 5;
        clrscr ( );
                       
        printf("\n\n\n\t\t Number is %d Stored at Address %u", n, &n );
        printf("\n\n\t\t Number is %d Stored at Address %u", *p, p );
        printf("\n\n\t\t Address is %u Stored at Address %u", p, &p );
                       
        getch ( );
}
Click here to contact us

Thursday 25 October 2012

Function041

Write a function to accept two strings as arguments. The function should return the index of the second string in the first string, if it exists and -1 otherwise. Don't use any library routines related to strings.

#include <stdio.h>
#include <conio.h>
void main ( )
{        
        char        str[80], substr[80];
        int           f;
        clrscr ( );
       
        printf("\n\n\n\t Enter the 1st string..:"); gets(str);
        printf("\n\t Enter the substring to search..:");
        gets ( substr );
        f = search ( str, substr );
        if ( f == -1 )
        {                                  
                 printf("\n\n\n\t String not found.");
        }                    
        else                         
        {                    
                 printf("\n\n\n\t String found in position %d",f);
        }
        getch ( );
}                         
                                   
int search ( char    str1[], char    str2[] )
{
        int     i = 0, f;
        for ( i = 0; str1[i] != '\0'; i++ )
        {                                     
                  if ( found ( str1, str2, i ) )
                                return i;
        }
        return -1;
}                     
                                          
int found ( char    str1[], char      str2[], int    i )
{                        
        int      j;
        for ( j = 0; str2[j] != '\0'; i++, j++ )
        {                                                
                  if ( str1[i] != str2 [ j] )
                             return 0;
        }
        return 1;
}
Click here to contact us

Wednesday 24 October 2012

Function040

Write a menu driven 'C' program , which prints the options 1,2,3 (as given below) and asks the user to select an option. If user options :
                   1  then find the factors of the entered number
                   2  then check whether the entered number is prime number or not
                   3  then directly exit from the program
The menu should be displayed by a function.


#include <stdio.h>
#include <conio.h>
#include <process.h>
void factor ( int    n )
{         
        int       i;
        printf("\n\t\t The factors are..:");
        for ( i = 1; i <= n; i++ )
        {                                
                  if ( n % i == 0 )
                  {                                            
                                printf("%d, ",i);
                  }                                         
        }                                      
}                                
                             
int prime ( int    n )
{                       
        int      i;
        for ( i = 2; i < n; i++ )
        {                                 
                  if ( n % i == 0 )
                                return 0;
        }
        return 1;
}                              

void menu ( )
{                 
        printf("\n\n\t\t*********************************************\n\n");
        printf("\n\t\t PRESS 1 TO FIND FACTORS\n");
        printf("\n\t\t PRESS 2 TO CHECK IF THE NUMBER IS PRIME\n");
        printf("\n\t\t PRESS 3 TO EXIT\n");
        printf("\n\n\t\t*********************************************\n\n");
}                              
                     
void main ( )
{            
        int      n, ch;
        clrscr ( );
        while ( 1 )
        {                           
               clrscr ( );
               menu ( );
               printf("\n\t\t Enter your choice..:"); scanf("%d",&ch);
               switch ( ch )
               {                                
                      case 1:
                                  printf("\n\t\t Enter a number..:"); scanf("%d",&n);
                                  factor ( n );
                                  break;
                      case 2: 
                                  printf("\n\t\t Enter a number..:"); scanf("%d",&n);
                                  if ( prime ( n ) )
                                             printf("\n\t\t The number is prime.");
                                  else                        
                                             printf("\n\t\t The number is not prime.");
                                  break;
                      case 3:       
                                  exit ( 0 );
                      default: 
                                  printf("\n Please enter a correct choice.");
               }
               getch ( );
        }
}
Click here to contact us

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

Function032

Write a C function wordcount ( ) to count the number of words in a given string and then call in main ( ).

#include <stdio.h>
#include <conio.h>
int wordcount  (char     str[] )
{            
          int     i, w = 1;
          for ( i = 0; str[i] != '\0'; i++ )
          {                                      
                      if ( str[i] == ' ' )
                      {                             
                                  w++;
                      }
          }
          return w;
}                         
                        
void main ( )
{           
        char         str[40];
        int            w = 0;
        clrscr ( );
                           
        printf("\n\n\n\t\t Enter a string..:"); gets(str);
        w = wordcount ( str );
        printf("\n\n\t\t Number of words = %d",w);
        getch ( );
}
Click here to contact us

Tuesday 16 October 2012

Function031

Input some elements in increasing order and write a function to search an element using binary search method.

#include <stdio.h>
#include <conio.h>
void main ( )
{                     
        int        arr[10], i, n, p;
        clrscr ( );
        printf("\n\n\n\t\t Input Elements in Increasing Order.\n\n\n");
                  
        for ( i = 0; i < 10; i++ )
        {                         
                  printf("\n\t Enter a number for position [%d] :",i); scanf("%d",&arr[i]);
                  if ( i != 0 && arr[i] < arr[i-1] )
                  {                      
                           printf("\n\t PLEASE ENTER ELEMENTS IN INCREASING ORDER.");
                           i--;
                  }
        }
        clrscr ( );
        printf("\n\n\n\t The array is..:");
        for ( i = 0; i < 10; i++ )
        {                                
                    printf("%d ",arr[i]);
        }               
        printf("\n\n\t Enter an element to search..:"); scanf("%d",&n);
        p = binsearch ( arr, n, 10 );
        if ( p == -1 )
        {                        
                    printf("\n\t Element not found.");
        }                
        else             
        {                
                    printf("\n\t Element found in position %d",p);
        }                        
        getch ( );
}                                
                                       
int binsearch ( int     arr[], int     n, int    len )
{
        int        m, l = 0, u = len - 1;
        while ( u >= l )
        {                          
                 m = ( l + u ) / 2;
                 if ( arr[m] == n )
                 {                                 
                                return m;
                 }
                 if ( arr[m] < n )
                 {                             
                                l = m + 1;
                 }                          
                 else                 
                 {       
                                u = m - 1;
                 }
        }
        return -1;
}
Click here to contact us

Monday 15 October 2012

Function030

Write a program to find a^n, where a and n can be any positive integer.Define a recursive function power ( ) to perform the operation. 

#include <stdio.h>
#include <conio.h>
int power ( int    a, int    n );
int power ( int    a, int    n )
{                 
       static int     p=1;
       if ( n == 0 )
                 return p;
       p = power ( a, n - 1 ) * a;
}                  
                  
void main ( )
{                      
        int      a, p = 1, n;
        clrscr ( );
                          
        printf("\n\n\n\t\t Enter the number..:"); scanf("%d",&a);
        printf("\n\t\t Enter the power...:"); scanf("%d",&n);
                   
        p = power ( a, n );
        printf("\n\n\t\t %d ^ %d = %d",a,n,p);
        getch ( );
}
Click here to contact us

Sunday 14 October 2012

Case structure006

Write a program to convert an input number in Binary, Octal and Hexadecimal number. The choice should be input by user.

#include <stdio.h>
#include <conio.h>
void main ( )
{            
        int          no, cho, md[50], c, i;
        clrscr ( );
                  
        while ( 1 )
        {                 
               printf ("\n\t\t\t\t MENU");
               printf ("\n\t\t\t\t ====");
               printf ("\n\n\t\t   1. CONVERT FROM DECIMAL TO BINARY\n");
               printf ("\t\t   2. CONVERT FROM DECIMAL TO OCTAL \n");
               printf ("\t\t   3. CONVERT FROM DECIMAL TO HEXADECIMAL \n");
               printf ("\t\t   4. EXIT \n");
               
               printf ("\n\n\n\t\t   Your Choice ? "); scanf ("%d", &cho);
                           
               switch ( cho )
               {             
                     case 1:
                                 printf ("\n\n\t\t Enter a decimal number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {            
                                          md[c] = no % 2;
                                          no = no / 2;
                                          c++;
                                 }
                                 printf ("\n\t\t BINARY NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {                     
                                          printf ("%d", md[i] );
                                 }
                                 break;
                          
                     case 2:       
                                 printf ("\n\n\t\t Enter the number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {
                                          md[c] = no % 8;
                                          no = no / 8;
                                          c++;
                                 }
                                 printf ("\n\t\t OCTAL NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {              
                                          printf ("%d", md[i] );
                                 }
                                 break;
                    
                     case 3:      
                                 printf ("\n\n\t\t Enter the number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {                    
                                          md[c] = no % 16;
                                          no = no / 16;
                                          c++;
                                 }
                                 printf ("\n\t\t HEXADECIMAL NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {            
                                          if ( md[i] > 9 )
                                          {                       
                                                      printf ("%c", md[i] + 55 );
                                          }
                                          else
                                          {
                                                      printf ("%d", md[i] );
                                          }
                                 }
                                 break;
                    
                     case 4:
                                 exit ( );
                                              
                     default:
                                 printf("\n WRONG CHOICE.");
              }
              getch ( );
              clrscr ( );
       }

}
Click here to contact us

Saturday 13 October 2012

Array043

Write a 'C' program to find the minimum of the eight floating point numbers and its place in the array.

#include <stdio.h>
#include <conio.h>
void main ( )
{                   
        float     arr[8], min;
        int        i, pos;
        clrscr ( );
                       
        for ( i = 0; i < 8; i++ )
        {                              
                    printf("\n\t\t Enter a number..:");    scanf("%f",&arr[i]);
        }
        min = arr[0];
        pos = 0;
        for ( i = 1; i < 8; i++ )
        {                           
                    if ( arr[i] < min )
                    {                                
                                 min =arr[i];
                                 pos = i;
                    }
        }
        printf("\n\n\n\t The minimum number is %f and it is in the %d index of the array.",min,pos);
        getch ( );
}

Click here to contact us
Related Posts Plugin for WordPress, Blogger...