USER DEFIEND FUNCTION C++ Codes

                      LEARN TO CODE WITH CODESCHOOLU,BLOGSPOT.COM
PROGRAMS USING SIMPLE FUNCTION
1.
//PROGRAM TO FIND SIMPLE INTEREST USING UDF
#include <iostream.h>
#include <conio.h>
float CAL(float,float,float);
void main()
{       clrscr();
            float p,r,t,SI;
            cout<<"Enter the principal amount : ";
            cin>>p;
            cout<<"Enter the rate of interest : ";
            cin>>r;
            cout<<"Enter the time of deposits : ";
            cin>>t;
            SI = CAL(p,r,t);
            cout<<"The simple interest is : "<<SI;
            getch();
}
float CAL(float p, float r, float t)
{           float a;
            a = (p * r * t)/100;
            return a;
}
2.
//PROGRAM TO FIND AREA OF CIRCLE USING UDF
#include <iostream.h>
#include <conio.h>
float area(float);
void main()
{       clrscr();
            float radius,ar;
            cout<<"Enter the radius of circle : ";
            cin>>radius;
            ar = area(radius);
            cout<<"The area of the circle is : "<<ar;
            getch();
}
float area(float r)
{           float a;
            a = 3.14 * r * r;
            return a;
}
3.
//Addition of two numbers using UDF
#include<iostream.h>
#include<conio.h>
int sum(int, int);    // Function Prototype or Function Declaration
void main()
{
 clrscr();
 int a,b,c;
 cout<<"enter two numbers";
 cin>>a>>b;
 c=sum(a,b);      // Function Call
 cout<<"the sum of two numbers="<<c;
 getch();
 }
int sum(int x, int y)    // Function Definition
{
 return(x+y);
}
4.
//PROGRAM TO FIND AREA OF A TRIANGLE USING HERON'S FORMULA USING UDF
#include <iostream.h>
#include <conio.h>
#include <math.h>
float area(int,int,int);
void main()
{       clrscr();
            float n1, n2, n3;
            cout<<"Enter the first side : ";
            cin>>n1;
            cout<<"Enter the second side : ";
            cin>>n2;
            cout<<"Enter the third side : ";
            cin>>n3;
            cout<<"The area is : "<<area(n1,n2,n3);
            getch();
}
float area(int n1, int n2, int n3)
{
            float s = (n1 + n2 + n3)/2.0;
            float d = s * (s - n1) * (s - n2) * (s - n3);
            return sqrt(d);
}

*PROGRAMS FOR PRACTICE
5. PERIMETER OF RECTANGLE
6. AVERAGE OF 5 NOS
7. Input basic sal and calculate net sal = BASIC SAL + 20% OF BASIC SAL – 5% OF BASIC SAL

PROGRAMS USING MULTIPLE UDFs
8.
//Convert the length entered in meters to centimeter, millimeter and kilometer using UDFs
#include<iostream.h>
#include<conio.h>
float cm(float);     // Function Prototype or Function Declaration
float mm(float);    // Function Prototype or Function Declaration
float km(float);     // Function Prototype or Function Declaration
void main()
{
 clrscr();
 float m,a,b,c;
 cout<<"enter the length in meters";
 cin>>m;
 a=cm(m);       // Function Call
 b=mm(m);     // Function Call
 c=km(m);       // Function Call
 cout<<"the length in centimeter="<<a;
 cout<<"\nthe length in millimeter="<<b;
 cout<<"\nthe length in kilometer="<<c;
 getch();
 }
float cm(float x)      // Function Definition
{
 return(x*100);
}
float mm(float x)
{
 return(x*1000);
}
float km(float x)
{
 return(x/1000.0);
}

