Practice 100 Functions and Control Flow Coding Questions, TechnoVlogs

Practice 100 Functions and Control Flow Coding Questions


Q1. Write a Rust program to create and call a simple function that prints "Hello, Rust!".  
Input:  
None  
Expected Output:
Hello, Rust!

Q2. Write a Rust program to create a function that takes two integers as arguments and returns their sum.  
Input:
a = 5, b = 10  
Expected Output:
Sum: 15

Q3. Write a Rust program to calculate the factorial of a number using a recursive function.  
Input:
n = 5  
Expected Output:
Factorial: 120

Q4. Write a Rust program to check if a number is even or odd using a function.  
Input:
n = 7  
Expected Output:
Odd

Q5. Write a Rust program to implement a function that prints the larger of two numbers.  
Input:
a = 8, b = 12  
Expected Output:
Larger number: 12

Q6. Write a Rust program to demonstrate the use of the if statement to check if a number is positive, negative, or zero.  
Input:
n = -5  
Expected Output:
Negative

Q7. Write a Rust program to demonstrate a for loop that prints numbers from 1 to 10.  
Input:  
None  
Expected Output:
1 2 3 4 5 6 7 8 9 10

Q8. Write a Rust program to demonstrate the use of a while loop to calculate the sum of numbers from 1 to 5.  
Input:  
None  
Expected Output:
Sum: 15

Q9. Write a Rust program to check if a character is a vowel or consonant using an if statement.  
Input:
char = 'a'  
Expected Output:
Vowel

Q10. Write a Rust program to demonstrate the use of a match statement to display the day of the week.  
Input:
day = 3  
Expected Output:
Wednesday

Q11. Write a Rust program to create a function that finds the maximum of three numbers.  
Input:
a = 3, b = 5, c = 1  
Expected Output:
Maximum: 5

Q12. Write a Rust program to use a for loop to print the elements of an array.  
Input:
Array: [2, 4, 6, 8]  
Expected Output:
2 4 6 8

Q13. Write a Rust program to demonstrate a break statement within a loop.  
Input:  
None  
Expected Output:
Loop terminated at count = 3

Q14. Write a Rust program to create a function that calculates the square of a number.  
Input:
n = 4  
Expected Output:
Square: 16

Q15. Write a Rust program to use a match statement to check the grade of a student based on marks.  
Input:
marks = 85  
Expected Output:
Grade: A

Q16. Write a Rust program to demonstrate an infinite loop using loop and terminate it with a break statement.  
Input:  
None  
Expected Output:
Breaking the infinite loop

Q17. Write a Rust program to implement a function that reverses a string.  
Input:
String: "Rust"  
Expected Output:
Reversed: "tsuR"

Q18. Write a Rust program to use the continue statement to skip printing even numbers in a loop.  
Input:  
None  
Expected Output:
Odd numbers: 1 3 5 7 9

Q19. Write a Rust program to create a function that returns the absolute value of a number.  
Input:
n = -7  
Expected Output:
Absolute value: 7

Q20. Write a Rust program to check if a number is divisible by both 3 and 5 using a function.  
Input:
n = 15  
Expected Output:
Divisible by 3 and 5

Q21. Write a Rust program to demonstrate the use of a default parameter in a function.  
Input:
Value provided: None  
Expected Output:
Default value: 10

Q22. Write a Rust program to use a for loop to calculate the sum of an array's elements.  
Input:
Array: [1, 2, 3, 4, 5]  
Expected Output:
Sum: 15

Q23. Write a Rust program to create a function that calculates the power of a number.  
Input:
Base = 2, Exponent = 3  
Expected Output:
Result: 8

Q24. Write a Rust program to demonstrate nested if statements to check the range of a number.  
Input:
n = 15  
Expected Output:
The number is between 10 and 20

Q25. Write a Rust program to use a match statement to check the type of a variable.  
Input:
Variable: "Rustacean"  
Expected Output:
It is a string

Q26. Write a Rust program to demonstrate a countdown from 10 to 1 using a while loop.  
Input:  
None  
Expected Output:
10 9 8 7 6 5 4 3 2 1

