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
|
Comments
Post a Comment