PROGRAMS INVOLVING SIMPLE IF
9.
//PROGRAM TO FIND SMALLEST OF 2 NUMBERS USING UDF
#include <iostream.h>
#include <conio.h>
float small(int,int);
void main()
{       clrscr();
            float n1, n2;
            cout<<"Enter the first number : ";
            cin>>n1;
            cout<<"Enter the second number : ";
            cin>>n2;
            cout<<"The smaller number is : "<<small(n1,n2);
            getch();
}
float small(int a, int b)
{
            if (a < b)
                        return a;
            else
                        return b;
}
10.
//PROGRAM TO CONVERT CASE OF AN ALPHABET USING UDF
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
char convert(char);
void main()
{       clrscr();
            char c1, c2;
            cout<<"Enter an alphabet : ";
            cin.get(c1);
            c2 = convert(c1);
            cout<<"The converted alphabet is : "<<c2;
            getch();
}
char convert(char x)
{       char y;
            if (isupper(x))
                        y = tolower(x);
            else
                        y = toupper(x);
            return y;
}
11.
//PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS EVEN OR ODD USING UDF
#include <iostream.h>
#include <conio.h>
void check(int)'
void main()
{       clrscr();
            int x;
            cout<<"Enter the number : ";
            cin>>x;
            check(x);
            getch();
}
void check(int a)
{           if (a % 2 == 0)
                        cout<<"The number "<<a<<" is even.";
            else
                        cout<<"The number "<<a<<" is odd.";
}
12.
//PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS +VE OR -VE USING UDF
#include <iostream.h>
#include <conio.h>
void check(int);
void main()
{       clrscr();
      int x;
      cout<<"Enter the number : ";
      cin>>x;
      check(x);
      getch();
}
void check(int a)
{    if (a > 0)
                  cout<<"The number "<<a<<" is positive.";
      else
                  cout<<"The number "<<a<<" is negative.";
}

13. Program to check whether a person is eligible to vote or not
14. Program to input a char and check whether it is vowel or not
15. Program to check whether a year is leap year or not
16.
//PROGRAM TO FIND LARGEST OF 3 NUMBERS USING UDF
#include <iostream.h>
#include <conio.h>
float large(int,int,int);
void main()
{       clrscr();
            float n1, n2, n3, lar;
            cout<<"Enter the first number : ";
            cin>>n1;
            cout<<"Enter the second number : ";
            cin>>n2;
            cout<<"Enter the third number : ";
            cin>>n3;
            lar = large(n1,n2,n3);
            cout<<"The largest number is : "<<lar;
            getch();
}
float large(int a, int b, int c)
{           float m;
            if ((a > b) && (a > c))
                        m = a;
            else
                        if ((b > a)&&(b > c))
                                    m = b;
                        else
                                    m = c;
            return m;
}
17.
//PROGRAM TO INPUT A CHAR AND CHECK WHETEHR IT IS A LOWERCASE< UPPERCASE< DIGIT OR SPECIAL CHARACTER USING UDF
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void identify(char);
void main()
{
  clrscr();
  char ch;
  cout<<"Enter a character ";
  cin>>ch;
  identify(ch);
  getch();
}

void identify (char x)
{
  if(isupper(x))
    cout<<"its an uppercase character";
 else
  if(islower(x))
    cout<<"its an lowercase character";
 else
  if(isdigit(x))
    cout<<"its a digit";
 else
       cout<<"its a special character";
}
18.
/* PROGRAM TO FIND the commission for the salesman according to sale done by him.
       Sales                                        Commission
 30,000 onwards                                 15%
 22,001 to 30,000                                10%
 12,001 to 22,000                                7%
 5,001 to 12,000                                  3%*/

#include<iostream.h>
#include<conio.h>
float calculate(float);
void main()
{
  clrscr();
  float s, c;
  cout<<"Enter sales ";
  cin>>s;
  cout<<"Commission=  "<<calculate(s);
  getch();
}

float calculate(float sales)
{
  float comm;
  if(sales>30000)
    comm=0.15*sales;
 else
    if(sales<=30000 && sales>=22001)
    comm=0.10*sales;
 else
    if(sales<=22000 && sales>=12001)
    comm=0.07*sales;
 else
 if(sales<=12000 && sales>=5001)
    comm=0.03*sales;
return(comm);
 }

19.  Write a program to calculate grades after calculating percentage with criteria:-
Per          grade
>=90        A
89 - 75     B
74 – 60    C
<60          D
20.  Write a program to calculate discount based on Sale Price
SP                            Discount
<200000                   10%
500000-200000        15%
>50000                      20%                                                                                                       


