EXAMPLES OF FUNCTION

WAP to find area of a rectangle using functions of with arguments and with return model.

#include<stdio.h>

int area(int l,int w)

{

  return l*w;

}

void main()

{

  int area(int,int);

  int l,w,a;

  printf(“Enter length,width :”);

  scanf(“%d%d”,&l,&w);

  a=area(l,w);

  printf(“Area=%d”,a);

Output :

Enter length,width : 10

5

Area=50

WAP to swap 2 numbers using functions of with arguments and without return.

 

#include<stdio.h>

void swap(int a,int b)

{

  int t;

  t=a;

  a=b;

  b=t;

  printf(“a=%d”,a);

  printf(“\nb=%d”,b);

}

void main()

{

  void swap(int,int);

  int a,b;

  printf(“Enter 2 numbers”);

  scanf(“%d%d”,&a,&b);

  swap(a,b);

}  

Output:

Enter 2 numbers

10

20

a=20

b=10

WAP to find area of circle using functions of without arguments and with return.

 

#include<stdio.h>

float area()

{

  int r;

  printf(“Enter radius :”);

  scanf(“%d”,&r);

  return 3.14*r*r;

}

void main()

{

  float area();

  float a;

  a=area();

  printf(“Area=%f”,a);

}   

Output:

Enter radius: 10

Area=314.000000

WAP to find length of a string using functions of without arguments and without return model.

 

#include<stdio.h>

void length()

{

  char str[100];

  printf(“Enter a string :”);

  scanf(“%s”,&str);

  int len;

  for(len=0;str[len]!='\0';len++);

  printf(“Length=%d”,len);

}

void main()

{

  void length();

  length();

}  

Output:

Enter a string : rama

Length=4

 

WAP to read a number. Find the factorial using recursion functions.

Fact( 1 )=1

Fact(2)=2*1=2*fact(1)

Fact(3)=3*2*1=3*fact(2)

Fact(10)=10*fact(9)

Fact(n)=n*fact(n-1)

#include<stdio.h>

long int fact(int n)

{

  if( n==1 )

  return 1;

  else

  return n*fact(n-1);

}

void main()

{

  long int fact(int);

  int n;

  long int f;

  printf(“Enter a number :”);

  scanf("%d",&n);

  f=fact(n);

  printf(“Factorial=%ld”,f);

}  

Output:

Enter a number: 5

Factorial=120

WAP to print Fibonacci series using recursion functions.

 

0 1 1 2 3 5 8 13 21 34

Fib( 1 )=0

Fib( 2 )=1

Fib( 3 )=fib( 2 )+fib( 1 )

Fib(10)=fib( 9 ) + fib( 8 )

Fib( n )= fib( n-1 ) + fib( n-2 )

#include<stdio.h>

int fib(int n)

{

  if( n==1 )

  return 0;

  else if( n==2 )

  return 1;

  else

  return fib(n-1)+fib(n-2);

}

void main()

{

  int n;

  for( n=1;n<=10;n++ )  

  printf(“%5d”,fib(n));

}  

Output:

0 1 1 2 3 5 8 13 21 34

 

 

WAP to find GCD of 2 numbers using recursion functions.

 

4 1,2,4

6 1,2,3,6

GCD=2

 A

B

R

4

6

4

6

4

2

4

2

0

#include<stdio.h>

void GCD(int a,int b)

{

  int r=a%b;

  if( r==0 )

  printf(“GCD=%d”,b);

  else

  GCD(b,r);

}

void main()

{

  void GCD(int,int);

  int a,b;

  printf(“enter 2 numbers :”);

  scanf(“%d%d”,&a,&b);

  GCD(a,b);

}   

Output:

enter 2 numbers: 6

4

GCD=2

 

WAP to read a number. Convert it to binary.

No

Q

R

1

0

1

#include<stdio.h>

void dec2bin(int no)

{

  int q=no/2;

  int r=no%2;

  if( q==0 )

  printf(“%d”,r);

  else

{

  dec2bin(q);

  printf(“%d”,r);

}

}

void main()

{

  void dec2bin(int);

  int no;

  printf(“Enter a number :”);

  scanf(“%d”,&no);

  dec2bin(no);

}   

Output:

Enter a number: 10

1010

 

WAP to read and print N elements into an array using functions.

#include<stdio.h>

void read(int a[100],int n)

{

  int i;

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,&a[i]);

}

