Experiment 07

Aim: Programming using user defined functions & strings (character array)

Task 1: Write a function that takes one integer parameter as a year, and displays its leap year or not.

#include <iostream>
using namespace std;
void leap(int i);
int main()
{
int y;
cout<<"Enter the year to be checked: ";
cin >>y;
leap(y);
return 0;
}
void leap(int i)
{
if (i%4==0)
{
if (i%100==0)
{
if (i%400==0)
cout<<endl<<"The year is a Leap Year"<<endl;
else
cout<<endl<<"The year is not a Leap Year"<<endl;
}
else
cout<<endl<<"The year is a Leap Year"<<endl;
}
else
cout<<endl<<"The year is not a Leap Year"<<endl;
}

Task 2: Write a function that takes two integer parameters x & y, and returns the result XY. (Don’t use pow( ))

#include <iostream>
using namespace std;
void power(int x, int y);
int main()
{
int a, b;
cout << "Enter the two integers X & Y (x^y)" << endl;
cin >> a >> b;
power(a, b);
return 0;
}
void power(int x, int y)
{
int i, j;
if (x == 0 && y == 0)
cout << "Infinity";
else
{
for (i = 0, j = 1; i < y; i++)
j = j * x;
cout << j;
}
}

Task 3: Implement a program using user defined function to return largest of three floating-point numbers.

#include <iostream>
using namespace std;
float large(float x, float y, float z);
int main()
{
float a, b, c;
cout << "Enter the 3 numbers:" << endl;
cin >> a >> b >> c;
cout << "Largest: " << large(a, b, c);
return 0;
}
float large(float x, float y, float z)
{
float a;
if (x > y)
{
if (x > z)
a = x;
else
a = z;
}
else
{
if (y > z)
a = y;
else
a = z;
}
return a;
}

Task 4: WAP using user defined function to calculate and return factorial of a given integer.

#include <iostream>
using namespace std;
int fact(int x);
int main()
{
int n;
cout << "Enter the number:";
cin >> n;
cout << "Factorial: " << fact(n);
return 0;
}
int fact(int x)
{
int i, f;
if (x == 0)
f = 1;
else
{
for (i = x, f = 1; i > 0; i--)
f = f * i;
}
return f;
}

Task 5: Write a menu driven program to compute sum of digits of a number, to find reverse of a number, to count number of digits by writing three different functions with parameters and return type.

#include <iostream>
using namespace std;
int sum(int x);
int rev(int y);
int dig(int z);
int main()
{
int n;
char ch;
cout << "Choose which operation to perform:" << endl << "S: Sum of digits" << endl << "R: Reverse the digits" << endl << "D: Find number of digits" << endl;
cin >> ch;
cout << "Enter the number: ";
cin >> n;
switch (ch)
{
case 'S':
case 's':
{
cout << endl << "The sum of digits of " << n << " is: " << sum(n) << endl;
}
break;
case 'R':
case 'r':
{
cout << endl << "The reversed form of " << n << " is: " << rev(n) << endl;
}
break;
case 'D':
case 'd':
{
cout << endl << "The number of digits in " << n << " is: " << dig(n) << endl;
}
break;
default:
cout << "Invalid Selection";
}
return 0;
}
int sum(int x)
{
int i;
for (i = 0; x > 0;)
{
i += x % 10;
x /= 10;
}
return i;
}
int rev(int y)
{
int r;
for (r = 0; y > 0;)
{
r = (r * 10) + (y % 10);
y /= 10;
}
return r;
}
int dig(int z)
{
int i;
for (i = 0; z > 0; i++)
{
z /= 10;
}
return i;
}

Task 6: Write user defined function “search” to search element is present in 1D array or not. Search function accepts array and key to search as parameters.

#include <iostream>
using namespace std;
void search(int x, int y[], int z);
int main()
{
int n, i, s;
int arr[10];
cout << "Enter the number of elements in Array(< 10 ) :";
cin >> n;
cout << "Enter the elements of Array:" << endl;
for (i = 0; i < n; i++)
cin >> arr[i];
cout << "Enter the element to be searched: ";
cin >> s;
search(s, arr, n);
return 0;
}
void search(int x, int y[], int z)
{
int j, k = 0;
for (j = 0; j < z; j++)
if (y[j] == x)
k++;
if (k == 1)
cout << "1 match found";
else if (k > 1)
cout << k << " matches found";
else
cout << "No match found";
}

Task 7: Write a program to print Fibonacci series up to n using recursion.

#include <iostream>
using namespace std;

int fib(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
else
return fib(n - 1) + fib(n - 2);
}

int main()
{
int n, i;
cout << "Enter the number: ";
cin >> n;
for (i = 0; i <= n; i++)
cout << fib(i) << ", ";
return 0;
}

Task 8: Write one program to perform following operations on strings
a. To find length of a string
b. To compare two string for equality
c. To Copy one string to other
d. To concatenate two string
e. To find reverse of a String

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char A1[10],B2[10],C3[10];
cout<<"Enter First string::";
cin>>A1;
cout<<"Enter Second string::";
cin>>B2;
cout<<"Length of first string is::"<<strlen(A1)<<endl;
cout<<"Length of second string is::"<<strlen(B2)<<endl;
{if(strcmp(A1,B2)==0)
cout<<"Both are the same string"<<endl;
else
cout<<"Both are different strings"<<endl;}
strcpy(C3,A1);
cout<<"The copied string is::"<<C3;
cout<<"The concatenation of given strings is::"<<strcat(C3,B2)<<endl;
cout<<"The reverse of the first string is::"<<strrev(A1)<<endl;
cout<<"The reverse of the second string is::"<<strrev(B2)<<endl;
return 0;}
do same

Task 9: WAP to copy one string to another string without using string handling function and display copied string.

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char A[10],B[10];
int i;
cout<<"Enter the string";
cin>>A;
for(i=0;A[i]!='\0';i++)
{
B[i]=A[i];
}
cout<<"The copied string is"<<B;
return 0;}
do same