Q27. Write a Rust program to implement a function that calculates the greatest common divisor (GCD) of two numbers.  
Input:
a = 24, b = 36  
Expected Output:
GCD: 12

Q28. Write a Rust program to create a function that checks if a string is a palindrome.  
Input:
String: "madam"  
Expected Output:
Palindrome: true

Q29. Write a Rust program to use a for loop to print all even numbers from 1 to 20.  
Input:  
None  
Expected Output:
Even numbers: 2 4 6 8 10 12 14 16 18 20

Q30. Write a Rust program to use the match statement to implement a simple calculator.  
Input:
Operation: '+', a = 5, b = 3  
Expected Output:
Result: 8

Q31. Write a Rust program to create a function that checks if a year is a leap year.  
Input:
Year: 2024  
Expected Output:
Leap year: true

Q32. Write a Rust program to demonstrate early return from a function using the return keyword.  
Input:
Value: 0  
Expected Output:
Returned early with value: 0

Q33. Write a Rust program to create a function that returns the smaller of two numbers.  
Input:
a = 12, b = 9  
Expected Output:
Smaller number: 9

Q34. Write a Rust program to use a while loop to print the Fibonacci sequence up to 10 terms.  
Input:  
None  
Expected Output:
Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34

Q35. Write a Rust program to create a function that reverses an array.  
Input:
Array: [1, 2, 3, 4, 5]  
Expected Output:
Reversed array: [5, 4, 3, 2, 1]

Q36. Write a Rust program to demonstrate the use of an else if chain.  
Input:
Score = 85  
Expected Output:
Grade: B

Q37. Write a Rust program to create a function that calculates the sum of digits of a number.  
Input:
Number: 1234  
Expected Output:
Sum of digits: 10

Q38. Write a Rust program to use a for loop to iterate over a range of numbers.  
Input:
Range: 1..6  
Expected Output:
1 2 3 4 5

Q39. Write a Rust program to create a function that counts the number of vowels in a string.  
Input:
String: "Rustacean"  
Expected Output:
Number of vowels: 4

Q40. Write a Rust program to use a match statement to classify numbers as positive, negative, or zero.  
Input:
Number: -7  
Expected Output:
Negative

Q41. Write a Rust program to implement a function that multiplies all elements in an array.  
Input:
Array: [2, 3, 4]  
Expected Output:
Product: 24

Q42. Write a Rust program to demonstrate a match statement to handle multiple cases for a character input.  
Input:
Character: 'a'  
Expected Output:
It's a vowel

Q43. Write a Rust program to create a function that returns the number of elements greater than a given value in an array.  
Input:
Array: [1, 3, 7, 10], Threshold: 5  
Expected Output:
Count: 2

Q44. Write a Rust program to use nested for loops to print a multiplication table for numbers 1 through 5.  
Input:  
None  
Expected Output:
1 2 3 4 5  
2 4 6 8 10  
3 6 9 12 15  
4 8 12 16 20  
5 10 15 20 25

Q45. Write a Rust program to demonstrate a simple function that swaps the values of two variables.  
Input:
a = 3, b = 8  
Expected Output:
Swapped: a = 8, b = 3

Q46. Write a Rust program to use a while loop to count the number of digits in a number.  
Input:
Number: 12345  
Expected Output:
Number of digits: 5

Q47. Write a Rust program to create a function that calculates the average of numbers in a vector.  
Input:
Vector: [10, 20, 30]  
Expected Output:
Average: 20.0

Q48. Write a Rust program to demonstrate the use of a loop to sum the first N natural numbers.  
Input:
N = 5  
Expected Output:
Sum: 15

Q49. Write a Rust program to use a match statement with a range to classify age groups.  
Input:
Age: 25  
Expected Output:
Category: Adult

Q50. Write a Rust program to create a function that finds the second largest number in an array.  
Input:
Array: [10, 20, 15, 5]  
Expected Output:
Second largest: 15

Q51. Write a Rust program to demonstrate an if-else condition to determine if a number is a prime number.  
Input:
Number: 13  
Expected Output:
Prime: true

Q52. Write a Rust program to create a function that removes duplicates from a vector.  
Input:
Vector: [1, 2, 2, 3, 4, 4, 5]  
Expected Output:
Unique vector: [1, 2, 3, 4, 5]

