# Explore hunderds of solved example topic wise.

## Looping

### Introduction of loop

Looping is a way to repeat a block of statement for a finite number of times. Here finite means that a loop should be terminated automatically or whenever a user wants.

We also learn that the program is a collection of instructions. In C there is various control instruction that controls the way to execute the program.

1. Sequence control instruction: In which the program statements are executed in the order in which they are written.
2. Decision making control instruction: In which we make a choice by using the if-else construct.
3. Switch/Case-control instruction: Alternate choice of the if-else construct.
4. Loop control instruction: Used to perform the task repeatedly.

We have already learned first 3 control instructions, and now we are going to discuss the last one loop control instruction. Let's start the discussion with an example.

Suppose you want to print "Hello", then you write print("Hello"); once. But if i tell you, print "Hello" for 100 times, then this is not a good way. If there is a way to repeat printf("Hello"); 100 times automatically then that can be optimized the solution. The way is called Looping/Iteration.

The way to achieve this repetition is that we repeat a task still the specified condition, If the condition becomes false then the repetition will automatically stop. This is shown in the following program snippet.

```i=1;
while(i<=100)
{
printf("Hello");
i++;
} ```

In the above program snippet you can see that i=1 statement initialize i with 1, then condition i<=100 imposed with while and i++ increment the i by one each time. Don't worry about the while keyword. we will cover it in a few minutes.

### Types of Loop

#### Entry Control loop / Top tested loop

In this type of loop, At first, the condition is evaluated, if the condition is true the statement within the loop is executed otherwise control immediately jumps out of the loop.

Examples: while loop, for loop

#### Exit Control loop/Bottom tested loop

In this type of loop once the statement of the loop is executed and then the condition is evaluated, if the condition becomes true, then the statement within the loop is again executed otherwise control immediately jumps out of the loop.

Example: do-while loop

### While loop

#### Syntax of while loop

```    initialization;
while(condition)
{
statement-1;
statement-2;
...........;
...........;
}
```
##### Syntax explained

The block associated with the while loop is called the body of the loop. In the above loop syntax, first initialization is performed, then the condition is checked, if the condition is true the control reaches inside the loop and body of the loop is executed, When control reaches the end of the loop, automatically move to the condition and loop is again executed, if the condition evaluates true. This process is repeated until the condition remains true. As soon as the condition becomes false control jumps outside the loop and the rest of the code will be executed.

### Program to print a series from 1 to N

#### Program: Print the series 1,2,3,4,.....N

```#include<stdio.h>
int main() {
int i,n;

printf("Enter how many terms: ");
scanf("%d",&n);
i=1;
while(i<=n)
{
printf("%-4d",i);
i++;
}

return 0;
}
```
```Enter how many terms: 10
1   2   3   4   5   6   7   8   9   10
```
##### Program Description

At first Line-7 and 8 input the value of n from the user. suppose user input 5 for n. For the step by step execution see the following table, Work with it and find out the logic

Round i i<=n Condition? output i++
1 1 1<=5 T 1 2
2 2 2<=5 T 2 3
3 3 3<=5 T 3 4
4 4 4<=5 T 4 5
5 5 5<=5 T 5 6
6 6 6<=5 F Loop Terminated
Within the printf statement, %-4d is used to make the 4 spaces to print the number and number will be left-aligned in the available spaces remaining spaces will be printed on the right side.

### Program to print a series of odd numbers

#### Program: Print 1,3,5,7................N terms

```int main() {
int n,cnt,term;

printf("Enter how many terms: ")	;
scanf("%d",&n);

cnt=1;term=1;
while(cnt<=n) {
printf("%-4d",term);
cnt++;
term+=2;
}
}
```
```Enter how many terms: 10
1   3   5   7   9   11  13  15  17  19
```
##### Program Description

Suppose the value of n is 5, input by the users. Trace the program with the help of the given table and find out the logic.

Round cnt term cnt<=n condition? cnt++ term+=2
1 1 1 1<=5 T 2 3
2 2 3 2<=5 T 3 5
3 3 5 3<=5 T 4 7
4 4 7 4<=5 T 5 9
5 5 9 5<=5 T 6 11
6 6 11 6<=5 F Loop Terminated

In the above-given table it is clear that if n is a natural number than 2n-1 generate the odd number series.Natural number means 1,2,3,4,......N.The following snippet implements this logic. In the following snippet "cnt" is a natural number.

```  cnt=1;
while(cnt<=n)
{
printf("%-4d",2*cnt-1);
cnt++;
}
```

### Program to print 1,0,1,0................N terms

```#include<stdio.h>
int main() {
int cnt,n;
printf("Enter how many terms: ");
scanf("%d",&n);

cnt=1;
while(cnt<=n) {
printf("%d ",cnt%2);
cnt++;
}

return 0;
}```
```Enter how many terms: 10
1 0 1 0 1 0 1 0 1 0```

