Friday, 5 October 2012

Function018

Write a function to calculate the factorial value of a given number.

#include <stdio.h>
#include <conio.h>
long fact ( long );
void main ( )
{                          
        long n,f;
        clrscr ( );
        printf("\n\n\n\t\t Enter a number..:"); scanf("%ld",&n);
                    
        f = fact ( n );
        printf("\n\n\n\t\t Factorial value of %ld is %ld.",n,f);
        getch();
}                              
                                  
long fact ( long    n )
{                        
        long     i, f = 1;
                               
        for ( i = 1; i <= n; i++ )
        {                                        
                    f = f * i;
        }
        return f;
}
Click here to contact us 

Thursday, 4 October 2012

Function017

Input the lower limit and upper limit and print all prime numbers between them using function.

#include <stdio.h>
#include <conio.h>
int prime ( int );
void main ( )
{                 
        int      l, u, i;
        clrscr ( );
        printf("\n\n\n\n\t\t Enter the lower limit..:"); scanf("%d",&l);
        printf("\n\n\t\t Enter the upper limit..:"); scanf("%d",&u);
        printf("\n\n\t All Prime numbers between %d and %d are..:\n\n ",l,u);
                                   
        for ( i = l; i <= u; i++ )
        {                                    
                 if ( prime ( i ) )
                 {                          
                           printf("%d\t",i);
                 }
        }
        getch ( );
}                       
                                
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

Function016

Write a function to determine whether the input number is prime or not.

#include <stdio.h>
#include <conio.h>
int prime ( int );
void main ( )
{                              
        int      n;
        clrscr ( );
        printf("\n\n\n\t\t Enter a number..:"); scanf("%d",&n);
                  
        if ( prime ( n ) )
        {              
                    printf("\n\t\t The number is a prime number.");
        }                                
        else                 
        {                                        
                    printf("\n\t\t The number is not a prime number.");
        }
        getch ( );
}               
                        
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

Wednesday, 3 October 2012

Function015

Write a recursive function to obtain the first twenty five numbers of a Fibonacci sequence.
N.B. [ Example : 1  1  2  3  5  8  13  21  34  55.............. ]

#include <stdio.h>
#include <conio.h>
void fibo ( int, int );                                     /* Function Prototype Declaration */
void main ( )
{                               
        int    i, t, old = 0, current = 1, new;
        clrscr ( );
                           
        printf("\n\n\n\t\t\t      FIBONACCI SEQUENCE");
        printf("\n\t\t\t     ____________________");
        printf("\n\n\n\n %d \t %d\t", old, current);
                            
        fibo ( old, current );                          /* Function calling */
        getch ( );
}                     
                                  
void fibo ( int    old, int    current )            /* Function Definition */
{                              
        static int      terms = 2;
        int    new;
                     
        if ( terms < 20 )
        {                                
                  new = old + current;
                  printf ("%d \t", new );
                  terms = terms + 1;
                  fibo ( current, new );
        }                     
        else                 
                  return;
}

Click here to contact us

Tuesday, 2 October 2012

Function014

A five digit positive integer ( less than 32767 ) is entered through the keyboard write a function to calculate sum of digits of the five digit number by using recursive function.

#include <stdio.h>
#include <conio.h>
int rsum ( int );                                                /* Function Prototype Declaration */
void main ( )
{         
        int        rs;
        int        n;
        clrscr ( );
                                 
        printf("\n\n\t\t Enter a number...:"); scanf("%d", &n);
                                    
        rs = rsum ( n );                                      /* Function calling */
        printf("\n\n\t\t Sum of Digits...: %d", rs);
        getch ( );
}                  
                        
int rsum ( int    num )                                    /* Function Definition */
{            
        int      sum = 0, rem;
                      
        if ( num != 0 )
        {                                     
                    rem = num % 10;
                    sum = rem + rsum ( num / 10 );
        }
        return sum;
}
Click here to contact us 

Monday, 1 October 2012

Function013

A name or a word and a number entered through the keyboard. Write a function to print that name or word from right up to entered number.

#include <stdio.h>
#include <conio.h>
void right ( char[], int );
void main ( )
{      
        char    n[80];
        int       c;
        clrscr ( );
                               
        printf("\n\n\n\n\t\t Enter a name...:"); gets ( n );
        printf("\n\n\t\t Enter a number...:"); scanf("%d", &c);
               
        right ( n, c );                                  /* Function calling */
        getch ( );
}                               
                                 
void right ( char    nm[80], int    x )        /* Function Definition */
{                     
        int      ln = 0, i = 0, y;
        for ( ln = 0; nm[ln] != '\0'; ln++ );
                    
        if ( x <= ln )
        {                   
                printf("\n\n\n\t\t OUTPUT....:");
                y = ln - x;
                for ( i = y; i < ln; i++ )
                {                     
                            printf("%c", nm[i] );
                }
        }               
        else            
        {                   
                printf("\n\n\n\t\t Invalid Input....");
        }
}
Click here to contact us

Function012

A name or a word and a number entered through the keyboard. Write a function to print that name or word from left up to entered number.

#include <stdio.h>
#include <conio.h>
void left ( char[], int );
void main ( )
{                                
        char    n[80];
        int       c;
        clrscr ( );
                 
        printf("\n\n\n\n\t\t Enter a name...:"); gets ( n );
        printf("\n\n\t\t Enter a number...:"); scanf("%d", &c);
                              
        left ( n, c );                                    /* Function calling */
                  
        getch ( );
}                                
                                            
void left ( char    nm[80], int    x )        /* Function Definition */
{               
        int      ln = 0, i;
        for ( ln = 0; nm[ln] != '\0'; ln++ );
                                                    
        if ( x <= ln )
        {                                
                  printf("\n\n\n\t\t OUTPUT....:");
                  for ( i = 0; i < x; i++ )
                  {                    
                              printf("%c", nm[i] );
                  }
        }                               
        else                                                   
        {                                   
                  printf("\n\n\n\t\t Invalid Input....");
        }
}
Click here to contact us
Related Posts Plugin for WordPress, Blogger...