0% found this document useful (0 votes)
14 views19 pages

EX - 7

The document contains multiple C++ programs demonstrating various functionalities such as checking for leap years, calculating power, finding the largest number, computing factorials, performing string operations, and searching elements in an array. Each program includes user input prompts and outputs results based on the provided inputs. The examples illustrate fundamental programming concepts including loops, conditionals, functions, and array manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

EX - 7

The document contains multiple C++ programs demonstrating various functionalities such as checking for leap years, calculating power, finding the largest number, computing factorials, performing string operations, and searching elements in an array. Each program includes user input prompts and outputs results based on the provided inputs. The examples illustrate fundamental programming concepts including loops, conditionals, functions, and array manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

#include <iostream>

using namespace std;

void year (int);

int main()

int r;

cout<<"enter the year = ";

cin>>r;

year(r);

return 0;

void year(int r)

if ((r%4==0 && r%100!=0) || r%400==0)

cout<<"It is leap year "<<endl;

else

cout<<"It is not leap year "<<endl;

}
enter the year = 1900

It is not leap year

enter the year = 2024

It is leap year
#include <iostream>

using namespace std;

int power(int x, int y) {

int result = 1;

for (int i = 0; i < y; i++) {

result *= x;

return result;

int main() {

int x, y;

cout << "Enter base (x): ";

cin >> x;

cout << "Enter exponent (y): ";

cin >> y;

cout << "Result: " << power(x, y) << endl;

return 0;

Enter base (x): 2

Enter exponent (y): 3

Result: 8
#include <iostream>

using namespace std;

float largest(float a, float b, float c) {

if (a >= b && a >= c) {

return a;

} else if (b >= a && b >= c) {

return b;

} else {

return c;

int main() {

float num1, num2, num3;

cout << "Enter three floating-point numbers: " << endl;

cin >> num1 >> num2 >> num3;

cout << "The largest number is: " << largest(num1, num2, num3) << endl;

return 0;

}
Enter three floating-point numbers:

21

32

69

The largest number is: 69


#include <iostream>

using namespace std;

int fact(int a);

int main()

int x;

cout<<"Enter the value of x "<<endl;

cin>>x;

int z=fact(x);

cout<<"Factorial of given number is "<<z;

return 0;

int fact(int a)

if (a<=1)

return 1;

else

return a*fact(a-1);

}
Enter the value of x

Factorial of given number is 120


#include <iostream>

using namespace std;

int sum_of_digits(int);

int rev_num(int);

int num_digit(int);

int main() {

cout<<"MENU"<<endl;

cout<<"enter 1 for addition "<<endl;

cout<<"enter 2 for reverse order "<<endl;

cout<<"enter 3 for number of digits "<<endl;

int choice,res,num;

cout<<"enter your choice = ";

cin>>choice;

cout<<"enter number"<<endl;

cin>>num;

switch(choice)

case 1:

res=sum_of_digits(num);

cout<<"Addition of "<<num<<" is "<<res;

break;

case 2:

res=rev_num(num);

cout<<"reverse order of "<<num<<" is "<<res;

break;

case 3:

res=num_digit(num);

cout<<"number of digits of "<<num<<" is "<<res;

break;

default:

cout<<"enter you choice between 1 to 3 "<<endl


}

return 0;

} int sum_of_digits(int x)

{ int digit,sum=0;

while(x!=0) { digit=x%10;

x=x/10;

sum=sum+digit;

} return sum;

}int rev_num(int x)

int digit,sum=0;

while(x!=0)

{ digit=x%10;

x=x/10;

sum=sum*10+digit;

return sum;

int num_digit(int x)

int count=0;

int digit,sum=0;

while(x!=0)

{ x=x/10; count++;

return count;

enter your choice = 1

enter number

1234

Addition of 1234 is 10
#include <iostream>

using namespace std;

void search_element(int[],int,int);

int main()

int size,key,element;

cout<<"Enter the size of array"<<endl ;

cin>>size;

int arr[size];

cout<<"enter the elements for array "<<endl;

cin>>element;

for(int i=0; i<size; i++)

cin>>arr[i];

cout<<"enter the element to be searched in array "<<endl;

cin>>key;

search_element(arr,size,key);

return 0;

void search_element(int a[],int s,int k)

{ int ctr=0;

for(int i=0; i<s; i++)

if (a[i]==k)

ctr++;

}
if(ctr>0)

cout<<"element is present "<<endl;

else

cout<<"element is absent "<<endl;

Enter the size of array

enter the elements for array

enter the element to be searched in array

element is present
#include <iostream>

using namespace std;

int fibo(int x)

if(x==0||x==1)

return (x);

else

return(fibo(x-1)+fibo(x-2));

int main()

int x,i;

cout<<"enter the value of x = ";

cin>>x;

for(i=0; i<x; i++)

cout<<fibo(i)<<" ";

return 0;

}
enter the value of x = 21

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765


#include <iostream>

#include <cstring>

using namespace std;

int stringLength(char str[]) {

int length = 0;

while (str[length] != '\0') {

length++;

} return length;

} bool compareStrings(char str1[], char str2[]) {

int i = 0;

while (str1[i] != '\0' && str2[i] != '\0') {

if (str1[i] != str2[i]) {

return false; } i++;

return str1[i] == str2[i];

} void copyString(char dest[], char src[]) {

int i = 0;

while (src[i] != '\0') {

dest[i] = src[i];

i++; } dest[i] = '\0';

} void concatenateStrings(char str1[], char str2[]) {

int i = 0, j = 0;

while (str1[i] != '\0') {

i++;

while (str2[j] != '\0') {

str1[i] = str2[j];

i++;

j++;

} str1[i] = '\0';

} void reverseString(char str[]) { int length = stringLength(str);


for (int i = 0; i < length / 2; i++) { char temp = str[i];

str[i] = str[length - i - 1];

str[length - i - 1] = temp; }

} int main() {

char str1[100], str2[100], copy[100];

cout << "Enter the first string: ";

cin.getline(str1, 100);

cout << "Enter the second string: ";

cin.getline(str2, 100);

cout << "Length of first string: " << stringLength(str1) << endl;

if (compareStrings(str1, str2)) {

cout << "Strings are equal." << endl;

} else { cout << "Strings are not equal." << endl;

} copyString(copy, str1);

cout << "Copied first string to a new string: " << copy << endl;

concatenateStrings(str1, str2);

cout << "Concatenated strings: " << str1 << endl;

reverseString(copy);

cout << "Reverse of the copied string: " << copy << endl;

return 0;

Enter the first string: 12

Enter the second string: 21

Length of first string: 2

Strings are not equal.

Copied first string to a new string: 12

Concatenated strings: 1221

Reverse of the copied string: 21


#include <iostream>

using namespace std;

int main() {

char source[100], destination[100];

int i = 0;

cout << "Enter a string: ";

cin.getline(source, 100);

while (source[i] != '\0') {

destination[i] = source[i];

i++;

destination[i] = '\0';

cout << "Copied string: " << destination << endl;

return 0;

}
Enter a string: 12

Copied string: 12

You might also like