Functions Exercise

Programming Exercise

Array Programming Exercise P-7

Programming Exercise Looping Structure P-1

Program 1:
Write a function that displays asterisks according to the following pattern:
// *********
// *******
// *****
// ***
// *

#include<iostream.h>
#include<conio.h> 
main()
{
void draw_asterisks(void);
clrscr();
draw_asterisks();
getch();
}

// definition of draw_asterisks function 
void draw_asterisks(void)
{
int u, i;
for( u = 9; u>=1; u-=2)
{
for ( i = 1; i<=u; i++)
cout<<"* ";
cout<<endl;
}
}

Program 2:
Write a program by defining a function “sum” to calculate the sum of three floating-point values. The entered values by user should pass to the function as arguments and display the result on screen.

#include<iostream.h>
#include<conio.h>
main()
{
void sum(float,float,float); 
float a,b,c;
clrscr();
cout<<"Enter first value ? ";
cin>>a;
cout<<"Enter second value ? ";
cin>>b;
cout<<"Enter third value ? ";
cin>>c;
sum(a, b, c);
cout<<"Ok";
getch();
}

// definition of sum() function 
void sum(float x, float y, float z)
{
cout<<"\nSum of "<<x<<","<<y<<" and "<<z<<" = "<<x+y+z<<endl;
}

Program 3:
Write a program that inputs a number and passes that number to the function. The function displays a table of the given number.

#include<iostream.h>
#include<conio.h>
main()
{
void table(int);
int n;
clrscr();
cout<<"Enter a number ? ";
cin>>n;
table(n);
getch();
}

// definition of table() function 
void table(int t)
{
int res, c;
for (c=1; c<=10; c++)
{
res = t * c;
cout<<t<<'x'<<c<<'='<<res<<endl;
}
}

Program 4:
Write a program that inputs two numbers and passes these numbers to a function. The function finds the greatest number and displays the result.

#include<iostream.h>
#include<conio.h>
main()
{
void max(int, int);
int a, b;
clrscr();
cout<<"Enter first number ? ";
cin>>a;
cout<<"Enter second number ? ";
cin>>b;
max(a, b);
getch();
}

// definition of max() function
void max(int x, int y)
{
int mx;
mx = (x>y)? x:y;
cout<<"The Greatest number is: "<<mx;
}

Program 5:
Write a program that inputs a number and passes the number to a function. The function calculates the factorial and displays the result.

#include<iostream.h>
#include<conio.h>
main()
{
void fact(int);
int n;
clrscr();
cout<<"Enter a number ? ";
cin>>n;
fact(n);
getch();
}

// definition of fact() function
void fact(int num)
{
int f = 1,c = 1;
while(c<=num)
{
f = f*c;
c = c + 1;
}
cout<<"Factorial of "<<num<<" = "<<f;
} 

Program 6:
Write a program that inputs two numbers and passes these numbers to a function. The function displays the result of first number raised to the power of second number.

#include<iostream.h>
#include<conio.h>
main()
{
void power(int, int);
int a, b;
clrscr();
cout<<"Enter first number ? ";
cin>>a;
cout<<"Enter second number ? ";
cin>>b; 
power(a,b); 
getch();
}

// definition of power() function
void power(int n, int p)
{
int res, c;
res = c = 1;
while(c<=p)
{
res = res * n;
c=c+1;
}
cout<<n<<" raised to the power "<<p<<" is "<<res;
}

Program 7:
Write a program by defining a function “even_odd” to test whether a given integer is even or odd. Pass the integer value as an argument to the function.

#include<iostream.h>
#include<conio.h>
main()
{
void even_odd(int);
int n;
clrscr();
cout<<"Enter any integer value ? ";
cin>>n;
even_odd(n);
getch();
}

// definition of even_odd() function 
void even_odd(int x)
{
if(x%2==0)
cout<<"Even number ";
else
cout<<"Odd number ";
}

Program 8:
Write a program by defining a function “perfect” that determines whether a number passed to a function is a perfect number. Use this function to determine and display all the perfect numbers between 1 and 1000.
//Note: An integer is said to be a perfect if the sum of its factors including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1+2+3.

#include<iostream.h>
#include<conio.h>
main()
{ 
void perfect(int);
int n;
clrscr();
for(n=1;n<=1000;n++)
perfect(n);
getch();
}

// definition of perfect() function 
void perfect(int x)
{
int i,s=0;
for(i = 1;i<=x/2;i++)
if(x%i==0)
s=s+i;
if(s==x)
cout<<x<<endl;
}

Program 9:
Write a program by defining a function “prime” that determines whether a number passed to the function is a prime number. Use this function to determine and display all the prime numbers between 2 and 100.
//Note: An integer is said to be a prime if it is divisible by only 1 and itself. For example, 2,3,5,7 etc. are prime numbers.

#include<iostream.h>
#include<conio.h>
main()
{
void prime(int);
long i;
clrscr();
for( i= 2;i<=100;i++)
prime(i);
getch();
}

// definition of prime() function 
void prime(int n)
{
int d;
for(d = 2; d < n; d++)
if (n%d ==0)
return;
cout<<n<<"\t";
}

Leave a Reply