0% found this document useful (0 votes)
20 views5 pages

Programming Loop and Function Exercises

The document outlines a series of programming exercises categorized by loops (for, while, do-while, switch), functions, recursion, and additional core problems. Each category includes easy, medium, and slightly challenging questions that focus on fundamental programming concepts. The exercises aim to enhance problem-solving skills and understanding of programming logic.

Uploaded by

depotonion
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Programming Loop and Function Exercises

The document outlines a series of programming exercises categorized by loops (for, while, do-while, switch), functions, recursion, and additional core problems. Each category includes easy, medium, and slightly challenging questions that focus on fundamental programming concepts. The exercises aim to enhance problem-solving skills and understanding of programming logic.

Uploaded by

depotonion
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) FOR LOOP (5 Questions)

Easy (3):

1. Print numbers from 1 to 10 using a for loop.


2. Print even numbers between 1 and 20.
3. Print the sum of numbers from 1 to N.

Medium (1):

4. Print the multiplication table of a given number.

Slightly Challenging (1):

5. Find the factorial of a given number N using for loop.

2) WHILE LOOP (5 Questions)


Easy (3):

1. Print numbers from 10 down to 1.


2. Calculate the sum of digits of a given number.
3. Count the number of digits in a number.

Medium (1):

4. Reverse a given number (e.g., 123 → 321).

Slightly Challenging (1):

5. Check if a given number is a palindrome (reverse = original).

3) DO-WHILE LOOP (5 Questions)


Easy (3):

1. Print numbers from 1 to 5 using do-while.


2. Continuously read numbers and print them until the user enters 0.
3. Print the squares of numbers 1 to N.

Medium (1):

4. Find the sum of odd numbers between 1 and N.


Slightly Challenging (1):

5. Check if a number is an Armstrong number (like 153 = 1³+5³+3³).

4) SWITCH (5 Questions)
Easy (3):

1. Menu-driven calculator: add, subtract, multiply, divide.


2. Print the day of the week based on a number (1=Mon, 7=Sun).
3. Print the name of a month given its number (1=Jan, etc.).

Medium (1):

4. Menu to convert temperature: Celsius to Fahrenheit or vice versa.

Slightly Challenging (1):

5. Menu-driven program to calculate:


o Area of circle (given radius)
o Area of rectangle (given length, breadth)
o Area of triangle (given base, height)

1) Solid Rectangle
*****
*****
*****

(Input: rows=3, columns=5)

2) Right-Angled Triangle of Stars


*
**
***
****

(Input: n=4)

3) Inverted Right-Angled Triangle


****
***
**
*

(Input: n=4)

4) Right-Angled Triangle of Numbers


1
12
123
1234

(Input: n=4)

5) Inverted Number Triangle


1234
123
12
1

(Input: n=4)

6) Right-Angled Triangle of Same Numbers


1
22
333
4444

(Input: n=4)

7) Square of Numbers
111
222
333

(Input: n=3)

8) Pyramid of Stars (Centered)


*
***
*****

(Input: n=3)

9) Inverted Pyramid
*****
***
*

(Input: n=3)

Functions
Easy (Basic function usage):

1. Write a function to print your name (void function, no parameters).


2. Write a function that takes 2 numbers and prints their sum.
3. Write a function that takes 2 numbers and returns their product.

Medium (introduce return values & parameters):


4. Write a function to check whether a number is even or odd (returns true/false).
5. Write a function that calculates the square of a number and returns it.
6. Write a function that takes marks of a student and returns the grade (A/B/C/Fail).

Slightly Challenging (logic inside functions):


7. Write a function to find the largest among three numbers.
8. Write a function to calculate factorial (iterative).
9. Write a function to calculate simple interest given P, R, and T.
10. Write a menu-driven program using functions (for add, subtract, multiply, divide).

Recursion
Easy (clear base case):

1. Print numbers from 1 to N recursively.


2. Print numbers from N to 1 recursively.
3. Calculate factorial of N recursively.

Medium (logic + recursion):


4. Calculate the sum of the first N natural numbers recursively.
5. Find the Nth Fibonacci number using recursion.
6. Calculate the power of a number (x^n) recursively.

Slightly Challenging (but not too hard):


7. Find the greatest common divisor (GCD) using recursion (Euclid’s algorithm).
8. Count the digits of a number recursively.
9. Sum the digits of a number recursively.
10. Check if a number is a palindrome using recursion (reverse number recursively and
compare).

Additional Core Problems (29–39)


29. Develop a program that prints the multiplication table for a given number.
30. Create a program to sum all odd numbers from 1 to a specified number N.
31. Write a function that calculates the factorial of a given number.
32. Create a program that computes the sum of the digits of an integer.
33. Create a program to find the Least Common Multiple (LCM) of two numbers.
34. Create a program to find the Greatest Common Divisor (GCD) of two integers.
35. Create a program to check whether a given number is prime.
36. Create a program to reverse the digits of a number.
37. Create a program to print the Fibonacci series up to a certain number.
38. Create a program to check if a number is an Armstrong number.
39. Create a program to verify if a number is a palindrome.

You might also like