SlideShare a Scribd company logo
1 of 8
Download to read offline
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
[A] What would be the output of the following programs:
(a) main( )
{
char suite = 3 ;
switch ( suite )
{
case 1 :
printf ( "nDiamond" ) ;
case 2 :
printf ( "nSpade" ) ;
default :
printf ( "nHeart") ;
}
printf ( "nI thought one wears a suite" ) ;
}
Ans)
Heart
I thought one wears a suite
(b) main( )
{
int c = 3 ;
switch ( c )
{
case 'v' :
printf ( "I am in case v n" ) ;
break ;
case 3 :
printf ( "I am in case 3 n" ) ;
break ;
case 12 :
printf ( "I am in case 12 n" ) ;
break ;
default :
printf ( "I am in default n" ) ;
}
}
Ans)
I am in case 3
(c) main( )
{
int k, j = 2 ;
switch ( k = j + 1 )
{
case 0 :
printf ( "nTailor") ;
case 1 :
printf ( "nTutor") ;
case 2 :
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
printf ( "nTramp") ;
default :
printf ( "nPure Simple Egghead!" ) ;
}
}
Ans)
Pure Simple Egghead!
(d) main( )
{
int i = 0 ;
switch ( i )
{
case 0 :
printf ( "nCustomers are dicey" ) ;
case 1 :
printf ( "nMarkets are pricey" ) ;
case 2 :
printf ( "nInvestors are moody" ) ;
case 3 :
printf ( "nAt least employees are good" ) ;
}
}
Ans)
Customers are dicey
Markets are pricy
Investors are moody
At least employees are good
(e) main( )
{
int k ;
float j = 2.0 ;
switch ( k = j + 1 )
{
case 3 :
printf ( "nTrapped" ) ;
break ;
default :
printf ( "nCaught!" ) ;
}
}
Ans)
Trapped
(f) main( )
{
int ch = 'a' + 'b' ;
switch ( ch )
{
case 'a' :
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
case 'b' :
printf ( "nYou entered b" ) ;
case 'A' :
printf ( "na as in ashar" ) ;
case 'b' + 'a' :
printf ( "nYou entered a and b" ) ;
}
}
Ans)
You entered a and b
(g) main( )
{
int i = 1 ;
switch ( i - 2 )
{
case -1 :
printf ( "nFeeding fish" ) ;
case 0 :
printf ( "nWeeding grass" ) ;
case 1 :
printf ( "nmending roof" ) ;
default :
printf ( "nJust to survive" ) ;
}
}
Ans)
Feeding Fish
Weeding Grass
Mending Roof
Just to survive
[B] Point out the errors, if any, in the following programs:
(a) main( )
{
int suite = 1 ;
switch ( suite ) ;
{
case 0 ;
printf ( "nClub" ) ;
case 1 ;
printf ( "nDiamond" ) ;
}
}
Ans) There is no requirement of a semicolon after switch. This is a syntax error.
(b) main( )
{
int temp ;
scanf ( "%d", &temp ) ;
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
switch ( temp )
{
case ( temp <= 20 ) :
printf ( "nOoooooohhhh! Damn cool!" ) ;
case ( temp > 20 && temp <= 30 ) :
printf ( "nRain rain here again!" ) ;
case ( temp > 30 && temp <= 40 ) :
printf ( "nWish I am on Everest" ) ;
default :
printf ( "nGood old nagpur weather" ) ;
}
}
Ans) Case can only contain character and integer constants.
(c) main( )
{
float a = 3.5 ;
switch ( a )
{
case 0.5 :
printf ( "nThe art of C" ) ;
break ;
case 1.5 :
printf ( "nThe spirit of C" ) ;
break ;
case 2.5 :
printf ( "nSee through C" ) ;
break ;
case 3.5 :
printf ( "nSimply c" ) ;
}
}
Ans) We dont write float values inside case. We can only write them inside switch.
(d) main( )
{
int a = 3, b = 4, c ;
c = b – a ;
switch ( c )
{
case 1 || 2 :
printf ( "God give me an opportunity to change things" ) ;
break ;
case a || b :
printf ( "God give me an opportunity to run my show" ) ;
break ;
}
}
Ans) Case can only contain integer or character values.
[C] Write a menu driven program which has following options:
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
1. Factorial of a number.
2. Prime or not
3. Odd or even
4. Exit
Make use of switch statement.
The outline of this program is given below:
/* A menu driven program */
main( )
{
int choice ;
while ( 1 )
{
printf ( "n1. Factorial" ) ;
printf ( "n2. Prime" ) ;
printf ( "n3. Odd/Even" ) ;
printf ( "n4. Exit" ) ;
printf ( "nYour choice? " ) ;
scanf ( "%d", &choice ) ;
switch ( choice )
{
case 1 :
/* logic for factorial of a number */
break ;
case 2 :
/* logic for deciding prime number */
break ;
case 3 :
/* logic for odd/even */
break ;
case 4 :
exit( ) ;
}
}
}
Note:
The statement while ( 1 ) puts the entire logic in an infinite loop. This is necessary since the menu
must keep reappearing on the screen once an item is selected and an appropriate action taken.
#include <stdio.h>
int main()
{
int choice;
printf("The choices are:n1. Factorialn2. Odd Evenn3. Prime Compositenn");
printf("1 is for Factorialn2 is for Odd and evenn3 is for Prime and composite.nEnter your choice:
");
scanf(" %d",&choice);
switch(choice)
{
case 1:
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
{
int a,num,fact = 1;
printf("Enter 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 %d is %d",num,fact);
}
break;
case 2:
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("The number is an even number");
}
else
{
printf("The number is an odd number");
}
}
break;
case 3:
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 1;
}
}
if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
else
printf("%d is not a prime number.", n);
}
}
break;
case 4:
Break;
default:
printf("Only write the choices mentioned those are case sensitive.");
}
return 0;
}
[D] Write a program which to find the grace marks for a student using switch. The user should enter
the class obtained by the student and the number of subjects he has failed in.
− If the student gets first class and the number of subjects he failed in is greater than 3, then he does
not get any grace. If the number of subjects he failed in is less than or equal to 3 then the grace is of
5 marks per subject.
− If the student gets second class and the number of subjects he failed in is greater than 2, then he
does not get any grace. If the number of subjects he failed in is less than or equal to 2 then the grace
is of 4 marks per subject.
− If the student gets third class and the number of subjects he failed in is greater than 1, then he
does not get any grace. If the number of subjects he failed in is equal to 1 then the grace is of 5
marks per subject
#include <stdio.h>
int main()
{
int class,subj;
printf("Enter the class obtained by the student(1 or 2 or 3): ");
scanf(" %d",&class);
printf("Enter the number of subjects you have failed: ");
scanf(" %d",&subj);
switch (class)
{
case 1:
{
if (subj > 3)
printf("You do not get any grace marks.");
else if (subj <= 3)
printf("The grace is of 5 marks per subject.");
}
break;
case 2:
{
if (subj > 2)
LET US C SLOUTIONS (5th
EDITION) CHAPTER 4
BY-KAVYA SHARMA
printf("You do not get any grace marks.");
else if (subj <= 2)
printf("The grace is 4 marks per subject.");
}
break;
case 3:
{
if (subj > 1)
printf("You do not get any grace marks.");
else if (subj == 1)
printf("The grace is 5 marks per subject.");
}
break;
default:
printf("If you have written 1st div or anything else. Please only write 1, 2 or 3.");
}
return 0;
}

