This article will show some typical loop questions in C language. We use loops to iterate over the same code block until the specified condition is met. The loop questions in C provided in this article will help you to excel in your interviews. We will begin by learning briefly about the loops in C and then look at some loop questions in C.
Loops in C
There are three loops in C programming, which are as follows:
while loop.
for loop.
do…while loop.
Let us learn about them one by one in greater detail. It will help you solve loop questions in C.
for (initializing; test condition; update statement) {
// Loop body
}
In the for loop, the first step is initializing the looping variable. After that, the condition (test expression) is checked. If it fails, the loop halts. If the condition holds true, statements inside the loop body get executed, and the updates to the value of the looping variables are made according to the specified update statement.
Look at the flowchart that follows to understand how the for loop functions.
Flowchart of for Loop in C
This image shows the flow chart of the for loop in C.
Let us now move forward to learn about the while loop in C.
While Loop in C
Following is the syntax of the while loop:
while (test condition) {
// The loop body
}
The while loop examines the test statement contained within the parentheses. The code contained in the while loop body executes if the test condition evaluates to true. After the execution of the code, the test condition is rechecked. The loop keeps going until the test condition is no longer valid. When the test condition fails, the loop is halted.
Look at the following flowchart to visualize how the while loop works.
Flowchart of while loop in C
The next image shows the flow of control in the while loop.
Now let us discuss the do-while loop in C.
Do while Loop in C
Below is the do-while loop’s syntax in C:
do{
// Body of the loop
}
while(test condition);
In the do-while loop, the code inside the loop executes first, and then the test condition is checked. While the test condition is true, the body runs repeatedly. It continues until the test condition fails. When it becomes false, the loop stops its execution and the control moves to the following statement.
There is always at least one execution of the do-while loop.
Flowchart of do while Loop in C
The following flowchart can help you visualize the flow of the do-while loop.
Now that we are familiar with loops in the C language, let us look at loop questions in C.
Top Loop Questions in C
Following are the top loop questions in C that candidates generally encounter in interviews.
Q1. Write a program in C language for printing all the odd numbers within a given range using the while loop.
Implementation in C
#include<stdio.h>
int main(){
int m=0, n=0;
// Taking range as input
printf("Enter the range: ");
scanf("%d", &m);
scanf("%d", &n);
// Using while loop
while(m<=n){
// Condition to print odd numbers
if(m%2 != 0){
printf("%d ", m);
}
m++;
}
return 0;
}
Q2. Write a program to print even numbers within a range in C language using the while loop.
Implementation in C
#include<stdio.h>
int main(){
int m=0, n=0;
// Taking range as input
printf("Enter the range: ");
scanf("%d", &m);
scanf("%d", &n);
// Using while loop
while(m<=n){
// Condition to print even numbers
if(m%2 == 0){
printf("%d ", m);
}
m++;
}
return 0;
}
Enter the range : 10 30
10 12 14 16 18 20 22 24 26 28 30
Q3. Print all the numbers within a range.
Implementation in C
#include<stdio.h>
int main(){
int m=0, n=0;
// Taking range as input
printf("Enter the range: ");
scanf("%d", &m);
scanf("%d", &n);
// Using while loop
while(m<=n){
// printing numbers within a range
printf("%d ", m);
m++;
}
return 0;
}
Q4. Write a program in C language to print the addition of numbers within a range.
Implementation in C
#include<stdio.h>
int main(){
int m=0, n=0, sum=0;
// Taking range as input
printf("Enter the range: ");
scanf("%d", &m);
scanf("%d", &n);
// Using while loop
while(m<=n){
// calculating sum of numbers within a range
sum = sum+m;
m++;
}
// Printing the sum
printf("sum = %d ", sum);
return 0;
}
Q5. Write a program in C language to print the factorial of a number.
Implementation in C
#include<stdio.h>
int main(){
int m=0, n=0, factorial=1;
// Taking range as input
printf("Enter the number to calculate factorial: ");
scanf("%d", &m);
// Using while loop
while(m>0){
// Factorial of numbers within a range
factorial = factorial*m;
m--;
}
// Printing the sum
printf("Factorial = %d ", factorial);
return 0;
}
a b c d e f g h i j k l m n o p q r s t u v w x y z
Q8. Write a program in C language to print the multiplication table of an integer.
Implementation in C
#include<stdio.h>
int main(){
// Reading an integer to print multiplication table
int num=0;
printf("Enter a number to print multiplication table: ");
scanf("%d", &num);
// Using for loop to print the table.
for(int i = 1; i<=10; i++){
printf("%d * %d = %d \n", num, i, num*i);
}
return 0;
}
Enter a number to print multiplication table : 48
48*1= 48
48*2= 96
48*3= 144
48*4= 192
48*5= 240
48*6= 288
48*7= 336
48*8= 384
48*9= 432
48*10= 480
Q9. Write a program to print the square roots of all the numbers within a range.
Implementation in C
#include<stdio.h>
#include<math.h>
int main(){
// Reading a range to print the square root
double num=0, sqroot=0;
printf("Enter the range to print square root of numbers: ");
scanf("%lf", &num);
// Using for loop to print square roots.
for(int i = 1; i<=num; i++){
sqroot = sqrt(i);
printf(" square root of %d is %.2lf \n",i, sqroot);
}
return 0;
}
Enter the range to print square root of numbers: 10
square root of 1 is 1.00
square root of 2 is 1.41
square root of 3 is 1.73
square root of 4 is 2.00
square root of 5 is 2.24
square root of 6 is 2.45
square root of 7 is 2.65
square root of 8 is 2.83
square root of 9 is 3.00
square root of 10 is 3.16
Q10. Write a program in C to print numbers the divisibility of numbers by 2, 3, and 5.
Implementation in C
#include<stdio.h>
#include<math.h>
int main(){
// Reading a range to print the divisibility with 2, 3, and 5
int range=0;
printf("Enter the range to check divisibility: \n ");
scanf("%d", &range);
// Printing the divisibility using the for loop.
for(int i = 1; i<=range; i++){
if(i%2==0){
printf("%d is divisible by 2 \n", i);
}
if(i%2==0 && i%3 ==0){
printf("%d is divisible by both 2 and 3 \n", i);
}
if(i%3==0){
printf("%d is divisible by 3 \n", i);
}
if(i%2==0 && i%5 ==0){
printf("%d is divisible by both 2 and 5 \n", i);
}
if(i%3==0 && i%5 ==0){
printf("%d is divisible by both 3 and 5 \n", i);
}
if(i%5==0){
printf("%d is divisible by 5 \n", i);
}
}
return 0;
}
Enter the range to check divisibility:
10
2 is divisible by 2
3 is divisible by 3
4 is divisible by 2
5 is divisible by 5
6 is divisible by 2
6 is divisible by both 2 and 3
6 is divisible by 3
8 is divisible by 2
9 is divisible by 3
10 is divisible by 2
10 is divisible by both 2 and 5
10 is divisible by 5
Q11. Write a program in C language to find harmonic series and its sum up to n.
Implementation in C
#include<stdio.h>
#include<math.h>
int main(){
int range = 0;
float sum = 0.0;
// Reading the range upto which we have to print the series.
printf("Enter the range: \n ");
scanf("%d", &range);
printf("Harmonic Sequence is: ");
// Finding out the harmonic series
for(int i=1; i<=range; i++){
if(i<range){
printf("1/%d + ", i);
sum = sum + 1/(float)i;
}
if(i==range){
printf("1/%d \n " , i);
sum = sum + 1/(float)i;
printf("sum of harmonic series: %f", sum);
}
}
return 0;
}
Enter the range:
10
Harmonic Sequence is: 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10
sum of harmonic series: 2.928968
Q12. Write a program in C to print arithmetic progression and its sum.
Implementation in C
#include<stdio.h>
#include<math.h>
int main(){
int first_term = 0, common_difference = 0,range =0, val=0, sum=0;
// Reading the values to calculate A.P.
printf("Enter the first term of the A.P.: \n ");
scanf("%d", &first_term);
printf("Enter the common difference: \n ");
scanf("%d", &common_difference);
printf("Input number of terms in the series: \n ");
scanf("%d", &range);
val = first_term;
// Printing the A.P. and calculating the sum of the series.
printf("Arithmetic Progression : ");
for(int i=0; i<range; i++){
printf("%d ", val);
sum = sum + val;
val = val + common_difference;
}
// Printing the sum.
printf("\n");
printf("The sum of the AP series is: %d", sum);
return 0;
}
First term: 2
Common difference: 4
Number of terms: 10
Output
Enter the first term of the A.P.:
2
Enter the common difference:
4
Input number of terms in the series:
10
Arithmetic Progression : 2 6 10 14 18 22 26 30 34 38
The sum of the AP series is: 200
Q13. Write a program in C to print right angled triangle star pattern.
Implementation in C
#include<stdio.h>
#include<math.h>
int main(){
int size = 0;
// Reading the size of the triangle.
printf("Enter the size of the triangle: \n ");
scanf("%d", &size);
for(int i=0; i<size; i++){
for (int j=0; j<=i; j++){
printf("*");
}
printf("\n");
}
return 0;
}
Enter the size of the triangle:
5
*
**
***
****
*****
Q14. Write a program in C to print an inverted star triangle.
Implementation in C
#include <stdio.h>
int main() {
int size=0;
// Reading the size of the triangle from the user.
printf("Enter the number of rows: ");
scanf("%d", &size);
// Printing the inverted triangle.
for (int i = size; i >= 1; --i) {
for (int j = 0; j < size - i; ++j){
printf(" ");
}
for (int k = i; k <= 2 * i - 1; ++k){
printf("* ");
}
for (int l = 0; l < i - 1; ++l){
printf("* ");
}
printf("\n");
}
return 0;
}
Enter the number of rows: 5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Q15. Write a program in C language to print the sum of the last and the first digit of a number given by the user.
Implementation in C
#include <stdio.h>
int main() {
int number=0, first=0, last=0, sum=0;
// Reading the number from the user.
printf("Enter the number: ");
scanf("%d", &number);
// Last digit of the number.
last = number % 10;
// Find the first digit of the number
while(number >= 10)
{
number = number / 10;
}
first = number;
// The sum of digits.
sum = first + last;
printf("sum of last and first digit = %d", sum);
return 0;
}
Enter the number: 98765
sum of last and first digit = 14
Q16. Write a C program to check whether the given number is an Armstrong number.
Implementation in C
#include <stdio.h>
int main() {
int number, rem,sum=0,x;
// Taking input from user
printf("Enter a number: ");
scanf("%d", &number);
// Checking whether the number is an Armstrong number.
x=number;
while(number>0)
{
rem=number%10;
sum=sum+(rem*rem*rem);
number=number/10;
}
if(x==sum){
printf("Given number is an Armstrong number.\n");
}
else{
printf("Given number is not an Armstrong number.\n");
}
return 0;
}
Enter a number: 371
Given number is an Armstrong number.
Q17. Write a program in C language to reverse a number.
Implementation in C
#include <stdio.h>
int main(){
int number, rev=0, remain;
// Reading a number from the user.
printf("Enter a number: ");
scanf("%d", &number);
// Reversing the digits.
while(number!=0) {
remain=number%10;
rev=rev*10+remain;
number/=10;
}
// Printing the number after reversal.
printf("Number after reversing : %d",rev);
return 0;
}
Q18. Write a program in C language to check if the given number is a palindrome.
Implementation in C
#include <stdio.h>
int main(){
int number, remain,sum=0,x;
// Reading a number from the user
printf("enter a number: ");
scanf("%d",&number);
// Storing the original number in x
x=number;
// Reversing the digits.
while(number>0){
remain=number%10;
sum=(sum*10)+remain;
number=number/10;
}
/* If the reversed number equals the original number,
the number is a palindrome*/
if(x==sum){
printf("Given number is a palindrome number: ");
}
else{
printf("not a palindrome number: ");
}
return 0;
}
Q19. Write a C program to find the H.C.F. of two numbers.
Implementation in C
#include <stdio.h>
int main()
{
int num1, num2, highestcf;
// Taking input from the user.
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
for(int i=1; i <= num1 && i <= num2; ++i)
{
// Checking if i is the factor of num1 and num 2
if(num1%i==0 && num2%i==0)
highestcf = i;
}
printf("H.C.F. is: %d", highestcf);
return 0;
}
Enter first number: 12
Enter second number: 24
H.C.F. is: 12
Q20. Write a C program to print the sum of the Fibonacci series.
Implementation in C
#include <stdio.h>
int main()
{
int prev=0, curr=1, range_of_series, next, sum=0;
// Input from the user
printf("Enter the range: ");
scanf("%d",&range_of_series);
// Printing the Fibonacci series and calculating the sum of the series.
printf("Fibonacci series: ");
while( prev <= range_of_series ){
printf("%d ", prev);
sum += prev;
next = prev + curr;
prev = curr;
curr = next;
}
// Printing the sum of the series.
printf("sum of the fibonacci series: %d", sum);
return 0;
}
Loops in C are used to iterate through the same piece of code repeatedly. Loops continue to execute until the test condition is evaluated as false.
How many types of loops are there in C language?
Three types of loops are there in C. These are the while loop, the do-while loop, and the for loop.
What is the significance of the loops?
With the aid of loops, we can avoid writing the same piece of code repeatedly. That saves a lot of effort and time.
What is the use of a do-while loop?
We use the do-while loop in C when we want the loop to execute at least once. If the test condition is false, the for and while loops do not run even once.
What are "break" and "continue" statements?
We use the “break” statement to exit from the loop as soon as the flow of control goes to this statement. The “continue” statement is used to omit the current iteration and jump to the next iteration of the loop.
Conclusion
In this blog, we learned about loops and how to use them. We also looked at some frequently asked loop questions in C.
You can use our practice platform Code studio to practice loop questions in C.
Don’t stop just yet. Refer to these articles to brush up on your knowledge of the C language.