SlideShare a Scribd company logo
2
Most read
3
Most read
11
Most read
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

PDF
Let us c(by yashwant kanetkar) chapter 2 solution
PDF
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
DOCX
Let us c (by yashvant kanetkar) chapter 1 solution
DOCX
Chapter 8 c solution
PDF
Forever Living Business Presentation
PDF
SOLUTION MANUAL OF WIRELESS COMMUNICATIONS BY THEODORE S RAPPAPORT
PPTX
Psychology
PPTX
Final presentation on chatbot
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by Yashwant Kanetkar) 5th edition solution chapter 1
Let us c (by yashvant kanetkar) chapter 1 solution
Chapter 8 c solution
Forever Living Business Presentation
SOLUTION MANUAL OF WIRELESS COMMUNICATIONS BY THEODORE S RAPPAPORT
Psychology
Final presentation on chatbot

What's hot (20)

PDF
Let us c chapter 4 solution
DOC
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
PDF
Chapter 2 : Balagurusamy_ Programming ANsI in C
PDF
LET US C (5th EDITION) CHAPTER 2 ANSWERS
PDF
Chapter 1 : Balagurusamy_ Programming ANsI in C
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
PDF
Chapter 6 Balagurusamy Programming ANSI in c
PDF
Chapter 3 : Balagurusamy Programming ANSI in C
PPTX
Strings in C
PPTX
Programming in c Arrays
PPTX
Control statements in c
PDF
The solution manual of programming in ansi by Robin
PPTX
C if else
PPTX
Functions in C
PPTX
Presentation on Function in C Programming
PPT
Function overloading(c++)
PPTX
Fundamentals of c programming
PDF
Constants Variables Datatypes by Mrs. Sowmya Jyothi
PPTX
Loops in C Programming Language
PPTX
class and objects
Let us c chapter 4 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Chapter 2 : Balagurusamy_ Programming ANsI in C
LET US C (5th EDITION) CHAPTER 2 ANSWERS
Chapter 1 : Balagurusamy_ Programming ANsI in C
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 3 : Balagurusamy Programming ANSI in C
Strings in C
Programming in c Arrays
Control statements in c
The solution manual of programming in ansi by Robin
C if else
Functions in C
Presentation on Function in C Programming
Function overloading(c++)
Fundamentals of c programming
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Loops in C Programming Language
class and objects
Ad

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

DOCX
ADA FILE
DOCX
PPT
All important c programby makhan kumbhkar
DOC
Basic c programs updated on 31.8.2020
PDF
C Programming Example
DOCX
DOCX
Operating system labs
PPTX
C programming
PDF
PCA-2 Programming and Solving 2nd Sem.pdf
DOCX
PCA-2 Programming and Solving 2nd Sem.docx
DOC
'C' language notes (a.p)
PDF
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
PDF
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
PDF
DSC program.pdf
DOCX
Write a program to check a given number is prime or not
DOCX
Practical write a c program to reverse a given number
DOCX
Practical write a c program to reverse a given number
PDF
PDF
programs on arrays.pdf
ADA FILE
All important c programby makhan kumbhkar
Basic c programs updated on 31.8.2020
C Programming Example
Operating system labs
C programming
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.docx
'C' language notes (a.p)
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
DSC program.pdf
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 number
programs on arrays.pdf
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
AI in Product Development-omnex systems
PPTX
Transform Your Business with a Software ERP System
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Understanding Forklifts - TECH EHS Solution
PDF
top salesforce developer skills in 2025.pdf
PPTX
Essential Infomation Tech presentation.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
AI in Product Development-omnex systems
Transform Your Business with a Software ERP System
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Operating system designcfffgfgggggggvggggggggg
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Choose the Right IT Partner for Your Business in Malaysia
Adobe Illustrator 28.6 Crack My Vision of Vector Design
wealthsignaloriginal-com-DS-text-... (1).pdf
Odoo POS Development Services by CandidRoot Solutions
VVF-Customer-Presentation2025-Ver1.9.pptx
Digital Strategies for Manufacturing Companies
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
2025 Textile ERP Trends: SAP, Odoo & Oracle
Understanding Forklifts - TECH EHS Solution
top salesforce developer skills in 2025.pdf
Essential Infomation Tech presentation.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Which alternative to Crystal Reports is best for small or large businesses.pdf

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