Introduction To Loop
Introduction To Loop
Loops are used in programming to repeat a block of code until a specific condition is met. There are three loops in C
programming:
1. for loop
2. while loop
3. do...while loop
for Loop
The syntax of for loop is:
4. for (initializationStatement; testExpression; updateStatement)
5. {
6. // codes
7. }
Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression
is
True (nonzero), codes inside the body of for loop is executed and the update expression is updated.
The for loop is commonly used when the number of iterations is known.
While loop
while (testExpression)
//codes
}
where, testExpression checks the condition is true or false before each loop.
If the test expression is true (nonzero), codes inside the body of while loop are exectued. The test expression is evaluated again.
The process goes on until the test expression is false.
do...while loop
The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed once,
before checking the test expression. Hence, the do...while loop is executed at least once.
do
// codes
while (testExpression);
How do...while loop works?
The code block (loop body) inside the braces is executed once.
Then, the test expression is evaluated. If the test expression is true, the loop body is executed again. This process goes on until
the test expression is evaluated to 0 (false).
When the test expression is false (nonzero), the do...while loop is terminated.
break Statement
The break statement terminates the loop (for, while and do...while loop) immediately when it is encountered. Its syntax is:
break;
The break statement is almost always used with if...else statement inside the loop.
Continue Statement
The continue statement skips statements after it inside the loop. Its syntax is:
continue;
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the
code.
C switch...case Statement
The if..else..if ladder allows you to execute a block code among many alternatives. If you are checking on the value of a single
variable in if...else...if, it is better to use switch statement.
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to
understand.
Syntax of switch...case
switch (n)
case constant1:
break;
case constant2:
break;
……. .
default:
When a case constant is found that matches the switch expression, control of the program passes to the block of code associated
with that case.
Suppose, the value of n is equal to constant2. The compiler executes the statements after case constant2: until break is
encountered. When break statement is encountered, switch statement terminates.
Loop
1. Write a C program to print all natural numbers from 1 to n. - using while loop
2. Write a C program to print all natural numbers in reverse (from n to 1). - using while loop
3. Write a C program to print all alphabets from a to z. - using while loop
4. Write a C program to print all even numbers between 1 to 100. - using while loop
5. Write a C program to print all odd number between 1 to 100.
6. Write a C program to find sum of all natural numbers between 1 to n.
7. Write a C program to find sum of all even numbers between 1 to n.
8. Write a C program to find sum of all odd numbers between 1 to n.
9. Write a C program to print multiplication table of any number.
10. Write a C program to count number of digits in a number.
11. Write a C program to find first and last digit of a number.
12. Write a C program to find sum of first and last digit of a number.
13. Write a C program to swap first and last digits of a number.
14. Write a C program to calculate sum of digits of a number.
15. Write a C program to calculate product of digits of a number.
16. Write a C program to enter a number and print its reverse.
17. Write a C program to check whether a number is palindrome or not.
18. Write a C program to find frequency of each digit in a given integer.
19. Write a C program to enter a number and print it in words.
20. Write a C program to print all ASCII character with their values.
21. Write a C program to find power of a number using for loop.
22. Write a C program to find all factors of a number.
23. Write a C program to calculate factorial of a number.
24. Write a C program to find HCF (GCD) of two numbers.
25. Write a C program to find LCM of two numbers.
26. Write a C program to check whether a number is Prime number or not.
27. Write a C program to print all Prime numbers between 1 to n.
28. Write a C program to find sum of all prime numbers between 1 to n.
29. Write a C program to find all prime factors of a number.
30. Write a C program to check whether a number is Armstrong number or not.
31. Write a C program to print all Armstrong numbers between 1 to n.
32. Write a C program to check whether a number is Perfect number or not.
33. Write a C program to print all Perfect numbers between 1 to n.
34. Write a C program to check whether a number is Strong number or not.
35. Write a C program to print all Strong numbers between 1 to n.
36. Write a C program to print Fibonacci series up to n terms.
37. Write a C program to find one's complement of a binary number.
38. Write a C program to find two's complement of a binary number.
39. Write a C program to convert Binary to Octal number system.
40. Write a C program to convert Binary to Decimal number system.
41. Write a C program to convert Binary to Hexadecimal number system.
42. Write a C program to convert Octal to Binary number system.
43. Write a C program to convert Octal to Decimal number system.
44. Write a C program to convert Octal to Hexadecimal number system.
45. Write a C program to convert Decimal to Binary number system.
46. Write a C program to convert Decimal to Octal number system.
47. Write a C program to convert Decimal to Hexadecimal number system.
48. Write a C program to convert Hexadecimal to Binary number system.
49. Write a C program to convert Hexadecimal to Octal number system.
50. Write a C program to convert Hexadecimal to Decimal number system.
51. Write a C program to print Pascal triangle upto n rows.
52. Star pattern programs - Write a C program to print the given star patterns.
53. Number pattern programs - Write a C program to print the given number patterns.
Switch case
54. Write a C program to print day of week name using switch case.
55. Write a C program print total number of days in a month using switch case.
56. Write a C program to check whether an alphabet is vowel or consonant using switch case.
57. Write a C program to find maximum between two numbers using switch case.
58. Write a C program to check whether a number is even or odd using switch case.
59. Write a C program to check whether a number is positive, negative or zero using switch case.
60. Write a C program to find roots of a quadratic equation using switch case.
61. Write a C program to create Simple Calculator using switch case.
Conditional Operator
62. Write a C program to find maximum between two numbers using conditional operator.
63. Write a C program to find maximum between three numbers using conditional operator.
64. Write a C program to check whether a number is even or odd using conditional operator.
65. Write a C program to check whether year is leap year or not using conditional operator.
66. Write a C program to check whether character is an alphabet or not using conditional operator.
Bitwise
67. Write a C program to check Least Significant Bit (LSB) of a number is set or not.
68. Write a C program to check Most Significant Bit (MSB) of a number is set or not.
69. Write a C program to get nth bit of a number.
70. Write a C program to set nth bit of a number.
71. Write a C program to clear nth bit of a number.
72. Write a C program to toggle nth bit of a number.
73. Write a C program to get highest set bit of a number.
74. Write a C program to get lowest set bit of a number.
75. Write a C program to count trailing zeros in a binary number.
76. Write a C program to count leading zeros in a binary number.
77. Write a C program to flip bits of a binary number using bitwise operator.
78. Write a C program to count total zeros and ones in a binary number.
79. Write a C program to rotate bits of a given number.
80. Write a C program to convert decimal to binary number system using bitwise operator.
81. Write a C program to swap two numbers using bitwise operator.
82. Write a C program to check whether a number is even or odd using bitwise operator.
If else
104. Write a C program to read and print elements of array. - using recursion.
105. Write a C program to print all negative elements in an array.
106. Write a C program to find sum of all array elements. - using recursion.
107. Write a C program to find maximum and minimum element in an array. - using recursion.
108. Write a C program to find second largest element in an array.
109. Write a C program to count total number of even and odd elements in an array.
110. Write a C program to count total number of negative elements in an array.
111. Write a C program to copy all elements from an array to another array.
112. Write a C program to insert an element in an array.
113. Write a C program to delete an element from an array at specified position.
114. Write a C program to count frequency of each element in an array.
115. Write a C program to print all unique elements in the array.
116. Write a C program to count total number of duplicate elements in an array.
117. Write a C program to delete all duplicate elements from an array.
118. Write a C program to merge two array to third array.
119. Write a C program to find reverse of an array.
120. Write a C program to put even and odd elements of array in two separate array.
121. Write a C program to search an element in an array.
122. Write a C program to sort array elements in ascending or descending order.
123. Write a C program to sort even and odd elements of array separately.
124. Write a C program to left rotate an array.
125. Write a C program to right rotate an array.
Matrix
String
144. Write a C program to find length of a string.
145. Write a C program to copy one string to another string.
146. Write a C program to concatenate two strings.
147. Write a C program to compare two strings.
151. Write a C program to find total number of alphabets, digits or special character in a string.
152. Write a C program to count total number of vowels and consonants in a string.
153. Write a C program to count total number of words in a string.
154. Write a C program to find reverse of a string.
155. Write a C program to check whether a string is palindrome or not.
156. Write a C program to reverse order of words in a given string.
157. Write a C program to find first occurrence of a character in a given string.
158. Write a C program to find last occurrence of a character in a given string.
159. Write a C program to search all occurrences of a character in given string.
160. Write a C program to count occurrences of a character in given string.
161. Write a C program to find highest frequency character in a string.
162. Write a C program to find lowest frequency character in a string.
163. Write a C program to count frequency of each character in a string.
164. Write a C program to remove first occurrence of a character from string.
165. Write a C program to remove last occurrence of a character from string.
166. Write a C program to remove all occurrences of a character from string.
167. Write a C program to remove all repeated characters from a given string.
168. Write a C program to replace first occurrence of a character with another in a string.
169. Write a C program to replace last occurrence of a character with another in a string.
170. Write a C program to replace all occurrences of a character with another in a string.
171. Write a C program to find first occurrence of a word in a given string.
172. Write a C program to find last occurrence of a word in a given string.
173. Write a C program to search all occurrences of a word in given string.
174. Write a C program to count occurrences of a word in a given string.
175. Write a C program to remove first occurrence of a word from string.
176. Write a C program to remove last occurrence of a word in given string.
177. Write a C program to remove all occurrence of a word in given string.
178. Write a C program to trim leading white space characters from given string.
179. Write a C program to trim trailing white space characters from given string.
180. Write a C program to trim both leading and trailing white space characters from given string.
181. Write a C program to remove all extra blank spaces from given string.
187. Write a C program to find all prime numbers between given interval using functions.
188. Write a C program to print all strong numbers between given interval using functions.
189. Write a C program to print all Armstrong numbers between given interval using functions.
190. Write a C program to print all perfect numbers between given interval using functions.
pointer
206. Write a C program to create, initialize and use pointers.
207. Write a C program to add two numbers using pointers.
208. Write a C program to swap two numbers using pointers.
209. Write a C program to input and print array elements using pointer.
210. Write a C program to copy one array to another using pointers.
211. Write a C program to swap two arrays using pointers.
212. Write a C program to reverse an array using pointers.
213. Write a C program to search an element in array using pointers.
214. Write a C program to access two dimensional array using pointers.
215. Write a C program to add two matrix using pointers.
216. Write a C program to multiply two matrix using pointers.
217. Write a C program to find length of string using pointers.
218. Write a C program to copy one string to another using pointers.
219. Write a C program to concatenate two strings using pointers.
220. Write a C program to compare two strings using pointers.
221. Write a C program to find reverse of a string using pointers.
222. Write a C program to sort array using pointers.
223. Write a C program to return multiple values from function using pointers.