C++ Functions
-------------
1- Area of circle without return
void main()
{
float radius;
void area(float);
cout << "Enter the radius of circle: ";
cin >> radius;
area(radius);
}
void area(float r)
{
float ar = 3.14 * r * r;
cout << "Area of circle: " << ar << endl;
}
2- Area of circle with return
void main()
{
float radius,ar=0;
float area(float);
cout << "Enter the radius of circle: ";
cin >> radius;
ar=area(radius);
cout << "Area of circle: " << ar << endl;
}
float area(float r)
{
float ar = 3.14 * r * r;
return(ar);
}
3- Divisibility by 11
void main()
{
int a;
void div(int);
cout << "Enter the number: ";
cin >> a;
div(a);
}
void div(int a)
{
if (a % 11 == 0)
{
cout << "The given number is divisible by 11" << endl;
}
else
{
cout << "The given number is not divisible by 11" <<endl;
}
}
4- Smaller of two numbers
void main()
{
int a, b;
void small(int,int);
cout<<"Enter two number : ";
cin>>a>>b;
small(a,b);
}
void small(int a,int b)
{
int small=0;
if(a<b)
{
small=a;
}
else
{
small=b;
}
cout<<"Smaller of the two number is "<<small;
}
5- Largest of three numbers
void main()
{
int num1, num2, num3,res=0;
large(int,int,int);
cout << "Enter three numbers: "<<endl;
cin >> num1 >> num2 >> num3;
res=large(num1,num2,num3);
cout << "The largest number"<<res;
}
large(int a,int b,int c)
{
if((a > b) && (a > c))
{
return(a);
}
else if((b>a) && (b>c))
{
return(b);
}
else
{
return(c);
}
}
6- Eligibility to vote
void main()
{
int age;
void eligib(int);
cout<<"enter your age:";
cin>>age;
eligib(age);
}
void eligib(int a)
{
if(a>=18)
{
cout<<"you are eligible for voting:";
}
else
{
cout<<"you are noneligible for voting:";
cout<<"wait for"<<18-age<<"year(s):";
}
}
7- Series of natural numbers upto nth term
void main()
{
int n;
void series(int);
cout << " Input a number of terms: ";
cin>> n;
series(n);
}
void series(int n)
{
int i;
cout << "The natural numbers upto "<<n<<"th terms are: \n";
for (i = 1; i <= n; i++)
{
cout << i << " ";
}
}
8- Table of a number
void main()
{
int n;
void table(int);
cout<<"Enter any number:";
cin>>n;
table(n);
}
void table(int n)
{
int i;
for(i=1;i<=10;++i)
{
cout<<"\n"<<n<<" * "<<i<<" = "<<n*i;
}
}
9- Star Pattern
void main()
{
void star(void);
star();
}
void star()
{
int i,j;
for(i=1;i<=5;++i)
{
for(j=1;j<=i;++j)
{
cout<<" * ";
}
cout<<�\n�;
}
}
10- Switch Case
void main()
{
int daynumber;
void day(int);
cout<<"Enter day number(1-7): ";
cin>>daynumber;
day(daynumber);
}
void daynumber(int n)
{
switch(n)
{
case 1: cout<<"Monday";
break;
case 2: cout<<"Tuesday";
break;
case 3: cout<<"Wednesday";
break;
case 4: cout<<"Thursday";
break;
case 5: cout<<"Friday";
break;
case 6: cout<<"Saturday";
break;
case 7: cout<<"Sunday";
break;
default: cout<<"Invalid input! Please enter week no.between 1-7.";
}
}