Selection statements (if/ switch) questions using functions
21.
// Input two numbers and any one of the arithmetic operators. Display the result according to the operation performed.
#include<iostream.h>
#include<conio.h>
float sum(float, float);
float diff(float, float);
float mul(float, float);
float div(float, float);
void main()
{
  clrscr();
  int a,b;
  char ch;
  cout<<"Enter two numbers";
  cin>>a>>b;
  cout<<"\nEnter Operator(+, -, *, /)";
  cin>>ch;
  switch(ch)
  {
    case '+': cout<<"Sum of two numbers is "<<sum(a,b);
           break;
    case '-': cout<<"Difference of two numbers is "<<diff(a,b);
           break;
    case '*': cout<<"Product of two numbers is "<<mul(a,b);
           break;
    case '/': cout<<"Quotient of two numbers is "<<div(a,b);
           break;
   }
   getch();
}
float sum(float x, float y)
{
  return(x+y);
}
float diff(float x, float y)
{
  return(x-y);
}
float mul(float x, float y)
{
  return(x*y);
}
float div(float x, float y)
{
  return(x/y);
}
22.
// Input the numbers from 1 to 7 and display the week days starting from Monday.
#include<iostream.h>
#include<conio.h>
void wd (int);
void main()
{
 clrscr();
  int d;
 cout << "Enter a number (1-7): ";
 cin >> d;
 wd(d);
 getch();
}
void wd (int day)
{
 switch(day)
 {
  case 1: cout<<"It's Monday";
                break;
  case 2: cout<<"It's Tuesday";
                break;
  case 3: cout<<"It's Wednesday";
                break;
  case 4: cout<<"It's Thursday";
                break;
  case 5: cout<<"It's Friday";
                break;
  case 6: cout<<"It's Saturday";
                break;
  case 7: cout<<"It's Sunday";
                break;
  default: cout<<"Invalid Input";
  }
}
23.
Write a program to input a 2 digit number and display it in words using UDF.
24.
Write a program to input a month number and display the corresponding month name using UDF.
25.
// Find the roots of the quadratic equations.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void roots (float,float,float);
void main()
{
 clrscr();
 int l,m,n;
 cout<<"enter the cofficient of x^2, x and a constant";
 cin>>l>>m>>n;
 roots(l,m,n);
 getch();
 }
void roots (float a,float b, float c)
{
 float d, r1, r2;
 d=b*b-4*a*c;
 if(d>0)
 {
  r1= (-b+sqrt(d))/(2.0*a);
  r2= (-b -sqrt(d))/(2.0*a);
  cout<<"Roots are real and distinct "<<r1<<"\t"<<r2;
 }
 else
 if(d==0)
{
  r1=-b/(2.0*a);
  cout<<"Roots are real and equal"<<r1;
 }
if(d<0)
    cout<<"Roots are imaginary";
 }

PROGRAMS USING FOR LOOP USING UDF
26.
//PROGRAM TO FIND SUM OF FIRST N INTEGERS USING UDF
#include <iostream.h>
#include <conio.h>
int sum(int);
void main()
{       clrscr();
            int n;
            cout<<"Enter the limit : ";
            cin>>n;
            cout<<"The sum is : "<<sum(n);
            getch();
}
int sum(int x)
{
            int s = 0;
            for (int i = 1;i <= x;i++)
                        s = s + i;
            return s;
}
27.
//PROGRAM TO FIND PRINT  FIRST N ODD NUMBERS USING UDF
#include <iostream.h>
#include <conio.h>
void print(int);
void main()
{       clrscr();
            int n;
            cout<<"Enter the limit : ";
            cin>>n;
            print(n);
            getch();
}
void print(int x)
{
            for (int i = 1;i <= x;i++)
                        cout<<i<<"   ";
            cout<<endl;
}
28.
//PROGRAM TO PRINT N TdERMS OF FIBONACCI SERIES USING UDF
#include <iostream.h>
#include <conio.h>
void fibo(int);
void main()
{       clrscr();
            int n;
            cout<<"Enter the limit : ";
            cin>>n;
            fibo(n);
            getch();
}
void fibo(int x)
{
            int a, b, c;
            a = 0;
            b = 1;
            cout<<"The series is :-\n";
            if (n == 1)
                        cout<<a<<"\t";
            else
                        if (n == 2)
                                    cout<<a<<"\t"<<b;
                        else     
                        {
                                    cout<<a<<"\t"<<b<<"\t";
                                    for (int i = 3;i <= x;i++)
                                    {           c = a + b;
                                                cout<<c<<"\t";
                                                a = b;
                                                b = c;
                                    }
                        }
            cout<<endl;
}
29.
// To check if the given number is prime or not using UDF
#include<iostream.h >
#include<conio.h>
void prime(int);
void main()
{
            clrscr();
            int num;
            cout<<"Enter a number";
            cin>>num;
            prime(num);
            getch();
}
void prime(int num)
{
            int x=1;
            for(int i=2;i<num;i++)
            if (num % i == 0)
                        x = 0;
            if(x == 1)
                        cout<<"The number is prime";
            else
                         cout<<"The number is not prime";
}
30.
//series X - X2/2! + X3/3! - X4/4! ..... Xn/n!
#include<iostream.h >
#include<conio.h>
 #include<math.h>
