Documente Academic
Documente Profesional
Documente Cultură
while loop
Rewrite the following for loop using a while loop 1. f (i 1; 5 1 for (i = 1 i<=5; i++) )
cout<<United Kingdom\ cout<<United Kingdom\n;
@Bharatiya Vidya Bhavan Middle East, All rights reserved
2. 2.
for (i 1; <=10; +=2) f (i = 1 i< 10 i+ 2)
Q. Write a program to input a number and print p p its multiplication table p using a while loop. g p
@Bharatiya Vidya Bhavan Middle East, All rights reserved
Q. Write a program to input a number and print p p the sum of its digits. g
Q. Write a program to input a number and p check whether it is a p prime number or not.
@Bharatiya Vidya Bhavan Middle East, All rights reserved
Q. Q Write a program to input a number and check whether it is an Armstrong number or not. (A number is Armstrong when the number and sum of h th b d f the cubes of its digits are the same.) Example:- 153 ) Example:p
@Bharatiya Vidya Bhavan Middle East, All rights reserved
for d hil l f and while loops are called entry controlled p p p loops or pre-check loops whereas do-while loop is called exit controlled loop ll d it t ll d l or post check loop. loop
@Bharatiya Vidya Bhavan Middle East, All rights reserved
In case of for and while loops loops, the text expression is evaluated at the beginning of the l th loop i b f i.e, before executing ti the loop body If test body. expression evaluates to false for the first time itself, the loop i never executed. l is t d
@Bharatiya Vidya Bhavan Middle East, All rights reserved
In both example 1 and 2, body of the loop will not get executed at all.
In case o do - while loop, the text of e oop, e e expression is evaluated at the bottom of the loop after executing the loop body So even when the body. p , test expression evaluates to false, the loop is executed at least once. In example 3, body of the loop will execute once even though the test expression is false.
@Bharatiya Vidya Bhavan Middle East, All rights reserved