C Programming Practice Questions - Basics
"You cannot be great at the start, but you have to start to be great. So, making a beginning is important. In this list of questions, get your feet wet,before beginning a more arduous C journey"
— All the best
-
Problem 1: Determine the Range of Suitability for Exponential vs. Polynomial Time.
Task: Specify the range of $n$ for which Algorithm A ($3^n$) or Algorithm B ($n^4$) is more efficient.
-
Problem 4: Compare a Polynomial Function to a Log-Linear Function.
Task: Find the approximate value of $n$ where the function $10n \log_2 n$ first becomes larger than $n^{1.5}$.
-
Problem 5: Determine Which Function Grows Asymptotically Faster (Polynomial Simplification).
Task: Compare $n^2 + 5n$ and $\frac{n^3}{\sqrt{n}}$ to identify the function with the highest order of growth.
-
Problem 2: Analyze the Time Complexity ($O$) of a Nested Loop with Variable Bounds.
Task: Determine the Big $O$ notation for the following algorithm segment, where the inner loop iterates up to $i^2$.
count = 0 for i := 1 to n do for j := 1 to i * i do count = count + 1 end for end for
-
Problem 3: Calculate the Exact Frequency Counts for Simple and Logarithmic Loops.
Task: For the two program segments below, calculate how many times the innermost statement (`x := x + 1;` or similar) executes as a function of $n$.
Segment A:
for i := 1 to 2 * n do for j := i to n do x := x + 1; end for; end for;
Segment B:
i := 1; while i < n do x := x + 1; i := i * 2; end;
-
Problem 6: Sort a Set of Functions Based on Their Order of Growth.
Task: List the following functions from the fastest-growing (highest order) to the slowest-growing (lowest order), identifying any that share the same asymptotic class.
$$\sqrt{n} \quad n^2 \log n \quad 2^n \quad n! \quad \log (n^3) \quad n \quad 7n^3 - 10n^2 \quad (\log n)^2 \quad 1$$
-
Problem 7: Apply the Definitions of Big $O$ Notation ($f(n) = O(g(n))$ or $g(n) = O(f(n))$).
Task: For each pair of functions $f(n)$ and $g(n)$, determine whether $f(n) = O(g(n))$, $g(n) = O(f(n))$, or both.
(a) f(n) = n^2 + log n, g(n) = n^3 (b) f(n) = 2^n, g(n) = n^100 (c) f(n) = n \sqrt{n} + 5n, g(n) = n^2 (d) f(n) = n^2 / 5, g(n) = n^2 + \sqrt{n} (e) f(n) = n \log n, g(n) = \sqrt{n} \log n (f) f(n) = 100, g(n) = log n
- Write a program that prints the following direct statement using a single
print
function and only double quotes (" "). - Print a the following message using a single
print
function and only triple quotes (''' '''). - Print a the following message using a single
print
function and only double quotes (""). No loops has to be used. - Write a program that prints a name 5 times vertically, with one name per line. Don't use loops.
- Write a program that prints a name 5 times horizontally on a single line. Don't use loops.
- Write a program that prints the title
Elvin's book
using a singleprint
statement and single inverted commas (' '). - Print the following pattern using format function within the print function:
- Print the following pattern using format function within the print function:
- Print a short poem you like, ensuring each line is on a new line.
- Print a description of your dream vacation using both single and double quotes.
- Print a message that uses emojis or special characters (like ♥️ or 😊) to express an emotion or idea.
- Write a program that takes a temperature in Fahrenheit ($F$) as input from the user and converts it to Celsius ($C$)
- Declare a variable called
helpline_number
to store the national emergency number of your country and print the message: "In any emergency, contact [helpline_number]." - Declare a variable to store the name of a country and a second variable called
helpline_number
to store the national emergency number. Print the message: "In any emergency in [country name], contact [helpline_number]." - Declare a variable to store a first number and a second variable to store a second number. Declare a third variable to store their sum and print it.
- Declare a variable to store a first number and a second variable to store a second number. Declare four extra variables to store the sum, difference, product, and division of the two numbers and print them.
- Assign the value "25" to a variable called
age
and print the message: "I am 25 years old." - Assign the values "25" and "Engineering" to variables
age
andmajor
, respectively. Print the message: "I am 25 years old and studying Engineering." - Declare a variable
city
with the value "New York" and print the message: "I live in \'New York.\'" - Create three variables,
city
,state
, andcountry
, with the values "New York", "NY", and "USA", respectively. Print the message: "I live in New York, NY, USA." - Declare two variables,
price
anddiscount
, with the values "100" and "20", respectively. Then, calculate and print the discounted price (price - discount). - Create three variables,
num1
,num2
, andnum3
, with the values "10", "20", and "30", respectively. Calculate and print the average of the three numbers. - Declare a variable
x
with the value "5". Then, calculate and print the value ofx
squared (x^2
). - Assign the value "10" to a variable
y
. Then, calculate and print the value ofy
multiplied by 3. - Create a variable
z
with the value "2". Then, calculate and print the value ofz
cubed (z^3
). - Create three variables,
e
,f
, andg
, with the values "2", "3", and "4", respectively. Then, calculate and print the value ofe
multiplied byf
plusg
. - Write the C Language expression for the following:
1.
2.
3.
4.
5.
6.
7.
8.
9.
Algorithm Analysis Practice Problems
I. Comparison and Suitability (Growth Rates)
II. Loop Analysis and Frequency Counts
Point out the errors
Question | Statement | Error? (Yes/No) | Type of Error / Correction (Answer Slot) |
---|---|---|---|
C.1 | int area; radius = 5; area = 3.14 * radius * radius; |
||
C.2 | float volume = (4/3) * pi * r * r * r; |
||
C.3 | char initial = 'j'; |
||
C.4 | if (x = 10) { printf("X is ten"); } |
||
C.5 | int total_amt, char name[20]; |
What is this life if, full of care, we have no time to stand and stare. No time to stand beneath the boughs and stare as long as sheep or cows.
She said to me,"I want to learn Python."


John
, the output should look like this:
John John John John John
John
, the output should look like this:
John John John John John
'Elvin's book'
Programming Questions
Write a complete C program (including input and output statements) for each of the following problems.