float series(float, int);
void main()
{
            clrscr();
            float x,n,s;
            cout<<"Enter a number and the limit";
            cin>>x>>n;
            s = series(x,n);
            cout<<"The sum of the series is"<<s;
            getch();
}
float series (float x, int n)
{
            float num, den=-1, sum=0;
            for(int i=1;i<=n;i++)
            {
                        num=pow(x,i);
                        den=den*i* -1;
                        sum+=num/den;
            }
            return(sum);
}

31. Sum of series : x,x2, x3………xn
32. Table of a number
33. Multiples of a number till a number
(34-37)At least 4 programs of series
38.
//PROGRAM TO REVERSE A NUMBER USING UDF
#include <iostream.h>
#include <conio.h>
int reverse(int);
void main()
{       clrscr();
           int n;
           cout<<"Enter the number : ";
           cin>>n;
           int y = reverse(n);
           cout<<"The reversed number is : "<<y;
           getch();
}
int reverse(int n)
{
           int r = 0, digit;
           while (n > 0)
           {
                       digit = n % 10;
                       n = n / 10;
                       r = r * 10 + digit;

           }
           return r;
}
39.
//PROGRAM TO COUNT EVEN NOS TILL USER ENTERS -1 USING UDF
#include <iostream.h>
#include <conio.h>
int count();
void main()
{       clrscr();
           int y = count();
           cout<<"The number of even nos enterfed are  : "<<y;
           getch();
}
int count()
{
           int r = 0, num;
           cout<<"Enter the list of numbers till -1\n";
           do
           {
                       cin>>num;
                       if (num % 2 == 0)
                                   r++;
           }
           while (num != -1);
           return r;
}

40. Program to find sum of digits of a positive integer.
41. Program to find sum of positive interegs till user enters -1
42.
//PROGRAM TO RUN A MENU USING UDF
#include <iostream.h>
#include <conio.h>
float circle(float);
float rect(float,float);
float square(float);
void main()
{       clrscr();
            float a, b, area;
            int choice;
            do
            {
                        cout<<"\n1. Area of Circle";
                        cout<<"\n2. Area of REctangle";
                        cout<<"\n3. Area of Square";
                        cout<<"\n4. Exit";
                        cout<<"\nEnter ur choice : ";
                        cin>>choice;
                        switch(choice)
                        {
                                    case 1 :
                                                cout<<"Enter the radius of circle:";
                                                cin>>a;
                                                area = circle(a);
                                                cout<<"The area of circle is : "<<area;
                                                break;
                                    case 2 :
                                                cout<<"Enter the length of rect:";
                                                cin>>a;
                                                cout<<"Enter the width of rect:";
                                                cin>>b;
                                                area = rect(a,b);
                                                cout<<"The area of rect is : "<<area;
                                                break;
                                    case 3 :
                                                cout<<"Enter the side of square:";
                                                cin>>a;
                                                area = square(a);
                                                cout<<"The area of square is : "<<area;
                                                break;
                                    case 4 :cout<<"ENd of program\n";
                                                break;
                                    default : cout<<"Wrong choice\n";
                        }
                        getch();
            }while (choice != 4);
            getch();
}
float circle(float r)
{          return (3.14 * r *r);     }
float rect(float len, float width)
{          return (len * width);   }
float square(float side)
{          return (side * side);    }
43.
//PROGRAM TO RUN A MENU USING UDF
#include <iostream.h>
#include <conio.h>
void circle();
void rect();
void square();
void main()
{       clrscr();
            int choice;
            do
            {
                        cout<<"\n1. Area of Circle";
                        cout<<"\n2. Area of REctangle";
                        cout<<"\n3. Area of Square";
                        cout<<"\n4. Exit";
                        cout<<"\nEnter ur choice : ";
                        cin>>choice;
                        switch(choice)
                        {
                                    case 1 :
                                                circle();
                                                break;
                                    case 2 :
                                                rect();
                                                break;
                                    case 3 :
                                                square();
                                                break;
                                    case 4 :cout<<"ENd of program\n";
                                                break;
                                    default : cout<<"Wrong choice\n";
                        }
                        getch();
            }while (choice != 4);
            getch();
}
void circle()
{
            float r, area;
            cout<<"Enter the radius of circle:";
            cin>>r;
            area = 3.14 * r * r;
            cout<<"The area of circle is : "<<area;
}
void rect()
{
            float len, width, area;
            cout<<"Enter the length of rect:";
            cin>>len;
            cout<<"Enter the width of rect:";
            cin>>width;
            area = len * width;
            cout<<"The area of rect is : "<<area;
}
void square()
{
            cout<<"Enter the side of square:";
            float a, area;
            cin>>a;
            area = a * a;
            cout<<"The area of square is : "<<area;
}
44.
Write a program to print n lines of following series using UDF
&
& &
& & &
& & & &
45.
Write a program to print n lines of following series using UDF
                   1
                1 2 1
             1 2 3 2 1
          1 2 3 4 3 2 1


