Saturday, 13 October 2012

Function029

Develop a function to calculate sum of n even integers starting from a given even integer.

#include <stdio.h>
#include <conio.h>
void main ( )
{                        
        int     s, n, t;
        clrscr ( );
      
        do
        {                          
                    printf("\n\n\n\t Enter an even integer to start with..:"); scanf("%d",&s);
        }while ( s % 2 != 0 );
                  
        printf("\n\t Enter the number of even integers..:"); scanf("%d",&n);
        t = sumeven ( s, n );
        printf("\n\n\n\t Starting from %d sum of %d even integers = %d",s,n,t);
        getch ( );
}              
                      
int sumeven ( int    s, int    n )
{                           
        int     i, t = 0;
        for ( i = 1; i <= n; i++, s = s+2 )
        {                                     
                    t = t + s;
        }
        return t;
}
Click here to contact us

Function028

Write a program using function to read an array from the user and print the odd number at odd position and prints the even numbers at even positions. The number of odd numbers and even numbers should be equal.

#include <stdio.h>
#include <conio.h>
void oddeven (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]);
        }                       
        oddeven ( arr, 10 );
        getch ( );
}                       
                                             
void oddeven ( int    arr[], int     l )
{                                  
        int       i, brr[10], j = 0;
                                                   
        printf("\n\n\n\t\t The array is..:\n");
        printf("\n\t POSITION:");
        for ( i = 0; i < l; i++ )
        {                                    
                    printf("%d ",i);
        }                                      
        printf("\n\t ELEMENTS:");
        for ( i = 0; i < l; i++ )
        {                                    
                    printf("%d ",arr[i]);
        }
        for ( i = 0; i < l; i++ )
        {                                      
                    if ( arr[i] % 2 == 0 )
                    {                                     
                                 brr[j] = arr [i];
                                 j = j + 2;
                    }
        }
        j = 1;
        for ( i = 0; i < l; i++ )
        {                                      
                    if ( arr [i] % 2 != 0 )
                    {                                        
                                 brr[j]=arr[i];
                                 j = j + 2;
                    }
        }             
        for ( i = 0; i < l; i++ )
        {                                
                     arr[i]=brr[i];
        }
        printf("\n\n\n\t\t The resultant array is..:\n");
        printf("\n\t POSITION:");
        for ( i = 0; i < l; i++ )
        {                                
                     printf("%d ",i);
        }
        printf("\n\t ELEMENTS:");
             
        for( i = 0; i < l; i++ )
        {                                    
                    printf("%d ",arr[i]);
        }
}
Click here to contact us

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
Related Posts Plugin for WordPress, Blogger...