Chapter 6: More Complex Repetitions – Let Us C Solutions

In the sixth 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 sixth chapter, More Complex Repetitions from Let Us C.

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

(a)

# include <stdio.h>
int main( )
{
int i = 0 ;
for ( ; i ; )
printf ( "Here is some mail for you\n" ) ;
return 0 ;
}

Output:
No Output

(b)

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

Output:
1 will be printed an indefinite number of times.

(c)

# include <stdio.h>
int main( )
{
int i = 1, j = 1 ;
for ( ; ; )
{
if ( i > 5 )
break ;
else
j += i ;
printf ( "%d\n", j ) ;
i += j ;
return 0 ;
}
}

Output:
2
5

[B] Answer the following:
(a) The three parts of the loop expression in the for loop are:
the initialization expression
the testing expression
the incrementation expression
(b) The break statement is used to exit from:

  1. an if statement
  2. a for loop
  3. a program
  4. the main( ) function

Answer:
(2) a for loop

(d) In what sequence the initialization, testing and execution of
body is done in a do-while loop?

  1. Initialization, execution of body, testing
  2. Execution of body, initialization, testing
  3. Initialization, testing, execution of body
  4. None of the above
    Answer:
    (1) Initialization, execution of body, testing

(e) Which of the following is not an infinite loop?

  1. int i = 1 ;
    while ( 1 )
    {
    i++ ;
    }
  2. for ( ; ; ) ;
  3. int True = 0, false ;
    while ( True )
    {
    False = 1 ;
    }
  4. int y, x = 0 ;
    do
    {
    y = x ;
    } while ( x == 0 ) ;

Answer:
3

(f) Which keyword is used to take the control to the beginning of
the loop?
Answer:
continue
(g) How many times the while loop in the following C code will
get executed?

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

Answer:

Infinite times, because of ; at the end of while ( j <= 255 )

(h) Which of the following statements are true for the following
program?

# include <stdio.h>
int main( )
{
int x = 10, y = 100 % 90 ;
for ( i = 1 ; i <= 10 ; i++ ) ;
if ( x != y ) ;
printf ( "x = %d y = %d\n", x, y ) ;
return 0 ;
}

(1) The printf( ) function is called 10 times.
(2) The program will produce the output x = 10 y = 10.
(3) The ; after the if ( x != y ) would NOT produce an error.
(4) The program will not produce any output.
(5) The printf( ) function is called infinite times.
Answer:
(2) The program will produce the output x = 10 y = 10

(i) Which of the following statement is true about a for loop used
in a C program?
(1) for loop works faster than a while loop.
(2) All things that can be done using a for loop can also be
done using a while loop.
(3) for ( ; ; ) implements an infinite loop.
(4) for loop can be used if we want statements in a loop to
get executed at least once.
(5) for loop works faster than a do-while loop.
Answer:
(2), (3), (4).
[C] Attempt the following:
(a) Write a program to print all prime numbers from 1 to 300.
(Hint: Use nested loops, break and continue)
Program:

# include <stdio.h>
int main( )
{
int i, n = 1 ;
printf ( "\nPrime numbers between 1 & 300 are :\n1\t" ) ;
while ( n <= 300 ) /* Loop to check numbers upto 300 */
{
i = 2 ;
while ( i < n ) /* Loop starting from 2 to the number */
{
if ( n % i == 0 )
break ; /* takes control out of the inner while as soon
 as the number is fully divisible */
else
i++ ;
}
if ( i == n )
printf ( "%d\t", n ) ;
n++ ;
}
return 0 ;
}

(b) Write a program to fill the entire screen with a smiling face.
The smiling face has an ASCII value 1.
Program:

# include <stdio.h>
int main( )
{
int r, c ;
for ( r = 0 ; r <= 24 ; r++ ) /* Fills rows 0 to 24 */
for ( c = 0 ; c <= 79 ; c++ ) /* Fills columns 0 to 79 */
printf ( "%c", 1 ) ;
return 0 ;
}
# include <stdio.h>
int main( )
{
int i = 1, j ;
float fact, sum = 0.0 ;
while ( i <= 7 )
{
fact = 1.0 ;
for ( j = 1 ; j <= i ; j++ )
fact = fact * j ;
sum = sum + i / fact ;
i++ ;
}
printf ( "Sum of series = %f\n", sum ) ;
return 0 ;
}

(d) Write a program to generate all combinations of 1, 2 and 3
using for loop.
Program:

