SlideShare ist ein Scribd-Unternehmen logo
1 von 14
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
[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
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;
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);
}
(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++;
}
}
(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);
}
(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 ( ; ; )
{
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)
(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);
}
}
(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()
{
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>
#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:
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;
}
printf("sum of the first seven terms of the give series =%d",result); }

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
 
Chapter 1 : Balagurusamy_ Programming ANsI in C
Chapter 1  :  Balagurusamy_ Programming ANsI in C Chapter 1  :  Balagurusamy_ Programming ANsI in C
Chapter 1 : Balagurusamy_ Programming ANsI in C
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
Ansi c
Ansi cAnsi c
Ansi c
 
Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C Chapter 3 : Balagurusamy Programming ANSI in C
Chapter 3 : Balagurusamy Programming ANSI in C
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
The solution manual of programming in ansi by Robin
The solution manual of programming in ansi by RobinThe solution manual of programming in ansi by Robin
The solution manual of programming in ansi by Robin
 
C string
C stringC string
C string
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
FUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdfFUNCTIONS IN C PROGRAMMING.pdf
FUNCTIONS IN C PROGRAMMING.pdf
 
LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS
 
Strings in c
Strings in cStrings in c
Strings in c
 
Functions in c
Functions in cFunctions in c
Functions in c
 

Ähnlich wie Let us C (by yashvant Kanetkar) chapter 3 Solution

Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 

Ähnlich wie Let us C (by yashvant Kanetkar) chapter 3 Solution (20)

ADA FILE
ADA FILEADA FILE
ADA FILE
 
C file
C fileC file
C file
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Pnno
PnnoPnno
Pnno
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
C programming
C programmingC programming
C programming
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 

Kürzlich hochgeladen

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Kürzlich hochgeladen (20)

Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

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;
  • 14. } printf("sum of the first seven terms of the give series =%d",result); }