Functions Exercise

Programming Exercise

Functions Programming Exercise P-4

How to save personal data on internet?

Q.1 Write a program that converts kilogram into grams using function.

#include<iostream.h> 
#include<conio.h> 
main()
{
float grams(float);
float kg;
clrscr();
cout<<"Enter value in kilograms ? ";
cin>>kg;
cout<<kg<<" kilograms = "<< grams(kg)<<" grams";
getch();
}
// definition of grams() function 
float grams(float k)
{
return k * 1000; 
}

Q.2 Write a program that accepts a number from user then calls a function table() which prints the table of that number (the function table has no argument).

#include<iostream.h> 
#include<conio.h> 
int tb ;
main()
{
void table(void);
clrscr();
cout<<"Enter value for table ? ";
cin>>tb;
table();
getch();
}
// definition of table() function
void table(void)
{

for(int i = 1; i<=10;i++)
cout<<tb<<" x "<<i<<" = "<< tb *i<<endl;
}

Q.3 Write a program that displays a triangle of asterisks using function.

#include<iostream.h> 
#include<conio.h>
#include<iomanip.h>
main()
{
void triangle(void);
clrscr();
triangle();
getch();
}
// definition of triangle() function 
void triangle(void)
{
int u, i, s = 20;
for(u = 1; u<=10; u++)
{
cout<<setw(s);
for(i = 1; i<=u; i++)
if(i==1 || i == u)
cout<<"* ";
else if(u == 10)
cout<<"* ";
else
cout<<" ";
cout<<endl;
s--;
}
}

Q.4 Write a program to calculate the area of a cylinder. Calculate the area of cylinder by making function of it.
// Formula of Area of cylinder is = 2*PI*radius*(height + radius)

#include<iostream.h>
#include<conio.h>
main()
{
float area(int, int);
int radius, height;
clrscr();
cout<<"Enter value for radius : ";
cin>>radius;
cout<<"Enter height of cylinder : ";
cin>>height;
cout<<"Area of a cylinder is :"<<area(radius, height);
getch();
}
// definition of area() function
float area(int r, int h)
{
return 2*3.141*r*(h+r);
}

Q.5 Write a program using function that inputs an integer and counts the number of zeros, odd digits and even digits of the entered integer.

#include<iostream.h> 
#include<conio.h> 
main()
{
void count(long);
long num;
clrscr();
cout<<"Enter an integer value : ";
cin>>num;
count(num);
getch();
}
// definition of count() function

void count(long n)
{
int zero =0, odd= 0, even= 0, r;
while(n!=0)
{
r = n % 10;
if(r == 0) zero++;
else if(r%2 == 0) even++;
else if(r%2 == 1) odd++;
n = n / 10;
}
cout<<"Number of zeros :"<<zero<<endl;
cout<<"Number of odd digits :"<<odd<<endl;
cout<<"Number of even digits :"<<even<<endl;
}

Q.6 Write a program that prints 5 natural numbers and their factorials using function.

#include<iostream.h>
#include<conio.h>
main()
{
int no_fact(int);
clrscr();
for(int i = 1; i<=5; i++)
cout<<"Numner = "<<i<<", Factorial = "<< no_fact(i)<<endl;
getch();
}
// definition of no_fact() function

int no_fact(int n)
{
int f = 1;
while(n!=0)
{
f = f*n;
n--;
}
return f;
}

Q.7 Write a program using function that displays the square of numbers from 1 to 7 such as;
// 1 4 9 16 25 36 49

#include<iostream.h> 
#include<conio.h> 
main()
{
int square(int);
clrscr();
for(int i = 1; i<=7; i++)
cout<<"Square of "<<i<<" is : "<< square(i)<<endl;
getch();
}
// definition of square() function

int square(int n)
{
return n*n;
}

Q.8 Write a program that displays the following Fibonacci series using function:
// 1, 1, 2, 3, 5, 8, 13, 21, … n
// Input the value of ’n’ in the main() function and pass this value to the function.

#include<iostream.h> 
#include<conio.h> 
main()
{
void fibonacci(int);
int n;
clrscr();
cout<<"Enter value of n ? ";
cin>>n;
fibonacci(n);
getch();
}
// definition of fibonacci() function

void fibonacci(int num)
{
int next, last, s;
for(next=0,last=1; last<=num;)
{
cout<<last<<"\t";
s = next + last;
next = last;
last = s;
}

}

Q.9 Write a program using function to find out the minimum of three numbers ‘a’, ‘b’, and ‘c’.

#include<iostream.h>
#include<conio.h>
main()
{
int smallest(int, int, int);
int a, b, c, mini;
clrscr();
cout<<"Enter first number ? ";
cin>>a;
cout<<"Enter second number ? ";
cin>>b;
cout<<"Enter third number ? ";
cin>>c;
mini = smallest(a, b, c);
cout<<"Smallest value is : "<<mini;
getch();
}
// definition of smallest() function

int smallest(int x, int y, int z)
{

int m;
if(x<y)
if(x<z)
m = x;
else
m = z;
else
if(y<z)
m = y;
else
m = z;
return m;
}

 

Leave a Reply