All rights reserved. No part of this slide
may be reproduced, stored in a retrieval
system, or transmitted, in any form or by
any means, without the prior permission.
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
1. Let us C
(by yashvant Kanetkar) chapter 3
Solution
Writtenby
Hazrat Bilal(studentat AWKUM)
&
Muhammad Ishfaq Khan(student at AWKUM)
Revised by
Maimoona Jahanzeb Khattak(student at AWKUM)
While Loop
2. [A] What would be the output of the following programs:
a) main( ) Ans) 1
{ 2
int i=1 ; 3
while( i<= 10 ) 4
{ 5
printf ( "%dn", i) ; 6
i = i+ + ; 7
} 8
} 9
10
(b) main( ) Ans) nooutputbecause condition
{ false.
intx = 4 ;
while ( x == 1 )
{
x = x - 1 ;
printf ( "%dn",x ) ;
--x;
}
}
(c) main( ) Ans) 2 3 3
{
intx = 4, y, z ;
y = --x ;
z = x-- ;
printf ( "%d %d%dn",x,y, z ) ;
}
(d) main( ) Ans) 3 3 1
{
int x = 4, y = 3, z ;
z = x-- -y ;
printf ( "%d %d %dn", x, y, z ) ;
}
(e) main( ) Ans) malyalam isa palindrome will be
{ printedindefinitelybecause
while ( 'a' < 'b' ) ASCIIvalue of A(65) is less
printf ( "malyalamisa palindromen") ; than B(66).
}
(f) main( ) Ans) 10 will be printedindefinitely
{ because i initializedinsideloop.
inti ;
while ( i = 10 )
{
printf ( "%dn",i ) ;
i = i + 1 ;
} }
(g) main( ) Ans) Nooutputbecause while do
{ not understandfloatandright
3. floatx = 1.1 ; conditionis(x>1) thenitprint
while ( x == 1.1 ) value .
{
printf ( "%fn",x ) ;
x = x – 0.1 ;
}
}
(h) main( ) Ans) x=3 y=1
{ x=1 y=3
intx = 4, y = 0, z ; x=0 y=4
while ( x >= 0 ) x=-1 y=5
{
x-- ;
y++ ;
if ( x == y)
continue ;
else
printf ( “x=%d y=%dn”,x,y ) ;
}
}
(p) main( ) Ans) x=4 y=0
{ x=3 y=1
intx = 4, y = 0, z ;
while ( x >= 0 )
{
if ( x == y)
break;
else
printf ( “%d %dn”,x,y ) ;
x-- ;
y++ ;
}
}
[B] Attempt the following:
(a) 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.
Ans) #include <stdio.h>
intmain()
{
intemploy,overtime,pay,hours;
for(employ=1;employ<=10;employ++)
{
printf("nEnternumberof hoursworkedby%demploy=",employ);
scanf("%d",&hours);
if(hours>40)
{
overtime=hours-40;
pay=overtime*12;
4. printf("The overtimepayforemploy=%dn",pay);
}
else if(hours<40)
printf("the isnoovertimepayforemploy");
}
}
(b)Write a program to find the factorial value of any number
entered through the keyboard.
Ans) #include <stdio.h>
int main()
{
int a,num,fact=1;
printf("nEnter the number=");
scanf("%d",&num);
if(num==0)
fact=1;
else if(num>0)
{
for(a=1; a<=num; a++)
fact=fact*a;
}
printf("the factorial of a number %d is = %d",num,fact);
}
(c)Two numbers are entered through the keyboard. Write a
program to find the value of one number raised to the power of
another.
Ans) #include <stdio.h>
int main()
{
int a,b,c,d,result=1;
printf("nEnter any two numbers=");
scanf("%d %d",&a,&b);
c=a;
d=b;
while(b>0)
{
result=result*a;
b--;
}
printf("%d raised to the power %d =%d",c,d,result);
}
5. (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.
Ans) #include <stdio.h>
int main()
{
int num=0;
while(num<=255)
{
printf("num=%d num=%cn",num,num);
num++;
}
}
(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 )
Ans) #include <stdio.h>
int main()
{
int a,b,c,d,e,num=1,result;
while(num<=500)
{
a=num%10;
b=num/10;
c=b%10;
d=b/10;
e=d%10;
if(num==(a*a*a)+(c*c*c)+(e*e*e))
{
printf("%dn",num);
}
num++;
}
}
6. (f) 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.
Ans) #include <stdio.h>
int main()
{
int a,b=0,c=0,d=0,num;
printf("How many numbers do u want to enter?n");
scanf("%d",&a);
while(a>0)
{
printf("Enter number=");
scanf("%d",&num);
if(num>0)
b++;
if(num<0)
c++;
if(num==0)
d++;
a--;
}
printf("n you entered : n %d positive numbers n %d negative
numbers n %d zeros",b,c,d);
}
(g)Write a program to find the octalequivalent of the entered
number.
Ans) #include <stdio.h>
#include <math.h>
int main()
{
int decimal_num,remainder,octal_num=0,p=0;
printf("Enter the decimal number :");
scanf("%d",&decimal_num);
while(decimal_num!=0)
{
remainder=decimal_num%8;
octal_num=octal_num+remainder*pow(10,p);
decimal_num=decimal_num/8;
p++;
}
printf("The octal equivalent = %dn",octal_num);
}
7. (h)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.
Ans #include<stdio.h>
main()
{
int max=0,min=0,temp,n,i;
printf("what is the lenght of number set?nnumber:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("nNow enter the %d number :",i);
scanf("%d",&temp);
if(temp>max)max=temp;
if(i==1)min=temp;
if(temp<min)
min=temp;
}
printf("The range of set is %d",max-min);
}
for, break, continue, do-while
[C] What would be the output of the following programs:
(a) main( ) Output) no output because
{ condition is not
int i = 0 ; valid.
for ( ; i ; )
printf ( "Here is some mail for youn" ) ;
}
(b) main( ) Output) 1 will be printed
{ indefinitely.
int i ;
for ( i = 1 ; i <= 5 ; printf ( "%dn", i ) ) ;
i++ ;
}
(c) main() Output)j=2
{ j=5
int i = 1, j = 1 ;
for ( ; ; )
{
8. if ( i > 5 )
break ;
else
j += i ;
printf ( "nj=%d", j ) ;
i += j ;
}
}
[D] Answer the following:
a) The three parts of the loop expression in the for loop are:
the initialization expression
the testing expression
the increment or decrement expression
(b) The break statement is used to exit from:
1. an if statement
2. a for loop (correct)
3. a program
4. the main( ) function
(c) A do-while loop is useful when we want that the statements within the
loop must be executed:
1. Only once
2. At least once (correct)
3. More than once
4. None of the above
(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 (correct)
4. None of the above
(e) Which of the following is not an infinite loop.
1). int i = 1 ; 2).for (; ;)
while ( 1 )
{
i++ ;
}
3). intTrue = 0, false ; (Correct) 4).inty,x=0;
while ( True ) do
{ {
False = 1 ; y=x;
} }while(x==0)
9. (f) Which of the following statement is used to take the control to the
beginning of the loop?
1. exit
2. break
3. continue (correct)
4. None of the above
[E] Attempt the following
(a) Write a programto print all prime numbers from 1 to 300.
Ans) #include <stdio.h>
main()
{
int i,j=1;
while(j<=300)
{
i=2;
while(i<j)
{
if(j%i==0)
break;
else
i++;
}
if(i==j)
printf("%dt",j);
j++;
}
printf("nntpress any key to exit");
}
(b) Write a program to fill the entire screen with a smiling face.
The smiling face has an ASCII value 1.
Ans) #include <stdio.h>
main()
{
intr,c;
for(r=0; r<=11; r++)
{
for(c=0; c<=24; c++)
printf("%c",1);
}
}
10. (c) Write a program to add first seven terms of the following
series using a for loop:
1+ 2 + 3+…….
1! 2! 3!
Ans) #include <stdio.h>
main()
{
inta=1,b;
floatfact,sum=0.0;
while(a<=7)
{
fact=1.0;
for(b=1; b<=a; b++)
fact=fact*b;
sum=sum+a/fact;
a++;
}
printf("sumof series=%f",sum);
}
(d) Write a program to generate all combinations of 1, 2 and 3
using for loop.
Ans) #include <stdio.h>
int main()
{
int a,b,c;
for(a=1; a<=3; a++)
{
for(b=1; b<=3; b++)
{
for(c=1; c<=3; c++)
printf("%d %d %dn",a,b,c);
}
}
}
(e) Write a program to producethe following output:
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
Ans) #include <stdio.h>
int main()
{
11. int a,x,b=71,c=70,d=1,sp;
for(x=1; x<=7; x++)
{
for(a=65; a<=b; a++)
printf("%c",a);
if(x==2)
c=70;
for(sp=2; sp<d; sp++)
printf(" ");
for(a=c; a>=65; a--);
printf("%c",a);
printf("n");
b--;
c--;
d=d+2;
}
}
(f) Write a program to producethe following output:
1
2 3
4 5 6
7 8 9 10
Ans) #include <stdio.h>
int main()
{
int a,b,n=6;
for(a=1; a<=10; a++)
{
if(a==1 || a==2 || a==4 || a==7)
{
printf("n");
for(b=1; b<=n; b++)
printf(" ");
n=n-2;
}
printf("%4d",a);
}
}
(g) Write a program to producethe following output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Ans) #include<stdio.h>
12. #include<conio.h>
void main()
{
clrscr();
int lines, n=1, spaces, tabs=8, x, y=1;
for(lines=1;lines<=5;lines++)
{
for(spaces=1;spaces<=tabs;spaces++)
printf(" ");
printf("%4d",n);
if(lines>=3)
{
for(x=1;x<=y;x++)
{
if(x==2&&y==3)
printf("%4d",lines+1);
else
printf("%4d",lines-1);
}
y++;
}
if(lines>=2)
printf("%4d",n);
tabs=tabs-2;
printf("n");
}
getch();
}
(h) 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
…
Ans) #include <stdio.h>
int main()
{
int a,b;
printf("Enter any number to know its table till 10=");
scanf("%d",&a);
for(b=1; b<=10; b++)
{
printf("%d X %d = %d",a,b,b*a);
printf("n");
}
}
(i)According to a study, the approximate level of intelligence of a person can
be calculated using the following formula:
13. i = 2 + ( y + 0.5 x )
Write a program, which 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.
Ans) #include<stdio.h>
void main()
{
float i, y, x;
printf("Values Ofn");
printf("ti tty ttxn");
for(y=1;y<=6;y++)
{
for(x=5.5;x<=12.5;x=x+0.5)
{
i=2+(y + (0.5*x));
printf("%10f t%10f t%10fn",i,y,x);
}
}
}
Ans) #include <stdio.h>
int main()
{
int a,b,x,d,e=1;
float result,c1=1;
printf("Enter the value of X=");
scanf("%d",&x);
for(a=1; a<=7; a++)
{
for(b=1,c1=1; b<=e; b++)
{
c1=c1*(x-1)/x;
}
e++;
if(a>=2)
result=result+(0.5*c1);
else
result=result+1;