Sunday 30 September 2012

Function011

Input two numbers and print whether they are amicable pairs or not.
N.B. [ Example of amicable pairs: - 220, 284, 1184, 1210, 2620, 2924, 5620, 5564, 6232, 6368 ]

#include <stdio.h>
#include <conio.h>
int s_o_f ( int );
int amicable( int,  int );
void main ( )
{                        
        int     n1, n2 , f;
        clrscr ( );
        printf("\n\n\n\t\t Enter 1st number..:"); scanf("%d",&n1);
        printf("\n\n\t\t Enter 2nd number..:"); scanf("%d",&n2);
                             
        f = amicable ( n1, n2 );
                        
        if ( f == 1 )
        {                                  
                 printf("\n\n\n\t\t Numbers are amicable pair.");
        }                        
        else                 
        {                                 
                 printf("\n\n\n\t\t Numbers are not amicable pairs.");
        }
        getch ( );
}                               
                  
int amicable ( int    n1, int    n2 )
{                           
        int    s1, s2;
        s1 = s_o_f ( n1 );
        s2 = s_o_f ( n2 );
                                         
        if ( s1 == n2 && s2 == n1 )
        {                                       
                    return 1;
        }                                       
        else                                     
        {                                           
                    return 0;
        }
}                                         
                                                    
int s_o_f ( int n )
{                                    
        int     s = 0, i;
        for ( i = 1; i < n; i++ )
        {                                   
                    if ( n % i == 0 )
                    {                           
                                  s = s + i;
                    }
        }
        return s;
}
Click here to contact us

Function010

Input ten numbers in an array and print its maximum element. 

#include <stdio.h>
#include <conio.h>
void input ( int[], int );
void print ( int[], int );
int maximum ( int[], int );
void main ( )
{                 
        int        arr[10], m;
        clrscr ( );
            
        input ( arr, 10 );
        printf("\n\n\n\t\t The array is..:");
        print ( arr, 10 );
        m = maximum ( arr, 10 );
        printf("\n\n\t\t The maximum number is..:%d",m);
        getch ( );
}                    
                                      
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]);
        }                                    
}                                                
                                                 
int maximum ( int    arr[], int    l )
{                                             
        int    i, max;
        max = arr[0];
        for ( i = 1; i < l; i++ )
        {                                              
                    if ( arr[i] > max )
                    {                             
                             max=arr[i];
                    }
        }
        return max;
}
Click here to contact us

Saturday 29 September 2012

Function009

Input a number and print whether it is even or odd.

#include <stdio.h>
#include <conio.h>
void evnodd ( int );                               /* Prototype declaration */
void main ( )
{          
        int n;
        clrscr ( );
        printf("\n\n\n\t\t Enter a number..:"); scanf("%d",&n);
        
        evnodd(n);                                    /* function calling */
        getch ( );
}              
                   
void evnodd ( int    p )                          /* Function definition */
{                 
        if ( p % 2 == 0 )
        {                    
                   printf("\n\n\t\t The number is even.");
        }                                             
        else                   
        {                       
                   printf("\n\n\t\t The number is odd.");
        }
}
Click here to contact us

Function008

Input a number and print its prime factors (Different method).

#include <stdio.h>
#include <conio.h>
int prime ( int );
void pfactor ( int );
void main ( )
{       
        int n;
        clrscr ( );
        printf("\n\n\n\t\t Enter a number..:"); scanf("%d",&n);
       
        printf("\n\n\n\t\t PRIME FACTORS...:");
        pfactor ( n );
        getch ( );
}                       
                                         
void pfactor ( int    n )
{                
        int    d, i;
                  
        for ( i = 2; i <= n; i++ )
        {                               
                    if ( prime ( i ) )
                    {              
                               if ( n % i == 0 )
                               {            
                                             printf("%d, ",i);
                                             n = n / i;
                                             i--;
                               }
                    }
        }
}         
                
int prime ( int    n )
{               
        int    i;
        for ( i = 2; i < n; i++ )
        {          
                    if ( n % i == 0 )
                    return 0;
        }
        return 1;
}
Click here to contact us

Friday 28 September 2012

Function007

A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.

#include <stdio.h>
#include <conio.h>
void pf ( int );                                              /* Function prototype declaration */
void main ( )
{     
        int    no;
        clrscr ( );
                            
        printf("\n\n\n\n\t\t Enter a number...:"); scanf("%d", &no);
            
        pf ( no );                                             /* Function calling */
        getch ( );
}          
                           
void pf ( int    x )                                          /* Function Definition */
{                          
                            
        int      a = 2 ;
        printf("\n\n\n\t\t Prime Factors of %d are....:", x);
                              
        while ( x != 1 )
        {                             
                      if ( x % a == 0 )
                      {                   
                                    printf(" %d,", a );
                      }                                   
                      else                                     
                      {                                  
                                    a++;
                                    continue;
                      }
                      x = x / a;
        }
}
Click here to contact us 

