Friday, 12 October 2012

Function027

Write a function which accepts an integer array of size n and prints every third value of the array. 

#include <stdio.h>
#include <conio.h>
void print ( int    arr[], int    n )
{                                           
        int      i;
        for ( i = 2; i < n; i = i + 3 )
        {                                             
                    printf(" %d",arr[i]);
        }
}
                                  
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 Printing every 3rd integer of the array. ");
        print(arr,10);
        getch ( );
}
Click here to contact us 

Function026

Write a function that takes time in seconds as input and print it in terms of hour, minute and seconds.

#include <stdio.h>
#include <conio.h>
void hrminsec ( int    sec )
{                                
        int      hr, min, t;
                     
        min = sec / 60;
        sec = sec % 60;
        hr = min / 60;
        min = min % 60;
        printf("\n\n\t\t Hr:%d Min:%d Sec:%d",hr,min,sec);
}                            
                       
void main ( )
{                        
        int         sec;
        clrscr ( );
                   
        printf("\n\n\n\n\t Enter the time in seconds..:"); scanf("%d",&sec);
        hrminsec ( sec );
        getch ( );
}
Click here to contact us

String032

Write a program which asks the user to enter the name and age of persons in his group. The number of persons is not known to the user in the beginning of the program. The user keeps on entering the data till the user enters the age as zero. The program finally prints the average age.

#include <stdio.h>
#include <conio.h>
void main ( )
{                                    
        int           age, totage = 0, n = 0;
        char        name[40];
        float        avgage = 0;
        clrscr ( );
                        
        do     
        {                                  
                fflush ( stdin );
                printf("\n\n\t\t Enter name.:"); gets(name);
                fflush ( stdin );
                printf("\t\t Enter age..:"); scanf("%d",&age);
                totage = totage + age;
                n++;
        } while ( age != 0 );
        avgage = ( float ) totage / n;
                 
        printf("\n\n\n\n\t\t Average age=%.2f",avgage);
        getch ( );
}
Click here to contact us

Thursday, 11 October 2012

Function025

Write a function which accepts an array of size n containing integer values and returns the average of all values. Call the function from main program. 

#include <stdio.h>
#include <conio.h>
float arravg ( int    [], int );
void main ( )
{                     
        int      arr[10], i;
        float   f;
        clrscr ( );
                            
        for( i = 0; i < 10; i++ )
        {                                      
                   printf("\n\t\t Enter a number..:"); scanf("%d",&arr[i] );
        }                      
                           
        f = arravg ( arr, 10 );
        printf("\n\n\n\t\t Average of the array elements = %.2f",f);
        getch ( );
}                        
                            
float arravg ( int    arr[], int    l )
{                         
        int            i, s = 0;
        float         avg;
                    
        for ( i = 0; i < l; i++ )
        {                                 
                    s = s + arr[i];
        }
        avg = ( float ) s / l;
        return avg;
}
Click here to contact us

Wednesday, 10 October 2012

Function024

Input a number and a position from user. Find out the digit in that position counted from right to left. Print appropriate error message if the position is invalid.

#include <stdio.h>
#include <conio.h>
int position ( int, int );
void main ( )
{                        
        int      n, p, d;
        clrscr ( );
        printf("\n\n\n\t\t Enter a number (less than 32767)..:"); scanf("%d",&n);
        printf("\n\t\t Enter the position..:"); scanf("%d",&p);
                                 
        d = position ( n, p );
        if ( d == -1 )
        {                                               
                   printf("\n\n\n\t\t Invalid position.");
        }                                              
        else           
        {                                            
                   printf("\n\n\n\t\t Required digit is .:%d",d);
        }
        getch ( );
}                             
                                      
int position ( int n,int d)
{                       
        int     i = 1, r;
        if ( d <= 0 )
                   return -1;
        while ( n > 0 )
        {                        
                     r = n % 10;
                     if ( i == d )
                              return r;
                     i++;
                     n = n / 10;
        }
        return -1;
}
Click here to contact us

Tuesday, 9 October 2012

Function023


Write a program to compute G.C.D. of any number of elements.

#include <stdio.h>
#include <conio.h>
void main ( )
{                  
        int         n, arr[50], gcdn, i;
        clrscr ( );
        printf("\n\n\n\t\t Enter the number of elements..:"); scanf("%d",&n);
       
        for ( i = 0; i < n; i++ )
        {                                      
                    printf("\n\n\n\t\t Enter a number..:"); scanf("%d",&arr[i]);
        }
        gcdn = arr [0];
        for ( i = 1; i < n; i++ )
        {                                        
                    gcdn = gcd ( gcdn, arr[i] );
        }                                                
        printf("\n\n\t\t GCD = %d",gcdn);
        getch ( );
}                                  
                                     
int gcd ( int    n1, int    n2 )
{                                              
        int       d;
        while ( n2 != 0 )
        {                              
                        d = n1 % n2;
                        n1 = n2;
                        n2 = d;
        }
        return n1;
}
Click here to contact us

Monday, 8 October 2012

Loop control structure071

Write a program to determine G.C.D. of two input number. 

#include <stdio.h>
#include <conio.h>
void main ( )
{                              
        int         n1, n2, d;
        clrscr ( );
                                      
        printf("\n\n\n\t\t Enter a number..:");    scanf("%d",&n1);
        printf("\n\n\t\t Enter a number..:");    scanf("%d",&n2);
                             
        while ( n2 != 0 )
        {                               
                       d = n1 % n2;
                       n1 = n2;
                       n2 = d;
        }
              
        printf("\n\n\n\t\t G.C.D = %d",n1);
        getch();
}
Click here to contact us 
Related Posts Plugin for WordPress, Blogger...