46.
//PROGRAM TO SOLVE SERIES USING UDF
//
#include <iostream.h>
#include <conio.h>
#include <math.h>        
long int fact(int);
float series(int,int);
void main()
{
               long int x, n;
               clrscr();
               cout<<"Enter the value of x and n : ";
               cin>>x>>n;
               cout<<"The result of the series is : ";
               cout<<series(x,n);
               getch();
}
float series(int x, int n)
{
               float num, den, term, sum = 0;
               for (int i = 1;i <= n;i++)
               {              num = pow(x,i);
                              den = fact(i);
                              term = num / den;
                              sum = sum + term;
               }
               return sum;
}
long int fact(int n)
{
               long int f = 1;
               for (int i = 1; i <= n;i++)
                              f = f * i;
               return f;
}
47.
//PROGRAM TO CALCULATE % AND GRADE OF A STUDENT
//PROGRAM TO FIND % AND GRADE USING UDF
#include <iostream.h>
#include <conio.h>
float percent(float,float,float,float,float);
char grade(float);
void main()
{       clrscr();
               floadt a,b,c,d,e,per;
               char g;
               cout<<"Enter the marks in first subject : ";
               cin>>a;
               cout<<"Enter the marks in second subject : ";
               cin>>b;
               cout<<"Enter the marks in third subject : ";
               cin>>c;
               cout<<"Enter the marks in fourth subject : ";
               cin>>d;
               cout<<"Enter the marks in fifth subject : ";
               cin>>e;
               per = percent(a,b,c,d,e);
               g = grade(per);
               cout<<"The grade is : "<<g;
               getch();
}
float percent(float p, float q, float r, float s, float t)
{
               float pt = (p + q + r + s + t)/5.0;
               return pt;
}
char grade(float p)
{              if (p >= 90)
                              return 'A';
               else if (p >= 75)
                              return 'B';
               else if (p >= 60)
                              return 'C';
               else if (p >= 45)
                              return 'D';
               else
                              return 'E';
}
48.
WRITE A PROGRAM TO IMPLEMENT THE GIVEN MENU USING UDF
1.      CONVERSION OF TEMPERATURE FROM KELVIN TO CELSIUS
2.      CONVERSION OF TEMOERATURE FROM FERNHEIT TO KELVIN
3.      CONVERSION OF TEMPERATURE FROM CELSIUS TO FERNHEIT
49.
//PROGRAM TO SWAP VALUES OF 2 VARIABLES USING UDF
#include <iostream.h>
#include <conio.h>
void exchange(int&, int&);
void main()
{       clrscr();
               int a,b;
               cout<<"Enter the first number : ";
               cin>>a;
               cout<<"Enter the second number : ";
               cin>>b;
               cout<<"The original values are :-\n";
               cout<<"a = "<<a<<"\tb = "<<b<<endl;
               exchange(a,b);
               cout<<"The exchanged values are :-\n";
               cout<<"a = "<<a<<"\tb = "<<b<<endl;
               getch();
}
void exchange(int &x, int &y)
{              int z;
               z = x;
               x = y;
               y = z;
}
50.
//PROGRAM TO CALCULATE 3RD AND 4TH POWER OF A NUMBER USING ONE UDF
#include <iostream.h>
#include <conio.h>
#include <math.h>
void cal(int,int&,int&);
void main()
{       clrscr();
               int a,b,c;
               cout<<"Enter the number : ";
               cin>>a;
               cal(a,b,c);
               cout<<"The original number is : "<<a;
               cout<<"\na^3 = "<<b<<"\ta ^ 4 = "<<c<<endl;
               getch();
}
void cal(int p, int &x, int &y)
{
               x = pow(p,3);
               y = pow(p,4);
}
51.
52.
Write a program to calculate area of a rectangle using separate UDFs for IPO.
Write a program to find roots of a quaratic equation using multiple UDFs.
53.
//PROGRAM TO COUNT AND SUM ODD NOS TILL -1 USING ONE UDF
#include <iostream.h>
#include <conio.h>
void cal(int&,int&);
void main()
{       clrscr();
               int a=0,b=0;
               cal(a,b);
               cout<<"The count is "<<a<<endl;
               cout<<"The sum is : "<<b;
               getch();
}
void cal(int &x, int &y)
{
               int num = 0;
               do
               {
                              cout<<"Enter the number ";
                              cin>>num;
                              if (num % 2 == 1)
                              {
                                             x++;
                                             y = y + num;
                              }
               }while (num != -1);
}
54.
//PROGRAM TO FIND SUM OF ALL ELEMENTS OF 1-D ARRAY USINF UDF
# include <iostream.h>
# include <conio.h>
int sum(int [],int);
void main()
{
               int a[10], n, s;
               clrscr();
               cout<<"Enter the number of elements of an array : ";
               cin>>n;
               cout<<"Enter the elments of the array :-\n";
               for (int i = 0; i < n;i++)
                              cin>>a[i];
               s = sum(a,n);
               cout<<"The sum of elements is : "<<s;
               getch();
}
int sum (int A[], int m)
{
               int sum1 = 0;
               for (int i = 0; i < m;i++)
                              sum1 = sum1 + A[i];
               return sum1;
}
55.
//PROGRAM TO COUNT ALL PRIME NOS. IN A 1-D ARRAY USING UDFs
# include <iostream.h>
# include <conio.h>
void large(int [],int,int&,int&);
void main()
{
               int A[10], n, lar=0, pos=0;
               clrscr();
               cout<<"Enter the number of elements of an array : ";
               cin>>n;
               cout<<"Enter the elments of the array :-\n";
               for (int i = 0; i < n;i++)
                              cin>>A[i];
               large(A,n,lar,pos);
               cout<<"The largest number is : "<<lar;
               cout<<"\n The position is : "<<pos;
               getch();
}
void large(int B[], int m, int &max, int &p)
{
               for (int i = 0; i < m;i++)
                              if (B[i] > max)
                              {
                                             max = B[i];
                                             p = i;
                              }
}
int prime(int x)
{
               if (x == 1 || x == 2)
                              return 1;
               for (int i = 2; i <= x/2; i++)
                              if (x % i == 0)
                                             return 0;
               return 1;
}
56.
//PROGRAM TO ADD TWO 1-D ARRAYS USING UDFs
# include <iostream.h>
# include <conio.h>
void input(int [], int&);
void add(int[], int[], int[], int);
void output(int[], int);
void main()
{
               int A[10], B[10], C[10], n, m;
               clrscr();
               cout<<"Enter the first array :-\n";
               input(A,n);
               cout<<"Enterthe second array :-\n";
               input(B,m);
               if (m == n)
               {
                      d      add(A,B,C,n);
                              clrscr();
                              cout<<"\nFirst array is :-\n";
                              output(A,n);
                              cout<<"\nThe second array is :-\n";
                              output(B,n);
                              cout<<"The array after additon is :-\n";
                              output(C,n);
               }
               else
                              cout<<"The arrays are not compatible for addition.";
               getch();
}
void input(int X[], int &x)
{
               cout<<"Enter the number of elements of the array : ";
               cin>>x;
               cout<<"Enter the elments of the array :-\n";
               for (int i = 0; i < x;i++)
                              cin>>X[i];
}
void add(int X[], int Y[], int Z[], int m)
{
               for (int i = 0; i < m;i++)
                              Z[i] = X[i] + Y[i];
}
void output(int Z[], int x)
{
               for (int i = 0;i < x;i++)
                              cout<<Z[i]<<"\t";
               cout<<endl;
}


