Problem Set For Loops

You might also like

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

Problem Set for Loops

Summary of Loops
1. The three types of loops available in C/C++ are for, while, and do-while.
2. A break statement takes the execution control out of the loop.
3. A continue statement skips the execution of the statements after it and takes the control to
the beginning of the loop.
4. A do-while loop is used to ensure that the statements within the loop are executed at least
once.
5. The loop within the body of another loop is called Nested Loop
6. Any type of loop can be nested in any other loop.
7. The increase of the level of nesting will cause the complexity of the program.

Show the output for the following C code snippets and draw a flowchart for each of them.
1. for(i = 2; i <= 6; i = i + 2)
printf("%d\t", i + 1);

2. for(i = 2; i != 11; i = i + 3)
printf("%d\t", i + 1);

3. for (char ch = ’a’ ; ch <= ’z’ ; ch++)


cout<<ch;

4. main( )
{
int j ;
while ( j <= 10 )
{
cout<<endl<<j;
j=j+1;
}
}

5. main( )
{
int x = 1 ;
while ( x == 1 )
{
x=x-1;
printf ( "\n%d", x ) ;
}
}
6. main( )
{
while ( '1' < '2' )
cout<< "\nIn while loop") ;
}
7. main( )
{
int x = 4, y = 0, z ;
while ( x >= 0 )
{
if ( x == y )
break ;
else
printf ( “\n%d %d”, x, y ) ;
x-- ;
y++ ;
}
}
8. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is
run?
A. 10
B. 9
C. 0
D. 1
9. Which is not a loop structure?
A. for
B. do while
C. while
D. repeat until
10. How many times is a do while loop guaranteed to loop?
A. 0
B. Infinitely
C. 1
D. Variable
11. When does the code block following while(x<100) execute?
A. When x is less than one hundred
B. When x is greater than one hundred
C. When x is equal to one hundred
D. While it wishes

Programs:
1. Print out all integers from 5 down to -5.
2. Write a program that writes out the decimal equivalent of the reciprocals 1/2, 1/3, 1/4, ... ,
1/19, 1/20
3. Write a program that reads in 7 non-negative integers from the user (one at a time) and
assigns the maximum value to a variable maxVal.
4. Write a program to calculate overtime pay of 10 employees. Overtime is paid at the rate
of Rs. 12.00 per hour for every hour worked above 40 hours. Assume that employees do
not work for fractional part of an hour.
5. Write a program to print all the ASCII values and their equivalent characters using a
while loop. The ASCII values vary from 0 to 255.
6. Write a program that display the series of even number from 0 to 110 and also display the
sum of that series.
7. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes
of each digit of the number is equal to the number itself, then the number is called an
Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
8. Write a program for a matchstick game being played between the computer and a user.
Your program should ensure that the computer always wins. Rules for the game are as
follows:
− There are 21 matchsticks.
− The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
− After the person picks, the computer does its picking.
− Whoever is forced to pick up the last matchstick loses the game.

9. Write a program to enter the numbers till the user wants and at the end it should display
the count of positive, negative and zeros entered.
10. Write a program to find the octal equivalent of the entered number.
11. Write a program to find the range of a set of numbers. Range is the difference between
the smallest and biggest number in the list.
12. Write a program to print all prime numbers from 500 to 3000.
13. Write a program to fill the entire screen with a smiling face. The smiling face has an
ASCII value 1.
14. Write a program to add first seven terms of the following series using a for loop:

15. Write a program to produce the following output:

You might also like