0% found this document useful (0 votes)
2 views

FY_Functions_Solved

Solved Functions HSC 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FY_Functions_Solved

Solved Functions HSC 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ : Functions

SOLVED PROGRAMS
Write a Function in C++ … 2
1. To find area of circle. [ float area(float) ]
2. to find greatest of three numbers. [ int max(int,int,int)]
3. to find greatest of four numbers. [ int max(int,int,int,int)]
4. to find square and cube of a number.
[ int sqr(int) and int cube(int)]
5. to find factorial of a number. [ int fact(int) ]
6. to print Fibonacci sequence. [ void fib(int) ]
7. to find g.c.d. of two integers. [ int gcd(int,int) ]
Write a function in C++ … 3
8. to read set of numbers and find sum of all numbers.
[ int sum_of_set(int) ]
9. to find sum of the digits of a number. [ int
sum_of_digit(int) ]
(e.g. : 125 = 1 + 2+ 5 = 8 )
10. to find reverse the digits of a number. [ int reverse(int) ]
(e.g. : 125 => 521 )
11. to check whether a number is palindrome or not. (e.g. :
121)
12. to check whether a number is prime or not.
[ int IsPrime(int) ]
To find area of circle. [ float area(float) ]

#include<iostream.h>
#include<conio.h>
float area(float);
const float PI = 3.14f;
void main()
{
clrscr();
float a,r;
cout << "Enter radius :";
cin>>r;
a = area(r);
cout<< "Area of circle = " << a;
getch();
}
float area(float r)
{
return 3.14*r*r;
}
to find greatest of three numbers. [ int max(int,int,int)]

#include<iostream.h> float max3(int a,int b, int c)


#include<conio.h> {
float max3(int,int,int);
int m;
void main()
if(a>b)
{
m=a;
clrscr();
int x,y,z,m;
else

cout << "Enter three numbers :"; m=b;


cin >>x >>y >>z; if(m<c)
m = max3(x,y,z); m=c;
cout<< "Greatest number = " << m; return m;
getch(); }
}
to find greatest of four numbers. [ int max(int,int,int,int)]

#include<iostream.h> float max4(int a,int b, int c, int d)


#include<conio.h> {
float max4(int,int,int,int); int m;
void main() if(a>b)
m=a;
{
else
clrscr();
m=b;
int a,b,c,d,m;
if(m<c)
cout << "Enter three numbers :";
m=c;
cin >>a >>b >>c >>d; if(m<d)
m = max4(a,b,c,d); m=d;
cout<< "Greatest number = " << m;
getch(); return m;
} }
to find square and cube of a number.
[ int sqr(int) and int cube(int)]
#include <iostream.h> int sqr (int x)
#include <conio.h>
{
int sqr (int);
return x * x;
int cube (int);
void main () }
{
int a; int cube (int x)
cout << "Enter a number : ";
{
cin >> a;
return x * x * x;
cout << "\nNumber=" << a << ", Square=“ }
<< sqr(a) << ", Cube=" << cube(a);
getch();
}
to find factorial of a number. [ int fact(int) ]

#include <iostream.h> int fact (int n)


#include <conio.h> {
int fact (int); int i,f=1;
void main () for(i=1; i<=n ; i++)
{
{
int n,f;
f=f*i;
cout << "Enter a number : ";
}
cin >> n;
return f;
f = fact(n);
}
cout << “Factorial of" << n << “ is “ << f ;
getch();
}
to print Fibonacci sequence. [ void fib(int) ]

#include <iostream.h> int fib (int n)


{
#include <conio.h>
int i,a=0,b=1,c;
void fib(int);
cout<<a<<“,”<<b<<“,” ;
void main () for(i=1; i<=n-2 ; i++)
{ {
int n,f; c=a+b;
cout<< c << “ “ ;
cout << "Enter number of terms to be printed : ";
a=b;
cin >> n;
b=c;
fib(n); }
getch(); }
}
to find g.c.d. of two integers. [ int gcd(int,int) ]

#include <iostream.h> int gcd (int a, int b)


#include <conio.h> {
int gcd(int,int); int r;
void main () r = a % b;
{ while (r!=0)
int a,b,g; {
cout << "Enter two numbers : "; a = b;
cin >> a >> b; b = r;
g = gcd(a,b); }
cout << “GCD of ” << a << “ and ” << b << “ is ” << g; return b;
getch(); }
}
to read set of numbers and find sum of all numbers.
[ int sum_of_set(int) ]
#include <iostream.h> int sum_of_set (int n)
#include <conio.h> {
int sum_of_set (int n); int i,a,s=0;
void main () cout<< “Enter ” << n << “ numbers :” ;
{ for(i=1; i<=n ; i++)
int n,s; {
cout << "Enter a number : "; cin >> a;
cin >> n; s=s+a;
s = sum_of_set(n); }
cout << “Sum = ” << s; return s;
getch(); }
}
to find sum of the digits of a number. [ int sum_of_digit(int) ]
(e.g. : 125 = 1 + 2+ 5 = 8 )

#include <iostream.h> int sum_of_digit (int n)


#include <conio.h> {
int sum_of_digit(int); int r,s=0;
void main () while(n>0)
{ {
int n,s; r=n%10;
cout << "Enter a number : "; s=s+r;
cin >> n; n=n/10;
s = sum_of_digit(n); }
cout << “Sum = ” << s; return s;
getch(); }
}
to find reverse the digits of a number. [ int reverse(int) ]
(e.g. : 125 => 521 )

#include <iostream.h> int reverse (int n)


#include <conio.h> {
int reverse(int); int r,s=0;
void main () while(n>0)
{ {

int n,rev; r=n%10;

cout << "Enter a number : "; s = s*10 + r;


n=n/10;
cin >> n;
}
rev = reverse (n);
return s;
cout << “ Reversed number is ” << rev;
}
getch();
}
to check whether a number is palindrome or not. (e.g. : 121)

#include <iostream.h> int reverse (int n)


#include <conio.h>
{
int reverse(int);
int r,s=0;
void main ()
{
while(n>0)
int n,rev; {
cout << "Enter a number : "; r=n%10;
cin >> n; s = s*10 + r;
rev = reverse (n);
n=n/10;
if(n == rev)
}
cout << “The number is palindrome”;
else return s;
cout << “The number is not a palindrome”; }
getch();
}
to check whether a number is prime or not. [ int IsPrime(int) ]

#include <iostream.h> int isPrime (int n)


#include <conio.h>
{
int isPrime(int);
for(i=2;i<n;i++)
void main ()
{ {
int n; if(n%i==0)
cout << "Enter a number : "; return 0;
cin >> n;
}
if (isPrime(n))
return 1;
cout << n << “ is Prime number”;
else }
cout << n << “ is not a Prime number”;
getch();
}

You might also like