Practice Question

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Practice Questions

1. Write a program that asks the user to enter a number and prints whether it is a prime
number or not using loops and conditionals.

2. Create a C++ program to print the sum of all odd numbers between 1 to 100 using loops
and conditionals.

3. Write a program that prompts the user to enter a number and then prints the multiplication
table for that number up to 10 using loops and conditionals.

4. Develop a program that asks the user to input a number and prints whether it is a
palindrome or not using loops and conditionals.

5. Write a C++ program to find the factorial of a number entered by the user. Use loops and
conditionals.

6. Create a program that prints the Fibonacci series up to a given number 'n' entered by the
user using loops and conditionals.

7. Write a C++ program to print the following pattern using loops and conditionals:

*
**
***
****
*****
8. Create a program that takes a number from the user and prints the reverse of that number
using loops and conditionals.
e.g
if your were given number 234
print 432

9. Write a C++ program to check whether a given year is a leap year or not using loops and
conditionals.
10. Write a C++ program to print the following pattern using loops and conditionals:

1
12
123
1234
12345
11. Write a C++ program to print the following pattern using loops and conditionals:

12345
1234
123
12
1
12. Given two sorted arrays nums1 and nums2 of size m and n respectively, calculate the
median of the two sorted arrays.
13. Given an array of integers nums and an integer target, Calculate indices of the two
numbers such that they add up to target.
14. Given an integer array nums sorted in non-decreasing order, remove the duplicates in-
place such that each unique element appears only once. The relative order of the elements
should be kept the same. Then tell the number of unique elements in nums.
15. Given an array nums of distinct integers, return all the possible permutations. You can
return the answer in any order.

Example 1:

Input: nums = [1,2,3]


Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Example 2:

Input: nums = [0,1]


Output: [[0,1],[1,0]]

Example 3:

Input: nums = [1]


Output: [[1]]

Example 3:

Input: nums = [1]


Output: [[1]]
16. You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and
two integers m and n, representing the number of elements in nums1 and nums2
respectively.
17. Write a C++ program to find the largest element in an array of integers.
18. Create a program that asks the user to input elements into an array and then prints the sum
of all the elements.
19. Write a C++ program to find the second largest element in an array of integers.
20. Develop a program that asks the user to input elements into an array and then prints the
array in reverse order.
21. Write a program to find the sum of all the even numbers in an array of integers.
22. Create a C++ program to merge two arrays of integers into a third array.
23. Write a program to count the number of occurrences of a specific element in an array.
24. Develop a program that finds the smallest element in an array of integers.
25. Write a C++ program to sort elements of an array in ascending order.

You might also like