Q53. Write a Rust program to use nested if statements to determine the largest number among three inputs.  
Input:
a = 3, b = 9, c = 5  
Expected Output:
Largest number: 9

Q54. Write a Rust program to demonstrate the use of a function that returns multiple values using a tuple.  
Input:
a = 10, b = 5  
Expected Output:
Sum: 15, Product: 50

Q55. Write a Rust program to use a match statement with an enum to display a message based on traffic light colors.  
Input:
Traffic light: Green  
Expected Output:
Go

Q56. Write a Rust program to create a function that finds the first occurrence of a value in an array.  
Input:
Array: [10, 20, 30, 40], Value: 30  
Expected Output:
Index: 2

Q57. Write a Rust program to use a while loop to reverse the digits of a number.  
Input:
Number: 1234  
Expected Output:
Reversed: 4321

Q58. Write a Rust program to create a function that checks if a string contains a substring.  
Input:
String: "Rust programming", Substring: "Rust"  
Expected Output:
Contains substring: true

Q59. Write a Rust program to demonstrate the use of an early exit from a for loop using a break statement.  
Input:
Stop at: 4  
Expected Output:
Loop exited at 4

Q60. Write a Rust program to use a match statement to handle results returned by a function.  
Input:
Value: Ok(5)  
Expected Output:
Value is 5

Q61. Write a Rust program to create a function that checks if a number is a power of two.  
Input:
Number: 16  
Expected Output:
Is power of two: true

Q62. Write a Rust program to use a for loop to calculate the factorial of a number.  
Input:
Number: 5  
Expected Output:
Factorial: 120

Q63. Write a Rust program to demonstrate the use of a recursive function to calculate the Fibonacci sequence.  
Input:
N = 6  
Expected Output:
Fibonacci: 8

Q64. Write a Rust program to create a function that calculates the area of a rectangle.  
Input:
Length = 10, Width = 5  
Expected Output:
Area: 50

Q65. Write a Rust program to use a while loop to find the smallest divisor of a number greater than 1.  
Input:
Number: 35  
Expected Output:
Smallest divisor: 5

Q66. Write a Rust program to demonstrate a function that returns the maximum value in a slice.  
Input:
Slice: [1, 4, 6, 8, 3]  
Expected Output:
Maximum: 8

Q67. Write a Rust program to create a function that concatenates two strings.  
Input:
String 1: "Hello", String 2: "World"  
Expected Output:
Concatenated: "HelloWorld"

Q68. Write a Rust program to demonstrate the use of a for loop to print the indices and values of an array.  
Input:
Array: [10, 20, 30]  
Expected Output:
Index 0: 10  
Index 1: 20  
Index 2: 30

Q69. Write a Rust program to create a function that counts the number of words in a string.  
Input:
String: "Rust is fun"  
Expected Output:
Word count: 3

Q70. Write a Rust program to demonstrate a simple calculator using if-else statements.  
Input:
Operation: '/', a = 20, b = 4  
Expected Output:
Result: 5

Q71. Write a Rust program to create a function that calculates the nth power of a number using a loop.  
Input:
Base = 2, Exponent = 4  
Expected Output:
Result: 16

Q72. Write a Rust program to demonstrate a function that takes a slice and reverses its elements in-place.  
Input:
Slice: [1, 2, 3, 4]  
Expected Output:
Reversed slice: [4, 3, 2, 1]

Q73. Write a Rust program to use a for loop to print the ASCII values of characters in a string.  
Input:
String: "ABC"  
Expected Output:
65 66 67

Q74. Write a Rust program to create a function that checks if a vector is sorted in ascending order.  
Input:
Vector: [1, 2, 3, 4, 5]  
Expected Output:
Is sorted: true

Q75. Write a Rust program to demonstrate the use of a match statement to handle different error codes.  
Input:
Error code: 404  
Expected Output:
Not Found

Q76. Write a Rust program to use a while loop to compute the greatest common divisor (GCD) of two numbers.  
Input:
a = 56, b = 98  
Expected Output:
GCD: 14

Q77. Write a Rust program to create a function that checks if two strings are anagrams.  
Input:
String 1: "listen", String 2: "silent"  
Expected Output:
Are anagrams: true

