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

More Related Content

What's hot

Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
Hazrat Bilal
 
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
BUBT
 
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
BUBT
 
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
BUBT
 
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
BUBT
 
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
Shariful Haque Robin
 
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
BUBT
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
RAJWANT KAUR
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
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
BUBT
 
String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
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
Abdullah Al Naser
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
Sreedhar Chowdam
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
Prabhu Govind
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
JeeSa Sultana
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
kash95
 

What's hot (20)

Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using 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 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 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 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
 
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
 
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
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Call by value
Call by valueCall by value
Call by value
 
Ansi c
Ansi cAnsi c
Ansi c
 
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
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Functions in c language
Functions in c language Functions in c language
Functions 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
 
Function in c program
Function in c programFunction in c program
Function in c program
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 

Similar to Let us C (by yashvant Kanetkar) chapter 3 Solution

ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
C file
C fileC file
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
sandeep 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
vrgokila
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
Cpds lab
Cpds labCpds lab
Pnno
PnnoPnno
Operating system labs
Operating system labsOperating system labs
Operating system labs
bhaktisagar4
 
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
Sazzad Hossain, ITP, MBA, CSCA™
 
C programming
C programmingC programming
C programming
Samsil Arefin
 
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
Ashutoshprasad27
 
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
Ashutoshprasad27
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
Ashishchinu
 
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
Srinivas Reddy Amedapu
 
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
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
Prof. Dr. K. Adisesha
 
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
aluavi
 
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
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment

Similar to 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 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
 
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
 
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
 

Recently uploaded

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
KrzysztofKkol1
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 

Recently uploaded (20)

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 

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