63.
//PROGRAM TO COUNT ALPHABETS, DIGITS AND SPECIAL CHARS IN STRING USING A UDF
# include <iostream.h>
# include <conio.h>
# include <ctype.h>
# include <stdio.h>
void count(char[], int&, int&, int&);
void main()
{
               char str[80];
               int a=0, b=0, c=0;
               clrscr();
               cout<<"Enter the string : ";
               gets(str);
               count(str,a,b,c);
               cout<<"Number of alphabets are : "<<a;
               cout<<"\nNumber of digits are : "<<b;
               cout<<"\nNumber of special chararcters are : "<<c;
               getch();
}
void count (char STR[], int &x, int &y, int &z)
{
               for (int i = 0;STR[i] != '\0';i++)
                              if (isalpha(STR[i]))
                                             x++;
                              else
                                             if (isdigit(STR[i]))
                                                            y++;
                                             else
                                                            z++;
}
64.
//PROGRAM TO CHECK WHETHER A STRING IS PALINDROME OR NOT USING A UDF
# include <iostream.h>
# include <conio.h>
# include <string.h>
# include <stdio.h>
int palin(char[]);
void main()
{
               char str[80];
               clrscr();
               cout<<"Enter the string : ";
               gets(str);
               int flag = palin(str);
               if (flag == 1)
                              cout<<"String "<<str<<" is a palindrome.";
               else
                              cout<<"String "<<str<<" is not a palindrome.";
               getch();
}
int palin(char STR[])
{
               int n = strlen(STR);
               for (int i = 0,j = n-1;i < n/2;i++,j--)
                              if (STR[i] != STR[j])
                                             return 0;
               return 1;
}
65.
//PROGRAM TO CONCATENdATE TWO STRINGS USING A UDF
# include <iostream.h>
# include <conio.h>
# include <string.h>
# include <stdio.h>
void concate(char[], char[]);
void main()
{
               char str1[80], str2[80];
               clrscr();
               cout<<"Enter the first string : ";
               gets(str1);
               cout<<"Enterthe second string : ";
               gets(str2);
               concate(str1, str2);
               cout<<"The concatenated string is "<<str1;
               getch();
}
void concate(char p[], char q[])
{
               int n = strlen(p);
               int m = strlen(q);
               for (int i = 0; i <= m;i++)
                              p[i+n] = q[i];
}


