SlideShare a Scribd company logo
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

Soal perkalian bilangan berpangkat pilihan ganda
Soal perkalian bilangan berpangkat pilihan gandaSoal perkalian bilangan berpangkat pilihan ganda
Soal perkalian bilangan berpangkat pilihan ganda
Dedih Supriadi
 
Bentuk akar dan pangkat.pptx
Bentuk akar dan pangkat.pptxBentuk akar dan pangkat.pptx
Bentuk akar dan pangkat.pptx
PutriDewintari1
 
Soal pilihan ganda validitas pembuktian
Soal pilihan ganda validitas pembuktianSoal pilihan ganda validitas pembuktian
Soal pilihan ganda validitas pembuktian
Anderzend Awuy
 
Materi 7 c++ array
Materi 7 c++ arrayMateri 7 c++ array
Materi 7 c++ array
imroneffendi1
 
Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021
Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021
Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021
Saprudin Eskom
 
Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)
Fahrul Usman
 
Logika dasr
Logika dasrLogika dasr
Integral calculus formula sheet
Integral calculus formula sheetIntegral calculus formula sheet
Integral calculus formula sheet
AjEcuacion
 
Text Mode Programming in Assembly
Text Mode Programming in AssemblyText Mode Programming in Assembly
Text Mode Programming in Assembly
Javeria Yaqoob
 
1. Algoritma, Struktur Data dan Pemrograman Terstruktur
1. Algoritma, Struktur Data dan Pemrograman Terstruktur1. Algoritma, Struktur Data dan Pemrograman Terstruktur
1. Algoritma, Struktur Data dan Pemrograman Terstruktur
Kelinci Coklat
 
Bab 1 bentuk pangkat, akar & logaritma
Bab 1 bentuk pangkat, akar & logaritmaBab 1 bentuk pangkat, akar & logaritma
Bab 1 bentuk pangkat, akar & logaritmamfebri26
 
Pewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas Majalengka
Pewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas MajalengkaPewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas Majalengka
Pewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas Majalengka
Fauzie N
 
PERSAMAAN KUADRAT
PERSAMAAN KUADRATPERSAMAAN KUADRAT
PERSAMAAN KUADRAT
PutriMutiarasari1
 
Materi Aljabar dalil sisa
Materi Aljabar dalil sisaMateri Aljabar dalil sisa
Materi Aljabar dalil sisa
Sriwijaya University
 
2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)
syarifaminullah2
 
Teori bilangan
Teori bilanganTeori bilangan
Teori bilangan
Ujang Kbm
 
Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)
Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)
Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)
Catur Prasetyo
 
Persamaan Diferensial Biasa (PDB) Orde 1
Persamaan Diferensial Biasa (PDB) Orde 1Persamaan Diferensial Biasa (PDB) Orde 1
Persamaan Diferensial Biasa (PDB) Orde 1
made dwika
 
Java (Netbeans) - Exception handling - Object Oriented Programming
Java (Netbeans) - Exception handling - Object Oriented ProgrammingJava (Netbeans) - Exception handling - Object Oriented Programming
Java (Netbeans) - Exception handling - Object Oriented ProgrammingMelina Krisnawati
 

What's hot (20)

Soal perkalian bilangan berpangkat pilihan ganda
Soal perkalian bilangan berpangkat pilihan gandaSoal perkalian bilangan berpangkat pilihan ganda
Soal perkalian bilangan berpangkat pilihan ganda
 
Bentuk akar dan pangkat.pptx
Bentuk akar dan pangkat.pptxBentuk akar dan pangkat.pptx
Bentuk akar dan pangkat.pptx
 
Soal pilihan ganda validitas pembuktian
Soal pilihan ganda validitas pembuktianSoal pilihan ganda validitas pembuktian
Soal pilihan ganda validitas pembuktian
 
Materi 7 c++ array
Materi 7 c++ arrayMateri 7 c++ array
Materi 7 c++ array
 
Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021
Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021
Soal uts/ pts semester genap mapel pbo kelas xi rpl tahun 2021
 
Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)
 
Logika dasr
Logika dasrLogika dasr
Logika dasr
 
Integral calculus formula sheet
Integral calculus formula sheetIntegral calculus formula sheet
Integral calculus formula sheet
 
Text Mode Programming in Assembly
Text Mode Programming in AssemblyText Mode Programming in Assembly
Text Mode Programming in Assembly
 
1. Algoritma, Struktur Data dan Pemrograman Terstruktur
1. Algoritma, Struktur Data dan Pemrograman Terstruktur1. Algoritma, Struktur Data dan Pemrograman Terstruktur
1. Algoritma, Struktur Data dan Pemrograman Terstruktur
 
Bab 1 bentuk pangkat, akar & logaritma
Bab 1 bentuk pangkat, akar & logaritmaBab 1 bentuk pangkat, akar & logaritma
Bab 1 bentuk pangkat, akar & logaritma
 
Pewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas Majalengka
Pewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas MajalengkaPewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas Majalengka
Pewarnaan Graf Kelompok 3 Pendidikan Matematika FKIP Universitas Majalengka
 
PERSAMAAN KUADRAT
PERSAMAAN KUADRATPERSAMAAN KUADRAT
PERSAMAAN KUADRAT
 
Graf pohon (bagian ke 6)
Graf pohon (bagian ke 6)Graf pohon (bagian ke 6)
Graf pohon (bagian ke 6)
 
Materi Aljabar dalil sisa
Materi Aljabar dalil sisaMateri Aljabar dalil sisa
Materi Aljabar dalil sisa
 
2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)2 inklusi eksklusi (kelas c)
2 inklusi eksklusi (kelas c)
 
Teori bilangan
Teori bilanganTeori bilangan
Teori bilangan
 
Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)
Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)
Smart solution un matematika sma 2013 (skl 5.2 aplikasi turunan fungsi)
 
Persamaan Diferensial Biasa (PDB) Orde 1
Persamaan Diferensial Biasa (PDB) Orde 1Persamaan Diferensial Biasa (PDB) Orde 1
Persamaan Diferensial Biasa (PDB) Orde 1
 
Java (Netbeans) - Exception handling - Object Oriented Programming
Java (Netbeans) - Exception handling - Object Oriented ProgrammingJava (Netbeans) - Exception handling - Object Oriented Programming
Java (Netbeans) - Exception handling - Object Oriented Programming
 

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

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
MomenMostafa
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Vcs4
Vcs4Vcs4
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
RAJWANT KAUR
 
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
BUBT
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
rakshithatan
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
Presentation1
Presentation1Presentation1
Presentation1
daisy_arcangel
 
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
alish sha
 
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
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
ab11cs001
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
Giri383500
 
control statement
control statement control statement
control statement
Kathmandu University
 
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
Hazrat Bilal
 

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

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
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
 
7 functions
7  functions7  functions
7 functions
 
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
 
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiM2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
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
 
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
 
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
 
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
 

Recently uploaded

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 

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; }