Q78. Write a Rust program to demonstrate a function that calculates the sum of squares of elements in an array.  
Input:
Array: [1, 2, 3]  
Expected Output:
Sum of squares: 14

Q79. Write a Rust program to use a for loop to print the elements of a 2D array.  
Input:
2D Array: [[1, 2], [3, 4]]  
Expected Output:
1 2  
3 4

Q80. Write a Rust program to create a function that calculates the median of a vector.  
Input:
Vector: [5, 3, 8, 1, 9]  
Expected Output:
Median: 5

Q81. Write a Rust program to use a recursive function to find the factorial of a number.  
Input:
Number: 4  
Expected Output:
Factorial: 24

Q82. Write a Rust program to create a function that converts a temperature from Celsius to Fahrenheit.  
Input:
Celsius: 25  
Expected Output:
Fahrenheit: 77.0

Q83. Write a Rust program to demonstrate the use of a for loop to iterate over the characters in a string.  
Input:
String: "Rust"  
Expected Output:
R  
u  
s  
t

Q84. Write a Rust program to create a function that calculates the sum of all odd numbers in a vector.  
Input:
Vector: [1, 2, 3, 4, 5]  
Expected Output:
Sum of odd numbers: 9

Q85. Write a Rust program to use a while loop to calculate the sum of the digits of a number until the sum becomes a single digit.  
Input:
Number: 987  
Expected Output:
Final sum: 6

Q86. Write a Rust program to create a function that counts the frequency of each character in a string.  
Input:
String: "hello"  
Expected Output:
h: 1, e: 1, l: 2, o: 1

Q87. Write a Rust program to demonstrate a function that returns the last element of an array.  
Input:
Array: [10, 20, 30, 40]  
Expected Output:
Last element: 40

Q88. Write a Rust program to use a for loop to print all the multiples of 3 from 1 to 30.  
Input:  
None  
Expected Output:
3 6 9 12 15 18 21 24 27 30

Q89. Write a Rust program to create a function that determines if a given string is a valid palindrome, ignoring spaces.  
Input:
String: "nurses run"  
Expected Output:
Is palindrome: true

Q90. Write a Rust program to demonstrate a function that takes two numbers and returns their greatest common divisor (GCD) and least common multiple (LCM).  
Input:
a = 12, b = 15  
Expected Output:
GCD: 3, LCM: 60

Q91. Write a Rust program to use a match statement to categorize a number as "small", "medium", or "large".  
Input:
Number: 75  
Expected Output:
Category: Large

Q92. Write a Rust program to create a function that calculates the product of all elements in a vector.  
Input:
Vector: [1, 2, 3, 4]  
Expected Output:
Product: 24

Q93. Write a Rust program to demonstrate a loop that continues until a random number between 1 and 10 equals 5.  
Input:  
Randomly generated  
Expected Output:
Generated: 8  
Generated: 2  
Generated: 5  
Stopped at 5

Q94. Write a Rust program to create a function that returns the number of uppercase and lowercase characters in a string.  
Input:
String: "RustLang"  
Expected Output:
Uppercase: 2, Lowercase: 6

Q95. Write a Rust program to demonstrate the use of an early exit from a recursive function using return.  
Input:
Limit = 3  
Expected Output:
Function stopped at recursion level 3

Q96. Write a Rust program to create a function that determines if a vector contains duplicates.  
Input:
Vector: [1, 2, 3, 4, 1]  
Expected Output:
Contains duplicates: true

Q97. Write a Rust program to use a for loop to print the reverse of a string.  
Input:
String: "Rust"  
Expected Output:
tsuR

Q98. Write a Rust program to create a function that calculates the harmonic mean of a vector of numbers.  
Input:
Vector: [1, 2, 3]  
Expected Output:
Harmonic mean: 1.636

Q99. Write a Rust program to demonstrate a while loop to repeatedly divide a number by 2 until it is less than 1.  
Input:
Number: 16  
Expected Output:
16  
8  
4  
2  
1

Q100. Write a Rust program to create a function that finds the longest word in a string.  
Input:
String: "Rust is amazing"  
Expected Output:
Longest word: amazing

Share on Social Media