70.
//PROGRAM TO FIND TRANSPOSE OF A SQUARE MATRIX USING UDF
# include <iostream.h>
# include <conio.h>
void out(int[][10],int);
void trans(int [][10],int[][10],int);
void main()
{
               int A[10][10], B[10][10], n, i, j;
               clrscr();
               cout<<"Enter the order of matrix : ";
               cin>>n;
               cout<<"Enter the matrix :-\n";
               for(i = 0;i < n;i++)
                              for (j = 0;j < n;j++)
                                             cin>>A[i][j];
               trans(A,B,n);
               cout<<"The original matrix is :-\n";
               out(A,n);
               cout<<"The transposed matris is :-\n";
               out(B,n);
               getch();
}
void trans(int X[][10], int Y[][10], int n)
{
       int i, j;
       for (i = 0;i < n;i++)
                              for(j = 0; j < n;j++)
                                             Y[i][j] = X[j][i];
}
void out(int X[][10], int n)
{
               int i, j;
               for (i = 0;i < n;i++)
               {
                              for (j = 0;j < n;j++)
                                             cout<<X[i][j]<<"\t";
                              cout<<endl;
               }
}
71.
//PROGRAM TO FIND TRANSPOSE OF A SQUARE MATRIX USING UDF
# include <iostream.h>
# include <conio.h>
void out(int[][10],int);
void trans(int [][10],int);
void main()
{
               int A[10][10], n, i, j;
               clrscr();
               cout<<"Enter the order of matrix : ";
               cin>>n;
               cout<<"Enter the matrix :-\n";
               for(i = 0;i < n;i++)
                              for (j = 0;j < n;j++)
                                             cin>>A[i][j];
               cout<<"The original matrix is :-\n";
               out(A,n);
               trans(A,n);
               cout<<"The transposed matris is :-\n";
               out(A,n);
               getch();
}
void trans(int X[][10], int n)
{
       int i, j, temp;
       for (i = 0;i < n;i++)
                              for(j = 0; j < n;j++)
                                             if (i < j)
                                             {
                                                            temp = X[j][i];
                                                            X[j][i] = X[i][j];
                                                            X[i][j] = temp;
                                             }
}
void out(int X[][10], int n)
{
               int i, j;
               for (i = 0;i < n;i++)
               {
                              for (j = 0;j < n;j++)
                                             cout<<X[i][j]<<"\t";
                              cout<<endl;
               }
}
72.
//PROGRAM TO FIND ROW SUM OF A MATRIX USING UDF
# include <iostream.h>
# include <conio.h>
void out(int[][10],int,int);
void rowsum(int[][10],int[],int,int);
void main()
{
               int A[10][10], B[10], m, n, i, j;
               clrscr();
               cout<<"Enter the order of matrix : ";
               cin>>m>>n;
               cout<<"Enter the matrix :-\n";
               for(i = 0;i < m;i++)
                              for (j = 0;j < n;j++)
                                             cin>>A[i][j];
               cout<<"The original matrix is :-\n";
               out(A,m,n);
               rowsum(A,B,m,n);
               cout<<"The row sum of the matris is :-\n";
               for (i = 0;i < m;i++)
                              cout<<B[i]<<endl;
               getch();
}
void rowsum(int X[][10], int R[], int m, int n)
{
       int i, j;
       for (i = 0;i < m;i++)
       {
                              R[i] = 0;
                              for(j = 0; j < n;j++)
                                             R[i] = R[i] + X[i][j];
       }
}
void out(int X[][10], int m, int n)
{
               int i, j;
               for (i = 0;i < m;i++)
               {
                              for (j = 0;j < n;j++)
                                             cout<<X[i][j]<<"\t";
                              cout<<endl;
               }
}
73.
//PROGRAM TO ADD TWO 2-D ARRAYS USING UDFs
# include <iostream.h>
# include <conio.h>
void input(int [][10], int&, int&);
void add(int[][10], int[][10], int[][10], int, int);
void output(int[][10], int, int);
void main()
{
               int A[10][10], B[10][10], C[10][10], p, q, n, m;
               clrscr();
               cout<<"Enter the first matrix :-\n";
               input(A,m,n);
               cout<<"Enter the second matrix :-\n";
               input(B,p,q);
               if (m == p && n == q)
               {
                              add(A,B,C,m,n);
                              clrscr();
                              cout<<"\nFirst matrix is :-\n";
                              output(A,m,n);
                              cout<<"\nThe second matrix is :-\n";
                              output(B,m,n);
                              cout<<"The matrix after additon is :-\n";
                              output(C,m,n);
               }
               else
                              cout<<"The matrices are not compatible for addition.";
               getch();
}
void input(int X[][10], int &x, int &y)
{       int j;
               cout<<"Enter the order of the matrix : ";
               cin>>x>>y;
               cout<<"Enter the elments of the matrix :-\n";
               for (int i = 0; i < x;i++)
                              for (j = 0;j < y;j++)
                                             cin>>X[i][j];
}
void add(int X[][10], int Y[][10], int Z[][10], int m, int n)
{       int j;
               for (int i = 0; i < m;i++)
                              for (j = 0;j < n;j++)
                                             Z[i][j] = X[i][j] + Y[i][j];
}
void output(int Z[][10], int x, int y)
{       int j;
               for (int i = 0;i < x;i++)
               {
                              for (j = 0;j < y;j++)
                                             cout<<Z[i][j]<<"\t";
                              cout<<endl;
               }
}




Comments