# include <stdio.h>
int main( )
{
int i = 1, j = 1, k = 1 ;
for ( i = 1 ; i <= 3 ; i++ ) /* 1st digit */
{
for ( j = 1 ; j <= 3 ; j++ ) /* 2nd digit */
{
for ( k = 1 ; k <= 3 ; k++ ) /* 3rd digit */
printf ( "%d%d%d\n" , i , j , k ) ;
}
}
return 0 ;
}

(e) A machine is purchased which will produce earning of Rs.
1000 per year while it lasts. The machine costs Rs. 6000 and
will have a salvage value of Rs. 2000 when it is condemned.
If 9 percent per annum can be earned on alternate
investments, write a program to determine what will be the
minimum life of the machine to make it a more attractive
investment compared to alternative investments?
Program:

# include "stdafx.h"
# include <stdio.h>
int main( )
{
int year = 1 ;
float principal = 6000, salvagevalue = 2000, yearlyprofit = 1000 ;
float valueoption1, valueoption2, interest ;
while ( true )
{
valueoption1 = salvagevalue + yearlyprofit * year ;
interest = principal * 0.09f * year ;
valueoption2 = principal + interest ;
printf ( "year = %2d value option 1 = %10.2f
value option 2 = %10.2f\n", year, valueoption1, valueoption2 ) ;
if ( valueoption1 > valueoption2 )
break ;
year++ ;
}
printf ( "Minimum life of the Machine is %d Years\n", year - 1 ) ;
return 0 ;
}

(f) Write a program to print the multiplication table of the
number entered by the user. The table should get displayed in
the following form:
29 * 1 = 29
29 * 2 = 58

Program:

# include <stdio.h>
int main( )
{
int i, num ;
printf ( "\nEnter the number: " ) ;
scanf ( "%d", &num ) ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "%d * %d = %d\n", num, i, num * i ) ;
return 0 ;
}

(g) According to a study, the approximate level of intelligence of
a person can be calculated using the following formula:
i = 2 + ( y + 0.5 x )
Write a program that will produce a table of values of i, y and
x, where y varies from 1 to 6, and, for each value of y, x
varies from 5.5 to 12.5 in steps of 0.5.
Program:

# include <stdio.h>
int main( )
{
int y ;
float i, x ;
for ( y = 1 ; y <= 6 ; y++ )
{
for ( x = 5.5 ; x <= 12.5 ; x += 0.5 )
{
i = 2 + ( y + 0.5 * x ) ;
printf ( "y = %d, x = %f i = %f\n", y, x, i ) ;
}
}
return 0 ;
}

(h) When interest compounds q times per year at an annual rate
of r % for n years, the principal p compounds to an amount a
as per the following formula
a = p ( 1 + r / q )nq
Write a program to read 10 sets of p, r, n & q and calculate
the corresponding as.
Program:

# include <stdio.h>
# include <math.h>
int main( )
{
float q, r, n, p, a ;
int i ;
for ( i = 1 ; i < 10 ; i++ )
{
printf ( "\nEnter the principal amount:" ) ;
scanf ( "%f", &p ) ;
printf ( "\nEnter the rate of interest:" ) ;
scanf ( "%f", &r ) ;
printf ( "\nEnter the number of years: " ) ;
scanf ( "%f", &n ) ;
printf ( "\nEnter the compounding period: " ) ;
scanf ( "%f", &q ) ;
a = p + pow ( (1 + ( r / q ) ), ( n * q ) ) ;
printf ( "\n\nTotal amount = %f\n", a ) ;
}
return 0 ;
}
# include <stdio.h>
# include <math.h>
int main( )
{
int x, i ;
float result = 0 ;
printf ( "\nEnter the value of x: " ) ;
scanf ( "%d", &x ) ;
for ( i = 1; i <= 7 ; i++ )
{
if ( i == 1 )
result = result + pow ( ( x - 1.0 ) / x, i ) ;
else
result = result + ( 1.0 / 2 ) * pow ( ( x - 1.0 ) / x, i ) ;
}
printf ( "\nLog ( %d ) = %f\n", x, result ) ;
return 0 ;
}

(j) Write a program to generate all Pythagorean Triplets with
side length less than or equal to 30.
Program:

