Write the functions to perform the following : 10m Dec2005

By | June 9, 2014

Write the functions to perform the following : 10m Dec2005  

(i) To find mn where m, n > 0

#include<stdio.h>
void main()
{
 long int power(int,int);
 int M,N;
 long int RES;
 printf(“ENTER M : “);
 scanf(“%d”,&M);
 printf(“ENTER N : “);
 scanf(“%d”,&N);
 RES=power(M,N);
 printf(“\nRESULT IS %ld “,RES);
 getch();
}

long int power(int M,int N)
{
 long int i,RESULT=1;
    for(i=0;i<N;i++)
    {
      RESULT*=M;
    }
 return(RESULT);
}

CODE : -

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  long int power(int,int);
  5.  int M,N;
  6.  long int RES;
  7.  printf("ENTER M : ");
  8.  scanf("%d",&M);
  9.  printf("ENTER N : ");
  10.  scanf("%d",&N);
  11.  RES=power(M,N);
  12.  printf("\nRESULT IS %ld ",RES);
  13.  getch();
  14. }
  15. long int power(int M,int N)
  16. {
  17.  long int i,RESULT=1;
  18.     for(i=0;i<N;i++)
  19.     {
  20.       RESULT*=M;
  21.     }
  22.  return(RESULT);
  23. }

SCREEN SHOTS :-

MpowN

 

MpowN_Output

 

(ii) To swap two variables

# include <stdio.h>
void main()
{
  int M,N;
  void swapRef ( int *, int * );
  clrscr();
  printf(“ENTER M : “);
  scanf(“%d”,&M);
  printf(“ENTER N : “);
  scanf(“%d”,&N);
  printf (“\nBefore calling function swapRef M=%d N=%d”,M,N);
  swapRef (&M,&N); /*address of arguments are passed */
  printf(“\nAfter calling function swapRef M=%d N=%d”,M,N);
  getch();
}
void swapRef (int *pm, int *pn)
{
  int temp;
  temp = *pm;
  *pm = *pn;
  *pn = temp;
  printf (“\nWithin function swapRef *pm=%d *pn=%d”,*pm,*pn);
  return;
}

CODE : -

  1. # include <stdio.h>
  2. void main()
  3. {
  4.   int M,N;
  5.   void swapRef ( int *, int * );
  6.   clrscr();
  7.   printf("ENTER M : ");
  8.   scanf("%d",&M);
  9.   printf("ENTER N : ");
  10.   scanf("%d",&N);
  11.   printf ("\nBefore calling function swapRef M=%d N=%d",M,N);
  12.   swapRef (&M,&N); /*address of arguments are passed */
  13.   printf("\nAfter calling function swapRef M=%d N=%d",M,N);
  14.   getch();
  15. }
  16. void swapRef (int *pm, int *pn)
  17. {
  18.   int temp;
  19.   temp = *pm;
  20.   *pm = *pn;
  21.   *pn = temp;
  22.   printf ("\nWithin function swapRef *pm=%d *pn=%d",*pm,*pn);
  23.   return;
  24. }

SCREEN SHOTS :-

 SWAP

 SWAP_Output

 Q  Write a program in ‘C’ to find and display the minimum and maximum values of an array of integers. Note : You should use “pointers” concept only 5m Dec2005 

Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-with-a-function-that-returns-the-minimum-and-the-maximum-value-in-an-array-of-integers

Leave a Reply