EXERCISE: 6 Logical Control Statement

What would be the output of the following programs:
(a) main( )
{
int j ;
while ( j <= 10 ) { printf ( "\n%d", j ) ; j = j + 1 ; } } Rectified: The program has an error. Correct is given bellow: #include
#include
void main()
{
int j=12 ;
while ( j >= 10 )
{
printf ( "\n%d", j ) ;
j = j - 1 ;
}getch();
}
Output: 12
11
10

(b) main( )
{
char x ;
while ( x = 0 ; x <= 255 ; x++ ) printf ( "\nAscii value %d Character %c", x, x ) ; } Rectified: The program has an error. Correct is given bellow: #include
#include
void main()
{
char x ;
clrscr();
for ( x = 0 ; x <= 10 ; x++ ) printf ( "\nAscii value %d Character %c", x, x ) ; getch(); } Output: (c) main( ) { int i = 1, j = 1 ; for ( ; ; ) { if ( i > 5 )
break ;
else
j += i ;
printf ( "\n%d", j ) ;
i += j ;
}
}
Output: 2
5

Attempt the following:
(a)Write a program to find the factorial value of any number entered through the keyboard.
Ans: #include
#include
long int i,j,v ;
main()
{ clrscr();
printf("enter value\n");
scanf("%ld", &v);
j=1;
for (i=1; i<=v; i++)
{
j=j*i;
}
printf("The factorial of %ld is %ld\n",v,j);
getch();
return 0;
}
Input: enter value
8
Output: The factorial of 8 is 40320

Followers