More Related Content

What's hot

The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin cVikash Dhal
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming DharmaKumariBhandari
 
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 ANSWERSKavyaSharma65
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSKavyaSharma65
 
Break and continue statement in C
Break and continue statement in CBreak and continue statement in C
Break and continue statement in CInnovative
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshareGagan Deep
 
C programs
C programsC programs
C programsMinu S
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For LoopSukrit Gupta
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programmingPriyansh Thakar
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow ChartRahul Sahu
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointersMomenMostafa
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 

What's hot (20)

CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming
 
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
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
Ansi c
Ansi cAnsi c
Ansi c
 
Break and continue statement in C
Break and continue statement in CBreak and continue statement in C
Break and continue statement in C
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
C programs
C programsC programs
C programs
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
C Programming
C ProgrammingC Programming
C Programming
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
7 functions
7  functions7  functions
7 functions
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
For Loop
For LoopFor Loop
For Loop
 

Similar to LET US C (5th EDITION) CHAPTER 4 ANSWERS

Similar to LET US C (5th EDITION) CHAPTER 4 ANSWERS (20)

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
Vcs4
Vcs4Vcs4
Vcs4
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
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
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
Presentation1
Presentation1Presentation1
Presentation1
 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
control statement
control statement control statement
control statement
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
Conditional statements
Conditional statementsConditional statements
Conditional statements
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Control flow in c
Control flow in cControl flow in c
Control flow in c
 
