0% found this document useful (0 votes)
43 views17 pages

C++ Function Questions

The document contains a series of C++ function questions designed for practice, covering a variety of topics such as printing names, calculating squares and cubes, checking even/odd numbers, finding maximum values, calculating factorials, and more. Each question includes a sample function code and suggests alternative approaches to solve the problem. The questions aim to enhance understanding of function implementation and manipulation in C++.

Uploaded by

harshsingh2526h
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views17 pages

C++ Function Questions

The document contains a series of C++ function questions designed for practice, covering a variety of topics such as printing names, calculating squares and cubes, checking even/odd numbers, finding maximum values, calculating factorials, and more. Each question includes a sample function code and suggests alternative approaches to solve the problem. The questions aim to enhance understanding of function implementation and manipulation in C++.

Uploaded by

harshsingh2526h
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Function Questions For Practice

Q1. Write a function to print your name.


Function code :
void printName(string s) {
cout << s << endl;
}

Solve using another approach (using a string function)

Q2. Write a function to calculate the square of a number.


Function code :
int square(int num) {
return num * num;
}

Solve using another approach (using a void function)

Q3. Write a function to calculate cube of a number.


Function code :
void cube(int num) {
return num * num* num ;
}

Solve using another approach (using a int function)

Q4. Write a function to check whether a number is even or odd.


Function code :
void Even_Odd(int num) {
if(num % 2 == 0)
cout << num << " is Even" << endl;
else
cout << num << " is Odd" << endl;
}

Solve using another approach (using a int function, bool function)​


There are 3 other ways to solve this question.
Solve it without using (%)

Q5. Write a function to find the maximum of two numbers a,b.


Function code :
int maximum(int a, int b) {
if(a > b)
return a;
else {
return b;
}
}

Solve using another approach (using a void function)

Q6. Write a function to find the maximum of three numbers a,b,c.


Function code :
int maximum(int a, int b, int c) {
if(a >= b && a >= c)
return a;
else if(b >= a && b >= c)
return b;
else
return c;
}

Solve using another approach (using a void function)

Q7. Write a function to calculate the factorial of a number.


Function code :

// Function to calculate factorial using loop (iterative method)


int factorial(int n) {
int fact = 1;
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
return fact;
}

// Function to calculate factorial recursive approach.

int recursive_factorial(int n) {
if(n == 0 || n == 1)
return 1;
return n * recursive_factorial(n - 1);
}

Q8. Write a function to check whether a number is prime or not.


Function code :
bool isPrime(int num) {
if(num <= 1)
return 0;
for(int i = 2; i < num; i++) {
if(num % i == 0)
return 0;
}
return 1;
}

Solve using another approach

Q9. Write a function to count vowels and consonants in a given string.


Function code :

void count_vowels(string str) {


int vowels = 0, consonants = 0;
for(int i = 0; i < [Link](); i++) {
// char ch = tolower(str[i]); // convert to lowercase for simplicity

if(str[i] >= 'a' && str[i]<= 'z') {


if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')
vowels++;
else
consonants++;
}
}

cout << "Vowels: " << vowels << endl;


cout << "Consonants: " << consonants << endl;
}

Solve using another approach (using a void function)

[Link] a function to print integers from 1 to n.


Function code :

// Function to print numbers from 1 to n using loops (iterative


approach)
void printNumbers(int n) {
for(int i = 1; i <= n; i++) {
cout << i << " ";
}
}

// Function to print numbers from 1 to n using recursion.


void Recursive_printNumbers(int n) {
if(n == 0)
Return;
Recursive_printNumbers(n - 1);
cout << n << " ";
}
[Link] a function to calculate sum of integers from 1 to n.
Function code :
// Function to calculate sum from 1 to n using loops (iterative
approach)
int sum(int n) {
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

// Function to calculate sum from 1 to n using recursion function.

int recursion_sum(int n) {
if(n == 0)
return 0;
return n + recursion_sum(n - 1);
}

Solve using another approach (using a void function)


Q12. Write a function to find the average of two numbers.
Function code :
// Function to find average of two numbers
float average(float a, float b) {
return (a + b) / 2;
}
Solve using another approach (using a void function)

Q13. Write a function to calculate the sum of elements in an array.


Function code :
// Function to calculate sum of array elements
int sumArray(int arr[], int n) {
int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}

Solve using another approach (using a void function)


Solve using recursive approach
Q14. Write a function to find the largest and smallest element in an
array.
Function code :
// Function to find largest element
int findLargest(int arr[], int n) {
int largest = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > largest)
largest = arr[i];
}
return largest;
}