Thursday 27 September 2012

Function006

Write a general - purpose function to convert any  given year into its roman equivalent. The following table shows the roman equivalent of decimal numbers:
Decimal                            Roman
Decimal                          Roman
1                                              i
5                                              v
10                                            x
50                                            l
100                                        c
500                                        d
1000                                      m



#include <stdio.h>
#include <conio.h>
void main ( )
{
        int          yr;
        clrscr ( );
                  
        printf("\n\n\n\n\t\t Enter year...:"); scanf("%d", &yr);
           
        printf("\n\n\t\t ROMAN EQUIVALENT...: ");
               
        yr = roman ( yr, 1000, 'm' );                              /* Function calling */
        yr = roman ( yr, 500, 'd' );
        yr = roman ( yr, 100, 'c' );
        yr = roman ( yr, 50, 'l' );
        yr = roman ( yr, 10, 'x' );
        yr = roman ( yr, 5, 'v' );
        yr = roman ( yr, 1, 'i' );
                   
        getch ( );
}                                  
                               
roman ( int    x, int   y, char    z )                                /* Function Definition */
{                
        int        a, b;
                             
        if ( x == 9 )
        {              
                  printf ( " ix " );
                  return ( x % 9 );
        }          
        if ( x == 4 )
        {              
                  printf ( " iv " );
                  return ( x % 4 );
        }                          
                               
        b = x / y;
        for ( a = 1; a <= b; a++ )
        {                               
                  printf ( "%c", z );
        }
        return ( x - y * b );
}
Click here to contact us

Wednesday 26 September 2012

Function005

Write a function power ( a, b), to calculate the value of a raised to b.

#include <stdio.h>
#include <conio.h>
long    power ( int,   int );                            /* Function prototype declaration */
void main ( )
{
         int         m, n;
         long      pow;
         clrscr ( );
                                              
         printf("\n\n\t\t Enter first number...:"); scanf("%d", &m);
         printf("\n\n\t\t Enter second number...:"); scanf("%d", &n);
                                                                           
         pow = power ( m, n );                       /* Function calling */
                                                                  
         printf("\n\n\t\t %d To the Power %d = % ld", m, n, pow );
         getch ( );
}                         
                                              
