Chapter – 4 INTRODUCTION TO LOOPS

1. Why do we use loop in C program?
Answer: Loop is use to repeat the same code for a finite number of times.

2. Do we need to use only one type of loop In a C program? Justify your answer by writing a C program.
Answer: No, we can use all types of loops in a C program.
C program has three different types of loops —
• for loop
• do.. while loop
• while loop
A C program can be written in all the three types of loops.

Example :-

Printing “Sebahelp.com” 5 times using all the three types of loop.

#include <stdio.h>
int main()
{
int n=1,a;
while(n<=5)
{
printf("Sebahelp.com\n");
n++;
}
do
{
printf("Sebahelp.com\n");
n++;
}while(n<=5);
for(a=1;a<=5;a++)
{
printf("Sebahelp.com\n");
}
return 0;
}

3. What will happen if we write a while loop with 1 in place of the condition? Try it in a simple C program.
Hint:

While (1)
{
Printf("We must raise our voice against corruption \n");
}

Answer: if we write a while loop with 1 in place of condition the loop will be execute forever as the condition is always remain true.

Code:-

#include<stdio.h>
int main()
{
int i=1;
while(1)
{
printf("\n");
i++;
}
return 0;
}

4. Name different portions of a for loop. Can we put more than one statement within a portion?

Answer: The three portions of for loop are –
• Initialization expression
• Condition / test expression
• Update expression
Yes we can put more than one statement within a portion.

5. Answer with True or False.
i) If the condition of the while loop is false, the control comes to the second statement inside the loop.
Answer: FALSE.
ii) We can use at most three loops in a single C program.
Answer: FALSE.
iii) The statement inside the do-while loop executes at least once even if the condition is false.
Answer: TRUE
iv) Only the first statement inside the do-while loop executes when the condition is false.
Answer: FALSE.
v) In a do-while loop, the condition is written at the end of the loop.
Answer: TRUE.

6. Programming Exercises
A.Write a C program to find the summation of the following series.
(a). 1²+ 2² +3¹ +4²+…+N²
Code:-

#include <stdio.h>
int main()
{
int b,n,sum=0;
printf("Enter the value of n :");
scanf("%d",&n);
for(int i=1; i<=n;i++)
{
b=i*i;
sum=sum+b;
}
printf("\n The summation is %d",sum);
return 0;
}

(b). 1³ +2³ +3³+ 4³+…+N³
Code:-

#include <stdio.h>
int main()
{
int b,n,sum=0;
printf("Enter the value of n :");
scanf("%d",&n);
for(int i=1; i<=n;i++)
{
b=i*i*i;
sum=sum+b;
}
printf("\n The summation is %d",sum);
return 0;
}

(c). 1*2 +2*3 +3*4 + … + N*(N+1)
Code:-

#include <stdio.h>
int main()
{
int b,n,sum=0;
printf("Enter the value of n :");
scanf("%d",&n);
for(int i=1; i<=n;i++)
{
b=i*(i+1);
sum=sum+b;
}
printf("\n The summation is %d",sum);
return 0;
}

B. Write a C program to continuously take a number as input and announce whether the number is odd or even.
Code:-

#include <stdio.h>
int main()
{
int b,n,sum=0;
printf("Enter the value of n :");
scanf("%d",&n);
for(int i=1; i<=n;i++)
{
printf("\n Enter Number:");
scanf("%d",&b);
if(a%2==0)
{
printf("\n The number entered is even.");
}
else
{
printf("\n The number entered is odd.");
} }
return 0;
}

C. Write a C program to display the following pattern.
1
11
111
1111
11111

Code:-

#include <stdio.h>
int main() {

for(int j=1;j<=1;j++) {
printf("%d",1); }
printf("\n");
for(int j=1;j<=2;j++) {
printf("%d",1); }
printf("\n");
for(int j=1;j<=3;j++) {
printf("%d",1); }
printf("\n");
for(int j=1;j<=4;j++) {
printf("%d",1); }
printf("\n");
for(int j=1;j<=5;j++) {
printf("%d",1); }
printf("\n");

return 0;
}

D. Write a C program to display the following pattern.
5
54
543
5432
54321

Code:-

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

for(j=5;j>=5;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=4;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=3;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=2;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=1;j--)
{
printf("%d",j);
}
printf("\n");

return 0;
}

E. Write a C program to display the following pattern.
54321
5432
543
54
5
Code:-

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

for(j=5;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=2;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=3;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=4;j--)
{
printf("%d",j);
}
printf("\n");
for(j=5;j>=5;j--)
{
printf("%d",j);
}
printf("\n");

return 0;
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here