// Function to find smallest element


int findSmallest(int arr[], int n) {
int smallest = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] < smallest)
smallest = arr[i];
}
return smallest;
}

Solve using another approach (using a void function)


Q15. Write a function to find the second largest element in an array.
Function code :
// Function to find second largest element
int secondLargest(int arr[], int n) {
int largest = INT_MIN; // Assuming largest element is INT_MIN
int second = INT_MIN; // Assuming second largest element is
INT_MIN
// NOTE- INT_MIN is built in function which gives lowest integer value

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


if(arr[i] > largest) {
second = largest;
largest = arr[i];
}
else if(arr[i] > second && arr[i] != largest) {
second = arr[i];
}
}

return second;
}

Solve using another approach (using a void function)


More basic approach
Q16. Write a function to check whether a given string is a palindrome.
Function code :
// Function to check palindrome using reverse
bool Palindrome(string str) {
string s = str; // copy original string
reverse([Link](), [Link]()); // reverse the copy
if (str == s){
return 1;
}
else{
return 0; // compare original with reversed
}

Solve using another approach (using a void function)


Q17. Write a function to swap two numbers using call by value and
call by reference.
Function code :
// Swap using call by value
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "Inside swapByValue: a = " << a << ", b = " << b << endl;
}

// Swap using call by reference


void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

Q18. Write a function to find the area of a square of side a.


Function code :
// Function to calculate area of a square
float area(float side) {
return side * side;
}

Solve using another approach (using a void function)

Q19. Write a function to find the area of a rectangle of side l, b.


Function code :
// Function to calculate area of rectangle
float areaOfRectangle(float length, float breadth) {
return length * breadth;
}
Solve using another approach (using a void function)

Q20. Write a function to calculate simple interest, SI= (P*R*T)/100


Function code :
// Function to calculate Simple Interest
float simple_interest(float principal, float rate, float time) {
return (principal * rate * time) / 100;
}

Solve using another approach (using a void function)


Q21. Write a function to convert temperature from celsius to
Fahrenheit, F = (C*(9/5)) + 32
Function code :

// Function to convert Celsius to Fahrenheit


float celsiusToFahrenheit(float celsius) {
return (celsius * 9 / 5) + 32;
}

Solve using another approach (using a void function)


Q22. Write a function to reverse the digits of a number. input = 123,
output = 321
Function code :
// Function to reverse digits of a number
int reverseNumber(int num) {
int reversed_num = 0;
while(num != 0) {
int digit = num % 10;
reversed_num = reversed_num * 10 + digit;
num = num / 10;
}
return reversed_num;
}

Solve using another approach (using a void function)


Q23. Write e function to check whether a number is palindrome or not.
input = 121, output = Palindrome
Function code :

bool isPalindrome(int num) {


int original_num = num;
int reversed_num = 0;

while(num != 0) {
int digit = num % 10;
reversed_num = reversed_num * 10 + digit;
num = num / 10;
}
return (original_num == reversed_num);
}

Solve using another approach (using a void function)


Q24. Write a function to check whether a number is Armstrong
number.
Function code :
// Function to check Armstrong number
bool isArmstrong(int num) {
int original = num;
int sum = 0;
int digits = 0;

// Count number of digits


int temp = num;
while(temp != 0) {
digits++;
temp = temp / 10;
}

// Calculate sum of digits raised to the power of digits


temp = num;
while(temp != 0) {
int digit = temp % 10;
sum += pow(digit, digits);
temp = temp / 10;
}

return (sum == original);


}

Solve using another approach (using a void function)


Q25. Write a function to add two numbers and return the result.
Function code :
// Function to add two numbers
int addNumbers(int a, int b) {
return a + b;
}

Solve using another approach (using a void function)


Q26. Write a function to find the Greatest common Divisor (GCD) of
two numbers a,b using loops.
Function code :
// Function to find GCD
int gcd(int a, int b) {
while(a != b) {
if(a > b)
a = a - b;
else
b = b - a;
}
return a; // or return b, both are same now
}

Solve using another approach (using a void function)

You might also like