void disp(int a[100],int n)

{

  int i;

  printf(“Array is :”);

  for( i=0;i<n;i++ )

  printf(“%5d”,a[i]);

}

void main()

{

  int a[100],n;

  printf(“Enter Array Size :”);

  scanf(“%d”,&n);

  read( a,n );

  disp( a,n );

}   

Output:

Enter Array Size: 5

Enter 5 elements: 10 20 30 40 50

Array is: 10 20 30 40 50

 

WAP to find sum of 2 arrays of size N using functions.

 

#include<stdio.h>

void read(int a[100],int n)

{

  int i;

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,&a[i]);

}

void disp(int a[100],int n)

{

  int i;

  printf(“Array is :”);

  for( i=0;i<n;i++ )

  printf(“%5d”,a[i]);

}

void add(int a[100],int b[100],int c[100],int n)

{

  int i;

  for( i=0;i<n;i++ )

  c[i]=a[i]+b[i];

}

void main()

{

  int a[100],b[100],c[100],n;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  read( a,n );

  read( b,n );

  add( a,b,c,n );

  disp( c,n );

}  

Output:

Enter array size: 5

Enter 5 elements: 10 20 30 40 50

Enter 5 elements: 1 2 3 4 5

Array is: 11 22 33 44 55

WAP to find equilibrium elements of an array using functions.

 

#include<stdio.h>

void read(int a[100],int n)

{

  int i;

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,&a[i]);

}

void disp(int a[100],int n)

{

  int i;

  printf(“\nArray is :”);

  for( i=0;i<n;i++ )

  printf(“%5d”,a[i]);

}

void findEquilibrium(int a[100],int n)

{

  int i,j,sum1=0,sum2=0,flag=0;

  for( i=0;i<n;i++ )

{

  for( j=0;j<i;j++ )

  sum1=sum1+a[j];

  for( j=i+1;j<n;j++ )

  sum2=sum2+a[j];

  if( sum1==sum2 )

{

  printf(“Equilibrium elements is : %d”,a[i]);

  flag=1;

}

  else

{

  sum1=sum2=0;

}

}

  if( flag==0 )

  printf(“\nThere is no equilibrium element”);

}

void main()

{

  int a[100],n;

  printf(“Enter array size : “);

  scanf(“%d”,&n);

  read( a,n );

  disp( a,n );

  findEquilibrium(a,n);

}

Output:

Enter array size: 6

Enter 6 elements: 5 15 10 40 5 25

Array is: 5 15 10 40 5 25

Equilibrium elements is: 40

 

WAP to read N elements into an array, print the array elements in reverse order using recursion function.

 

#include<stdio.h>

void read( int a[100],int n)

{

  int i;

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,&a[i]);

}

void reverse( int a[100],int ind)

{

  if( ind==-1 )

  return;

  else

{

  printf(“%5d”,a[ind]);

  reverse( a,ind-1);

}

}

void main()

{

  int a[100],n;

  printf(“Enter array size :”);  

  scanf(“%d”,&n);

  read( a,n );

  reverse( a,n-1 );

}

Output:

Enter array size: 6

Enter 6 elements: 10 20 30 40 50 60

60 50 40 30 20 10

 

WAP to read mxn elements into an array, print it on screen using functions.

 

#include<stdio.h>

void read( int a[10][10],int m,int n )

{

  int i,j;

  printf(“Enter %d elements :”,m*n);

  for( i=0;i<m;i++ )

  for( j=0;j<n;j++ )

  scanf(“%d”,&a[i][j]);

}

