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