
Practice 100 Rust Basic Coding Question Exercises
Q1. Write a Rust program to print "Hello, World!".
Input: None
Expected Output:
Hello, World!
Q2. Write a Rust program to take a number as input and print it.
Input:
42
Expected Output:
You entered: 42
Q3. Write a Rust program to find the sum of two numbers.
Input:
5 10
Expected Output:
Sum: 15
Q4. Write a Rust program to check if a number is even or odd.
Input:
7
Expected Output:
7 is odd
Q5. Write a Rust program to find the square of a number.
Input:
4
Expected Output:
Square: 16
Q6. Write a Rust program to calculate the factorial of a number.
Input:
5
Expected Output:
Factorial: 120
Q7. Write a Rust program to find the largest of three numbers.
Input:
3 9 7
Expected Output:
Largest: 9
Q8. Write a Rust program to check if a number is positive, negative, or zero.
Input:
-5
QExpected Output:
The number is negative
Q9. Write a Rust program to swap two numbers without using a temporary variable.
Input:
a = 3, b = 7
Expected Output:
After swap: a = 7, b = 3
Q10. Write a Rust program to find the GCD of two numbers.
Input:
12 15
Expected Output:
GCD: 3
Q11. Write a Rust program to find the LCM of two numbers.
Input:
4 5
Expected Output:
LCM: 20
Q12. Write a Rust program to check if a number is prime.
Input:
7
Expected Output:
7 is a prime number
Q13. Write a Rust program to generate the first n Fibonacci numbers.
Input:
n = 5
Expected Output:
0 1 1 2 3
Q14. Write a Rust program to reverse a given string.
Input:
hello
Expected Output:
olleh
Q15. Write a Rust program to check if a string is a palindrome.
Input:
madam
Expected Output:
madam is a palindrome
Q16. Write a Rust program to calculate the sum of digits of a number.
Input:
1234
Expected Output:
Sum of digits: 10
Q17. Write a Rust program to count the number of vowels in a string.
Input:
education
Expected Output:
Number of vowels: 5
Q18. Write a Rust program to find the length of a string.
Input:
rustacean
Expected Output:
Length of the string: 9
Q19. Write a Rust program to find the largest element in an array.
Input:
[3, 7, 2, 9, 5]
Expected Output:
Largest element: 9
Q20. Write a Rust program to sort an array in ascending order.
Input:
[4, 2, 8, 1]
Expected Output:
Sorted array: [1, 2, 4, 8]
Q21. Write a Rust program to calculate the sum of elements in an array.
Input:
[1, 2, 3, 4]
Expected Output:
Sum: 10
Q22. Write a Rust program to count the occurrences of a specific element in an array.
Input:
Array: [1, 2, 2, 3, 4], Element: 2
Expected Output:
Occurrences of 2: 2
Q23. Write a Rust program to check if an element exists in an array.
Input:
Array: [1, 3, 5, 7], Element: 3
Expected Output:
3 exists in the array
Q24. Write a Rust program to create a 2D array and print its elements.
Input:
[[1, 2], [3, 4]]
Expected Output:
1 2
3 4
Q25. Write a Rust program to find the transpose of a matrix.
Input:
Matrix: [[1, 2], [3, 4]]
Expected Output:
Transpose: [[1, 3], [2, 4]]
Q26. Write a Rust program to calculate the average of numbers in an array.
Input:
[10, 20, 30, 40]
Expected Output:
Average: 25
Q27. Write a Rust program to check if a number is an Armstrong number.
Input:
153
Expected Output:
153 is an Armstrong number
Q28. Write a Rust program to convert a temperature from Celsius to Fahrenheit.
Input:
Celsius: 25
Expected Output:
Fahrenheit: 77
Q29. Write a Rust program to find the sum of all even numbers between 1 and n.
Input:
n = 10
Expected Output:
Sum of even numbers: 30
Q30. Write a Rust program to check if a character is a vowel or consonant.
Input:
e
Expected Output:
e is a vowel
Q31. Write a Rust program to calculate the power of a number using a loop.
Input:
Base: 2, Exponent: 3
Expected Output:
Result: 8
Q32. Write a Rust program to print all prime numbers between 1 and n.
Input:
n = 10
Expected Output:
Prime numbers: 2, 3, 5, 7
Q33. Write a Rust program to check if a year is a leap year.
Input:
2024
Expected Output:
2024 is a leap year
Q34. Write a Rust program to print a multiplication table for a given number.
Input:
Number: 3
Expected Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
3 x 10 = 30
Q35. Write a Rust program to calculate the sum of the first n natural numbers.
Input:
n = 5
Expected Output:
Sum: 15
Q36. Write a Rust program to count the frequency of characters in a string.
Input:
hello
Expected Output:
h: 1, e: 1, l: 2, o: 1
Q37. Write a Rust program to merge two arrays.
Input:
Array1: [1, 2], Array2: [3, 4]
Expected Output:
Merged array: [1, 2, 3, 4]
Q38. Write a Rust program to remove duplicates from an array.
Input:
[1, 2, 2, 3, 4, 4]
Expected Output:
Array without duplicates: [1, 2, 3, 4]
Q39. Write a Rust program to find the second largest element in an array.
Input:
[5, 1, 8, 7]
Expected Output:
Second largest: 7
Q40. Write a Rust program to print the reverse of an array.
Input:
[1, 2, 3, 4]
Expected Output:
Reversed array: [4, 3, 2, 1]
Q41. Write a Rust program to convert a string to uppercase.
Input:
hello
Expected Output:
HELLO
Q42. Write a Rust program to count the number of words in a string.
Input:
Rust is amazing
Expected Output:
Number of words: 3
Q43. Write a Rust program to find the minimum and maximum elements in an array.
Input:
[10, 2, 8, 4]
Expected Output:
Minimum: 2, Maximum: 10
Q44. Write a Rust program to convert a binary number to decimal.
Input:
Binary: 1010
Expected Output:
Decimal: 10
Q45. Write a Rust program to convert a decimal number to binary.
Input:
Decimal: 5
Expected Output:
Binary: 101
Q46. Write a Rust program to check if a string contains only numeric characters.
Input:
12345
Expected Output:
String contains only numeric characters
Q47. Write a Rust program to concatenate two strings.
Input:
String1: "Rust", String2: "Lang"
Expected Output:
Concatenated string: RustLang
Q48. Write a Rust program to generate a random number between 1 and 100.
Input: None
Expected Output:
Random number: 42 (or any number between 1 and 100)
Q49. Write a Rust program to print the ASCII value of a character.
Input:
A
Expected Output:
ASCII value: 65
Q50. Write a Rust program to check if two strings are anagrams.
Input:
String1: "listen", String2: "silent"
Expected Output:
The strings are anagrams
Q51. Write a Rust program to print the first n even numbers.
Input:
n = 5
Expected Output:
2, 4, 6, 8, 10
Q52. Write a Rust program to check if a number is a perfect square.
Input:
16
Expected Output:
16 is a perfect square
Q53. Write a Rust program to find the product of elements in an array.
Input:
[2, 3, 4]
Expected Output:
Product: 24
Q54. Write a Rust program to find the sum of all odd numbers between 1 and n.
Input:
n = 10
Expected Output:
Sum of odd numbers: 25
Q55. Write a Rust program to print all factors of a number.
Input:
12
Expected Output:
Factors: 1, 2, 3, 4, 6, 12
Q56. Write a Rust program to check if a string starts with a specific prefix.
Input:
String: "Rustacean", Prefix: "Rust"
Expected Output:
String starts with the prefix
Q57. Write a Rust program to check if a string ends with a specific suffix.
Input:
String: "programming", Suffix: "ing"
Expected Output:
String ends with the suffix
Q58. Write a Rust program to find the length of each word in a string.
Input:
"Rust is fun"
Expected Output:
Rust: 4, is: 2, fun: 3
Q59. Write a Rust program to calculate the area of a circle.
Input:
Radius: 5
Expected Output:
Area: 78.54
Q60. Write a Rust program to check if a number is a palindrome.
Input:
121
Expected Output:
121 is a palindrome
Q61. Write a Rust program to find the median of an array.
Input:
[1, 3, 4, 2]
Expected Output:
Median: 2.5
Q62. Write a Rust program to check if two arrays are equal.
Input:
Array1: [1, 2, 3], Array2: [1, 2, 3]
Expected Output:
Arrays are equal
Q63. Write a Rust program to find the mode of an array.
Input:
[1, 2, 2, 3]
Expected Output:
Mode: 2
Q64. Write a Rust program to remove all vowels from a string.
Input:
Rustacean
Expected Output:
String without vowels: Rstcn
Q65. Write a Rust program to replace all spaces in a string with underscores.
Input:
Rust is amazing
Expected Output:
Rust_is_amazing
Q66. Write a Rust program to find the longest word in a sentence.
Input:
Rust is fun and amazing
Expected Output:
Longest word: amazing
Q67. Write a Rust program to find the smallest positive number missing from an array.
Input:
[1, 2, 0, 3]
Expected Output:
Smallest missing positive number: 4
Q68. Write a Rust program to calculate the dot product of two vectors.
Input:
Vector1: [1, 2, 3], Vector2: [4, 5, 6]
Expected Output:
Dot product: 32
Q69. Write a Rust program to find the HCF and LCM of two numbers.
Input:
12 18
Expected Output:
HCF: 6, LCM: 36
Q70. Write a Rust program to rotate an array n times to the right.
Input:
Array: [1, 2, 3, 4], n = 2
Expected Output:
Rotated array: [3, 4, 1, 2]
Q71. Write a Rust program to convert a character to uppercase.
Input:
a
Expected Output:
Uppercase: A
Q72. Write a Rust program to generate the first n terms of a geometric sequence.
Input:
Start: 2, Ratio: 3, Terms: 4
Expected Output:
2, 6, 18, 54
Q73. Write a Rust program to find the frequency of each word in a sentence.
Input:
"Rust is fun and Rust is fast"
Expected Output:
Rust: 2, is: 2, fun: 1, and: 1, fast: 1
Q74. Write a Rust program to calculate the nth triangular number.
Input:
n = 5
Expected Output:
Triangular number: 15
Q75. Write a Rust program to count the number of digits in a number.
Input:
12345
Expected Output:
Number of digits: 5
Q76. Write a Rust program to find the greatest common divisor (GCD) using recursion.
Input:
12 15
Expected Output:
GCD: 3
Q77. Write a Rust program to count the number of uppercase and lowercase letters in a string.
Input:
RustLang
Expected Output:
Uppercase: 2, Lowercase: 6
Q78. Write a Rust program to find the maximum difference between any two elements in an array.
Input:
[10, 3, 5, 20]
Expected Output:
Maximum difference: 17
Q79. Write a Rust program to calculate the sum of the squares of the first n natural numbers.
Input:
n = 3
Expected Output:
Sum of squares: 14
Q80. Write a Rust program to reverse the words in a sentence.
Input:
Rust is fun
Expected Output:
fun is Rust
Q81. Write a Rust program to find all substrings of a string.
Input:
abc
Expected Output:
a, ab, abc, b, bc, c
Q82. Write a Rust program to calculate the sum of diagonal elements in a square matrix.
Input:
Matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Expected Output:
Sum of diagonals: 15
Q83. Write a Rust program to find the first non-repeating character in a string.
Input:
swiss
Expected Output:
First non-repeating character: w
Q84. Write a Rust program to calculate the sum of the elements above the main diagonal of a matrix.
Input:
Matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Expected Output:
Sum above main diagonal: 11
Q85. Write a Rust program to find the union and intersection of two arrays.
Input:
Array1: [1, 2, 3], Array2: [2, 3, 4]
Expected Output:
Union: [1, 2, 3, 4], Intersection: [2, 3]
Q86. Write a Rust program to print a pyramid pattern of stars.
Input:
Rows: 4
Expected Output:
*
***
*****
*******
Q87. Write a Rust program to check if a number is a perfect number.
Input:
28
Expected Output:
28 is a perfect number
Q88. Write a Rust program to convert a number from decimal to hexadecimal.
Input:
255
Expected Output:
Hexadecimal: FF
Q89. Write a Rust program to calculate the sum of the cubes of the first n natural numbers.
Input:
n = 3
Expected Output:
Sum of cubes: 36
Q90. Write a Rust program to count the number of trailing zeros in a factorial of a number.
Input:
5!
Expected Output:
Trailing zeros: 1
Q91. Write a Rust program to check if a string is a subsequence of another string.
Input:
String1: "ace", String2: "abcde"
Expected Output:
String1 is a subsequence of String2
Q92. Write a Rust program to generate all permutations of a string.
Input:
abc
Expected Output:
abc, acb, bac, bca, cab, cba
Q93. Write a Rust program to print a diamond pattern of stars.
Input:
Rows: 3
Expected Output:
*
***
*****
***
*
Q94. Write a Rust program to remove all non-alphanumeric characters from a string.
Input:
He@llo!123
Expected Output:
Hello123
Q95. Write a Rust program to check if an array is sorted in ascending order.
Input:
[1, 2, 3, 4]
Expected Output:
Array is sorted
Q96. Write a Rust program to count the number of unique elements in an array.
Input:
[1, 2, 2, 3, 4, 4]
Expected Output:
Number of unique elements: 4
Q97. Write a Rust program to calculate the determinant of a 2x2 matrix.
Input:
Matrix: [[1, 2], [3, 4]]
Expected Output:
Determinant: -2
Q98. Write a Rust program to find the common elements in three arrays.
Input:
Array1: [1, 2, 3], Array2: [2, 3, 4], Array3: [3, 4, 5]
Expected Output:
Common elements: [3]
Q99. Write a Rust program to print a zigzag pattern of stars.
Input:
Rows: 3, Columns: 5
Expected Output:
* *
* *
*
Q100. Write a Rust program to check if a matrix is symmetric.
Input:
Matrix: [[1, 2, 3], [2, 5, 6], [3, 6, 9]]
Expected Output:
Matrix is symmetric