SlideShare une entreprise Scribd logo
1  sur  14
Let us C
(by yashvant Kanetkar) chapter 3
Solution
Writtenby
Hazrat Bilal(studentat AWKUM)
&
Muhammad Ishfaq Khan(student at AWKUM)
Revised by
Maimoona Jahanzeb Khattak(student at AWKUM)
While Loop
[A] What would be the output of the following programs:
a) main( ) Ans) 1
{ 2
int i=1 ; 3
while( i<= 10 ) 4
{ 5
printf ( "%dn", i) ; 6
i = i+ + ; 7
} 8
} 9
10
(b) main( ) Ans) nooutputbecause condition
{ false.
intx = 4 ;
while ( x == 1 )
{
x = x - 1 ;
printf ( "%dn",x ) ;
--x;
}
}
(c) main( ) Ans) 2 3 3
{
intx = 4, y, z ;
y = --x ;
z = x-- ;
printf ( "%d %d%dn",x,y, z ) ;
}
(d) main( ) Ans) 3 3 1
{
int x = 4, y = 3, z ;
z = x-- -y ;
printf ( "%d %d %dn", x, y, z ) ;
}
(e) main( ) Ans) malyalam isa palindrome will be
{ printedindefinitelybecause
while ( 'a' < 'b' ) ASCIIvalue of A(65) is less
printf ( "malyalamisa palindromen") ; than B(66).
}
(f) main( ) Ans) 10 will be printedindefinitely
{ because i initializedinsideloop.
inti ;
while ( i = 10 )
{
printf ( "%dn",i ) ;
i = i + 1 ;
} }
(g) main( ) Ans) Nooutputbecause while do
{ not understandfloatandright
floatx = 1.1 ; conditionis(x>1) thenitprint
while ( x == 1.1 ) value .
{
printf ( "%fn",x ) ;
x = x – 0.1 ;
}
}
(h) main( ) Ans) x=3 y=1
{ x=1 y=3
intx = 4, y = 0, z ; x=0 y=4
while ( x >= 0 ) x=-1 y=5
{
x-- ;
y++ ;
if ( x == y)
continue ;
else
printf ( “x=%d y=%dn”,x,y ) ;
}
}
(p) main( ) Ans) x=4 y=0
{ x=3 y=1
intx = 4, y = 0, z ;
while ( x >= 0 )
{
if ( x == y)
break;
else
printf ( “%d %dn”,x,y ) ;
x-- ;
y++ ;
}
}
[B] Attempt the following:
(a) Write a program to calculate overtime pay of 10 employees.
Overtime is paid at the rate of Rs. 12.00 per hour for every
hour worked above 40 hours. Assume that employees do not
work for fractional part of an hour.
Ans) #include <stdio.h>
intmain()
{
intemploy,overtime,pay,hours;
for(employ=1;employ<=10;employ++)
{
printf("nEnternumberof hoursworkedby%demploy=",employ);
scanf("%d",&hours);
if(hours>40)
{
overtime=hours-40;
pay=overtime*12;
printf("The overtimepayforemploy=%dn",pay);
}
else if(hours<40)
printf("the isnoovertimepayforemploy");
}
}
(b)Write a program to find the factorial value of any number
entered through the keyboard.
Ans) #include <stdio.h>
int main()
{
int a,num,fact=1;
printf("nEnter the number=");
scanf("%d",&num);
if(num==0)
fact=1;
else if(num>0)
{
for(a=1; a<=num; a++)
fact=fact*a;
}
printf("the factorial of a number %d is = %d",num,fact);
}
(c)Two numbers are entered through the keyboard. Write a
program to find the value of one number raised to the power of
another.
Ans) #include <stdio.h>
int main()
{
int a,b,c,d,result=1;
printf("nEnter any two numbers=");
scanf("%d %d",&a,&b);
c=a;
d=b;
while(b>0)
{
result=result*a;
b--;
}
printf("%d raised to the power %d =%d",c,d,result);
}
(d)Write a program to print all the ASCII values and their
equivalent characters using a while loop. The ASCII values
vary from 0 to 255.
Ans) #include <stdio.h>
int main()
{
int num=0;
while(num<=255)
{
printf("num=%d num=%cn",num,num);
num++;
}
}
(e) Write a program to print out all Armstrong numbers between 1
and 500. If sum of cubes of each digit of the number is equal to
the number itself, then the number is called an Armstrong
number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 *
3 * 3 )
Ans) #include <stdio.h>
int main()
{
int a,b,c,d,e,num=1,result;
while(num<=500)
{
a=num%10;
b=num/10;
c=b%10;
d=b/10;
e=d%10;
if(num==(a*a*a)+(c*c*c)+(e*e*e))
{
printf("%dn",num);
}
num++;
}
}
(f) Write a program to enter the numbers till the user wants and at the
end it should display the count of positive, negative and zeros
entered.
Ans) #include <stdio.h>
int main()
{
int a,b=0,c=0,d=0,num;
printf("How many numbers do u want to enter?n");
scanf("%d",&a);
while(a>0)
{
printf("Enter number=");
scanf("%d",&num);
if(num>0)
b++;
if(num<0)
c++;
if(num==0)
d++;
a--;
}
printf("n you entered : n %d positive numbers n %d negative
numbers n %d zeros",b,c,d);
}
(g)Write a program to find the octalequivalent of the entered
number.
Ans) #include <stdio.h>
#include <math.h>
int main()
{
int decimal_num,remainder,octal_num=0,p=0;
printf("Enter the decimal number :");
scanf("%d",&decimal_num);
while(decimal_num!=0)
{
remainder=decimal_num%8;
octal_num=octal_num+remainder*pow(10,p);
decimal_num=decimal_num/8;
p++;
}
printf("The octal equivalent = %dn",octal_num);
}
(h)Write a program to find the range of a set of numbers. Range is
the difference between the smallest and biggest number in the
list.
Ans #include<stdio.h>
main()
{
int max=0,min=0,temp,n,i;
printf("what is the lenght of number set?nnumber:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("nNow enter the %d number :",i);
scanf("%d",&temp);
if(temp>max)max=temp;
if(i==1)min=temp;
if(temp<min)
min=temp;
}
printf("The range of set is %d",max-min);
}
for, break, continue, do-while
[C] What would be the output of the following programs:
(a) main( ) Output) no output because
{ condition is not
int i = 0 ; valid.
for ( ; i ; )
printf ( "Here is some mail for youn" ) ;
}
(b) main( ) Output) 1 will be printed
{ indefinitely.
int i ;
for ( i = 1 ; i <= 5 ; printf ( "%dn", i ) ) ;
i++ ;
}
(c) main() Output)j=2
{ j=5
int i = 1, j = 1 ;
for ( ; ; )
{
if ( i > 5 )
break ;
else
j += i ;
printf ( "nj=%d", j ) ;
i += j ;
}
}
[D] Answer the following:
a) The three parts of the loop expression in the for loop are:
the initialization expression
the testing expression
the increment or decrement expression
(b) The break statement is used to exit from:
1. an if statement
2. a for loop (correct)
3. a program
4. the main( ) function
(c) A do-while loop is useful when we want that the statements within the
loop must be executed:
1. Only once
2. At least once (correct)
3. More than once
4. None of the above
(d) In what sequence the initialization, testing and execution of body is done
in a do-while loop
1. Initialization, execution of body, testing
2. Execution of body, initialization, testing
3. Initialization, testing, execution of body (correct)
4. None of the above
(e) Which of the following is not an infinite loop.
1). int i = 1 ; 2).for (; ;)
while ( 1 )
{
i++ ;
}
3). intTrue = 0, false ; (Correct) 4).inty,x=0;
while ( True ) do
{ {
False = 1 ; y=x;
} }while(x==0)
(f) Which of the following statement is used to take the control to the
beginning of the loop?
1. exit
2. break
3. continue (correct)
4. None of the above
[E] Attempt the following
(a) Write a programto print all prime numbers from 1 to 300.
Ans) #include <stdio.h>
main()
{
int i,j=1;
while(j<=300)
{
i=2;
while(i<j)
{
if(j%i==0)
break;
else
i++;
}
if(i==j)
printf("%dt",j);
j++;
}
printf("nntpress any key to exit");
}
(b) Write a program to fill the entire screen with a smiling face.
The smiling face has an ASCII value 1.
Ans) #include <stdio.h>
main()
{
intr,c;
for(r=0; r<=11; r++)
{
for(c=0; c<=24; c++)
printf("%c",1);
}
}
(c) Write a program to add first seven terms of the following
series using a for loop:
1+ 2 + 3+…….
1! 2! 3!
Ans) #include <stdio.h>
main()
{
inta=1,b;
floatfact,sum=0.0;
while(a<=7)
{
fact=1.0;
for(b=1; b<=a; b++)
fact=fact*b;
sum=sum+a/fact;
a++;
}
printf("sumof series=%f",sum);
}
(d) Write a program to generate all combinations of 1, 2 and 3
using for loop.
Ans) #include <stdio.h>
int main()
{
int a,b,c;
for(a=1; a<=3; a++)
{
for(b=1; b<=3; b++)
{
for(c=1; c<=3; c++)
printf("%d %d %dn",a,b,c);
}
}
}
(e) Write a program to producethe following output:
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
Ans) #include <stdio.h>
int main()
{
int a,x,b=71,c=70,d=1,sp;
for(x=1; x<=7; x++)
{
for(a=65; a<=b; a++)
printf("%c",a);
if(x==2)
c=70;
for(sp=2; sp<d; sp++)
printf(" ");
for(a=c; a>=65; a--);
printf("%c",a);
printf("n");
b--;
c--;
d=d+2;
}
}
(f) Write a program to producethe following output:
1
2 3
4 5 6
7 8 9 10
Ans) #include <stdio.h>
int main()
{
int a,b,n=6;
for(a=1; a<=10; a++)
{
if(a==1 || a==2 || a==4 || a==7)
{
printf("n");
for(b=1; b<=n; b++)
printf(" ");
n=n-2;
}
printf("%4d",a);
}
}
(g) Write a program to producethe following output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Ans) #include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int lines, n=1, spaces, tabs=8, x, y=1;
for(lines=1;lines<=5;lines++)
{
for(spaces=1;spaces<=tabs;spaces++)
printf(" ");
printf("%4d",n);
if(lines>=3)
{
for(x=1;x<=y;x++)
{
if(x==2&&y==3)
printf("%4d",lines+1);
else
printf("%4d",lines-1);
}
y++;
}
if(lines>=2)
printf("%4d",n);
tabs=tabs-2;
printf("n");
}
getch();
}
(h) Write a program to print the multiplication table of the
number entered by the user. The table should get displayed in the
following form.
29 * 1 = 29
29 * 2 = 58
…
Ans) #include <stdio.h>
int main()
{
int a,b;
printf("Enter any number to know its table till 10=");
scanf("%d",&a);
for(b=1; b<=10; b++)
{
printf("%d X %d = %d",a,b,b*a);
printf("n");
}
}
(i)According to a study, the approximate level of intelligence of a person can
be calculated using the following formula:
i = 2 + ( y + 0.5 x )
Write a program, which will produce a table of values of i, y and x, where y
varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps
of 0.5.
Ans) #include<stdio.h>
void main()
{
float i, y, x;
printf("Values Ofn");
printf("ti tty ttxn");
for(y=1;y<=6;y++)
{
for(x=5.5;x<=12.5;x=x+0.5)
{
i=2+(y + (0.5*x));
printf("%10f t%10f t%10fn",i,y,x);
}
}
}
Ans) #include <stdio.h>
int main()
{
int a,b,x,d,e=1;
float result,c1=1;
printf("Enter the value of X=");
scanf("%d",&x);
for(a=1; a<=7; a++)
{
for(b=1,c1=1; b<=e; b++)
{
c1=c1*(x-1)/x;
}
e++;
if(a>=2)
result=result+(0.5*c1);
else
result=result+1;
}
printf("sum of the first seven terms of the give series =%d",result); }

