# Chapter 5: Loop Control Instruction – Let Us C Solutions

In the fifth chapter, Let Us C covered all the basic things we need to get started in the journey of learning C Programming. Now, let’s have a look at the solutions of the exercise of the first chapter, Getting Started from Let Us C.

#### [A] What will be the output of the following programs:

(a)

``````# include <stdio.h>
int main( )
{
int i = 1 ;
while ( i <= 10 ) ;
{
printf ( "%d\n", i ) ;
i++ ;
}
return 0 ;
}``````

Answer: No Output Indefinite while loop because of a ‘;’ at the end of
while

(b)

``````# include <stdio.h>
int main( )
{
int x = 4, y, z ;
y = --x ;
z = x-- ;
printf ( "%d %d %d\n", x, y, z ) ;
return 0 ;
}``````

Answer: 2 3 3

(c)

``````# include <stdio.h>
int main( )
{
int x = 4, y = 3, z ;
z = x-- - y ;
printf ( "%d %d %d\n", x, y, z ) ;
return 0 ;
}``````

Answer: 3 3 1

(d)

``````# include <stdio.h>
int main( )
{
while ( 'a' < 'b' )
printf ( " malayalam is a palindrome\n" ) ;
return 0 ;
}``````

Answer: ‘malayalam is a palindrome’ will be printed indefinitely

(e)

``````# include <stdio.h>
int main( )
{
int i ;
while ( i = 10 )
{
printf ( "%d\n", i ) ;
i = i + 1 ;
}
return 0 ;
}``````

Answer: 10 will be printed indefinitely

(f)

``````# include <stdio.h>
int main( )
{
float x = 1.1 ;
while ( x == 1.1 )
{
printf ( "%f\n", x ) ;
x = x – 0.1 ;
}
return 0 ;
}``````

Answer: No output. Since a float variable is compared with double
constant, the condition will not satisfy.

[B] Attempt the following:
(a)
Write a program to calculate the 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 the fractional part of an hour.

``````# include <stdio.h>
int main( )
{
float otpay ;
int hour, i = 1 ;
while ( i <= 10 )
{
printf ( "\nEnter hours worked: " ) ;
scanf ( "%d", &hour ) ;
if ( hour >= 40 )
{
otpay = ( hour - 40 ) * 12 ;
printf ( "No of hours worked = %d \n
and Overtime pay = Rs.%f\n", hour, otpay ) ;
}
else
{
otpay = 0 ;
printf ( "No of hours worked (%d) is less than
40 Hrs.\nHence no overtime pay\n", hour ) ;
}
i++ ;
}
return 0 ;
}``````

(b) Write a program to find the factorial value of any number
entered through the keyboard.
Program:

``````# include <stdio.h>
int main( )
{
int num, i = 1 ;
unsigned long int fact = 1 ;
printf ( "Enter any number (less than 34): " ) ;
scanf ( "%d", &num ) ;
while ( i <= num )
{
fact = fact * i ;
i++ ;
}
printf ( "factorial of %d = %lu\n", num, fact ) ;
return 0 ;
}``````

(c) Two numbers are entered through the keyboard. Write a
program to find the value of one number raised to the power
of another.
Program:

``````# include <stdio.h>
int main( )
{
int x, y, i = 1 ;
long int power = 1 ;
printf ( "\nEnter two numbers: " ) ;
scanf ( "%d %d", &x, &y ) ;
while ( i <= y )
{
power = power * x ;
i++ ;
}
printf ( "%d to the power %d is %ld\n", x, y, power ) ;
return 0 ;
}``````

(d) 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.
Program:

``````# include <stdio.h>
int main( )
{
int i = 0 ;
while ( i <= 255 )
{
printf ( "%d %c\n", i, i ) ;
i++ ;
}
return 0 ;
}``````

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

``````# include <stdio.h>
int main( )
{
int i = 1, a, b, c ;
printf ( "Armstrong numbers between 1 & 500 are:\n" ) ;
while ( i <= 500 )
{
a = i % 10 ; /* extract last digit */
b = i % 100 ;
b = ( b - a ) / 10 ; /* extract second digit */
c = i / 100 ; /* extract first digit */
if ( ( a * a * a ) + ( b * b * b ) + ( c * c * c ) == i )
printf ( "%d\n", i ) ;
i++ ;
}
return 0 ;
}``````

(f) 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.

Program:

``````# include <stdio.h>
int main( )
{
int m = 21, p, c ;
while ( 1 )
{
printf ( "\n\nNo. of matches left = %d\n", m ) ;
printf ( "Pick up 1, 2, 3 or 4 matches: " ) ;
scanf ( "%d", &p ) ;
if ( p > 4 || p < 1 )
continue ;
m = m - p ;
printf ( "No. of matches left = %d\n", m ) ;
c = 5 - p ;
printf ( "Out of which computer picked up %d\n", c ) ;
m = m - c ;
if ( m == 1 )
{
printf ( "Number of matches left %d\n\n", m ) ;
printf ( "You lost the game !!\n" ) ;
break ;
}
}
return 0 ;
}``````

(g) Write a program to enter numbers till the user wants. At the
end it should display the count of positive, negative and zeros
entered.
Program:

``````# include <stdio.h>
int main( )
{
int pos, neg, zero, num ;
char ans = 'y' ;
pos = neg = zero = 0 ;
while ( ans == 'y' || ans == 'Y' )
{
printf ( "\nEnter a number: " ) ;
scanf ( "%d", &num ) ;
if ( num == 0 )
zero++ ;
if ( num > 0 )
pos++ ;
if ( num < 0 )
neg++ ;
fflush ( stdin ) ; // clears standard input stream
printf ( "\nDo you want to continue? " ) ;
scanf ( "%c", &ans ) ;
}
printf ( "You entered %d positive number(s)\n", pos ) ;
printf ( "You entered %d negative number(s)\n", neg ) ;
printf ( "You entered %d zero(s)\n", zero ) ;
return 0 ;
}``````

(h) Write a program to receive an integer and find its octal
equivalent. Hint: To obtain octal equivalent of an integer,
divide it continuously by 8 till dividend doesn’t become zero,
then write the remainders obtained in reverse direction.
Program:

``````# include <stdio.h>
# include <math.h>
int main( )
{
int n1, n2, rem, oct, p ;
printf ( "\nEnter any number: " ) ;
scanf ( "%d", &n1 ) ;
n2 = n1 ;
p = oct = 0 ;
while ( n1 > 0 )
{
rem = n1 % 8 ;
n1 = n1 / 8 ;
oct = oct + rem * pow ( 10, p ) ;
p++ ;
}
printf ( "The octal equivalent of %d is %d\n", n2, oct ) ;
return 0 ;
}``````

(i) 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.

Program:

``````# include <stdio.h>
int main( )
{
int n, no, flag, small, big, range ;
flag = 0 ;
printf ( "\nHow many numbers are there in a set? " ) ;
scanf ( "%d", &n ) ;
while ( n > 0 )
{
printf ( "\nEnter no: " ) ;
scanf ( "%d", &no ) ;
if ( flag == 0 )
{
small = big = no ;
flag = 1 ;
}
else
{
if ( no > big )
big = no ;
if ( no < small )
small = no ;
}
n-- ;
}
if ( small < 0 )
range = small + big ;
else
range = big - small ;
if ( range < 0 )
range = range * -1 ;
printf ( "\nThe range of given set of numbers is %d\n", range ) ;
return 0 ;
}``````