Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
85 views

C++ Function Programs

The document describes 10 C++ functions that demonstrate different programming concepts: 1) Calculating the area of a circle without returning a value, 2) Calculating the area of a circle and returning the value, 3) Checking if a number is divisible by 11, 4) Finding the smaller of two numbers, 5) Finding the largest of three numbers, 6) Checking voting eligibility based on age, 7) Printing natural numbers up to a given value, 8) Printing the multiplication table of a number, 9) Printing a star pattern, and 10) Using a switch statement to print the day of the week. Each function is defined and called in a main function to demonstrate its usage.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

C++ Function Programs

The document describes 10 C++ functions that demonstrate different programming concepts: 1) Calculating the area of a circle without returning a value, 2) Calculating the area of a circle and returning the value, 3) Checking if a number is divisible by 11, 4) Finding the smaller of two numbers, 5) Finding the largest of three numbers, 6) Checking voting eligibility based on age, 7) Printing natural numbers up to a given value, 8) Printing the multiplication table of a number, 9) Printing a star pattern, and 10) Using a switch statement to print the day of the week. Each function is defined and called in a main function to demonstrate its usage.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.";
}
}

You might also like