#include <stdio.h>
int main( )
{
int i, j, k ;
for ( i = 1 ; i <= 30 ; i++ )
{
for ( j = 1 ; j <= 30 ; j++ )
{
for ( k = 1 ; k <= 30 ; k++ )
{
if ( i * i + j * j == k * k )
printf ( "%d %d %d\n", i, j, k ) ;
}
}
}
return 0 ;
}

(k) Population of a town today is 100000. The population has
increased steadily at the rate of 10 % per year for last 10 years. Write a program to determine the population at the end
of each year in the last decade.
Program:

#include <stdio.h>
#include <math.h>
int main( )
{
int pop, n ;
float p, r ;
r = 10 ;
p = 100000 ;
for ( n = 1 ; n <= 10 ; n++ )
{
pop = p / pow ( ( 1 + r / 100 ), n ) ;
printf ( "Population %d years ago = %d\n", n, pop ) ;
}
return 0 ;
}

(l) Ramanujan number is the smallest number that can be
expressed as sum of two cubes in two different ways. Write a
program to print all such numbers up to a reasonable limit.
Program:

#include <stdio.h>
int main( )
{
int i, j, k, l ;
for ( i = 1 ; i <= 30 ; i++ )
{
for ( j = 1 ; j <= 30 ; j++ )
{
for ( k = 1 ; k <= 30 ; k++ )
{
for ( l = 1 ; l <= 30 ; l++ )
{
if ( ( i != j && i != k && i != l ) &&
( j != i && j != k && j != l ) &&
( k != i && k != j && k != l ) &&
( l != i && l != j && l != k ) )
{
if ( i * i * i + j * j * j == k * k * k + l * l * l )
printf ( "%d %d %d %d\n", i, j, k, l ) ;
}
}
}
}
}
return 0 ;
}

(m) Write a program to print 24 hours of day with suitable
suffixes like AM, PM, Noon and Midnight.
Program:

#include <stdio.h>
int main( )
{
int hour ;
for ( hour = 0 ; hour <= 23 ; hour++ )
{
if ( hour == 0 )
{
printf ( "12 Midnight\n" ) ;
continue ;
}
if ( hour < 12 )
printf ( "%d AM\n", hour ) ;
if ( hour == 12 )
printf ( "12 Noon\n" ) ;
if ( hour > 12 )
printf ( "%d PM\n", hour % 12 ) ;
}
return 0 ;
}

(n) Write a program to produce the following output:

# include <stdio.h>
int main( )
{
int i , j, k, l, sp ;
sp = 20 ;
for ( i = 1, k = 1 ; i < 5 ; i++ )
{
for ( l = 1 ; l <= sp ; l++ )
printf ( " " ) ;
sp -= 2 ;
for ( j = 1 ; j <= i ; j++, k++ )
printf ( " %d ", k ) ;
printf ( "\n" ) ;
}
return 0 ;
}

(o) Write a program to produce the following output:

# include <stdio.h>
int main( )
{
int i = 1, x = 71, blanks = 0, j, val , k ;
while ( i <= 7 )
{
j = 65 ; /* ASCII value of A */
val = x ;
while ( j <= val )
{
printf ( "%c", j ) ;
j++ ;
}
if ( i == 1 )
val-- ;
k = 1 ;
while ( k <= blanks )
{
printf ( " " ) ;
k++ ;
}
blanks = 2 * i - 1 ;
while ( val >= 65 )
{
printf ( "%c", val ) ;
val--;
}
printf ( "\n" ) ;
x-- ;
i++ ;
}
return 0 ;
}

(p) Write a program to produce the following output:
1
1 1

1 2 1
1 3 3 1
1 4 6 4 1

Program:

# include <stdio.h>
int main( )
{
int i, j, k, t, f1, f2, f3, z, sp ;
sp = 20 ;
for ( i = 0 ; i <= 4 ; i++ )
{
for ( k = 0 ; k < sp - i ; k++ )
printf ( " " ) ;
sp -= 2 ;
for ( j = 0 ; j <= i ; j++ )
{
f1 = f2 = f3 = 1 ;
t = i ;
while ( t != 0 )
{
f1 = f1 * t ;
t-- ;
}
t = j ;
while ( t != 0 )
{
f2 = f2 * t ;
t-- ;
}
t = i - j ;
while ( t != 0 )
{
f3 = f3 * t ;
t-- ;
}
z = f1 / ( f2 * f3 ) ;
printf ( " %4d ", z ) ;
}
printf ( "\n" ) ;
}
return 0 ;
}

Leave a Reply

Your email address will not be published.