void disp( int a[10][10],int m,int n)

{

  int i,j;

  printf(“Array is : \n”);

  for( i=0;i<m;i++ )

{

  for( j=0;j<n;j++ )

  printf(“%5d”,a[i][j]);

  printf(“\n”);

}

}

void main()

{

  int a[10][10],m,n;

  printf(“Enter Array size :”);

  scanf(“%d%d”,&m,&n);

  read( a,m,n );

  disp( a,m,n );

}

Output:

Enter Array size: 2 3

Enter 6 elements: 10 20 30 40 50 60

Array is:

10 20 30

40 50 60

 

WAP to read mxn elements into 2 arrays, find the addition array using functions.

 

#include<stdio.h>

int read( int a[10][10],int m,int n)

{

  int i,j;

  printf(“Enter %d elements :”,m*n);

  for( i=0;i<m;i++ )

  for( j=0;j<n;j++ )

  scanf(“%d”,&a[i][j]);

}

void disp( int a[10][10],int m,int n)

{

  int i,j;

  printf(“Result is : \n”);

  for( i=0;i<m;i++ )

{

  for( j=0;j<n;j++ )

  printf(“%5d”,a[i][j]);

  printf(“\n”);

}

}

void add( int a[10][10],int b[10][10],int c[10][10],int m,int n)

{

  int i,j;

  for( i=0;i<m;i++ )

  for( j=0;j<n;j++ )

  c[i][j]=a[i][j]+b[i][j];

}

void main()

{

  int a[10][10],b[10][10],c[10][10],m,n;

  printf(“enter array size :”);

  scanf(“%d%d”,&m,&n);

  read( a,m,n );

  read( b,m,n );

  add( a,b,c,m,n );

  disp( c,m,n);

}

Output:

enter array size: 2 2

Enter 4 elements: 10 20 30 40

Enter 4 elements: 1 2 3 4

Result is:

11 22

33 44

 

WAP to read mxn elements into an array, print the array using functions and using single for loop.

 

#include<stdio.h>

void read(int a[10][10],int m,int n)

{

  int i,row,col;

  printf(“Enter %d elements :”,m*n);

  for( i=0;i<m*n;i++ )

{

  row=i/n;

  col=i%n;

  scanf(“%d”,&a[row][col]);

}

}

void disp( int a[10][10],int m,int n)

{

  int i,row,col;

  printf(“Array is : \n”);

  for( i=0;i<m*n;i++ )

{

  row=i/n;

  col=i%n;

  if( col==0 )

  printf(“\n”);

  printf(“%5d”,a[row][col]);

}

}

void main()

{

  int a[10][10],m,n;

  printf(“Enter array size :”);

  scanf(“%d%d”,&m,&n);

  read( a,m,n );

  disp( a,m,n );

}

Output:

Enter array size: 2 3

Enter 6 elements: 10 20 30 40 50 60

Array is:

10 20 30

40 50 60

 

WAP to read a string, find the max character using functions.

 

#include<stdio.h>

void read( char str[100] )

{

  printf(“Enter a string :”);

  scanf(“%s”,str);

}

void findMax( char str[100] )

{

  int max=str[0];

  int i;

  for( i=0;str[i]!='\0';i++ )

{

  if( str[i]>max )

  max=str[i];

}

  printf(“Max Character=%c”,max);

}

void main()

{

  char str[100];

  read( str );

  findMax(str);

}   

Output:

Enter a string : rama

Max Character=r

WAP to read a string, find the min character using functions

 

#include<stdio.h>

void read( char str[100] )

{

  printf(“Enter a string : “);

  scanf(“%s”,str);

}

void findMin( char str[100] )

{

  int min=str[0];

  int i;

  for( i=0;str[i]!='\0';i++ )

{

  if( str[i]<min )

  min=str[i];

}

  printf(“Min Character=%c”,min);

}

void main()

{

  char str[100];

  read( str );

  findMin(str);

}   

Output:

