0% found this document useful (0 votes)
27 views

Ch-5 Computer Science Class 10th

Uploaded by

Asad Naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Ch-5 Computer Science Class 10th

Uploaded by

Asad Naeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

www.classnotes.

xyz Computer Science

CHAPTER 5
LOOP CONTROL STRUCTURE

ClassNotes, Inc. O 2020 1


www.classnotes.xyz Computer Science

SHORT AND LONG QUESTIONS

Q1. Highlight the functions of loops.

Ans: Function of Loop:

Loops are used in programs to repeat a block of statements. Repeating a


block of statements is a very common and useful task in programming. Loops

make the task of writing programs easier and efficient.

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:

A loop is a statenment in a programming language that allowS one or more


statements to be repeatedly executed as many times as required.

Essential elements of a 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

true. Loops terminate based on test conditions.

Programmer might want to execute a loop:


i. A given number of times
ii. Until a given value exceeds another value

iii. Until a particular character is entered

Q3. Write a program to print numbers from 1 to 5by using for () loop.

ClassNotes, Inc. © 2020 2


www.classnotes.xyz Computer Science

Ans: Program to print numbers from 1 to 5 using for ()loop.


Ilmanc
1nclude cstdio.h>
#anclude cconio.h>
void main (void)

int 3
for ()-l: jS:
3++)
printt("\ng d, j):
getch 0:

ded Insert 10 Linesnlie

P ogram to print numbers from 1 to 5

When the program is compiled and executed it will produce the output shown

in Fig.

BAD Dive FoldesDer-Cpp\Project.exe

Output of program

The expression j=1 inside the parenthesis initializes the jvariable to 1.

Initialization is done as SOon as the loop is entered.

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

ClassNotes, Inc. © 2020 3


www.classnotes.xyz Computer Science

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

defined and contains the value assigned by the last increment. In

Program 1, the last value assigned to j will be 6. However, a well designed


program will not Use this feature.

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:

The execution of program is shown in Fig, when the value of n is 7. The

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

format given in printf() function.

ClassNotes, Inc. O 2020 4


www.classnotes.xyz Computer Science

DerC4992 - [Projetd3 1-Project3 der

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):

tor (j-1: je12: J+)


prantt("\nt2d x 12d 134", n, 3, j*):
çetch () :

122 13Lres in fie

Program to print table of a number

D DAD Drive Folders\Dev-CpplProject33.et

Enter a nuaber whose table is required: ?

Output of Program

ClassNotes, Inc. O 2020


www.classnotes.xyz Computer Science

• 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.

Ans: Program to print the sum of odd numbers from 1 to 100:

Dev-C*. 4992 - ( Preject33] - Project33.dev

File Edn Search View Project Execute Debug Tools CVS Window Help

ONew retTogde U Goto

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 () :

Program to print sum of odd numbers

• When this program is executed, the variable sum is initialized to zero

and the loop variable j to 1.

The first value of j which is l isadded to sum. Then j is incremented by 2


and the number 3 added. is

• 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

ClassNotes, Inc. 2020 6


www.classnotes.xyz Computer Science

added to the sum. The output of this program is shown in Fig. This

program does not require any input from the keyboard.

DAD Drive Folders\Dev-Cpp\Project33.ee

Sun= 2580

Output of Program

Q6. Write a program to reada number and print its factorialby using for ()loop.

Ans: Program to read a number and print its factorial:

De-C4992 - (Project331- Preject33.dev

File Edit Search View Project Execute Debug Tools CVS Window Help

Plgie nanc

#includecstdio .h>

#includecconio.h>
void main (void)
int n, J, tact:

printt (^\nEnter a rurber: "):


9canf ("td", 6a):
tact-:
tor (j-: j<n: j++)
tact=fact*j:
printf (*\nThe factorial of td is Id, n, fact):
getch () :

Program to print factorial ofa number

ClassNotes, Inc. © 2020 7


www.classnotes.xyz Computer Science

Execution of the program is shown in Fig.

D AD Drive foldersDer-CpplProject33.ere

Enter a nunber: 5

The factorial of 5 is 120

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.

Ans: Program to print the given sequence of numbers ona singleline.


147 10 13 16 19 22 25 28 3134 3740

ClassNotes, Inc. O 2020 8


www.classnotes.xyz Computer Science

DerCo+ 4992 - (Project33]- Project33.dev

File Edt Search View Project Execute Debug Tools CVS Window Help

DNew nsert Togge Golo

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

Output of Program is shown in Fig.

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

reverse order. 30 27 24 21 18 15 129 630-3-6 -9 by using for()


loop.

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

ClassNotes, Inc. O 2020


www.classnotes.xyz Computer Science

Der-C*+4992 - ( Project83- Project33.der

File Edit Search View Project EKte Debug Took CVS Window Help

vi? ONe inaetogde g Golo

Pojer manc

#1ncludecstdi0.h>
includecconio.h>
void main (void)

int n;

printf ("\a"):
for (n=30; n=-9; nn-3)
prnt (*td ".n):
getch():

Program to print numbers in reverse order

The output of program is shown in Fig.

DDAD Drive FoldersDer-CpplProject33.et

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.

ClassNotes, Inc. © 2020 10


www.classnotes.xyz Computer Science

Dev-C 4992 - (Project33 ]- Project33.dev


Fle Edt Search View Project Execute Debug Took CVs Window Help

S D8 ? ONex naert Togde Gdo

Poe nanc

Pro ancludecstdio.h>
include<conio .h>
void main (void)

int a,b, small,reml,ren. ).gcd:


printt ("\nEnter two nunbers:"):
scant ("sd, sa",ka, 6b) :
1f (acb)
small-a:
else
small-b;
tor (j-1: jmall: j++)

reml"aj: reg2-b\j:
1f((renl-0)66 (rem=0) )
gcdj:

printt (\aGCD-td",.gcd):
getch () :

Program to find GCD of two numbers

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

remainder Is assigned to the variable gcd. Execution of this program is shown


in Fig.

ClassNotes, Inc. C 2020 11


www.classnotes.xyz Computer Science

DAD Drive Folders\ Dev-CpplProject33.ee


|oll

Enter twO nunbers:30,58

GCD=10

Execution of Program

Q10. Write a program to convert kilograms to pounds using while loop.

Ans: Program to convert kilograms to pounds using while loop:

a Dev C-4992-(le]- whiedev


Fie Edr Search Ve Projet Eecute Debug Tools CVS Window Help

ncludecstdio.h>
tincludeccorio.h>
vo1d ma 1n (void)

tloat kgs, lbs:


prantti"\nEnter veight in Kilograms (zero to quit): ):
scanf"", stgs) :
vhile (kgs!-0)

lbs-.2*kgs:
pricc ("6.2tKlogzanst6.2fPounds\n",
kgs, lbs):

prant("\n\nEater veght Kilogrs


a in (zero to quLt) : "):

printt ("\aGood Bye"):


cetch ():

20 Lrss n f

ClassNotes, Inc. O 2020 12


www.classnotes.xyz Computer Science

Execution of the programns is shown in Fig.

DD Drive Folders\DevCpplwhile et
Enter a ve ight in Kilograns (zero to quit): 34
34.89 Kilograns = 4.88 Pounds

Enteraveight in Kilograns (zero


22.00 Kilograns 48.4 Pounds
to quit): 22

Enter a ve ight in Kilograns (zero to quit): 0


Good Bye

Execution of Program

Q11. Write a program to print ASCIl table using while loop.

Ans: Program to print ASCIl table using while loop:

Dev-C+o 4992 ( -
- while while.dev

File Edt Search View Project Execute Debug Tools CVS Window Help

#O88 ? DNew insetTogge Goo

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 ():

Program to print ASCII characters

ClassNotes, Inc. O 2020 13


www.classnotes.xyz Computer Science

In ASCI table each of the numbers from 0 to 255 represents a separate


character.

Following are the types of characters included in ASCIl table

O-31 control codes sUch as tab cariage return and bell

32-127 uSUal printing characters

128-255 graphics and foreign language characters

Output of the program is shown in Fig.

CADer-CppProject at

39

78-N 79-0 80-P


87-4 88-X 89-Y 98-Z
97-a 98=h 99=c 188-d
1e7-k 188-1 189-n 10-0
117-u 118 -v 119y
1270 128-C
137-2
147=
157

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

in a space of 3 characters and the numbers are properly aligned in columns.

ClassNotes, Inc. © 2020 14


www.classnotes.xyz Computer Science

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

both displaying numberand character. This is done by changing the format


specifier, that is %d prints the number and %c prints the character.

KEY POINTS

A loop is a statement in a programming language that allows one or more

statements to be repeatedly executed as many times as required.

The for statement is used to executed a set of statements repeatedly for a

fixed number of times ina program.

The while statement is used to implement repetitionstructure in a program


when the number of iterations is not known in advance and the repetition
continues until some conditions remains true.
The do while statement is used to implement loop structure in a program
when it is required to execute the loop at least once
The break statements is Used to exit from a loop as soon as certain condition

OccUrs within the loop and program execution continues from the next

statement.

The continue statement causes the loop to be continued with the next

iteration after skipping the remaining statement in a loop.

ClassNotes, Inc. O 2020 15

You might also like