V.Vidhya Logo

V.Vidhya

Functions


Whenever you write code for the exercises below, use functions to keep your solution reusable and scalable.


Q1.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q2.

You need to display the same error message in multiple places in your program. Instead of writing the same std::cout statement repeatedly, design a reusable solution that makes your code more efficient and easier to maintain. Write a function that prints the error message and then demonstrate it by calling the function twice.

Expected Output:

Invalid command!   // first time calling the function

Invalid command!   // second time calling the function

Q3.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q4.

In a busy cafe, the barista needs to take orders from multiple customers, each time asking the number of cups for order and making sure it is a valid positive integer number.

To solve this problem, write a program that defines a function that repeatedly prompts the user to enter the number of cups for their order until a valid positive integer is provided, then the function should return the valid number of cups once it is obtained.

Expected Output:

Enter the number of cups for your order: -3
Invalid input! Please enter a positive integer.
Enter the number of cups: 0
Invalid input! Please enter a positive integer.
Enter the number of cups: 2

Thank you for your order!
You have ordered for 2 cups of coffee.

Q5.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q6.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q7.

Write a program that takes a person’s height in feet as input and converts it to centimeters.

Expected Output:

Enter person's height (in ft): 5.4   // user input

Person's height (in cm): 164.592

 // 1 ft = 30.48 cm

Q8.

Write a program that takes student marks as input, then gives the result grade accordingly.

Expected Output:

Enter student's marks (out of 100): 55   // user input

Student's grade: C

Q9.

Write a program that takes two alphabets as input, then tells which alphabet comes first as per the alphabetical order.

Treat uppercase and lowercase letters the same, so that ‘a’ and ‘A’ are not considered different when comparing.

Expected Output:

// case 1
Enter first alphabet: q   // user input
Enter second alphabet: g   // user input

Output: g

// case 2
Enter first alphabet: b   // user input
Enter second alphabet: B   // user input

Output: both are equal

// case 3
Enter first alphabet: t   // user input
Enter second alphabet: Z   // user input

Output: t

Q10.

Write a program to calculate simple interest (i) for principal amount (p), rate of interest (r), number of years (n) given by user.

Expected Output:

Enter principal amount: 50000   // user input
Enter rate of interest: 5   // user input
Enter no of years: 2   // user input

Simple Interest: 5000

// i = (p*r*n)/100

Q11.

Write a program to check whether a given year is a leap year or not.

Expected Output:

Enter year: 2024   // user input

2024 is a leap year.

Q12.

In a digital calendar app, users enter meeting times in 24-hour format but you need to display them as 12-hour times with AM/PM. Write a program to convert a given 24-hour time to 12-hour format.

Expected Output:

Enter time in 24-hour format (HH MM): 14 30   // user input

12-hour format: 02:30 PM

Q13.

You’ve decided to buy apples in bulk and want to get the best deal. You call three different suppliers and ask for their per-kilogram price. Write a program that asks for the price per kilogram from three suppliers and then tells you what is the lowest price.

Expected Output:

Enter price from Supplier 1 (per kg):  42.50
Enter price from Supplier 2 (per kg):  40.75
Enter price from Supplier 3 (per kg):  41.20

Lowest apple price: 40.75 per kg

Q14. (Adv.)

On a remote island, treasure hunters find chests marked with numbers. Only the magic chests - those whose numbers can’t be split evenly into smaller piles, hold the rare gems. Write a program that asks the user for a chest number and then declares whether it’s a magic chest with a special treasure or just an ordinary chest.

Expected Output:

Enter chest number: 23   // user input

Magic chest! You've uncovered the hidden gem.

Prev Post
Strings
Next Post
References