Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Looping

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

3.

1 Iteration and Loops


3.1.1 Introduction
Looping is the concept to implement iteration with some specified condition. It is to
execute a set of statements (line of codes) repeatedly according to the condition given
in the loop. It means it execute the same code multiple times. So, Loop is
combination of condition and iteration (Repetition).

3.1.2 Types of looping statement

Fig. Types of loops


1. Entry Controlled loop: In this type of loop the test condition is tested before
entering the loop body. for Loop and while loop are entry-controlled loops.

Fig. Entry controlled loop


2. Exit Controlled Loop: In this type of loop the test condition is tested or
evaluated at the end of loop body. The loop body will execute at least once,
irrespective of whether the condition is true or false. do while loop is exit-
controlled loop.

Fig. Exit controlled loop


Entry Controlled loop
for loop:
for loop is usually known as definite loop because the programmer knows exactly
how many times the loop will repeat.
for allows us to specify three things about a loop in a single line:
(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached the
number of repetitions desired.
(c) Increasing /decreasing the value of loop counter each time the body of the
loop has been executed.
Syntax :
for (initialization; test expression ; update expression)
{
//for loop body
Statement1;
Statement 2;
}
Flow Chart:

Fig: Flow chart of for loop


Working of for loop:
 The initialization statement is executed only once.
 Then, the test expression is evaluated. If the test expression is evaluated to
false, the for loop is terminated.
 However, if the test expression is evaluated to true, statements inside the body
of for loop are executed, and the update expression is updated.
 Again, the test expression is evaluated.
 This process goes on until the test expression is false.
 When the test expression is false, the loop terminates.

// Program to calculate the sum of first n natural numbers using for loop
// Positive integers 1,2,3...n are known as natural numbers

#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// for loop terminates when num is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

return 0;
}

while loop:
while loop allows a part of the code to be executed multiple times depending
upon a given Boolean condition. It can be viewed as a repeating if statement. The
while loop is mostly used in the case where the number of iterations is not
known in advance. It is also called a pre-tested loop.
Syntax :
initialisation;
while(testExpression)
{
// statements inside the body of the loop
Increment/decrement;
}

Flow Chart:

Fig: Flow chart of while loop


Working of while loop:
 The while loop evaluates the test expression inside the parenthesis ().
 If the test expression is true, statements inside the body of while loop are
executed. Then, the test expression is evaluated again.
 The process goes on until the test expression is evaluated to false.
 If the test expression is false, the loop terminates (ends).
 It is also known as entry controlled loops.

