Ch-5 Computer Science Class 10th
Ch-5 Computer Science Class 10th
CHAPTER 5
LOOP CONTROL STRUCTURE
Three types of loops are used in C program. These are for, while and do
while loops.
Q2. What is loop? List essential elements of a loop. Why Programmer might want
to execute a loop.
Ans: Loop:
There are two essential elements of a loop. The block of statements forms
the body of the loop that is to be executed repeatedly until loop condition is
Q3. Write a program to print numbers from 1 to 5by using for () loop.
int 3
for ()-l: jS:
3++)
printt("\ng d, j):
getch 0:
When the program is compiled and executed it will produce the output shown
in Fig.
Output of program
The second expression (test condition) j<=5 tests each time through the
loop to see if j is less than or equal to 5. If the test is true, the body of the
loop is executed. If the test is false, the loop will be terminated and control
will be transfered to the next statement following the forloop.
The thirds expression j++ increments the loop variable j, each time the
loop is executed. In general, any expression can be used forincrementing
the loop is executed. When a far loop terminates, the loop variable is still
Q4. Write a program to read a number and prints its table by using for ()loop.
Ans: Program to read a number and print its table using for ()loop:
program will print the value of n in first column, the value of j in second
column and the product of n*jin the third column according to the
File Edit Search Vie Prcjet Estcute Debug Toos VS Window Help
DPro #includecstdio.b>
tincludeconio.h>
vo1d main (void)
int n, J:
printt("\nEnter a nunber vhose table 1s required: "):
Scant ("%d,6n):
Output of Program
• In the printf() function %2d means to print the values of n and in field
j
width of 2 and %3d means to print the product of in in field width of 3.
Q5. Write a program to print the sum of odd numbers from 1 to 100 by using for ()
loop.
File Edn Search View Project Execute Debug Tools CVS Window Help
Prqje mainc
OPo #includecstdio.h>
#includecconio.h>
void main (void)
int J, sun;
sun 0:
tor (0-1: j<100: j-1+2)
sun-suj:
printf("\aSum= d",sum) :
getch () :
• One ach pass through the loop the value ofj is incremented by 2 and
added to the previous sum. This continues till the last odd number 99 is
added to the sum. The output of this program is shown in Fig. This
Sun= 2580
Output of Program
Q6. Write a program to reada number and print its factorialby using for ()loop.
File Edit Search View Project Execute Debug Tools CVS Window Help
Plgie nanc
#includecstdio .h>
#includecconio.h>
void main (void)
int n, J, tact:
D AD Drive foldersDer-CpplProject33.ere
Enter a nunber: 5
Output of Program
Q7. Write a program to print the given sequence of numbers on a single line. 1 4
710 13 16 19 2225 28 31 34 3740 by using for () loop.
File Edt Search View Project Execute Debug Tools CVS Window Help
Projex mainc
#include<stdio.h>
$includecconio.h>
void ain (vo1d)
int n:
printr ("\a"):
tor (n=; nc40: nn+3)
printf ("%d ,n):
getch () :
Program to print a sequence of numbers
DDADDive Folders\Dev-Cpp\Project33.ee
1 4 7 18 13 16 19 22 25 28 31 34 37 40
Output of Program
Q8. Write a program to print the given sequence of numbers on a single line in
Ans: Program to print the given sequence of numbers on a single line in reverse
order. 30 27 24 21 18 15 12 9 630-3 -6 -9
File Edit Search View Project EKte Debug Took CVS Window Help
Pojer manc
#1ncludecstdi0.h>
includecconio.h>
void main (void)
int n;
printf ("\a"):
for (n=30; n=-9; nn-3)
prnt (*td ".n):
getch():
30 27 24 21 18 15 12 9 6 39-3 -6 -9
Output of Program
Q9. Write a program to find the Greatest Common Divisor (GCD) of two numbers
by using for ()loop.
Ans: Program to print the Greatest Common Divisor (GCD)of two numbers.
Poe nanc
Pro ancludecstdio.h>
include<conio .h>
void main (void)
reml"aj: reg2-b\j:
1f((renl-0)66 (rem=0) )
gcdj:
printt (\aGCD-td",.gcd):
getch () :
In this program a and b are two variables whose GCD is required. After
reading the two numbers from the keyboard, if-else statement will determine
the smaller of the two numbers and assign It on the variable small.
• The GCD is in the range of 1 to the smaller number. The two variables reml
and rem2 are used to determine whether the numbers a and b are exactly
divisiblej by using the remainder operator.
• The greatest value of j that exactly divides both variables without any
GCD=10
Execution of Program
ncludecstdio.h>
tincludeccorio.h>
vo1d ma 1n (void)
lbs-.2*kgs:
pricc ("6.2tKlogzanst6.2fPounds\n",
kgs, lbs):
20 Lrss n f
DD Drive Folders\DevCpplwhile et
Enter a ve ight in Kilograns (zero to quit): 34
34.89 Kilograns = 4.88 Pounds
Execution of Program
Dev-C+o 4992 ( -
- while while.dev
File Edt Search View Project Execute Debug Tools CVS Window Help
Projet leec
whi includecstdio.h>
#include<conio.h>
void main (void)
Snt n:
print ("\n") #
n-32:
while (n<-255)
printt ("43d=%c\E",n,n)
n"n+1:
:
getch ():
CADer-CppProject at
39
Output of Program
• In this program tab(\t) caused the next item printed to start eight spaces
from the start of the last item. Therefore, on an 80 column standard screen. 10
items can be displayed so the screen is divided into ten columns. The printf()
function used a field-width specifier of %3d so that each ASCIl code is printed
The printf() statement in the above program in printing both the character
Code and its ASCIl code. In C language, we can Use the same variable, n for
KEY POINTS
OccUrs within the loop and program execution continues from the next
statement.
The continue statement causes the loop to be continued with the next