Enter a string: rama

Min Character=a

WAP to copy a string into another string using functions.

 

#include<stdio.h>

void read( char str[100])

{

  printf(“Enter a string : “);

  scanf(“%s”,str);

}

void copy( char str[100],char des[100] )

{

  int i;

  for( i=0;str[i]!='\0';i++ )

{  

  des[i]=str[i];

}

  des[i]='\0';

}

void disp( char str[100],char des[100])

{

  printf(“Original String =%s”,str);

  printf(“\nCopied string=%s”,des);

}

void main()

{

  char str[100],des[100];

  read( str );

  copy( str, des );

  disp( str, des );

}   

Output:

Enter a string : india

Original String =india

Copied string=india

WAP to concatenate 2 strings using function.

 

#include<stdio.h>

void read( char str[100] )

{

  printf(“Enter a string :”);

  scanf(“%s”,str);

}

void merge( char str1[100],char str2[100])

{

  int i,j,len;

  for( len=0;str1[len]!='\0';len++);

  for( j=0,i=len;str2[j]!='\0';j++,i++ )

  str1[i]=str2[j];

  str1[i]='\0';

}

void main()

{

  char str1[100],str2[100];

  read( str1 );

  read( str2 );

  printf(“First string before merge = %s”,str1);

  printf(“\nSecond string before merge = %s”,str2);

  merge( str1,str2 );

  printf(“\nFirst string after merge = %s”,str1);

  printf(“\nSecond string after merge = %s”,str2);

}

Output:

Enter a string : rama

Enter a string : rao

First string before merge = rama

Second string before merge = rao

First string after merge = ramarao

Second string after merge = rao

WAP to read a string, convert it to upper using functions.

 

#include<stdio.h>

void read( char str[100] )

{

  printf(“Enter a string :”);

  scanf(“%s”,str);

}

void convert( char str[100] )

{

  int i;

  for( i=0;str[i]!='\0';i++ )

{

  if( str[i]>='a' && str[i]<='z' )

  str[i]=str[i]-32;

}

}

void main()

{

  char str[100];

  read( str );

  printf(“Original String=%s”,str);

  convert( str );

  printf(“\nUpper case string=%s”,str);

}   

Output:

Enter a string: sita

Original String= sita

Upper case string=SITA

WAP to read a string, convert it to lower case using functions.

#include<stdio.h>

void read( char str[100] )

{

  printf(“Enter a string :”);

  scanf(“%s”,str);

}

void convert( char str[100] )

{

  int i;

  for( i=0;str[i]!='\0';i++ )

{

  if( str[i]>='A' && str[i]<='Z' )

  str[i]=str[i]+32;

}

}

void main()

{

  char str[100];

  read( str );

  printf(“Original String=%s”,str);

  convert( str );

  printf(“\nLower case string=%s”,str);

}   

Output :

Enter a string : RAma

Original String=RAma

Lower case string=rama

WAP to read a string, convert it to title case using function.

 

#include<stdio.h>

void read(char str[100])

{

  printf(“Enter a string :”);

  gets( str );

}

  void toTitle( char str[100])

{

  int i;

  if( str[0]>='a'&&str[0]<='z')

  str[0]=str[0]-32;

  for( i=1;str[i]!='\0';i++ )

{

  if( str[i-1]==' ')

{

  if( str[i]>='a' && str[i]<='z')

  str[i]=str[i]-32;

}

}

}

  void disp(char str[100])

{

  printf(“String after conversion = %s”,str);

}

void main()

{

  char str[100];

  read( str );

  toTitle( str );

  disp( str );

}   

Output:

Enter a string : rama is a good boy

String after conversion = Rama Is A Good Boy

 

WAP to read a string, convert to toggle case using functions.

 

#include<stdio.h>

void read( char str[100] )

{

  printf(“Enter a string :”);

  gets( str );

}

  void disp( char str[100])

{

  printf(“String after conversion=%s”,str);

}

void toToggle( char str[100])