#### Program Description

 cnt 1 2 3 4 5 cnt%2 1%2 2%2 3%2 4%2 5%2 output 1 0 1 0 1

In the above table, you observe that if the mod is performed with any number the possible output is 0 or 1. When a number is completely divisible by 2, then the remainder is 0 otherwise 1. so we rotate the loop from 1 to n and print the output of number%2.

The mod operator shows the same behavior with other numbers also. Suppose you mod a number by 3 then the possible output is 0,1,2 because in every 3rd step number is divisible by 3 and output will be 0, then for the next successive number output is 1 and then for next successive number output is 2 and then 0.

So the conclusion is that a%n returns the output 0 to n-1, where a can be any integer number.

### Program to print 1,-1,1,-1................N terms

```#include<stdio.h>
int main() {
int n,i,term;

printf("Enter how many terms: ")	;
scanf("%d",&n);

i = 1;term = 1;
while(i < =n) {
printf("%-4d",term);
term = term * -1;
i++;
}
return 0;
}```
```Enter how many terms: 10
1   -1  1   -1  1   -1  1   -1  1   -1
```

#### Program Description

Suppose that the value of n is 5, input by the user.

 round i i <= n condition? term=term*-1 term output 1 1 1<=5 T not executed first time 1 1 2 2 2<=5 T -1 -1 -1 3 3 3<=5 T 1 1 1 4 4 4<=5 T -1 -1 -1 5 5 5<=5 T 1 1 1 6 6 6<=5 F Loop Terminated

One more possible way to print the above series is possible. The logic is if you observe the above series, then you see that on each even term -1 gets printed and on each odd term 1 gets printed. The logic is implemented in the following program snippet.

```i = 1;
while(i <= n) {
if(i % 2 == 0)
printf("%-4d",-1)
else
printf("%4d",1);
}
```

Because in c, every non-zero is treated as true and every zero is treated as false. So the above-given logic can be modified as given below

```i = 1;
while(i <= n) {
if(i % 2)
printf("%-4d",1);
else
printf("%-4d",-1);
}
```

### Program to print 1,2,3,0,1,2,3,0,.....N terms

```#include<stdio.h>
int main() {
int n,cnt;
printf("Enter how many terms: ");
scanf("%d",&n);

i = 1;
while(i<=n) {
printf("%-4d",i % 4);
i++;
}
return 0;
}
```
```Enter how many terms: 10
1   2   3   0   1   2   3   0   1   2
```

#### Program Description

The logic in the above program is not new. Observe the given series, you see that after print 1,2,3 again 0 gets printed. That can be achieved by printing the output of mod(%) by 4 for increasing sequences running from 1 to n.

### Program to count the digits in a given number

```#include<stdio.h>
int main() {
int n,cnt;
printf("Enter any number: ");
scanf("%d",&n);

cnt=0;
//while(n>0)//This condition will work only for positive number
while(n!=0) {
cnt++;
n /= 10;
}
printf("Number of digits= %d",cnt);
return 0;
}```

#### Output

```Enter any number: 1234
Number of digits= 4```

#### Program Description

suppose the cnt = 0 because before testing the number we can't say how many digits in the number. so we assume that in number the digits are 0 and then apply the following process.

Round n n != 0 cnt++ n /= 10
1 Suppose n = 1234, entered by the user T 1 123
2 123 T 2 12
3 12 T 3 1
4 1 T 4 0
5 0 F Loop Terminated.

### Palindrome number

Palindrome number is a number which reverse is the same as the original number.

To check whether the reverse of a number is the same as the original or not. So first we reverse the number and then check it with the original number. The process of reverse a number is given in the following program snippet. Try to understand the given table below.

```
rev = 0;
while(n != 0) {
dig = n%10;
rev = rev * 10 + dig;
n = n / 10;
}
```
Round n n != 0 dig = n % 10 rev = rev * 10 + dig n / 10
1 123 T 3 0*10+3=3 12
2 12 T 2 3*10+2=32 1
3 1 T 1 32*10+1=321 0
4 0 F Loop Terminated

In the above table you see that in the last executing round(round-3), the number becomes 0. To check the palindrome we have to compare the reverse with the original. but the original becomes 0. So We have to preserve the original value by storing in other variable old.

### Program to check whether a number is palindrome or not

```#include<stdio.h>
int main() {
int n,dig,rev,old;
printf("Enter any number: ");
scanf("%d",&n);

rev = 0; old = n;
while(n != 0) {
dig = n % 10;
rev = rev * 10 + dig;
n = n / 10;
}
if(old == rev)
printf("%d is palindrome",old);
else
printf("%d is not palindrome",old);
return 0;
}
```

#### Output

```Enter any number: 121
121 is palindrome

Enter any number: 1331
1331 is palindrome
```