//program to find the sum of digits of a given number using while loop
#include <stdio.h>
int main ()
{
int n, t, sum = 0, r;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
r= t % 10;
sum = sum + r;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
Output:
Enter an integer 12345
Sum of digits of 12345 = 15
Exit Controlled loop
do while loop:
Unlike for and while loops, which test the loop condition at the top of the loop, the
do...while loop in C programming language checks its condition at the bottom of the
loop. A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
Syntax :
do
{
Statement1;
Statement 2;
} while ( condition );
Flow Chart:

Fig: Flow chart of do-while loop

Working of do while loop:


 The body of do...while loop is executed. After execution of body, the test
expression is evaluated.
 If the test expression is true, the body of the loop is executed again and the test
expression is evaluated.
 This process goes on until the test expression becomes false.
 If the test expression is false, the loop ends.
// Program to find the factorial of a number using do while loop
#include<stdio.h>
void main()
{
int n,i=1,f=1;
printf("\n Enter The Number:");
scanf("%d",&n);
do
{
f=f*i;
i++;
}while(i<=n);
printf("\n The Factorial of %d is %d",n,f);
}
Differences between while and do while loop
while loop do while loop

In this loop the test condition or In this loop the test condition or
criteria is checked and evaluated first criteria is checked after the loop is
before loop starts executing. executed.
while loop is entry-controlled loop. do-while loop is exit- controlled loop.
Loop conditions are not terminated Loop conditions are terminated with
with semicolon. semicolon.
Body of loop never executed if the Body of loop is executed for at least
condition is false. It means body of one time even after the condition is
the loop executed zero time, if the false.
condition is false.
If there is single statement in body of Brackets are always required.
loop, brackets are not required.
Variable in condition is initialized variable may be initialized before or
before the execution of loop. within the loop.
Syntax: Syntax:
while (condition) do
{ {
Block of statements; Block of statements;
} } while(condition);

//Write a program using while // write a program using do while


loop loop
Programs on Loops
//Program: 1
//Write a program in C to display the first 10 natural numbers.
#include <stdio.h>
int main() {
int i;
printf("The first 10 natural numbers are:\n");
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
return 0;
}
Output:
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10

//Write a C program to compute the sum of the first n natural numbers.


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

printf("enter the nth number:\n");


scanf("%d",&n);
for (i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("\nThe Sum is : %d\n", sum);
}
Output:
enter the nth number:
5
The Sum is : 15

//Write a program in C to read 10 numbers from the keyboard and find their
sum and average.
#include <stdio.h>
int main() {
int i, n, sum = 0;
float avg;
printf("Input the 10 numbers : \n");
for (i = 1; i <= 10; i++)
{
printf("Number-%d :", i);

scanf("%d", &n);
sum += n;
}
avg = sum / 10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\n", sum, avg);
return 0;
}
Output:
Input the 10 numbers :
Number-1 :10
Number-2 :10
Number-3 :10
Number-4 :10
Number-5 :10
Number-6 :20
Number-7 :20
Number-8 :20
Number-9 :20
Number-10 :10
The sum of 10 no is : 140
The Average is : 14.000000

//Write a program in C to display the multiplication table for a given integer.


#include <stdio.h>
int main() {
int j, n;
printf("Input the number for which Table to be calculated : ");
scanf("%d", &n);
for (j = 1; j <= 10; j++)
{
printf("\n%d X %d = %d ", n, j, n * j);
}
return 0;
}
Output:
Input the number for which Table to be calculated : 10

10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100

//Write a C program to display the n terms of odd natural numbers and their
sum.
#include <stdio.h>
int main() {
int i, n, sum = 0,odd;
printf("Input number of terms : ");
scanf("%d", &n);
printf("\nThe odd numbers are :");
for (i = 1; i <= n; i++)
{
odd= 2*i-1;
printf("%d ",odd);
sum += odd;
}
printf("\nThe Sum of odd Natural Number upto %d terms : %d \n", n, sum);
return 0;
}
Output:
Input number of terms : 5
The odd numbers are :1 3 5 7 9
The Sum of odd Natural Number upto 5 terms : 25
//Write a C program to display the n terms of even natural numbers and their
sum.
#include <stdio.h>
int main() {
int i, n, sum = 0,even;
printf("Input number of terms : ");
scanf("%d", &n);
printf("\nThe even numbers are :");
for (i = 1; i <= n; i++)
{
even= 2*i;
printf("%d ",even);
sum += even;
}
printf("\nThe Sum of even Natural Number upto %d terms : %d \n", n, sum);
return 0;
}
Output:
Input number of terms : 5
The even numbers are :2 4 6 8 10
The Sum of even Natural Number upto 5 terms : 30

//Write a C program that displays the n terms of square natural numbers and
their sum.
#include <stdio.h>
int main() {
int i, n, sum = 0, sq;
printf("Input number of terms : ");
scanf("%d", &n);
printf("\nThe square numbers are :");
for (i = 1; i <= n; i++)
{
sq= i*i;
printf("%d ",sq);
sum += sq;
}
printf("\nThe Sum of square Natural Number upto %d terms : %d \n", n, sum);
return 0;
}
Output:
Input number of terms : 5
The square numbers are :1 4 9 16 25
The Sum of square Natural Number upto 5 terms : 55

//Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n
terms.
#include <stdio.h>
int main() {
int n, i;
long sum = 0;
long int t = 1;
printf("Input the number of terms : ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
sum = sum + t;
t = (t * 10) + 1;
}
printf("\nThe Sum is : %ld\n", sum);
return 0;
}
Output:
Input the number of terms : 5
The Sum is : 12345

//Write a program to find the reverse of a given number.


#include <stdio.h>
int main()
{
int n, rev= 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("reverse of a number is\t%d",rev);
return 0;
}
Output:
Enter an integer: 12345
Reverse of a number is 54321

//Write a program to check the number is palindrome or not.


// An integer is a palindrome if the reverse of that number is equal to the
original number.
#include <stdio.h>
int main()
{
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
// reversed integer is stored in reversed variable
while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if original and reversed are equal
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
Output:
Enter an integer: 1001
1001 is a palindrome.

// Write a program to print check a number is prime number or not


// A prime number is a positive integer that is divisible only by 1 and itself. For
example: 2, 3, 5, 7, 11, 13, 17.
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Output:
Enter a positive integer: 29
29 is a prime number.

OR
// Write a program to print check a number is prime number or not
#include <stdio.h>
int main()
{
int n, i, count= 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

for (i = 1; i <= n ; ++i)


{
if (n % i == 0) {
count ++;
}
}
if (count == 2)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}

// Write a C program to add first seven terms of the following series using for
loop.
1/1! +2/2! +3/3! +--------
#include<stdio.h>
int main()
{
int i;
float fact=1.0, res, n_res=0;
for (i=1;i<8;i++)
{
//find factorial for 1 to 7
fact = fact * i;
//find the i/factof(i)
res = i/fact;
//add all the results
n_res = n_res + res;
}
printf("%f", n_res);
}
Output:
2.718056

// Write a program to input a floating-point number and find leftmost digit of


integral part of a number
#include<stdio.h>
#include<math.h>
main()
{
float num;
int ip,rm,count=0,temp;
printf("Enter a floating point number \n");
scanf("%f",&num);
ip=(int)num;
temp=ip;
while(temp>0)
{
temp=temp/10;
count++;
}
rm=ip/pow(10,count-1);
printf("Left most digit of integral part=%d \n",rm);
}
OR
// Write a program to input a floating-point number and find leftmost digit of
integral part of a number
#include<stdio.h>
#include<math.h>
main()
{
float num;
int n,r;
printf("Enter a floating point number \n");
scanf("%f",&num);
n=(int)num;
printf("%d",n);
while(n>0)
{
r=n%10;
n=n/10;

printf("Left most digit of integral part=%d \n",r);


}

//Write a C program to calculate the factorial of a given number.


#include <stdio.h>

int main(){
int i, f = 1, num;
printf("Input the number : ");
scanf("%d", &num);

for(i = 1; i <= num; i++)


{
f = f * i;
}
printf("The Factorial of %d is: %d\n", num, f);
return 0;
}
Output:
. Input the number : 4
The Factorial of 4 is: 24

//Write a C program to check whether a given number is a 'Perfect' number or


not.
/* Perfect number is a positive number which sum of all positive divisors
excluding that number is equal to that number.
For example, 6 is a perfect number since the divisors of 6 are 1, 2, and 3. Sum
of its divisors is 1 + 2 + 3 = 6 */
#include <stdio.h>
int main()
{
int n, i, sum;
printf("Input the number: ");
scanf("%d", &n);
sum = 0;
for (i = 1; i < n; i++)
{
if (n % i == 0)
{
sum = sum + i;
}
}
if (sum == n)
printf("\n number is perfect.\n");
else
printf("\n number is not perfect.\n");

return 0;
}
Output:
Input the number: 12
number is not perfect.
Or
Input the number: 6
number is perfect.

//Write a C program to check whether a given number is an Armstrong number


or not.
#include <stdio.h>
#include<math.h>
void main()
{
int num, r, sum=0, temp, count=0;
printf("Input a number: ");
scanf("%d",&num);
temp=num;
while(temp>0)
{
temp=temp/10;
count++;
}
temp=num;
for( ;num!=0;num=num/10)
{
r=num % 10;
sum=sum+pow(r,count);
}
if(sum==temp)
printf("%d is an Armstrong number.\n",temp);
else
printf("%d is not an Armstrong number.\n",temp);
}
Output:
Input a number: 153
153 is an Armstrong number.

//Write a C program to convert a binary number into a decimal number


#include <stdio.h>
#include <math.h>
void main()
{
int n1, n;
int dec = 0, i = 0, j, d;
printf("Input the binary number:");
scanf("%d", &n);
while (n != 0)
{
d = n % 10;
dec = dec + d * pow(2, i);
n = n / 10;
i++;
}
printf("\nThe equivalent Decimal Number is: %d\n", dec);
return 0;
}
Output:
Input the binary number:110
The equivalent Decimal Number is: 6

//Print fibonacci series up to n terms


#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
switch(number)
{
case 0:
printf(" ");
break;
case 1:
printf("%d",n1);
break;
case 2:
printf("%d %d",n1,n2);
break;
default:
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
}
return 0;
}
Output:
Enter the number of elements:5
01123

//print fibonacci series up to n number


#include<stdio.h>
int main()
{
int n1=0,n2=1,n3=1,i,number;
printf("Enter the nth number");
scanf("%d",&number);
switch(number)
{
case 0:
printf("%d",n1);
break;
case 1:
printf("%d %d %d ",n1, n2, n3);
break;
default:
printf("%d %d",n1, n2);
while(number>=n3)
{
printf(" %d",n3);
n1=n2;
n2=n3;
n3=n1+n2;
}
}
return 0;
}
Output:
Enter the nth number5
01 1 2 3 5
2017-18 (RCS-101)
1. Why we use do-while loop in c? Also tell any properties which you
know? 2
2. Write a C program to add first seven terms of the following series using
for loop. 1/1! +2/2! +3/3! +-------- 7
3. Write a program to check the number is palindrome of not. The program
should accept any arbitrary number typed by user. 7
4. Write a program to print check a number is prime number or not. 7
2017-18(RCS-201)
1. Write a program to find the Armstrong number from 1 to 100. 7
2. Write a program to generate a following numbers structure: 7
12345
1234
123
12
3. Write down the output of the following. 2
main()
{
int i=1;
for( ; ; )
{
printf(“%d”,i);
if(i= =7)
break;
}
}
2018-19(KCS-101)
1. Write the syntax format for while, do while and for loops. 3
2. How to use break statement in C? Explain it with some sort of code. 2

2018-19(KCS-201)
1. Write a program in C to print following pattern with appropriate comments:
10
98
765
4321 10
2. What is the use of break statement in loops? Write a program in C using
while loop to elaborate the use of break statement. 10
3. Differentiate while and do while loop. 2
2019-20(KCS-101)
1. Take the three digit number from the user then write a program to check
entered number is palindrome or not. 10
2. Write a program in C to generate the Fibonacci series up to the last Fibonacci
number less than 100. Also finds the sum of all Fibonacci numbers and total
count of all Fibonacci numbers. 10
3. Write a program in C to print the following pattern: 10
234567
34567
4567
567
67
7
4. Differentiate while and do while loop. 2
2020-21(KCS-101T)
1. Write a program to find the entered number is palindrome or not. 10
2. Write a program in C to print the following pattern: 10
****
***
**
*
3. Differentiate while and do while loop. 2

2021-22(KCS-101T)
1. Differentiate between type conversion and type casting. Write a program to
input a floating-point number and find leftmost digit of integral part of a
number. 10
2. Show the usage of break statement. 2

2021-22(KCS-201T)
1. Write a program to print the pattern 10
1
12
123
1234
123
12
1
2. Write a Program for pattern 10
*****
****
***
**
*
**
***
****
*****

2022-23(BCS-101)
1. Write a program to print the pattern 7
*
***
*****
*******
*********
2. Find the output of the following code: 2
#include<stdio.h>
main()
{
int i=1;
for(;;)
{ printf("%d",i);
if(i=5)
break;
}
}

2022-23(BCS-201)
1 Write a program in C to reverse a given number N having any number of
digits. 7
2 Differentiate between while and do-while loop. Write a program in C to print
the following pattern: 7
12345
1234
123
12
1
2023-24(BCS-101)
1 Write a program to print the pattern 7
*
**
***
****
***
**
*
2 Write a program to check whether the entered number is prime or not. 7
3 Find the output of the code 2
#include<stdio.h>
main()
{
int a,b;
for(a=6,b=4;a<=24;a=a+6)
{
if(a%b==0)
break;
}
printf("%d",a);
}

You might also like