{

  int i;

  for( i=0;str[i]!='\0';i++ )

{

  if( str[i]>='a' && str[i]<='z' )

  str[i]=str[i]-32;

  else if( str[i]>='A' && str[i]<='Z')

  str[i]=str[i]+32;

}

}   

void main()

{

  char str[100];

  read( str );

  toToggle( str );

  disp( str );

}  

Output :

Enter a string : Rama is a Good Boy

String after conversion= rAMA IS A gOOD bOY

 

WAP to read password and confirmation password. Print whether they are equal or not using function.

 

#include<stdio.h>

void read( char str[100])

{

  char ch;

  int i=0;

  do

{

  ch=getch();

  if( ch!=13 )

  printf("*");

  str[i]=ch;

  i++;

}

  while( ch!=13 );

  str[i]='\0';

}

void test( char pword[100],char cpword[100])

{

  int flag=1,i;

  for( i=0;pword[i]!='\0';i++)

{

  if( pword[i]!=cpword[i])

  flag=0;

}

  if( cpword[i]!='\0')

  flag=0;

  if( flag==1 )

  printf(“\nPassword matched with confirmation password”);

  else

  printf(“\nPassword does not matched with confirmation password”);

void main()

{

  char pword[100],cpword[100];

  printf(“Enter Password :”);

  read( pword);

  printf(“\nEnter a Confirmation Password :”);

  read( cpword);

  printf(“\nPassword=%s”,pword);

  printf(“\nConfirmation Password=%s”,cpword);

  test( pword, cpword);

}  

Output 1:

Enter Password : ****

Enter a Confirmation Password : ****

Password=rama

Confirmation Password=sita

Password does not matched with confirmation password

Output 2:

Enter Password : ****

Enter a Confirmation Password : ****

Password=rama

Confirmation Password=rama

Password matched with confirmation password

WAP without using main method and print welcome message.

#include<stdio.h>

#define india main

void india()

{

  printf(“Welcome”);

}  

 

WAP to find addition of 2 numbers without using any operator and using functions.

 

#include<stdio.h>

int sum(int a,int b)

{

  int r=printf(“%*c%*c”,a,' ',b,' ');

  return r;

}

void main()

{

  int a,b,r;

  printf(“Enter 2 numbers :”);

  scanf(“%d%d”,&a,&b);

  r=sum(a,b);

  printf(“Sum=%d”,r);

}  

Output:

Enter 2 numbers : 5 7

Sum=12

Explanation :

A=5

B=7

int r=printf("%*c%*c",a,' ',b,' ');

int r=printf(“%*C%*c”,5,’ ‘,7,’ ‘);

int r=printf(“%5c%7c”,’ ‘,’ ‘);

int r=print(“ “); // print 12 spaces

int r=12;

WAP to read a number. Print whether it is odd or even using loop and don’t use “%” symbol and using functions.

 

#include<stdio.h>

void test(int no)

{

  while( no>1 )

{

  no=no-2;

}

  if( no==1 )

  printf(“Odd number”);

  else

  printf(“even number”);

}

void main()

{

  int no;

  printf(“Enter a number :”);

  scanf(“%d”,&no);

  test(no);

}  

 

Output:

Enter a number: 46

even number

 

Write a program to read amount, print the denomination for the amount using functions.

 

#include<stdio.h>

void denomination( int amount )

{

  if( amount>=2000 )

{

  printf(“\n2000 x %d”,amount/2000);

  amount=amount%2000;

}

  if( amount>=500 )

{

  printf(“\n500 x %d”,amount/500);

  amount=amount%500;

}

  if( amount>=200 )

{

  printf(“\n200 x %d”,amount/200);

  amount=amount%200;

}

  if( amount>=100 )

{

  printf(“\n100 x %d”,amount/100);

  amount=amount/100;

}

  if( amount>=50 )

{

  printf(“\n50 x %d”,amount/50);

  amount=amount%50;

}

  if( amount>=20 )

{

  printf(“\n20 x %d”,amount/20);

  amount=amount%20;

}

  if( amount>=10 )

{

  printf(“\n10 x %d”,amount/10);

  amount=amount%10;

}

  if( amount>=5 )

{

  printf(“\n5 x %d”,amount/5);  

  amount=amount%5;

}

  if( amount>=2 )

{

  printf(“\n2 x %d”,amount/2);

  amount=amount%2;

}

  if( amount>=1 )

{

  printf(“\n1 x %d”,amount);

}

}

void main()

{

  int amount;

  printf(“Enter amount :”);

  scanf(“%d”,&amount);

  denomination( amount );

}

Output:

Enter amount: 5233

2000 x 2

500 x 2

200 x 1

20 x 1

10 x 1

2 x 1

1 x 1

 

WAP to print prime numbers between 1 to 100.

 

#include<stdio.h>

void isPrime( int no )

{

  int i,c=0;

  for( i=1;i<=no;i++ )

{

  if( no%i==0 )

  c++;

}

  if( c==2 )

  printf(“%5d”,no);

}

void main()

{

  int no;

  for( no=1;no<=100;no++ )

  isPrime( no );

}  

Output:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

 

WAP to print perfect numbers between 1 to 100.

 

#include<stdio.h>

void isPrime( int no )

{

  int i,c=0;

  for( i=1;i<=no;i++ )

{

  if( no%i==0 )

  c++;

}

  if( c==2 )

  printf(“%5d”,no);

}

void main()

{

  int no;

  for( no=1;no<=100;no++ )

  isPrime( no );

}  

Output:

6 28

 

WAP to print palindrome numbers from 1 to 100.

 

#include<stdio.h>

void isPalindrome( int no )

{

  int dup=no,rem,rev=0;

  while( no!=0 )

{

  rem=no%10;

  rev=rev*10+rem;

  no=no/10;

}

  if( rev==dup )

  printf(“%5d”,dup);

}

void main()

{

  int no;

  for( no=1;no<=100; no++ )

  isPalindrome( no );

}

Output:

1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99

 

WAP to read N elements into an array, reverse it using pointers.

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a,n,i;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int*)malloc( n*sizeof(int));

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,a+i);

  //&a[i]=a+i

  printf(“\nArray in reverse :”);

  for( i=n-1;i>=0;i-- )

  printf("%5d",*(a+i));

  // a[i]=*(a+i)

}  

