Professional Documents
Culture Documents
3.
4.
5.
6.
size out of asterisks and blanks. For example, if user enters 5 it should
print.
*****
*
*
*
*
*
*
*****
Write a program that by defining a function even_odd(), to test
whether a given integer is even or odd. Pass an integer value as an
argument to a function.
Write a function multiple() that determines for a pair of integers
whether the second is multiple of first or not.
Write a function smallest() that takes three arguments to the function
and prints the smallest of all three.
Write a function prime() that finds whether the given number is prime
number or not.
5. Write a program that accept a string array from the user and
displays its length.
6. Write a program that accept two strings as a parameter from the
user and concatenate those strings into a third string.
7. Write a program that accept a string array from the user and count
the total number of vowel and consonants in a string.
8. Write a program that accept a string from the user and search the
character u in the array, and replace it with the character p
Enter a string: documents and files should be submitted on
time
Enter character you want to replace: u
Enter character with which you want to replace: p
Output string: docpments and files shopld be spbmitted on
time
9. Write a program that takes string as an input from the user and
then count:
a. The total number of characters in the string.
b. The total number of words in the string.
c. Total spaces in the array string.
d. Displays total number of punctuation marks if any in the string.
10. Write a program that accept a string from the user and then
convert all uppercase character to lower case characters and lower
case character to upper case.
11. Write a function that takes the string from the user and check
whether the given string is Palindrome or not. (Using built-infunction and user defined function).
12. Write a program that will read a string and delete from it all
occurrences of the word is.
* * * * *
12.
Write a Function that that take size of the side of a square from
the user and pass it as a parameter; it then prints a hollow square of
that size out of asterisks and blanks. For example, if user enters 5 it
should print.
*****
*
*
*
*
*
*
*****
13.
Write a function comparison() that takes two values as
parameter. It prints sum of x and y if x is greater than y, otherwise it
prints x minus two times y.
14.
Write a function Dconversion() that converts the binary integer
and print its decimal equivalent.
Hint: The decimal equivalent of binary 1101 is 1*1+0*2+1*4+1*8 or
1+0+4+8=13
15.
Write a function printable() that takes a positive integer less
than 50 and prints its multiplication table.
16.
Write a program that by defining a function even_odd(), to test
whether a given integer is even or odd. Pass an integer value as an
argument to a function.
17.
Write a function multiple() that determines for a pair of integers
whether the second is multiple of first or not.
18.
Write a function smallest() that takes three arguments to the
function and prints the smallest of all three.
19.
Write a function prime() that finds whether the given number is
prime number or not.
20.
Write a function fact() to calculate the factorial value of any
integer entered through the keyboard.
21.
Write a function reverse() that takes an integer value and
returns the number with its digits reversed. For example, given the
number 7631, the function should return 1367.
22.
Write a function power() that displays first number raised to the
power of second number. i.e. xy
23.
Write a function Fibonacci() that displays first 10 Fibonacci
numbers.
i.e. Fibonacci series is
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
24.
Write a function Factor() that takes a number from user and
displays its factors.
25.
.Write a function perfect() that takes a number from user and
must select one of the three functions and show the result. The
userInterface() function must quit if the user selects the quit option.
Note: Area of circle is: = r2
Area of Square is: = a2
Area of Rectangle is = w h
void stat()
{
static int a=0;
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
stat();
}
e) Pa
ss
f)
void numbers(int x, int &y)
{
int b;
x+=6;
y+=11;
b=55;
cout<<b<<" "<<x<<" "<<y<<endl;
}
void main()
{
int a,b,c;
a=22;
b=90;
c=14;
numbers(a,a);
numbers(a,b);
numbers(b,a);
cout<<a<<" "<<b<<" "<<c<<endl;
}
Lab 08 (functions)
1. What will be the output of the following programs, debug the program and
then write the answer:
a) Static variable:
Simple variable
Static Variable
void simpl()
{
int a=0;
void stat()
{
static int a=0;
cout<<a<<endl;
a++;
}
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
simpl ();
}
Output:
main()
{
for(int i=0;i<5;i++)
stat();
}
Output:
b) Function Overloading:
Output:
int cube(int a)
{
return a*a*a;
}
double cube(double a)
{
return a*a*a;
}
void main ()
{
cout<<"cube
of
integer
is :"<<cube(5)<<endl;
cout<<"\n\ncube
of
float
is :"<<cube(5.4)<<endl;
}
value
value
5
10.24
Single Index
2.
Output:
if y=0
gcd(y, x%y)
if y0
4. Write a program to find the exponential power of an integer using
recursive function.
5. Write a recursive program that performs multiplication of two digits,
using addition operator.
6. Following program takes a decimal number from user and prints its
Binary using recursive function. You are required to write this program
in IDE, compile and then execute it. You have to make a word
document in which you will show execution of the following program.
#include<iostream.h>
#include<conio.h>
/*------ Function for Decimal to Binary Conversion -----*/
void decToBin(int num, int base)
{
if(num > 0)
{
decToBin(num / base, base);
cout << num % base;
}
}
void printStars()
{
cout<<"\n\n***************************\n";
cout<<"***************************\n\n";
}
void main()
{
clrscr();
int decimalNum;
int base;
base = 2;
cout<<"\n\n Enter number in Decimal\t";
cin>>decimalNum;
cout<<endl;
printStars();
cout<<"\n\n Decimal " <<" = " << " Binary \n\n" ;
cout<<" " << decimalNum <<"\t
";
decToBin ( decimalNum, base);
printStars();
getch();
}
7. Write recursive GCD function, that finds greatest common divisor of
two integers. Mathematical Rule is given below.
gcd (x,y) = x
gcd(y, x%y)
8.
if y=0
if y0
Write a function that check whether the given year is a leap year or not.
void leap (void)
{
int y;
cout<<" \n Enter The year : ";
cin>> y;
if (y%4==0 || y%100>0 && y%400==0 )
cout<<y<<" is a leap year ";
}
int main ()
{
leap ();
getch ();
}
LAB 10 (Pointers)
1.
a)
int num, *numPtr;
num = 987;
numPtr = #
cout<<"The value in num is ,"<< num<<endl;
cout<<"The address of num is, "<<&num<<endl;
cout<<"The value in numPtr is ,"<< numPtr<<endl;
cout<<"The value in the address pointed by numPtr is ,"<<
*numPtr<<endl;
cout<<"The address of numPtr is ,"<< &numPtr<<endl;
cout<<"The value in the address of num is ,
"<<*&num<<endl;
Output:
int *p = &a;
int *q = &b;
What is the value of each of the following expression?
i. ++a
ii. ++(*p)
iii. &a
v. *p
vi.
--(*q)
vii. --b;
viii. &b
ix. q++
iv. p++
x. *q
Output:
Output:
int main()
{
int num[5] = {3,4,6,2,1};
int *p = num;
int *q = num+2;
int *r = &num[1];
cout<<num[2]<<setw(3)<<*(num +
2)<<endl;
cout<<*p<<setw(3)<<*(p +1)<<endl;
cout<<*q<<setw(3)<<*(q+1)<<endl;
cout<<*r<<setw(3)<<*(r+1)<<endl;
a)
} Given the following declaration.
h) Default Arguments
void show_parameters(int a=1, int b=2, int c=3)
{
cout << "a " << a << " b " << b << " c " << c << endl;
}
void main(void)
{
show_parameters();
show_parameters(1001);
show_parameters(1001, 2002);
show_parameters(1001, 2002, 3003);
}
void stat()
{
static int a=0;
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
stat();
}
k) Pa
ss
l)
void numbers(int x, int &y)
{
int b;
x+=6;
y+=11;
b=55;
cout<<b<<" "<<x<<" "<<y<<endl;
}
void main()
{
int a,b,c;
a=22;
b=90;
c=14;
numbers(a,a);
numbers(a,b);
numbers(b,a);
cout<<a<<" "<<b<<" "<<c<<endl;
}
Lab 08 (functions)
6. What will be the output of the following programs, debug the program and
then write the answer:
d) Static variable:
Simple variable
Static Variable
void simpl()
{
int a=0;
void stat()
{
static int a=0;
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
simpl ();
}
Output:
cout<<a<<endl;
a++;
}
main()
{
for(int i=0;i<5;i++)
stat();
}
Output:
e) Function Overloading:
Output:
int cube(int a)
{
return a*a*a;
}
double cube(double a)
{
return a*a*a;
}
void main ()
{
cout<<"cube
of
integer
is :"<<cube(5)<<endl;
cout<<"\n\ncube
of
float
is :"<<cube(5.4)<<endl;
}
value
value
10.24
Single Index
void show_array(int &a)
{
a=100;
}
void main()
{
int number[5] = { 1, 2, 3, 4,
5 };
show_array(number[2]);
for(int i=0;i<5;i++)
cout<<number[i]<<endl;
}
Output:
7. Twenty-five numbers are entered from the keyboard into an array. The
number to be searched is entered through the keyboard by the user.
Write a program to find if the number to be searched is present in the
array or not, and if it is present then display that it is present also
display the number of times it appears in the array.
8.
Write a function that copies one-dimension array into a twodimensional array of k rows and j columns. The rows must be valid
factor of the number of elements in the one-dimensional array; that is
k * j = n.
if y=0
gcd(y, x%y)
if y0
12. Write a program to find the exponential power of an integer using
recursive function.
13. Write a recursive program that performs multiplication of two digits,
using addition operator.
14. Following program takes a decimal number from user and prints its
Binary using recursive function. You are required to write this program
in IDE, compile and then execute it. You have to make a word
document in which you will show execution of the following program.
#include<iostream.h>
#include<conio.h>
/*------ Function for Decimal to Binary Conversion -----*/
void decToBin(int num, int base)
{
if(num > 0)
{
decToBin(num / base, base);
cout << num % base;
}
}
void printStars()
{
cout<<"\n\n***************************\n";
cout<<"***************************\n\n";
}
void main()
{
clrscr();
int decimalNum;
int base;
base = 2;
cout<<"\n\n Enter number in Decimal\t";
cin>>decimalNum;
cout<<endl;
printStars();
cout<<"\n\n Decimal " <<" = " << " Binary \n\n" ;
cout<<" " << decimalNum <<"\t
";
decToBin ( decimalNum, base);
printStars();
getch();
}
15. Write recursive GCD function, that finds greatest common divisor of
two integers. Mathematical Rule is given below.
gcd (x,y) = x
gcd(y, x%y)
if y=0
if y0
16. Write a function that check whether the given year is a leap year or not.
void leap (void)
{
int y;
cout<<" \n Enter The year : ";
cin>> y;
if (y%4==0 || y%100>0 && y%400==0 )
cout<<y<<" is a leap year ";
}
int main ()
{
leap ();
getch ();
}
LAB 10 (Pointers)
6.
c)
int num, *numPtr;
num = 987;
numPtr = #
cout<<"The value in num is ,"<< num<<endl;
cout<<"The address of num is, "<<&num<<endl;
cout<<"The value in numPtr is ,"<< numPtr<<endl;
cout<<"The value in the address pointed by numPtr is ,"<<
*numPtr<<endl;
cout<<"The address of numPtr is ,"<< &numPtr<<endl;
cout<<"The value in the address of num is ,
"<<*&num<<endl;
Output:
iv. p++
x. *q
Output:
Output:
int main()
{
int num[5] = {3,4,6,2,1};
int *p = num;
int *q = num+2;
int *r = &num[1];
cout<<num[2]<<setw(3)<<*(num +
2)<<endl;
cout<<*p<<setw(3)<<*(p +1)<<endl;
cout<<*q<<setw(3)<<*(q+1)<<endl;
cout<<*r<<setw(3)<<*(r+1)<<endl;
b)
} Given the following declaration.
char *pa = a;
int i = 2;
int j = 4;
int *pi = &i;
What is the value of each of the following expression?
ii.
*(pa + j)
ii. *(pa + *pi)
Output:
int main()
{
int a[5]={1,2,3,4,5};
int *p;
cout<<"Array itself a Pointer\n";
for(int i=0;i<5;i++)
{
cout<<*a<<"\t";
*a+=1;
}
cout<<endl;
p=&a[2];
cout<<"Array Pointer "<<a[0]<<"\tPointer
2]<<endl;
cout<<"Array Pointer "<<a[1]<<"\tPointer
1]<<endl;
cout<<"Array Pointer "<<a[2]<<"\tPointer
"<<p[0]<<endl;
cout<<"Array Pointer "<<a[3]<<"\tPointer
"<<p[1]<<endl;