C programming
C programmingC programming
C programming
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 

Recently uploaded

ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
How to Manage Engineering to Order in Odoo 17
How to Manage Engineering to Order in Odoo 17How to Manage Engineering to Order in Odoo 17
How to Manage Engineering to Order in Odoo 17Celine George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Production of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptxProduction of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptxAnupkumar Sharma
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptxMECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptxAnupkumar Sharma
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 

Recently uploaded (20)

ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
How to Manage Engineering to Order in Odoo 17
How to Manage Engineering to Order in Odoo 17How to Manage Engineering to Order in Odoo 17
How to Manage Engineering to Order in Odoo 17
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Production of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptxProduction of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptx
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptxMECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 

LET US C (5th EDITION) CHAPTER 4 ANSWERS

  • 1. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA [A] What would be the output of the following programs: (a) main( ) { char suite = 3 ; switch ( suite ) { case 1 : printf ( "nDiamond" ) ; case 2 : printf ( "nSpade" ) ; default : printf ( "nHeart") ; } printf ( "nI thought one wears a suite" ) ; } Ans) Heart I thought one wears a suite (b) main( ) { int c = 3 ; switch ( c ) { case 'v' : printf ( "I am in case v n" ) ; break ; case 3 : printf ( "I am in case 3 n" ) ; break ; case 12 : printf ( "I am in case 12 n" ) ; break ; default : printf ( "I am in default n" ) ; } } Ans) I am in case 3 (c) main( ) { int k, j = 2 ; switch ( k = j + 1 ) { case 0 : printf ( "nTailor") ; case 1 : printf ( "nTutor") ; case 2 :
  • 2. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA printf ( "nTramp") ; default : printf ( "nPure Simple Egghead!" ) ; } } Ans) Pure Simple Egghead! (d) main( ) { int i = 0 ; switch ( i ) { case 0 : printf ( "nCustomers are dicey" ) ; case 1 : printf ( "nMarkets are pricey" ) ; case 2 : printf ( "nInvestors are moody" ) ; case 3 : printf ( "nAt least employees are good" ) ; } } Ans) Customers are dicey Markets are pricy Investors are moody At least employees are good (e) main( ) { int k ; float j = 2.0 ; switch ( k = j + 1 ) { case 3 : printf ( "nTrapped" ) ; break ; default : printf ( "nCaught!" ) ; } } Ans) Trapped (f) main( ) { int ch = 'a' + 'b' ; switch ( ch ) { case 'a' :
  • 3. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA case 'b' : printf ( "nYou entered b" ) ; case 'A' : printf ( "na as in ashar" ) ; case 'b' + 'a' : printf ( "nYou entered a and b" ) ; } } Ans) You entered a and b (g) main( ) { int i = 1 ; switch ( i - 2 ) { case -1 : printf ( "nFeeding fish" ) ; case 0 : printf ( "nWeeding grass" ) ; case 1 : printf ( "nmending roof" ) ; default : printf ( "nJust to survive" ) ; } } Ans) Feeding Fish Weeding Grass Mending Roof Just to survive [B] Point out the errors, if any, in the following programs: (a) main( ) { int suite = 1 ; switch ( suite ) ; { case 0 ; printf ( "nClub" ) ; case 1 ; printf ( "nDiamond" ) ; } } Ans) There is no requirement of a semicolon after switch. This is a syntax error. (b) main( ) { int temp ; scanf ( "%d", &temp ) ;
  • 4. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA switch ( temp ) { case ( temp <= 20 ) : printf ( "nOoooooohhhh! Damn cool!" ) ; case ( temp > 20 && temp <= 30 ) : printf ( "nRain rain here again!" ) ; case ( temp > 30 && temp <= 40 ) : printf ( "nWish I am on Everest" ) ; default : printf ( "nGood old nagpur weather" ) ; } } Ans) Case can only contain character and integer constants. (c) main( ) { float a = 3.5 ; switch ( a ) { case 0.5 : printf ( "nThe art of C" ) ; break ; case 1.5 : printf ( "nThe spirit of C" ) ; break ; case 2.5 : printf ( "nSee through C" ) ; break ; case 3.5 : printf ( "nSimply c" ) ; } } Ans) We dont write float values inside case. We can only write them inside switch. (d) main( ) { int a = 3, b = 4, c ; c = b – a ; switch ( c ) { case 1 || 2 : printf ( "God give me an opportunity to change things" ) ; break ; case a || b : printf ( "God give me an opportunity to run my show" ) ; break ; } } Ans) Case can only contain integer or character values. [C] Write a menu driven program which has following options:
  • 5. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA 1. Factorial of a number. 2. Prime or not 3. Odd or even 4. Exit Make use of switch statement. The outline of this program is given below: /* A menu driven program */ main( ) { int choice ; while ( 1 ) { printf ( "n1. Factorial" ) ; printf ( "n2. Prime" ) ; printf ( "n3. Odd/Even" ) ; printf ( "n4. Exit" ) ; printf ( "nYour choice? " ) ; scanf ( "%d", &choice ) ; switch ( choice ) { case 1 : /* logic for factorial of a number */ break ; case 2 : /* logic for deciding prime number */ break ; case 3 : /* logic for odd/even */ break ; case 4 : exit( ) ; } } } Note: The statement while ( 1 ) puts the entire logic in an infinite loop. This is necessary since the menu must keep reappearing on the screen once an item is selected and an appropriate action taken. #include <stdio.h> int main() { int choice; printf("The choices are:n1. Factorialn2. Odd Evenn3. Prime Compositenn"); printf("1 is for Factorialn2 is for Odd and evenn3 is for Prime and composite.nEnter your choice: "); scanf(" %d",&choice); switch(choice) { case 1:
  • 6. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA { int a,num,fact = 1; printf("Enter 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 %d is %d",num,fact); } break; case 2: { int number=0; printf("Enter a number:"); scanf("%d",&number); if(number%2==0) { printf("The number is an even number"); } else { printf("The number is an odd number"); } } break; case 3: { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &n); for (i = 2; i <= n / 2; ++i) { if (n % i == 0) { flag = 1; } } if (n == 1) { printf("1 is neither prime nor composite."); } else { if (flag == 0) printf("%d is a prime number.", n);
  • 7. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA else printf("%d is not a prime number.", n); } } break; case 4: Break; default: printf("Only write the choices mentioned those are case sensitive."); } return 0; } [D] Write a program which to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in. − If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. If the number of subjects he failed in is less than or equal to 3 then the grace is of 5 marks per subject. − If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. If the number of subjects he failed in is less than or equal to 2 then the grace is of 4 marks per subject. − If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. If the number of subjects he failed in is equal to 1 then the grace is of 5 marks per subject #include <stdio.h> int main() { int class,subj; printf("Enter the class obtained by the student(1 or 2 or 3): "); scanf(" %d",&class); printf("Enter the number of subjects you have failed: "); scanf(" %d",&subj); switch (class) { case 1: { if (subj > 3) printf("You do not get any grace marks."); else if (subj <= 3) printf("The grace is of 5 marks per subject."); } break; case 2: { if (subj > 2)
  • 8. LET US C SLOUTIONS (5th EDITION) CHAPTER 4 BY-KAVYA SHARMA printf("You do not get any grace marks."); else if (subj <= 2) printf("The grace is 4 marks per subject."); } break; case 3: { if (subj > 1) printf("You do not get any grace marks."); else if (subj == 1) printf("The grace is 5 marks per subject."); } break; default: printf("If you have written 1st div or anything else. Please only write 1, 2 or 3."); } return 0; }