Output:

Enter array size: 5

Enter 5 elements: 10 20 30 40 50

Array in reverse: 50 40 30 20 10

 

WAP to read N elements into an array, find sum of elements using pointers.

 

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a,n,i,sum=0;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int*)malloc( n*sizeof(int));

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,a+i);

  for( i=0;i<n;i++ )

  sum=sum+*(a+i);

  printf(“Sum =%d”,sum);

}

Output:

Enter array size: 5

Enter 5 elements:10 20 30 40 50

Sum=150

 

WAP to read N elements into an array, find the odd elements count, even elements count using pointers.

 

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a,n,i,oc=0,ec=0;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int *)malloc( n*sizeof(int));

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,a+i);

  for( i=0;i<n;i++ )

{

  if( *(a+i)%2==0 )

  ec++;

  else

  oc++;

}

  printf(“Odd numbers count=%d”,oc);

  printf(“\nEven numbers count=%d”,ec);

}   

Output:

Enter array size: 7

Enter 7 elements: 1 5 7 10 25 14 100

Odd numbers count=4

Even numbers count=3

WAP to read N single digit elements into an array, spell all the numbers using pointers.

 

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a,n,i;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int*)malloc( n*sizeof(int));

  printf(“Enter %d single digit elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,a+i);

  for( i=0;i<n;i++ )