Contenu connexe

Tendances

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
 
LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSKavyaSharma65
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cppNilesh Dalvi
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CRaj vardhan
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
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 cBUBT
 
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 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 CBUBT
 
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 CBUBT
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEMMansi Tyagi
 
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 cBUBT
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointersSamiksha Pun
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiPriya Chauhan
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automataElakkiyaS11
 

Tendances (20)

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
 
LET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERSLET US C (5th EDITION) CHAPTER 1 ANSWERS
LET US C (5th EDITION) CHAPTER 1 ANSWERS
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
 
Function in c program
Function in c programFunction in c program
Function in c program
 
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
 
This pointer
This pointerThis pointer
This pointer
 
C string
C stringC string
C string
 
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 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
 
Chapter 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
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEM
 
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
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Programming in ansi C by Balaguruswami
Programming in ansi C by BalaguruswamiProgramming in ansi C by Balaguruswami
Programming in ansi C by Balaguruswami
 
Branching in C
Branching in CBranching in C
Branching in C
 
Ansi c
Ansi cAnsi c
Ansi c
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 

Similaire à While Loop programs to find factors, Armstrong numbers and series

All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep 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.2020vrgokila
 
Operating system labs
Operating system labsOperating system labs
Operating system labsbhaktisagar4
 
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.pdfAshutoshprasad27
 
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.docxAshutoshprasad27
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
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 HyderabadSrinivas Reddy Amedapu
 
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 HyderabadSrinivas Reddy Amedapu
 
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 notaluavi
 
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 numberMainak 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 numberMainak Sasmal
 

Similaire à While Loop programs to find factors, Armstrong numbers and series (20)

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

Dernier

Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 

Dernier (20)

Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 

While Loop programs to find factors, Armstrong numbers and series

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