Ad

Chapter - 4: The Loop Control Structure

What would be the output of the following programs:


Ad
A
Sections
1
Exercises

(a)

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

Output: 

1

2

3

4

5

6

7

8

9

10


(b)

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

Output: Nothing, just blank screen.


(c)

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

Output: 2 3 4


(d)

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

Output: 3 3 1


(e)

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

Output: malyalam is a palindrome (infinite times.)


(f)

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

Output: 10 (infinite times.)


(g)

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

Output: Nothing, just blank screen.


(h)

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

Output: 

 3  1

 1  3

 0  4

-1  5


(i)

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

Output: 

4 0

3 1

(Then infinite loop printing nothing)

Ad

© 2024 Garbage Valuegarbage value logo