long power ( int    i, int    j )                      /* Function Definition */
{                                      
         int         a;
         long      b = 1;
                                          
         for ( a = 1; a <= j; a++ )
         {                                     
                          b = b * i;
         }
                          
         return ( b );
}
Click here to contact us`

Function004

Write a function to calculate the factorial value of any integer entered through the keyboard.
N.B. It will work up to nineteen number. 

#include <stdio.h>
#include <conio.h>
long       fact ( int );                                     /* Function prototype declaration */
void main ( )
{
          int         no;
          long      factorial;
          clrscr ( );
                                        
          printf("\n\n\t\t Enter a number...:"); scanf("%d", &no);
                                        
          factorial = fact ( no );                        /* Function calling */
                                      
          printf("\n\n\t\t FACTORIAL OF %d = % ld", no, factorial);
          getch ( );
}                                 
                                    
long  fact ( int    j )                                  /* Function Definition */
{                              
          int       i;
          long    f = 1;
                                   
          for ( i = 1; i <= j; i++ )
          {                                     
                          f = f * i;
          }
                                
          return ( f );
}
Click here to contact us

Tuesday 25 September 2012

Function003

Write a function to input amount, rate, time and print simple interest.

#include <stdio.h>
#include <conio.h>
float       interest ( float, float, float );                /* Function prototype declaration */
void main ( )
{     
        float       amt, rat, tim;
        float       sud;
        clrscr ( );
                            
        printf("\n\n\t\t Enter amount.............:"); scanf("%f", &amt);
        printf("\n\n\t\t Enter rate of interest...:"); scanf("%f", &rat);
        printf("\n\n\t\t Enter time duration......:"); scanf("%f", &tim);
                                
        sud = interest ( amt, rat, tim );                       /* Function calling */
                                                         
        printf("\n\n\t\t INTEREST = % .2f", sud);
        getch ( );
}                         
                            
float  interest ( float    x, float    y, float    z )           /* Function Definition */
{                               
        float       w;
        w = ( x * z ) * y / 100.0;
        return ( w );
}
Click here to contact us

Function002

Write a function to input two numbers and print there sum and average.

#include <stdio.h>
#include <conio.h>
int        sum ( int, int );                         /* Function prototype declaration */
float     avg  ( int );                              /* Function prototype declaration */
void main ( )
{
        int           i, j, k;
        float        p;
        clrscr ( );
                  
        printf("\n\n\t\t Enter first number....:"); scanf("%d", &i);
        printf("\n\n\t\t Enter second number...:"); scanf("%d", &j);
                                      
        k = sum ( i, j );                         /* Function calling */
        p = avg ( k );                            /* Function calling */
                
        printf("\n\n\t\t SUM = %d", k);
        printf("\n\n\t\t AVERAGE = % .2f", p);
        getch ( );
}              
                
int    sum ( int    x, int    y )                /* Function Definition */
{                     
        int    z;
        z = x + y;
        return ( z );
}                                                                      
                                 
float  avg ( int    m )                         /* Function Definition */
{                  
        float    n;
        n = m / 2;
        return ( n );
}
Click here to contact us

Function001

Write a function to input two numbers and print there sum.

#include <stdio.h>
#include <conio.h>
int          sum ( int, int );                        /* Function prototype declaration */
void main ( )
{    
        int    i, j, k;
        clrscr ( );
                
        printf("\n\n\t\t Enter first number....:"); scanf("%d", &i);
        printf("\n\n\t\t Enter second number...:"); scanf("%d", &j);
                      
        k = sum ( i, j );                          /* Function calling */
                                    
        printf("\n\n\t\t SUM = %d", k);
        getch ( );
}                             
             
int     sum ( int    x, int    y )               /* Function Definition */
{          
         int    z;
         z = x + y;
         return ( z );
}
Click here to contact us

Monday 24 September 2012

String030

Input state and print capital of that state.

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ( )
{
        char       nm[80];
        char       state[6][20] = { "WEST BENGAL", "BHIHAR", "ORISHA", "ASSAM",
                                               "GUJARAT", "RAJASTHAN" };
        char       cap[6][20] = { "kolkata", "patna", "bhubaneswar", "dispur",
                                             "gandhinagar", "jaipur" };
        int          c, i = 1;
        clrscr ( );
                     
        printf("\n\n\n\t NAME OF STATE \n\t_______________\n\t");
        for ( c = 0; c < 6; c++)
        {                                             
                     printf("\n\n\t  %s", state[c] );
        }
                                     
        printf("\n\n\n\t Enter the name of state....:"); gets(nm);
                              
        for ( c = 0; c <= 5; c++)
        {                                                   
                     if ( strcmp( nm, state[c] ) == 0 )
                     {                                    
                                    printf("\n\n\n\t Capital.....: %s", cap[c] );
                                    i = 0;
                                    break;
                     }
        }                                       
        if ( i == 1 )
        {                      
                 printf("\n\n\n\t\t NOT FOUND!");
        }
        getch ( );
}
Click here to contact us

String029

Password program.

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ( )
{
         char     masterlist[6][10] = { "akshya", "dipankar", "sanjay", "nilanjan", "sankar", "gopal" };
         char     yourname[10];
         int        i, flag = 0;
         clrscr ( );
                                 
         printf("\n\n\n\t Enter your name....:"); gets(yourname);
                         
         for ( i = 0; i <= 5; i++)
         {                              
                     if ( strcmp( yourname, masterlist[i] ) == 0 )
                     {                                     
                                    printf("\n\n\n\t\t WELCOME, YOU CAN ENTER THE PALACE!");
                                    flag = 1;
                                    break;
                     }
         }          
         if ( flag == 0 )
         {                                                       
                     printf("\n\n\n\t\t SORRY, YOU ARE A TRESPASSER!");
         }
         getch ( );
}
Click here to contact us

Sunday 23 September 2012

String028

Input a sentence and a word, write a program to  find whether the word is in the sentence or not.

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main ( )
{
          char       nm1[80], nm2[80][80], nm3[80], nm4[80] ;
          int          i = 0, j = 0, n = 0, y = 0, ln, m = 1;
          clrscr ( );
                                                      
          printf("\n\n\n\t Enter a sentence....:"); gets(nm1);
          printf("\n\n\n\t Enter a word....:"); gets(nm3);
          ln = strlen (nm1);
                                            
          for ( y = 0; y <= ln; y++ )
          {                                                   
                       if ( nm1[y] != ' ' && nm1[y] != '\0' )
                       {                                                    
                                       nm2[i][ j] = nm1[y];
                                       j++;
                       }                                              
                       else                                                 
                       {                                                     
                                       nm2[i][ j] = '\0';
                                       i++;
                                       j = 0;
                       }
          }                
          for ( j = 0; j < i; j++)
          {                                                                                 
                       n = strcmp ( nm2[ j], nm3 );
                       if ( n == 0 )   
                       {                      
                                 m = 0;
                                 break;
                       }
          }                 
          if ( m == 0 )
          {                                  
                       printf("\n\n\n\t\t WORD FOUND!");
          }                                                                 
          else                                                          
          {                                                             
                       printf("\n\n\n\t\t WORD NOT FOUND!");
          }
          getch ( );
}
Click here to contact us
Related Posts Plugin for WordPress, Blogger...