{

  switch( *(a+i) )

{

  case 0: printf(“\nZero”);break;

  case 1: printf(“\nOne”); break;

  case 2: printf(“\nTwo”); break;

  case 3: printf(“\nThree”); break;

  case 4: printf(“\nFour”); break;

  case 5: printf(“\nFive”); break;

  case 6: printf(“\nSix”); break;

  case 7: printf(“\nSeven”); break;

  case 8: printf(“\nEight”); break;

  case 9: printf(“\nNine”); break;

}

}

}   

Output:

Enter array size: 5

Enter 5 single digit elements: 1 5 3 7 6

One

Five

Three

Seven

Six

 

 

WAP to read N elements into an array, find the squares using pointers.

 

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a,n,i;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int*)malloc( n*sizeof(int));

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,a+i);

  for( i=0;i<n;i++ )

  *(a+i)=*(a+i) * *(a+i);

  printf(“Squares for given array are :”);  

  for( i=0;i<n;i++ )

  printf(“%5d”,*(a+i));

}   

Output:

Enter array size:

5

Enter 5 elements: 1 3 2 4 5

Squares for given array are: 1 9 4 16 25

WAP to read N elements into an array, find the max and min element of array using pointers.

 

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a,n,i,max,min;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int*)malloc( n*sizeof(int));

  printf(“Enter %d elements :”,n);

  for(i=0;i<n;i++ )

  scanf(“%d”,a+i);

  max=min=*(a+0);

  for( i=0;i<n;i++)

{

  if( *(a+i)>max )

  max=*(a+i);

  if( *(a+i)<min )

  min=*(a+i);

}

  printf(“Maximum=%d”,max);

  printf(“\nMinimum=%d”,min);

}   

 

 

WAP to read N elements into an array, count multiple of 10 elements in an array using pointers.

 

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a,n,i,c=0;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int*)malloc( n*sizeof(int));

  printf(“Enter %d elements :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,a+i);

  for( i=0;i<n;i++ )

{

  if( *(a+i)%10==0 )

  c++;

}

  printf(“multiples of 10 elements count=%d”,c);

}  

Output:

Enter array size: 6

Enter 6 elements: 10 2 5 40 25 7

multiples of 10 elements count=2

 

WAP to read N elements into 2 arrays, find the addition array using pointers.

 

#include<stdio.h>

#include<malloc.h>

void main()

{

  int *a, *b, *c, n,i;

  printf(“Enter array size :”);

  scanf(“%d”,&n);

  a=(int*)malloc(n*sizeof(int));

  b=(int*)malloc(n*sizeof(int));

  c=(int*)malloc(n*sizeof(int));

  printf(“\nEnter %d elements into A : “,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,a+i);

  printf(“\nEnter %d elements into B :”,n);

  for( i=0;i<n;i++ )

  scanf(“%d”,b+i);

  for( i=0;i<n;i++ )

  *(c+i)=*(a+i) + *(b+i);

  printf(“Resultant array is :”);

  for( i=0;i<n;i++ )

  printf(“%5d”,*(c+i));

}  

Output:

Enter array size: 5

Enter 5 elements into A: 10 20 30 40 50

Enter 5 elements into B: 1 2 3 4 5

Resultant array is: 11 22 33 44 55

 

 

 

 

WAP to find nearest palindrome number for a given number using pointer.

 

#include<stdio.h>

int palindrome(int no)

{

  int dup=no,rem,rev=0;

  while( no!=0 )

{

  rem=no%10;

  rev=rev*10+rem;

  no=no/10;

}

  if( rev==dup )

  return 1;

  else

  return 0;

}

void findNearestPalindrome( int no )

{

  int i=0;

  while( 1 )

{

  if( palindrome( no+i)==1 )

{

  printf(“Nearest palindrome number is %d”,no+i);

  return;  

}

  if( palindrome( no-i)==1 )

{

  printf(“Nearest palindrome umber is %d”,no-i);

  return;

}

  i++;

}

}

void main()

{

  int no;

  printf(“Enter a number :”);

  scanf(“%d”,&no);

  findNearestPalindrome( no );

}   

Output:

Enter a number: 102

Nearest palindrome number is 101